EditorHttpControllerTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorHttpController'
  5. describe "EditorHttpController", ->
  6. beforeEach ->
  7. @EditorHttpController = SandboxedModule.require modulePath, requires:
  8. '../Project/ProjectEntityHandler' : @ProjectEntityHandler = {}
  9. "./EditorRealTimeController": @EditorRealTimeController = {}
  10. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  11. "./EditorController": @EditorController = {}
  12. @project_id = "mock-project-id"
  13. @doc_id = "mock-doc-id"
  14. @parent_folder_id = "mock-folder-id"
  15. @req = {}
  16. @res =
  17. send: sinon.stub()
  18. json: sinon.stub()
  19. describe "restoreDoc", ->
  20. beforeEach ->
  21. @req.params =
  22. Project_id: @project_id
  23. doc_id: @doc_id
  24. @req.body =
  25. name: @name = "doc-name"
  26. @ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
  27. @doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
  28. @folder_id = "mock-folder-id"
  29. )
  30. @EditorRealTimeController.emitToRoom = sinon.stub()
  31. @EditorHttpController.restoreDoc @req, @res
  32. it "should restore the doc", ->
  33. @ProjectEntityHandler.restoreDoc
  34. .calledWith(@project_id, @doc_id, @name)
  35. .should.equal true
  36. it "should the real-time clients about the new doc", ->
  37. @EditorRealTimeController.emitToRoom
  38. .calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
  39. .should.equal true
  40. it "should return the new doc id", ->
  41. @res.json
  42. .calledWith(doc_id: @new_doc_id)
  43. .should.equal true
  44. describe "addDoc", ->
  45. beforeEach ->
  46. @doc = { "mock": "doc" }
  47. @req.params =
  48. Project_id: @project_id
  49. @req.body =
  50. name: @name = "doc-name"
  51. parent_folder_id: @parent_folder_id
  52. @EditorController.addDoc = sinon.stub().callsArgWith(5, null, @doc)
  53. @EditorHttpController.addDoc @req, @res
  54. it "should call EditorController.addDoc", ->
  55. @EditorController.addDoc
  56. .calledWith(@project_id, @parent_folder_id, @name, [], "editor")
  57. .should.equal true
  58. it "should send the doc back as JSON", ->
  59. @res.json
  60. .calledWith(@doc)
  61. .should.equal true
  62. describe "addFolder", ->
  63. beforeEach ->
  64. @folder = { "mock": "folder" }
  65. @req.params =
  66. Project_id: @project_id
  67. @req.body =
  68. name: @name = "folder-name"
  69. parent_folder_id: @parent_folder_id
  70. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  71. @EditorHttpController.addFolder @req, @res
  72. it "should call EditorController.addFolder", ->
  73. @EditorController.addFolder
  74. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  75. .should.equal true
  76. it "should send the folder back as JSON", ->
  77. @res.json
  78. .calledWith(@folder)
  79. .should.equal true
  80. describe "renameEntity", ->
  81. beforeEach ->
  82. @req.params =
  83. Project_id: @project_id
  84. entity_id: @entity_id = "entity-id-123"
  85. entity_type: @entity_type = "entity-type"
  86. @req.body =
  87. name: @name = "new-name"
  88. @EditorController.renameEntity = sinon.stub().callsArg(4)
  89. @EditorHttpController.renameEntity @req, @res
  90. it "should call EditorController.renameEntity", ->
  91. @EditorController.renameEntity
  92. .calledWith(@project_id, @entity_id, @entity_type, @name)
  93. .should.equal true
  94. it "should send back a success response", ->
  95. @res.send.calledWith(204).should.equal true
  96. describe "renameEntity with long name", ->
  97. beforeEach ->
  98. @req.params =
  99. Project_id: @project_id
  100. entity_id: @entity_id = "entity-id-123"
  101. entity_type: @entity_type = "entity-type"
  102. @req.body =
  103. name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  104. @EditorController.renameEntity = sinon.stub().callsArg(4)
  105. @EditorHttpController.renameEntity @req, @res
  106. it "should send back a bad request status code", ->
  107. @res.send.calledWith(400).should.equal true
  108. describe "moveEntity", ->
  109. beforeEach ->
  110. @req.params =
  111. Project_id: @project_id
  112. entity_id: @entity_id = "entity-id-123"
  113. entity_type: @entity_type = "entity-type"
  114. @req.body =
  115. folder_id: @folder_id = "folder-id-123"
  116. @EditorController.moveEntity = sinon.stub().callsArg(4)
  117. @EditorHttpController.moveEntity @req, @res
  118. it "should call EditorController.moveEntity", ->
  119. @EditorController.moveEntity
  120. .calledWith(@project_id, @entity_id, @folder_id, @entity_type)
  121. .should.equal true
  122. it "should send back a success response", ->
  123. @res.send.calledWith(204).should.equal true
  124. describe "deleteEntity", ->
  125. beforeEach ->
  126. @req.params =
  127. Project_id: @project_id
  128. entity_id: @entity_id = "entity-id-123"
  129. entity_type: @entity_type = "entity-type"
  130. @EditorController.deleteEntity = sinon.stub().callsArg(4)
  131. @EditorHttpController.deleteEntity @req, @res
  132. it "should call EditorController.deleteEntity", ->
  133. @EditorController.deleteEntity
  134. .calledWith(@project_id, @entity_id, @entity_type, "editor")
  135. .should.equal true
  136. it "should send back a success response", ->
  137. @res.send.calledWith(204).should.equal true