HttpControllerTests.coffee 7.7 KB

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