HttpControllerTests.coffee 6.0 KB

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