HttpControllerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. chai.should()
  5. expect = chai.expect
  6. modulePath = require('path').join __dirname, '../../../app/js/HttpController'
  7. ObjectId = require("mongojs").ObjectId
  8. describe "HttpController", ->
  9. beforeEach ->
  10. @HttpController = SandboxedModule.require modulePath, requires:
  11. "./DocManager": @DocManager = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  13. @res = { send: sinon.stub(), json: sinon.stub() }
  14. @req = {}
  15. @next = sinon.stub()
  16. @project_id = "mock-project-id"
  17. @doc_id = "mock-doc-id"
  18. @doc = {
  19. _id: @doc_id
  20. lines: ["mock", "lines"]
  21. version: 42
  22. rev: 5
  23. }
  24. describe "getDoc", ->
  25. beforeEach ->
  26. @req.params =
  27. project_id: @project_id
  28. doc_id: @doc_id
  29. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
  30. @HttpController.getDoc @req, @res, @next
  31. it "should get the document", ->
  32. @DocManager.getDoc
  33. .calledWith(@project_id, @doc_id)
  34. .should.equal true
  35. it "should return the doc as JSON", ->
  36. @res.json
  37. .calledWith({
  38. _id: @doc_id
  39. lines: @doc.lines
  40. rev: @doc.rev
  41. version: @doc.version
  42. })
  43. .should.equal true
  44. describe "getAllDocs", ->
  45. describe "normally", ->
  46. beforeEach ->
  47. @req.params =
  48. project_id: @project_id
  49. @docs = [{
  50. _id: ObjectId()
  51. lines: ["mock", "lines", "one"]
  52. version: 1
  53. rev: 2
  54. }, {
  55. _id: ObjectId()
  56. lines: ["mock", "lines", "two"]
  57. version: 3
  58. rev: 4
  59. }]
  60. @DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  61. @HttpController.getAllDocs @req, @res, @next
  62. it "should get all the docs", ->
  63. @DocManager.getAllDocs
  64. .calledWith(@project_id)
  65. .should.equal true
  66. it "should return the doc as JSON", ->
  67. @res.json
  68. .calledWith([{
  69. _id: @docs[0]._id.toString()
  70. lines: @docs[0].lines
  71. rev: @docs[0].rev
  72. version: @docs[0].version
  73. }, {
  74. _id: @docs[1]._id.toString()
  75. lines: @docs[1].lines
  76. rev: @docs[1].rev
  77. version: @docs[1].version
  78. }])
  79. .should.equal true
  80. describe "with a null doc", ->
  81. beforeEach ->
  82. @req.params =
  83. project_id: @project_id
  84. @docs = [{
  85. _id: ObjectId()
  86. lines: ["mock", "lines", "one"]
  87. version: 1
  88. rev: 2
  89. },
  90. null,
  91. {
  92. _id: ObjectId()
  93. lines: ["mock", "lines", "two"]
  94. version: 3
  95. rev: 4
  96. }]
  97. @DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  98. @HttpController.getAllDocs @req, @res, @next
  99. it "should return the non null docs as JSON", ->
  100. @res.json
  101. .calledWith([{
  102. _id: @docs[0]._id.toString()
  103. lines: @docs[0].lines
  104. rev: @docs[0].rev
  105. version: @docs[0].version
  106. }, {
  107. _id: @docs[2]._id.toString()
  108. lines: @docs[2].lines
  109. rev: @docs[2].rev
  110. version: @docs[2].version
  111. }])
  112. .should.equal true
  113. it "should log out an error", ->
  114. @logger.error
  115. .calledWith(
  116. err: new Error("null doc")
  117. project_id: @project_id
  118. "encountered null doc"
  119. )
  120. .should.equal true
  121. describe "updateDoc", ->
  122. beforeEach ->
  123. @req.params =
  124. project_id: @project_id
  125. doc_id: @doc_id
  126. describe "when the doc lines exist and were updated", ->
  127. beforeEach ->
  128. @req.body =
  129. lines: @lines = ["hello", "world"]
  130. version: @version = 42
  131. @DocManager.updateDoc = sinon.stub().callsArgWith(4, null, true, @rev = 5)
  132. @HttpController.updateDoc @req, @res, @next
  133. it "should update the document", ->
  134. @DocManager.updateDoc
  135. .calledWith(@project_id, @doc_id, @lines, @version)
  136. .should.equal true
  137. it "should return a modified status", ->
  138. @res.json
  139. .calledWith(modified: true, rev: @rev)
  140. .should.equal true
  141. describe "when the doc lines exist and were not updated", ->
  142. beforeEach ->
  143. @req.body =
  144. lines: @lines = ["hello", "world"]
  145. @DocManager.updateDoc = sinon.stub().callsArgWith(4, null, false, @rev = 5)
  146. @HttpController.updateDoc @req, @res, @next
  147. it "should return a modified status", ->
  148. @res.json
  149. .calledWith(modified: false, rev: @rev)
  150. .should.equal true
  151. describe "when the doc lines are not provided", ->
  152. beforeEach ->
  153. @req.body = {}
  154. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  155. @HttpController.updateDoc @req, @res, @next
  156. it "should not update the document", ->
  157. @DocManager.updateDoc.called.should.equal false
  158. it "should return a 400 (bad request) response", ->
  159. @res.send
  160. .calledWith(400)
  161. .should.equal true
  162. describe "deleteDoc", ->
  163. beforeEach ->
  164. @req.params =
  165. project_id: @project_id
  166. doc_id: @doc_id
  167. @DocManager.deleteDoc = sinon.stub().callsArg(2)
  168. @HttpController.deleteDoc @req, @res, @next
  169. it "should delete the document", ->
  170. @DocManager.deleteDoc
  171. .calledWith(@project_id, @doc_id)
  172. .should.equal true
  173. it "should return a 204 (No Content)", ->
  174. @res.send
  175. .calledWith(204)
  176. .should.equal true