TpdsControllerTests.coffee 3.8 KB

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