TpdsControllerTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @TpdsUpdateHandler.newUpdate = sinon.stub().callsArg(5)
  24. res = send: =>
  25. @TpdsUpdateHandler.newUpdate.calledWith(@user_id, "projectName","/here.txt", req).should.equal true
  26. done()
  27. @TpdsController.mergeUpdate req, res
  28. describe 'getting a delete update', ->
  29. it 'should process the delete with the update reciver', (done)->
  30. path = "/projectName/here.txt"
  31. req =
  32. params:{0:path, "user_id":@user_id}
  33. session:
  34. destroy:->
  35. @TpdsUpdateHandler.deleteUpdate = sinon.stub().callsArg(4)
  36. res = send: =>
  37. @TpdsUpdateHandler.deleteUpdate.calledWith(@user_id, "projectName", "/here.txt").should.equal true
  38. done()
  39. @TpdsController.deleteUpdate req, res
  40. describe 'parseParams', ->
  41. it 'should take the project name off the start and replace with slash', ->
  42. path = "noSlashHere"
  43. req = params:{0:path, user_id:@user_id}
  44. result = @TpdsController.parseParams(req)
  45. result.user_id.should.equal @user_id
  46. result.filePath.should.equal "/"
  47. result.projectName.should.equal path
  48. it 'should take the project name off the start and return it with no slashes in', ->
  49. path = "/project/file.tex"
  50. req = params:{0:path, user_id:@user_id}
  51. result = @TpdsController.parseParams(req)
  52. result.user_id.should.equal @user_id
  53. result.filePath.should.equal "/file.tex"
  54. result.projectName.should.equal "project"
  55. it 'should take the project name of and return a slash for the file path', ->
  56. path = "/project_name"
  57. req = params:{0:path, user_id:@user_id}
  58. result = @TpdsController.parseParams(req)
  59. result.projectName.should.equal "project_name"
  60. result.filePath.should.equal "/"
  61. describe 'updateProjectContents', ->
  62. beforeEach ->
  63. @UpdateMerger.mergeUpdate = sinon.stub().callsArg(3)
  64. @req =
  65. params:
  66. 0: @path = "chapters/main.tex"
  67. project_id: @project_id = "project-id-123"
  68. session:
  69. destroy: sinon.stub()
  70. @res =
  71. send: sinon.stub()
  72. @TpdsController.updateProjectContents @req, @res
  73. it "should merge the update", ->
  74. @UpdateMerger.mergeUpdate
  75. .calledWith(@project_id, "/" + @path, @req)
  76. .should.equal true
  77. it "should return a success", ->
  78. @res.send.calledWith(200).should.equal true
  79. it "should clear the session", ->
  80. @req.session.destroy.called.should.equal true
  81. describe 'deleteProjectContents', ->
  82. beforeEach ->
  83. @UpdateMerger.deleteUpdate = sinon.stub().callsArg(2)
  84. @req =
  85. params:
  86. 0: @path = "chapters/main.tex"
  87. project_id: @project_id = "project-id-123"
  88. session:
  89. destroy: sinon.stub()
  90. @res =
  91. send: sinon.stub()
  92. @TpdsController.deleteProjectContents @req, @res
  93. it "should delete the file", ->
  94. @UpdateMerger.deleteUpdate
  95. .calledWith(@project_id, "/" + @path)
  96. .should.equal true
  97. it "should return a success", ->
  98. @res.send.calledWith(200).should.equal true
  99. it "should clear the session", ->
  100. @req.session.destroy.called.should.equal true