|
|
@@ -122,168 +122,6 @@ describe('UserEmailsController', function () {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- describe('Add', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.newEmail = 'new_email@baz.com'
|
|
|
- this.req.body = {
|
|
|
- email: this.newEmail,
|
|
|
- university: { name: 'University Name' },
|
|
|
- department: 'Department',
|
|
|
- role: 'Role',
|
|
|
- }
|
|
|
- this.EmailHelper.parseEmail.returns(this.newEmail)
|
|
|
- this.UserEmailsConfirmationHandler.sendConfirmationEmail = sinon
|
|
|
- .stub()
|
|
|
- .yields()
|
|
|
- })
|
|
|
-
|
|
|
- it('passed audit log to addEmailAddress', function (done) {
|
|
|
- this.res.sendStatus = sinon.stub()
|
|
|
- this.res.sendStatus.callsFake(() => {
|
|
|
- const addCall = this.UserUpdater.promises.addEmailAddress.lastCall
|
|
|
- expect(addCall.args[3]).to.deep.equal({
|
|
|
- initiatorId: this.user._id,
|
|
|
- ipAddress: this.req.ip,
|
|
|
- })
|
|
|
- done()
|
|
|
- })
|
|
|
- this.UserEmailsController.add(this.req, this.res)
|
|
|
- })
|
|
|
-
|
|
|
- it('adds new email', function (done) {
|
|
|
- this.UserEmailsController.add(
|
|
|
- this.req,
|
|
|
- {
|
|
|
- sendStatus: code => {
|
|
|
- code.should.equal(204)
|
|
|
- assertCalledWith(this.EmailHelper.parseEmail, this.newEmail)
|
|
|
- assertCalledWith(
|
|
|
- this.UserUpdater.promises.addEmailAddress,
|
|
|
- this.user._id,
|
|
|
- this.newEmail
|
|
|
- )
|
|
|
-
|
|
|
- const affiliationOptions =
|
|
|
- this.UserUpdater.promises.addEmailAddress.lastCall.args[2]
|
|
|
- Object.keys(affiliationOptions).length.should.equal(3)
|
|
|
- affiliationOptions.university.should.equal(this.req.body.university)
|
|
|
- affiliationOptions.department.should.equal(this.req.body.department)
|
|
|
- affiliationOptions.role.should.equal(this.req.body.role)
|
|
|
-
|
|
|
- done()
|
|
|
- },
|
|
|
- },
|
|
|
- this.next
|
|
|
- )
|
|
|
- })
|
|
|
-
|
|
|
- it('sends a security alert email', function (done) {
|
|
|
- this.res.sendStatus = sinon.stub()
|
|
|
- this.res.sendStatus.callsFake(() => {
|
|
|
- const emailCall = this.EmailHandler.promises.sendEmail.getCall(0)
|
|
|
- emailCall.args[0].should.to.equal('securityAlert')
|
|
|
- emailCall.args[1].to.should.equal(this.user.email)
|
|
|
- emailCall.args[1].actionDescribed.should.contain(
|
|
|
- 'a secondary email address'
|
|
|
- )
|
|
|
- emailCall.args[1].to.should.equal(this.user.email)
|
|
|
- emailCall.args[1].message[0].should.contain(this.newEmail)
|
|
|
- done()
|
|
|
- })
|
|
|
-
|
|
|
- this.UserEmailsController.add(this.req, this.res)
|
|
|
- })
|
|
|
-
|
|
|
- it('sends an email confirmation', function (done) {
|
|
|
- this.UserEmailsController.add(
|
|
|
- this.req,
|
|
|
- {
|
|
|
- sendStatus: code => {
|
|
|
- code.should.equal(204)
|
|
|
- assertCalledWith(
|
|
|
- this.UserEmailsConfirmationHandler.promises.sendConfirmationEmail,
|
|
|
- this.user._id,
|
|
|
- this.newEmail
|
|
|
- )
|
|
|
- done()
|
|
|
- },
|
|
|
- },
|
|
|
- this.next
|
|
|
- )
|
|
|
- })
|
|
|
-
|
|
|
- it('handles email parse error', function (done) {
|
|
|
- this.EmailHelper.parseEmail.returns(null)
|
|
|
- this.UserEmailsController.add(
|
|
|
- this.req,
|
|
|
- {
|
|
|
- sendStatus: code => {
|
|
|
- code.should.equal(422)
|
|
|
- assertNotCalled(this.UserUpdater.promises.addEmailAddress)
|
|
|
- done()
|
|
|
- },
|
|
|
- },
|
|
|
- this.next
|
|
|
- )
|
|
|
- })
|
|
|
-
|
|
|
- it('should pass the error to the next handler when adding the email fails', function (done) {
|
|
|
- this.UserUpdater.promises.addEmailAddress.rejects(new Error())
|
|
|
- this.UserEmailsController.add(this.req, this.res, error => {
|
|
|
- expect(error).to.be.instanceof(Error)
|
|
|
- done()
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- it('should call the HTTP conflict handler when the email already exists', function (done) {
|
|
|
- this.UserUpdater.promises.addEmailAddress.rejects(
|
|
|
- new Errors.EmailExistsError()
|
|
|
- )
|
|
|
- this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
|
|
|
- req.should.exist
|
|
|
- res.should.exist
|
|
|
- message.should.equal('email_already_registered')
|
|
|
- done()
|
|
|
- })
|
|
|
- this.UserEmailsController.add(this.req, this.res, this.next)
|
|
|
- })
|
|
|
-
|
|
|
- it("should call the HTTP conflict handler when there's a domain matching error", function (done) {
|
|
|
- this.UserUpdater.promises.addEmailAddress.rejects(
|
|
|
- new Error('422: Email does not belong to university')
|
|
|
- )
|
|
|
- this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
|
|
|
- req.should.exist
|
|
|
- res.should.exist
|
|
|
- message.should.equal('email_does_not_belong_to_university')
|
|
|
- done()
|
|
|
- })
|
|
|
- this.UserEmailsController.add(this.req, this.res, this.next)
|
|
|
- })
|
|
|
-
|
|
|
- it('should fail to add new emails when the limit has been reached', function (done) {
|
|
|
- this.user.emails = []
|
|
|
- for (let i = 0; i < 10; i++) {
|
|
|
- this.user.emails.push({ email: `example${i}@overleaf.com` })
|
|
|
- }
|
|
|
- this.UserEmailsController.add(
|
|
|
- this.req,
|
|
|
- {
|
|
|
- status: code => {
|
|
|
- expect(code).to.equal(422)
|
|
|
- return {
|
|
|
- json: error => {
|
|
|
- expect(error.message).to.equal('secondary email limit exceeded')
|
|
|
- done()
|
|
|
- },
|
|
|
- }
|
|
|
- },
|
|
|
- },
|
|
|
- this.next
|
|
|
- )
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
describe('addWithConfirmationCode', function () {
|
|
|
beforeEach(function () {
|
|
|
this.newEmail = 'new_email@baz.com'
|