HttpControllerTests.coffee 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../app/js/HttpController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "HttpController", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "./ContactManager": @ContactManager = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub() }
  12. @user_id = "mock-user-id"
  13. @contact_id = "mock-contact-id"
  14. @req = {}
  15. @res = {}
  16. @res.status = sinon.stub().returns @res
  17. @res.end = sinon.stub()
  18. @res.send = sinon.stub()
  19. @next = sinon.stub()
  20. describe "addContact", ->
  21. beforeEach ->
  22. @req.params =
  23. user_id: @user_id
  24. @ContactManager.touchContact = sinon.stub().callsArg(2)
  25. describe "with a valid user_id and contact_id", ->
  26. beforeEach ->
  27. @req.body =
  28. contact_id: @contact_id
  29. @HttpController.addContact @req, @res, @next
  30. it "should update the contact in the user's contact list", ->
  31. @ContactManager.touchContact
  32. .calledWith(@user_id, @contact_id)
  33. .should.equal true
  34. it "should update the user in the contact's contact list", ->
  35. @ContactManager.touchContact
  36. .calledWith(@contact_id, @user_id)
  37. .should.equal true
  38. it "should send back a 204 status", ->
  39. @res.status.calledWith(204).should.equal true
  40. @res.end.called.should.equal true
  41. describe "with an invalid contact id", ->
  42. beforeEach ->
  43. @req.body =
  44. contact_id: ""
  45. @HttpController.addContact @req, @res, @next
  46. it "should return 400, Bad Request", ->
  47. @res.status.calledWith(400).should.equal true
  48. @res.send.calledWith("contact_id should be a non-blank string").should.equal true