HttpControllerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. beforeEach ->
  46. @req.params =
  47. project_id: @project_id
  48. @docs = [{
  49. _id: ObjectId()
  50. lines: ["mock", "lines", "one"]
  51. version: 1
  52. rev: 2
  53. }, {
  54. _id: ObjectId()
  55. lines: ["mock", "lines", "two"]
  56. version: 3
  57. rev: 4
  58. }]
  59. @DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  60. @HttpController.getAllDocs @req, @res, @next
  61. it "should get all the docs", ->
  62. @DocManager.getAllDocs
  63. .calledWith(@project_id)
  64. .should.equal true
  65. it "should return the doc as JSON", ->
  66. @res.json
  67. .calledWith([{
  68. _id: @docs[0]._id.toString()
  69. lines: @docs[0].lines
  70. rev: @docs[0].rev
  71. version: @docs[0].version
  72. }, {
  73. _id: @docs[1]._id.toString()
  74. lines: @docs[1].lines
  75. rev: @docs[1].rev
  76. version: @docs[1].version
  77. }])
  78. .should.equal true
  79. describe "updateDoc", ->
  80. beforeEach ->
  81. @req.params =
  82. project_id: @project_id
  83. doc_id: @doc_id
  84. describe "when the doc lines exist and were updated", ->
  85. beforeEach ->
  86. @req.body =
  87. lines: @lines = ["hello", "world"]
  88. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, true)
  89. @HttpController.updateDoc @req, @res, @next
  90. it "should update the document", ->
  91. @DocManager.updateDoc
  92. .calledWith(@project_id, @doc_id, @lines)
  93. .should.equal true
  94. it "should return a modified status", ->
  95. @res.json
  96. .calledWith(modified: true)
  97. .should.equal true
  98. describe "when the doc lines exist and were not updated", ->
  99. beforeEach ->
  100. @req.body =
  101. lines: @lines = ["hello", "world"]
  102. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  103. @HttpController.updateDoc @req, @res, @next
  104. it "should return a modified status", ->
  105. @res.json
  106. .calledWith(modified: false)
  107. .should.equal true
  108. describe "when the doc lines are not provided", ->
  109. beforeEach ->
  110. @req.body = {}
  111. @DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false)
  112. @HttpController.updateDoc @req, @res, @next
  113. it "should not update the document", ->
  114. @DocManager.updateDoc.called.should.equal false
  115. it "should return a 400 (bad request) response", ->
  116. @res.send
  117. .calledWith(400)
  118. .should.equal true
  119. describe "deleteDoc", ->
  120. beforeEach ->
  121. @req.params =
  122. project_id: @project_id
  123. doc_id: @doc_id
  124. @DocManager.deleteDoc = sinon.stub().callsArg(2)
  125. @HttpController.deleteDoc @req, @res, @next
  126. it "should delete the document", ->
  127. @DocManager.deleteDoc
  128. .calledWith(@project_id, @doc_id)
  129. .should.equal true
  130. it "should return a 204 (No Content)", ->
  131. @res.send
  132. .calledWith(204)
  133. .should.equal true