overleaf/services/web/test/unit/coffee/Contact/ContactControllerTests.coffee

68 lines
2.7 KiB
CoffeeScript
Raw Normal View History

sinon = require('sinon')
chai = require('chai')
should = chai.should()
assert = chai.assert
expect = chai.expect
modulePath = "../../../../app/js/Features/Contacts/ContactController.js"
SandboxedModule = require('sandboxed-module')
describe "ContactController", ->
beforeEach ->
2016-09-07 15:40:49 +00:00
@AuthenticationController =
getLoggedInUserId: sinon.stub()
@ContactController = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
"../User/UserGetter": @UserGetter = {}
"./ContactManager": @ContactManager = {}
"../Authentication/AuthenticationController": @AuthenticationController = {}
"../../infrastructure/Modules": @Modules = { hooks: {} }
2016-09-07 15:40:49 +00:00
'../Authentication/AuthenticationController': @AuthenticationController
@next = sinon.stub()
@req = {}
@res = {}
@res.status = sinon.stub().returns @req
@res.send = sinon.stub()
describe "getContacts", ->
beforeEach ->
@user_id = "mock-user-id"
@contact_ids = ["contact-1", "contact-2", "contact-3"]
@contacts = [
{ _id: "contact-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", unsued: "foo" }
{ _id: "contact-2", email: "jane@example.com", first_name: "Jane", last_name: "Example", unsued: "foo", holdingAccount: true }
{ _id: "contact-3", email: "jim@example.com", first_name: "Jim", last_name: "Example", unsued: "foo" }
]
2016-09-07 15:40:49 +00:00
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@user_id)
@ContactManager.getContactIds = sinon.stub().callsArgWith(2, null, @contact_ids)
@UserGetter.getUsers = sinon.stub().callsArgWith(2, null, @contacts)
@Modules.hooks.fire = sinon.stub().callsArg(3)
2016-09-07 15:40:49 +00:00
@ContactController.getContacts @req, @res, @next
2016-09-07 15:40:49 +00:00
it "should look up the logged in user id", ->
@AuthenticationController.getLoggedInUserId
.calledWith(@req)
.should.equal true
2016-09-07 15:40:49 +00:00
it "should get the users contact ids", ->
@ContactManager.getContactIds
.calledWith(@user_id, { limit: 50 })
.should.equal true
2016-09-07 15:40:49 +00:00
it "should populate the users contacts ids", ->
@UserGetter.getUsers
.calledWith(@contact_ids, { email: 1, first_name: 1, last_name: 1, holdingAccount: 1 })
.should.equal true
2016-09-07 15:40:49 +00:00
it "should fire the getContact module hook", ->
@Modules.hooks.fire
.calledWith("getContacts", @user_id)
.should.equal true
2016-09-07 15:40:49 +00:00
it "should return a formatted list of contacts in contact list order, without holding accounts", ->
@res.send.args[0][0].contacts.should.deep.equal [
{ id: "contact-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", type: "user" }
{ id: "contact-3", email: "jim@example.com", first_name: "Jim", last_name: "Example", type: "user" }
]