overleaf/services/web/test/unit/coffee/User/UserEmailsControllerTests.coffee

211 lines
6.4 KiB
CoffeeScript
Raw Normal View History

2018-06-08 17:05:19 +00:00
sinon = require('sinon')
assertCalledWith = sinon.assert.calledWith
assertNotCalled = sinon.assert.notCalled
chai = require('chai')
should = chai.should()
assert = chai.assert
modulePath = "../../../../app/js/Features/User/UserEmailsController.js"
SandboxedModule = require('sandboxed-module')
MockRequest = require "../helpers/MockRequest"
MockResponse = require "../helpers/MockResponse"
Errors = require("../../../../app/js/Features/Errors/Errors")
2018-06-08 17:05:19 +00:00
describe "UserEmailsController", ->
beforeEach ->
@req = new MockRequest()
@user =
_id: 'mock-user-id'
@UserGetter =
getUserFullEmails: sinon.stub()
@AuthenticationController =
getLoggedInUserId: sinon.stub().returns(@user._id)
@UserUpdater =
addEmailAddress: sinon.stub()
removeEmailAddress: sinon.stub()
setDefaultEmailAddress: sinon.stub()
updateV1AndSetDefaultEmailAddress: sinon.stub()
2018-06-08 17:05:19 +00:00
@EmailHelper =
parseEmail: sinon.stub()
2018-07-05 08:46:06 +00:00
@endorseAffiliation = sinon.stub().yields()
2018-06-08 17:05:19 +00:00
@UserEmailsController = SandboxedModule.require modulePath, requires:
"../Authentication/AuthenticationController": @AuthenticationController
"./UserGetter": @UserGetter
"./UserUpdater": @UserUpdater
"../Helpers/EmailHelper": @EmailHelper
"./UserEmailsConfirmationHandler": @UserEmailsConfirmationHandler = {}
2018-07-10 19:49:24 +00:00
"../Institutions/InstitutionsAPI": endorseAffiliation: @endorseAffiliation
"../Errors/Errors": Errors
2018-06-08 17:05:19 +00:00
"logger-sharelatex":
log: -> console.log(arguments)
err: ->
describe 'List', ->
beforeEach ->
it 'lists emails', (done) ->
fullEmails = [{some: 'data'}]
@UserGetter.getUserFullEmails.callsArgWith 1, null, fullEmails
@UserEmailsController.list @req,
json: (response) =>
assert.deepEqual response, fullEmails
assertCalledWith @UserGetter.getUserFullEmails, @user._id
done()
describe 'Add', ->
beforeEach ->
@newEmail = 'new_email@baz.com'
@req.body =
email: @newEmail
university: { name: 'University Name' }
department: 'Department'
role: 'Role'
2018-06-08 17:05:19 +00:00
@EmailHelper.parseEmail.returns @newEmail
@UserEmailsConfirmationHandler.sendConfirmationEmail = sinon.stub().yields()
@UserUpdater.addEmailAddress.callsArgWith 3, null
2018-06-08 17:05:19 +00:00
it 'adds new email', (done) ->
2018-06-08 17:05:19 +00:00
@UserEmailsController.add @req,
sendStatus: (code) =>
code.should.equal 204
2018-06-08 17:05:19 +00:00
assertCalledWith @EmailHelper.parseEmail, @newEmail
assertCalledWith @UserUpdater.addEmailAddress, @user._id, @newEmail
affiliationOptions = @UserUpdater.addEmailAddress.lastCall.args[2]
Object.keys(affiliationOptions).length.should.equal 3
affiliationOptions.university.should.equal @req.body.university
affiliationOptions.department.should.equal @req.body.department
affiliationOptions.role.should.equal @req.body.role
2018-06-08 17:05:19 +00:00
done()
it 'sends an email confirmation', (done) ->
2018-06-08 17:05:19 +00:00
@UserEmailsController.add @req,
sendStatus: (code) =>
code.should.equal 204
assertCalledWith @UserEmailsConfirmationHandler.sendConfirmationEmail, @user._id, @newEmail
2018-06-08 17:05:19 +00:00
done()
it 'handles email parse error', (done) ->
@EmailHelper.parseEmail.returns null
2018-06-08 17:05:19 +00:00
@UserEmailsController.add @req,
sendStatus: (code) =>
code.should.equal 422
assertNotCalled @UserUpdater.addEmailAddress
2018-06-08 17:05:19 +00:00
done()
describe 'remove', ->
beforeEach ->
@email = 'email_to_remove@bar.com'
@req.body.email = @email
@EmailHelper.parseEmail.returns @email
it 'removes email', (done) ->
@UserUpdater.removeEmailAddress.callsArgWith 2, null
@UserEmailsController.remove @req,
sendStatus: (code) =>
code.should.equal 200
assertCalledWith @EmailHelper.parseEmail, @email
assertCalledWith @UserUpdater.removeEmailAddress, @user._id, @email
done()
it 'handles email parse error', (done) ->
@EmailHelper.parseEmail.returns null
@UserEmailsController.remove @req,
sendStatus: (code) =>
code.should.equal 422
assertNotCalled @UserUpdater.removeEmailAddress
done()
describe 'setDefault', ->
beforeEach ->
@email = "email_to_set_default@bar.com"
@req.body.email = @email
@EmailHelper.parseEmail.returns @email
it 'sets default email', (done) ->
@UserUpdater.updateV1AndSetDefaultEmailAddress.callsArgWith 2, null
2018-06-08 17:05:19 +00:00
@UserEmailsController.setDefault @req,
sendStatus: (code) =>
code.should.equal 200
assertCalledWith @EmailHelper.parseEmail, @email
assertCalledWith @UserUpdater.updateV1AndSetDefaultEmailAddress, @user._id, @email
2018-06-08 17:05:19 +00:00
done()
it 'handles email parse error', (done) ->
@EmailHelper.parseEmail.returns null
@UserEmailsController.setDefault @req,
sendStatus: (code) =>
code.should.equal 422
assertNotCalled @UserUpdater.setDefaultEmailAddress
done()
2018-07-05 08:46:06 +00:00
describe 'endorse', ->
beforeEach ->
@email = 'email_to_endorse@bar.com'
@req.body.email = @email
@EmailHelper.parseEmail.returns @email
it 'endorses affiliation', (done) ->
@req.body.role = 'Role'
@req.body.department = 'Department'
@UserEmailsController.endorse @req,
sendStatus: (code) =>
code.should.equal 204
assertCalledWith @endorseAffiliation, @user._id, @email, 'Role', 'Department'
done()
describe 'confirm', ->
beforeEach ->
@UserEmailsConfirmationHandler.confirmEmailFromToken = sinon.stub().yields()
@res =
sendStatus: sinon.stub()
json: sinon.stub()
@res.status = sinon.stub().returns(@res)
@next = sinon.stub()
@token = 'mock-token'
@req.body = token: @token
describe 'successfully', ->
beforeEach ->
@UserEmailsController.confirm @req, @res, @next
it 'should confirm the email from the token', ->
@UserEmailsConfirmationHandler.confirmEmailFromToken
.calledWith(@token)
.should.equal true
it 'should return a 200 status', ->
@res.sendStatus.calledWith(200).should.equal true
describe 'without a token', ->
beforeEach ->
@req.body.token = null
@UserEmailsController.confirm @req, @res, @next
it 'should return a 422 status', ->
@res.sendStatus.calledWith(422).should.equal true
describe 'when confirming fails', ->
beforeEach ->
@UserEmailsConfirmationHandler.confirmEmailFromToken = sinon.stub().yields(
new Errors.NotFoundError('not found')
)
@UserEmailsController.confirm @req, @res, @next
it 'should return a 404 error code with a message', ->
@res.status.calledWith(404).should.equal true
@res.json.calledWith({
message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
}).should.equal true
2018-06-08 17:05:19 +00:00