GoogleDrive.spec.js 2.7 KB

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