TpdsControllerTests.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsController.js'
  5. describe 'TpdsController', ->
  6. beforeEach ->
  7. @TpdsUpdateHandler = {}
  8. @TpdsController = SandboxedModule.require modulePath, requires:
  9. './TpdsUpdateHandler':@TpdsUpdateHandler
  10. './UpdateMerger': @UpdateMerger = {}
  11. 'logger-sharelatex':
  12. log:->
  13. err:->
  14. @user_id = "dsad29jlkjas"
  15. describe 'getting an update', ->
  16. it 'should process the update with the update reciver', (done)->
  17. path = "/projectName/here.txt"
  18. req =
  19. pause:->
  20. params:{0:path, "user_id":@user_id}
  21. session:
  22. destroy:->
  23. headers:
  24. "x-sl-update-source": @source = "dropbox"
  25. @TpdsUpdateHandler.newUpdate = sinon.stub().callsArg(5)
  26. res = send: =>
  27. @TpdsUpdateHandler.newUpdate.calledWith(@user_id, "projectName","/here.txt", req, @source).should.equal true
  28. done()
  29. @TpdsController.mergeUpdate req, res
  30. describe 'getting a delete update', ->
  31. it 'should process the delete with the update reciver', (done)->
  32. path = "/projectName/here.txt"
  33. req =
  34. params:{0:path, "user_id":@user_id}
  35. session:
  36. destroy:->
  37. headers:
  38. "x-sl-update-source": @source = "dropbox"
  39. @TpdsUpdateHandler.deleteUpdate = sinon.stub().callsArg(4)
  40. res = send: =>
  41. @TpdsUpdateHandler.deleteUpdate.calledWith(@user_id, "projectName", "/here.txt", @source).should.equal true
  42. done()
  43. @TpdsController.deleteUpdate req, res
  44. describe 'parseParams', ->
  45. it 'should take the project name off the start and replace with slash', ->
  46. path = "noSlashHere"
  47. req = params:{0:path, user_id:@user_id}
  48. result = @TpdsController.parseParams(req)
  49. result.user_id.should.equal @user_id
  50. result.filePath.should.equal "/"
  51. result.projectName.should.equal path
  52. it 'should take the project name off the start and return it with no slashes in', ->
  53. path = "/project/file.tex"
  54. req = params:{0:path, user_id:@user_id}
  55. result = @TpdsController.parseParams(req)
  56. result.user_id.should.equal @user_id
  57. result.filePath.should.equal "/file.tex"
  58. result.projectName.should.equal "project"
  59. it 'should take the project name of and return a slash for the file path', ->
  60. path = "/project_name"
  61. req = params:{0:path, user_id:@user_id}
  62. result = @TpdsController.parseParams(req)
  63. result.projectName.should.equal "project_name"
  64. result.filePath.should.equal "/"
  65. describe 'updateProjectContents', ->
  66. beforeEach ->
  67. @UpdateMerger.mergeUpdate = sinon.stub().callsArg(4)
  68. @req =
  69. params:
  70. 0: @path = "chapters/main.tex"
  71. project_id: @project_id = "project-id-123"
  72. session:
  73. destroy: sinon.stub()
  74. headers:
  75. "x-sl-update-source": @source = "github"
  76. @res =
  77. send: sinon.stub()
  78. @TpdsController.updateProjectContents @req, @res
  79. it "should merge the update", ->
  80. @UpdateMerger.mergeUpdate
  81. .calledWith(@project_id, "/" + @path, @req, @source)
  82. .should.equal true
  83. it "should return a success", ->
  84. @res.send.calledWith(200).should.equal true
  85. it "should clear the session", ->
  86. @req.session.destroy.called.should.equal true
  87. describe 'deleteProjectContents', ->
  88. beforeEach ->
  89. @UpdateMerger.deleteUpdate = sinon.stub().callsArg(3)
  90. @req =
  91. params:
  92. 0: @path = "chapters/main.tex"
  93. project_id: @project_id = "project-id-123"
  94. session:
  95. destroy: sinon.stub()
  96. headers:
  97. "x-sl-update-source": @source = "github"
  98. @res =
  99. send: sinon.stub()
  100. @TpdsController.deleteProjectContents @req, @res
  101. it "should delete the file", ->
  102. @UpdateMerger.deleteUpdate
  103. .calledWith(@project_id, "/" + @path, @source)
  104. .should.equal true
  105. it "should return a success", ->
  106. @res.send.calledWith(200).should.equal true
  107. it "should clear the session", ->
  108. @req.session.destroy.called.should.equal true