| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- const should = require('chai').should()
- const SandboxedModule = require('sandboxed-module')
- const path = require('path')
- const sinon = require('sinon')
- const modulePath = path.join(
- __dirname,
- '../../../../app/src/Features/User/UserUpdater'
- )
- const tk = require('timekeeper')
- const { expect } = require('chai')
- describe('UserUpdater', function() {
- beforeEach(function() {
- tk.freeze(Date.now())
- this.mongojs = {
- db: {},
- ObjectId(id) {
- return id
- }
- }
- this.UserGetter = {
- getUserEmail: sinon.stub(),
- getUserByAnyEmail: sinon.stub(),
- promises: {
- ensureUniqueEmailAddress: sinon.stub(),
- getUser: sinon.stub()
- }
- }
- this.logger = {
- error: sinon.stub(),
- log() {},
- warn: sinon.stub()
- }
- this.addAffiliation = sinon.stub().yields()
- this.removeAffiliation = sinon.stub().callsArgWith(2, null)
- this.refreshFeatures = sinon.stub().yields()
- this.NewsletterManager = {
- promises: {
- changeEmail: sinon.stub()
- }
- }
- this.RecurlyWrapper = {
- promises: {
- updateAccountEmailAddress: sinon.stub()
- }
- }
- this.UserUpdater = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- 'logger-sharelatex': this.logger,
- '../../infrastructure/mongojs': this.mongojs,
- 'metrics-sharelatex': {
- timeAsyncMethod: sinon.stub()
- },
- './UserGetter': this.UserGetter,
- '../Institutions/InstitutionsAPI': (this.InstitutionsAPI = {
- addAffiliation: this.addAffiliation,
- removeAffiliation: this.removeAffiliation,
- promises: {
- addAffiliation: sinon.stub()
- }
- }),
- '../Email/EmailHandler': (this.EmailHandler = {
- promises: {
- sendEmail: sinon.stub()
- }
- }),
- '../../infrastructure/Features': (this.Features = {
- hasFeature: sinon.stub().returns(false)
- }),
- '../Subscription/FeaturesUpdater': {
- refreshFeatures: this.refreshFeatures
- },
- 'settings-sharelatex': (this.settings = {}),
- request: (this.request = {}),
- '../Newsletter/NewsletterManager': this.NewsletterManager,
- '../Subscription/RecurlyWrapper': this.RecurlyWrapper,
- './UserAuditLogHandler': (this.UserAuditLogHandler = {
- promises: {
- addEntry: sinon.stub().resolves()
- }
- })
- }
- })
- this.stubbedUser = {
- _id: '3131231',
- name: 'bob',
- email: 'hello@world.com'
- }
- this.newEmail = 'bob@bob.com'
- this.callback = sinon.stub()
- })
- afterEach(function() {
- return tk.reset()
- })
- describe('addAffiliationForNewUser', function(done) {
- beforeEach(function() {
- this.UserUpdater.updateUser = sinon
- .stub()
- .callsArgWith(2, null, { n: 1, nModified: 1, ok: 1 })
- })
- it('should not remove affiliationUnchecked flag if v1 returns an error', function(done) {
- this.addAffiliation.yields(true)
- this.UserUpdater.addAffiliationForNewUser(
- this.stubbedUser._id,
- this.newEmail,
- (error, updated) => {
- expect(error).to.exist
- expect(updated).to.be.undefined
- sinon.assert.notCalled(this.UserUpdater.updateUser)
- done()
- }
- )
- })
- it('should remove affiliationUnchecked flag if v1 does not return an error', function(done) {
- this.addAffiliation.yields()
- this.UserUpdater.addAffiliationForNewUser(
- this.stubbedUser._id,
- this.newEmail,
- error => {
- should.not.exist(error)
- sinon.assert.calledOnce(this.UserUpdater.updateUser)
- sinon.assert.calledWithMatch(
- this.UserUpdater.updateUser,
- { _id: this.stubbedUser._id, 'emails.email': this.newEmail },
- { $unset: { 'emails.$.affiliationUnchecked': 1 } }
- )
- done()
- }
- )
- })
- })
- describe('changeEmailAddress', function() {
- beforeEach(function() {
- this.auditLog = {
- initiatorId: 'abc123',
- ipAddress: '0:0:0:0'
- }
- this.UserGetter.getUserEmail.callsArgWith(1, null, this.stubbedUser.email)
- this.UserUpdater.addEmailAddress = sinon.stub().callsArgWith(4)
- this.UserUpdater.setDefaultEmailAddress = sinon.stub().yields()
- this.UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
- })
- it('change email', function(done) {
- this.UserUpdater.changeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- this.auditLog,
- err => {
- should.not.exist(err)
- this.UserUpdater.addEmailAddress
- .calledWith(this.stubbedUser._id, this.newEmail, {}, this.auditLog)
- .should.equal(true)
- this.UserUpdater.setDefaultEmailAddress
- .calledWith(
- this.stubbedUser._id,
- this.newEmail,
- true,
- this.auditLog,
- true
- )
- .should.equal(true)
- this.UserUpdater.removeEmailAddress
- .calledWith(this.stubbedUser._id, this.stubbedUser.email)
- .should.equal(true)
- done()
- }
- )
- })
- it('validates email', function(done) {
- this.UserUpdater.changeEmailAddress(
- this.stubbedUser._id,
- 'foo',
- this.auditLog,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('handle error', function(done) {
- this.UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
- this.UserUpdater.changeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- this.auditLog,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- })
- describe('addEmailAddress', function() {
- beforeEach(function() {
- this.UserGetter.promises.ensureUniqueEmailAddress = sinon
- .stub()
- .resolves()
- this.UserUpdater.promises.updateUser = sinon.stub().resolves()
- })
- it('add email', function(done) {
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- {},
- { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
- err => {
- this.UserGetter.promises.ensureUniqueEmailAddress.called.should.equal(
- true
- )
- expect(err).to.not.exist
- const reversedHostname = this.newEmail
- .split('@')[1]
- .split('')
- .reverse()
- .join('')
- this.UserUpdater.promises.updateUser
- .calledWith(this.stubbedUser._id, {
- $push: {
- emails: {
- email: this.newEmail,
- createdAt: sinon.match.date,
- reversedHostname
- }
- }
- })
- .should.equal(true)
- done()
- }
- )
- })
- it('add affiliation', function(done) {
- const affiliationOptions = {
- university: { id: 1 },
- role: 'Prof',
- department: 'Math'
- }
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- affiliationOptions,
- { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
- err => {
- should.not.exist(err)
- this.InstitutionsAPI.promises.addAffiliation.calledOnce.should.equal(
- true
- )
- const { args } = this.InstitutionsAPI.promises.addAffiliation.lastCall
- args[0].should.equal(this.stubbedUser._id)
- args[1].should.equal(this.newEmail)
- args[2].should.equal(affiliationOptions)
- done()
- }
- )
- })
- it('handle affiliation error', function(done) {
- this.InstitutionsAPI.promises.addAffiliation.rejects(new Error('nope'))
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- {},
- { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
- err => {
- should.exist(err)
- this.UserUpdater.promises.updateUser.called.should.equal(false)
- done()
- }
- )
- })
- it('validates email', function(done) {
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- 'bar',
- {},
- { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('updates the audit log', function(done) {
- this.ip = '127:0:0:0'
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- {},
- { initiatorId: this.stubbedUser._id, ipAddress: this.ip },
- error => {
- expect(error).to.not.exist
- this.InstitutionsAPI.promises.addAffiliation.calledOnce.should.equal(
- true
- )
- const { args } = this.UserAuditLogHandler.promises.addEntry.lastCall
- expect(args[0]).to.equal(this.stubbedUser._id)
- expect(args[1]).to.equal('add-email')
- expect(args[2]).to.equal(this.stubbedUser._id)
- expect(args[3]).to.equal(this.ip)
- expect(args[4]).to.deep.equal({
- newSecondaryEmail: this.newEmail
- })
- done()
- }
- )
- })
- describe('errors', function() {
- describe('via UserAuditLogHandler', function() {
- const anError = new Error('oops')
- beforeEach(function() {
- this.UserAuditLogHandler.promises.addEntry.throws(anError)
- })
- it('should not add email and should return error', function(done) {
- this.UserUpdater.addEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- {},
- { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
- error => {
- expect(error).to.exist
- expect(error).to.equal(anError)
- expect(this.UserUpdater.promises.updateUser).to.not.have.been
- .called
- done()
- }
- )
- })
- })
- })
- })
- describe('removeEmailAddress', function() {
- beforeEach(function() {
- this.UserUpdater.updateUser = sinon
- .stub()
- .callsArgWith(2, null, { nMatched: 1 })
- })
- it('remove email', function(done) {
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- this.UserUpdater.updateUser
- .calledWith(
- { _id: this.stubbedUser._id, email: { $ne: this.newEmail } },
- { $pull: { emails: { email: this.newEmail } } }
- )
- .should.equal(true)
- done()
- }
- )
- })
- it('remove affiliation', function(done) {
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- this.removeAffiliation.calledOnce.should.equal(true)
- const { args } = this.removeAffiliation.lastCall
- args[0].should.equal(this.stubbedUser._id)
- args[1].should.equal(this.newEmail)
- done()
- }
- )
- })
- it('refresh features', function(done) {
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
- done()
- }
- )
- })
- it('handle error', function(done) {
- this.UserUpdater.updateUser = sinon
- .stub()
- .callsArgWith(2, new Error('nope'))
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('handle missed update', function(done) {
- this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('handle affiliation error', function(done) {
- this.removeAffiliation.callsArgWith(2, new Error('nope'))
- this.UserUpdater.removeEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- this.UserUpdater.updateUser.called.should.equal(false)
- done()
- }
- )
- })
- it('validates email', function(done) {
- this.UserUpdater.removeEmailAddress(this.stubbedUser._id, 'baz', err => {
- should.exist(err)
- done()
- })
- })
- })
- describe('setDefaultEmailAddress', function() {
- beforeEach(function() {
- this.auditLog = {
- initiatorId: this.stubbedUser,
- ipAddress: '0:0:0:0'
- }
- this.stubbedUser.emails = [
- {
- email: this.newEmail,
- confirmedAt: new Date()
- }
- ]
- this.UserGetter.promises.getUser.resolves(this.stubbedUser)
- this.NewsletterManager.promises.changeEmail.callsArgWith(2, null)
- this.RecurlyWrapper.promises.updateAccountEmailAddress.resolves()
- })
- it('set default', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- err => {
- should.not.exist(err)
- this.UserUpdater.promises.updateUser
- .calledWith(
- { _id: this.stubbedUser._id, 'emails.email': this.newEmail },
- { $set: { email: this.newEmail } }
- )
- .should.equal(true)
- done()
- }
- )
- })
- it('set changed the email in newsletter', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- err => {
- should.not.exist(err)
- this.NewsletterManager.promises.changeEmail
- .calledWith(this.stubbedUser, this.newEmail)
- .should.equal(true)
- this.RecurlyWrapper.promises.updateAccountEmailAddress
- .calledWith(this.stubbedUser._id, this.newEmail)
- .should.equal(true)
- done()
- }
- )
- })
- it('handle error', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub().rejects(Error('nope'))
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('handle missed update', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('validates email', function(done) {
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- '.edu',
- false,
- this.auditLog,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('updates audit log', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- error => {
- expect(error).to.not.exist
- expect(
- this.UserAuditLogHandler.promises.addEntry
- ).to.have.been.calledWith(
- this.stubbedUser._id,
- 'change-primary-email',
- this.auditLog.initiatorId,
- this.auditLog.ipAddress,
- {
- newPrimaryEmail: this.newEmail,
- oldPrimaryEmail: this.stubbedUser.email
- }
- )
- done()
- }
- )
- })
- it('blocks email update if audit log returns an error', function(done) {
- this.UserUpdater.promises.updateUser = sinon.stub()
- this.UserAuditLogHandler.promises.addEntry.rejects(new Error('oops'))
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- error => {
- expect(error).to.exist
- expect(this.UserUpdater.promises.updateUser).to.not.have.been.called
- done()
- }
- )
- })
- describe('when email not confirmed', function() {
- beforeEach(function() {
- this.stubbedUser.emails = [
- {
- email: this.newEmail,
- confirmedAt: null
- }
- ]
- this.UserUpdater.promises.updateUser = sinon.stub()
- })
- it('should callback with error', function() {
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- error => {
- expect(error).to.exist
- expect(error.name).to.equal('UnconfirmedEmailError')
- this.UserUpdater.promises.updateUser.callCount.should.equal(0)
- this.NewsletterManager.promises.changeEmail.callCount.should.equal(
- 0
- )
- }
- )
- })
- })
- describe('when email does not belong to user', function() {
- beforeEach(function() {
- this.stubbedUser.emails = []
- this.UserGetter.promises.getUser.resolves(this.stubbedUser)
- this.UserUpdater.promises.updateUser = sinon.stub()
- })
- it('should callback with error', function() {
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- error => {
- expect(error).to.exist
- expect(error.name).to.equal('Error')
- this.UserUpdater.promises.updateUser.callCount.should.equal(0)
- this.NewsletterManager.promises.changeEmail.callCount.should.equal(
- 0
- )
- }
- )
- })
- })
- describe('security alert', function() {
- it('should be sent to old and new email when sendSecurityAlert=true', function(done) {
- // this.UserGetter.promises.getUser.resolves(this.stubbedUser)
- this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- true,
- error => {
- expect(error).to.not.exist
- this.EmailHandler.promises.sendEmail.callCount.should.equal(2)
- const toOldEmailAlert = this.EmailHandler.promises.sendEmail
- .firstCall
- expect(toOldEmailAlert.args[0]).to.equal('securityAlert')
- const toNewEmailAlert = this.EmailHandler.promises.sendEmail
- .lastCall
- expect(toOldEmailAlert.args[1].to).to.equal(this.stubbedUser.email)
- expect(toNewEmailAlert.args[0]).to.equal('securityAlert')
- expect(toNewEmailAlert.args[1].to).to.equal(this.newEmail)
- done()
- }
- )
- })
- describe('errors', function() {
- const anError = new Error('oops')
- describe('EmailHandler', function() {
- beforeEach(function() {
- this.EmailHandler.promises.sendEmail.rejects(anError)
- this.UserUpdater.promises.updateUser = sinon
- .stub()
- .resolves({ n: 1 })
- })
- it('should log but not pass back the error', function(done) {
- this.UserUpdater.setDefaultEmailAddress(
- this.stubbedUser._id,
- this.newEmail,
- false,
- this.auditLog,
- true,
- error => {
- expect(error).to.not.exist
- const loggerCall = this.logger.error.firstCall
- expect(loggerCall.args[0]).to.deep.equal({
- error: anError,
- userId: this.stubbedUser._id
- })
- expect(loggerCall.args[1]).to.contain(
- 'could not send security alert email when primary email changed'
- )
- done()
- }
- )
- })
- })
- })
- })
- })
- describe('confirmEmail', function() {
- beforeEach(function() {
- this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 1 })
- })
- it('should update the email record', function(done) {
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- this.UserUpdater.updateUser
- .calledWith(
- { _id: this.stubbedUser._id, 'emails.email': this.newEmail },
- { $set: { 'emails.$.confirmedAt': new Date() } }
- )
- .should.equal(true)
- done()
- }
- )
- })
- it('add affiliation', function(done) {
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- this.addAffiliation.calledOnce.should.equal(true)
- sinon.assert.calledWith(
- this.addAffiliation,
- this.stubbedUser._id,
- this.newEmail,
- { confirmedAt: new Date() }
- )
- done()
- }
- )
- })
- it('handle error', function(done) {
- this.UserUpdater.updateUser = sinon
- .stub()
- .callsArgWith(2, new Error('nope'))
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('handle missed update', function(done) {
- this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- done()
- }
- )
- })
- it('validates email', function(done) {
- this.UserUpdater.confirmEmail(this.stubbedUser._id, '@', err => {
- should.exist(err)
- done()
- })
- })
- it('handle affiliation error', function(done) {
- this.addAffiliation.callsArgWith(3, new Error('nope'))
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.exist(err)
- this.UserUpdater.updateUser.called.should.equal(false)
- done()
- }
- )
- })
- it('refresh features', function(done) {
- this.UserUpdater.confirmEmail(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- should.not.exist(err)
- sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
- done()
- }
- )
- })
- })
- })
|