GoogleDrive.spec.js 3.0 KB

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