DropboxHandlerTests.coffee 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/Dropbox/DropboxHandler.js'
  6. thirdPartyDataStoreApiUrl = "http://third-party-json-store.herokuapp.com"
  7. siteUrl = "www.sharelatex.com"
  8. describe 'third party data store', ->
  9. user_id = "123nd3ijdks"
  10. beforeEach ->
  11. @stubGet = sinon.stub()
  12. @stubDel = sinon.stub()
  13. @projectEntityHandler = flushProjectToThirdPartyDataStore:sinon.stub().callsArgWith(1)
  14. @projectModel = findAllUsersProjects : sinon.stub()
  15. @handler = SandboxedModule.require modulePath, requires:
  16. "settings-sharelatex": {siteUrl:siteUrl, apis: {thirdPartyDataStore: {url: thirdPartyDataStoreApiUrl}}}
  17. "../../models/Project":{Project:@projectModel}
  18. '../Project/ProjectEntityHandler':@projectEntityHandler
  19. "request":
  20. get:@stubGet
  21. del: @stubDel
  22. 'logger-sharelatex':
  23. log:->
  24. err:->
  25. it 'should be able to get userStatus', (done)->
  26. body = JSON.stringify({registered:true})
  27. opts =
  28. url: "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/dropbox/status"
  29. timeout: 5000
  30. @stubGet.withArgs(opts).callsArgWith(1, null, {statusCode:200}, body)
  31. @handler.getUserRegistrationStatus user_id, (err, status)->
  32. status.registered.should.equal true
  33. done()
  34. it 'should be able to get auth url with callback url on it', (done)->
  35. url = "http://www.dropbox.com"
  36. body = JSON.stringify({authorize_url:url})
  37. opts =
  38. url: "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/dropbox/register"
  39. timeout: 5000
  40. @stubGet.withArgs(opts).callsArgWith(1, null, {statusCode:200}, body)
  41. @handler.getDropboxRegisterUrl user_id, (err, returnedUrl)->
  42. returnedUrl.should.equal "#{url}&oauth_callback=#{siteUrl}/dropbox/completeRegistration"
  43. done()
  44. it 'should be able to complete registration and get getAccessToken from dropbox', (done)->
  45. body = JSON.stringify({success:true})
  46. opts =
  47. url: "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/dropbox/getaccesstoken"
  48. timeout: 5000
  49. @stubGet.withArgs(opts).callsArgWith(1, null, {statusCode:200}, body)
  50. @handler.flushUsersProjectToDropbox = sinon.stub()
  51. @handler.completeRegistration user_id, (err, successful)=>
  52. @handler.flushUsersProjectToDropbox.called.should.equal true
  53. successful.should.equal true
  54. done()
  55. it 'should tell the tpds to unlink the account', (done)->
  56. opts =
  57. url: "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/dropbox"
  58. timeout: 5000
  59. @stubDel.callsArgWith(1, null, {statusCode:200})
  60. @handler.unlinkAccount user_id, (err)=>
  61. @stubDel.calledWith(opts).should.equal true
  62. done()
  63. it 'should tell the project entity handler to flush project to tpds', (done)->
  64. user_id = "123u9oijllkj"
  65. projectList = [{_id:"123lk"}, {_id:"12ji3ojio"}, {_id:"2jiojdoi"}]
  66. collabProjectList = [{_id:"213ds"}]
  67. @projectModel.findAllUsersProjects.callsArgWith(2, projectList, collabProjectList)
  68. @handler.flushUsersProjectToDropbox user_id, =>
  69. @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(projectList[0]._id).should.equal true
  70. @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(projectList[1]._id).should.equal true
  71. @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(projectList[2]._id).should.equal true
  72. @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(collabProjectList[0]._id).should.equal true
  73. done()