ContactControllerTests.coffee 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. assert = chai.assert
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/Features/Contacts/ContactController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "ContactController", ->
  9. beforeEach ->
  10. @ContactController = SandboxedModule.require modulePath, requires:
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. "../User/UserGetter": @UserGetter = {}
  13. "./ContactManager": @ContactManager = {}
  14. "../Authentication/AuthenticationController": @AuthenticationController = {}
  15. "../../infrastructure/Modules": @Modules = { hooks: {} }
  16. @next = sinon.stub()
  17. @req = {}
  18. @res = {}
  19. @res.status = sinon.stub().returns @req
  20. @res.send = sinon.stub()
  21. describe "getContacts", ->
  22. beforeEach ->
  23. @user_id = "mock-user-id"
  24. @contact_ids = ["contact-1", "contact-2", "contact-3"]
  25. @contacts = [
  26. { _id: "contact-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", unsued: "foo" }
  27. { _id: "contact-2", email: "jane@example.com", first_name: "Jane", last_name: "Example", unsued: "foo", holdingAccount: true }
  28. { _id: "contact-3", email: "jim@example.com", first_name: "Jim", last_name: "Example", unsued: "foo" }
  29. ]
  30. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id)
  31. @ContactManager.getContactIds = sinon.stub().callsArgWith(2, null, @contact_ids)
  32. @UserGetter.getUsers = sinon.stub().callsArgWith(2, null, @contacts)
  33. @Modules.hooks.fire = sinon.stub().callsArg(3)
  34. @ContactController.getContacts @req, @res, @next
  35. it "should look up the logged in user id", ->
  36. @AuthenticationController.getLoggedInUserId
  37. .calledWith(@req)
  38. .should.equal true
  39. it "should get the users contact ids", ->
  40. @ContactManager.getContactIds
  41. .calledWith(@user_id, { limit: 50 })
  42. .should.equal true
  43. it "should populate the users contacts ids", ->
  44. @UserGetter.getUsers
  45. .calledWith(@contact_ids, { email: 1, first_name: 1, last_name: 1, holdingAccount: 1 })
  46. .should.equal true
  47. it "should fire the getContact module hook", ->
  48. @Modules.hooks.fire
  49. .calledWith("getContacts", @user_id)
  50. .should.equal true
  51. it "should return a formatted list of contacts in contact list order, without holding accounts", ->
  52. @res.send.args[0][0].contacts.should.deep.equal [
  53. { id: "contact-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", type: "user" }
  54. { id: "contact-3", email: "jim@example.com", first_name: "Jim", last_name: "Example", type: "user" }
  55. ]