HttpControllerTests.coffee 6.6 KB

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