GoogleDrive.spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import test from 'tape'
  2. import nock from 'nock'
  3. import Utils from '../../src/core/Utils'
  4. import Google from '../../src/plugins/GoogleDrive'
  5. test('checkAuthentication success', function (t) {
  6. t.plan(1)
  7. nock('http://localhost:3020')
  8. .get('/google/authorize')
  9. .reply(200, {
  10. isAuthenticated: true
  11. })
  12. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  13. GoogleDrive.checkAuthentication()
  14. .then((isAuthed) => {
  15. t.equal(isAuthed, true)
  16. })
  17. })
  18. test('checkAuthentication fail', function (t) {
  19. t.plan(1)
  20. nock('http://localhost:3020')
  21. .get('/google/authorize')
  22. .reply(200, {
  23. isAuthenticated: false
  24. })
  25. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  26. GoogleDrive.checkAuthentication()
  27. .then((isAuthed) => {
  28. t.equal(isAuthed, false)
  29. })
  30. })
  31. test('getFile: success', function (t) {
  32. t.plan(1)
  33. nock('http://localhost:3020')
  34. .post('/google/get')
  35. .reply(201, (uri, requestBody) => {
  36. return {
  37. ok: true,
  38. id: '12345'
  39. }
  40. })
  41. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  42. GoogleDrive.getFile('12345')
  43. .then((result) => {
  44. t.equal(result.ok, true)
  45. })
  46. })
  47. test('getFile: fileId not a string', function (t) {
  48. t.plan(1)
  49. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  50. var result = GoogleDrive.getFile()
  51. t.equal(result instanceof Error, true)
  52. })
  53. test('getFolder: success', function (t) {
  54. t.plan(1)
  55. nock('http://localhost:3020')
  56. .get('/google/list')
  57. .reply(200, {
  58. items: [{
  59. mimeType: 'application/vnd.google-apps.folder'
  60. }, {
  61. mimeType: 'application/vnd.google-apps.spreadsheet'
  62. }, {
  63. mimeType: 'application/vnd.google-apps.spreadsheet'
  64. }, {
  65. mimeType: 'application/vnd.google-apps.folder'
  66. }]
  67. })
  68. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  69. GoogleDrive.getFolder('/')
  70. .then((res) => {
  71. const allFolders = Utils.every(res.folders, function (folder) {
  72. return folder.mimeType === 'application/vnd.google-apps.folder'
  73. })
  74. const allFiles = Utils.every(res.files, (file) => {
  75. return file.mimeType !== 'application/vnd.google-apps.folder'
  76. })
  77. t.equal(allFolders && allFiles, true)
  78. })
  79. })
  80. test('getFolder: fail', function (t) {
  81. t.plan(1)
  82. nock('http://localhost:3020')
  83. .get('/google/list')
  84. .reply(500, 'Not authenticated')
  85. var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
  86. GoogleDrive.getFolder('/')
  87. .then((err) => {
  88. t.equal(err instanceof Error, true)
  89. })
  90. })