HttpControllerTests.coffee 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require("chai").assert
  3. sinon = require('sinon')
  4. chai = require('chai')
  5. chai.should()
  6. expect = chai.expect
  7. modulePath = require('path').join __dirname, '../../../app/js/HttpController'
  8. ObjectId = require("mongojs").ObjectId
  9. describe "HttpController", ->
  10. beforeEach ->
  11. @HttpController = SandboxedModule.require modulePath, requires:
  12. "./DocManager": @DocManager = {}
  13. "./DocArchiveManager": @DocArchiveManager = {}
  14. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  15. @res = { send: sinon.stub(), json: sinon.stub(), setHeader:sinon.stub() }
  16. @req = { query:{}}
  17. @next = sinon.stub()
  18. @project_id = "mock-project-id"
  19. @doc_id = "mock-doc-id"
  20. @doc = {
  21. _id: @doc_id
  22. lines: ["mock", "lines", " here", "", "", " spaces "]
  23. version: 42
  24. rev: 5
  25. }
  26. @deletedDoc = {
  27. deleted:true
  28. _id: @doc_id
  29. lines: ["mock", "lines", " here", "", "", " spaces "]
  30. version: 42
  31. rev: 5
  32. }
  33. describe "getDoc", ->
  34. describe "without deleted docs", ->
  35. beforeEach ->
  36. @req.params =
  37. project_id: @project_id
  38. doc_id: @doc_id
  39. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
  40. @HttpController.getDoc @req, @res, @next
  41. it "should get the document (including deleted)", ->
  42. @DocManager.getDoc
  43. .calledWith(@project_id, @doc_id)
  44. .should.equal true
  45. it "should return the doc as JSON", ->
  46. @res.json
  47. .calledWith({
  48. _id: @doc_id
  49. lines: @doc.lines
  50. rev: @doc.rev
  51. deleted: false
  52. })
  53. .should.equal true
  54. describe "which is deleted", ->
  55. beforeEach ->
  56. @req.params =
  57. project_id: @project_id
  58. doc_id: @doc_id
  59. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @deletedDoc)
  60. it "should get the doc from the doc manager", ->
  61. @HttpController.getDoc @req, @res, @next
  62. @DocManager.getDoc.calledWith(@project_id, @doc_id).should.equal true
  63. it "should return 404 if the query string delete is not set ", ->
  64. @HttpController.getDoc @req, @res, @next
  65. @res.send.calledWith(404).should.equal true
  66. it "should return the doc as JSON if include_deleted is set to true", ->
  67. @req.query.include_deleted = "true"
  68. @HttpController.getDoc @req, @res, @next
  69. @res.json
  70. .calledWith({
  71. _id: @doc_id
  72. lines: @doc.lines
  73. rev: @doc.rev
  74. deleted: true
  75. })
  76. .should.equal true
  77. describe "getRawDoc", ->
  78. beforeEach ->
  79. @req.params =
  80. project_id: @project_id
  81. doc_id: @doc_id
  82. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
  83. @HttpController.getRawDoc @req, @res, @next
  84. it "should get the document", ->
  85. @DocManager.getDoc
  86. .calledWith(@project_id, @doc_id)
  87. .should.equal true
  88. it "should set the content type header", ->
  89. @res.setHeader.calledWith('content-type', 'text/plain').should.equal true
  90. it "should send the raw version of the doc", ->
  91. assert.deepEqual @res.send.args[0][0], "#{@doc.lines[0]}\n#{@doc.lines[1]}\n#{@doc.lines[2]}\n#{@doc.lines[3]}\n#{@doc.lines[4]}\n#{@doc.lines[5]}"
  92. describe "getAllDocs", ->
  93. describe "normally", ->
  94. beforeEach ->
  95. @req.params =
  96. project_id: @project_id
  97. @docs = [{
  98. _id: ObjectId()
  99. lines: ["mock", "lines", "one"]
  100. rev: 2
  101. }, {
  102. _id: ObjectId()
  103. lines: ["mock", "lines", "two"]
  104. rev: 4
  105. }]
  106. @DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  107. @HttpController.getAllDocs @req, @res, @next
  108. it "should get all the docs", ->
  109. @DocManager.getAllDocs
  110. .calledWith(@project_id)
  111. .should.equal true
  112. it "should return the doc as JSON", ->
  113. @res.json
  114. .calledWith([{
  115. _id: @docs[0]._id.toString()
  116. lines: @docs[0].lines
  117. rev: @docs[0].rev
  118. deleted: false
  119. }, {
  120. _id: @docs[1]._id.toString()
  121. lines: @docs[1].lines
  122. rev: @docs[1].rev
  123. deleted: false
  124. }])
  125. .should.equal true
  126. describe "with a null doc", ->
  127. beforeEach ->
  128. @req.params =
  129. project_id: @project_id
  130. @docs = [{
  131. _id: ObjectId()
  132. lines: ["mock", "lines", "one"]
  133. rev: 2
  134. },
  135. null,
  136. {
  137. _id: ObjectId()
  138. lines: ["mock", "lines", "two"]
  139. rev: 4
  140. }]
  141. @DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  142. @HttpController.getAllDocs @req, @res, @next
  143. it "should return the non null docs as JSON", ->
  144. @res.json
  145. .calledWith([{
  146. _id: @docs[0]._id.toString()
  147. lines: @docs[0].lines
  148. rev: @docs[0].rev
  149. deleted: false
  150. }, {
  151. _id: @docs[2]._id.toString()
  152. lines: @docs[2].lines
  153. rev: @docs[2].rev
  154. deleted: false
  155. }])
  156. .should.equal true
  157. it "should log out an error", ->
  158. @logger.error
  159. .calledWith(
  160. err: new Error("null doc")
  161. project_id: @project_id
  162. "encountered null doc"
  163. )
  164. .should.equal true
  165. describe "updateDoc", ->
  166. beforeEach ->
  167. @req.params =
  168. project_id: @project_id
  169. doc_id: @doc_id
  170. describe "when the doc lines exist and were updated", ->
  171. beforeEach ->
  172. @req.body =
  173. lines: @lines = ["hello", "world"]
  174. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, true, @rev = 5)
  175. @HttpController.updateDoc @req, @res, @next
  176. it "should update the document", ->
  177. @DocManager.updateDoc
  178. .calledWith(@project_id, @doc_id, @lines)
  179. .should.equal true
  180. it "should return a modified status", ->
  181. @res.json
  182. .calledWith(modified: true, rev: @rev)
  183. .should.equal true
  184. describe "when the doc lines exist and were not updated", ->
  185. beforeEach ->
  186. @req.body =
  187. lines: @lines = ["hello", "world"]
  188. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false, @rev = 5)
  189. @HttpController.updateDoc @req, @res, @next
  190. it "should return a modified status", ->
  191. @res.json
  192. .calledWith(modified: false, rev: @rev)
  193. .should.equal true
  194. describe "when the doc lines are not provided", ->
  195. beforeEach ->
  196. @req.body = {}
  197. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  198. @HttpController.updateDoc @req, @res, @next
  199. it "should not update the document", ->
  200. @DocManager.updateDoc.called.should.equal false
  201. it "should return a 400 (bad request) response", ->
  202. @res.send
  203. .calledWith(400)
  204. .should.equal true
  205. describe "deleteDoc", ->
  206. beforeEach ->
  207. @req.params =
  208. project_id: @project_id
  209. doc_id: @doc_id
  210. @DocManager.deleteDoc = sinon.stub().callsArg(2)
  211. @HttpController.deleteDoc @req, @res, @next
  212. it "should delete the document", ->
  213. @DocManager.deleteDoc
  214. .calledWith(@project_id, @doc_id)
  215. .should.equal true
  216. it "should return a 204 (No Content)", ->
  217. @res.send
  218. .calledWith(204)
  219. .should.equal true
  220. describe "archiveAllDocs", ->
  221. beforeEach ->
  222. @req.params =
  223. project_id: @project_id
  224. @DocArchiveManager.archiveAllDocs = sinon.stub().callsArg(1)
  225. @HttpController.archiveAllDocs @req, @res, @next
  226. it "should archive the project", ->
  227. @DocArchiveManager.archiveAllDocs
  228. .calledWith(@project_id)
  229. .should.equal true
  230. it "should return a 204 (No Content)", ->
  231. @res.send
  232. .calledWith(204)
  233. .should.equal true