ContactControllerTests.coffee 2.7 KB

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