HttpControllerTests.coffee 5.9 KB

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