GoogleDrive.spec.js 2.9 KB

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