UserUpdaterTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const modulePath = path.join(
  5. __dirname,
  6. '../../../../app/src/Features/User/UserUpdater'
  7. )
  8. const tk = require('timekeeper')
  9. const { expect } = require('chai')
  10. const { normalizeQuery } = require('../../../../app/src/Features/Helpers/Mongo')
  11. describe('UserUpdater', function () {
  12. beforeEach(function () {
  13. tk.freeze(Date.now())
  14. this.mongodb = {
  15. db: {},
  16. ObjectId(id) {
  17. return id
  18. },
  19. }
  20. this.UserGetter = {
  21. getUserEmail: sinon.stub(),
  22. getUserByAnyEmail: sinon.stub(),
  23. promises: {
  24. ensureUniqueEmailAddress: sinon.stub(),
  25. getUser: sinon.stub(),
  26. },
  27. }
  28. this.addAffiliation = sinon.stub().yields()
  29. this.removeAffiliation = sinon.stub().callsArgWith(2, null)
  30. this.refreshFeatures = sinon.stub().yields()
  31. this.NewsletterManager = {
  32. promises: {
  33. changeEmail: sinon.stub(),
  34. },
  35. }
  36. this.RecurlyWrapper = {
  37. promises: {
  38. updateAccountEmailAddress: sinon.stub(),
  39. },
  40. }
  41. this.UserUpdater = SandboxedModule.require(modulePath, {
  42. requires: {
  43. '../Helpers/Mongo': { normalizeQuery },
  44. '../../infrastructure/mongodb': this.mongodb,
  45. '@overleaf/metrics': {
  46. timeAsyncMethod: sinon.stub(),
  47. },
  48. './UserGetter': this.UserGetter,
  49. '../Institutions/InstitutionsAPI': (this.InstitutionsAPI = {
  50. addAffiliation: this.addAffiliation,
  51. removeAffiliation: this.removeAffiliation,
  52. promises: {
  53. addAffiliation: sinon.stub(),
  54. },
  55. }),
  56. '../Email/EmailHandler': (this.EmailHandler = {
  57. promises: {
  58. sendEmail: sinon.stub(),
  59. },
  60. }),
  61. '../../infrastructure/Features': (this.Features = {
  62. hasFeature: sinon.stub().returns(false),
  63. }),
  64. '../Subscription/FeaturesUpdater': (this.FeaturesUpdater = {
  65. refreshFeatures: this.refreshFeatures,
  66. promises: {
  67. refreshFeatures: sinon.stub().resolves(),
  68. },
  69. }),
  70. 'settings-sharelatex': (this.settings = {}),
  71. request: (this.request = {}),
  72. '../Newsletter/NewsletterManager': this.NewsletterManager,
  73. '../Subscription/RecurlyWrapper': this.RecurlyWrapper,
  74. './UserAuditLogHandler': (this.UserAuditLogHandler = {
  75. promises: {
  76. addEntry: sinon.stub().resolves(),
  77. },
  78. }),
  79. },
  80. })
  81. this.stubbedUserEmail = 'hello@world.com'
  82. this.stubbedUser = {
  83. _id: '3131231',
  84. name: 'bob',
  85. email: this.stubbedUserEmail,
  86. emails: [
  87. {
  88. email: this.stubbedUserEmail,
  89. },
  90. ],
  91. }
  92. this.newEmail = 'bob@bob.com'
  93. this.callback = sinon.stub()
  94. })
  95. afterEach(function () {
  96. return tk.reset()
  97. })
  98. describe('addAffiliationForNewUser', function (done) {
  99. beforeEach(function () {
  100. this.UserUpdater.updateUser = sinon
  101. .stub()
  102. .callsArgWith(2, null, { n: 1, nModified: 1, ok: 1 })
  103. })
  104. it('should not remove affiliationUnchecked flag if v1 returns an error', function (done) {
  105. this.addAffiliation.yields(true)
  106. this.UserUpdater.addAffiliationForNewUser(
  107. this.stubbedUser._id,
  108. this.newEmail,
  109. (error, updated) => {
  110. expect(error).to.exist
  111. expect(updated).to.be.undefined
  112. sinon.assert.notCalled(this.UserUpdater.updateUser)
  113. done()
  114. }
  115. )
  116. })
  117. it('should remove affiliationUnchecked flag if v1 does not return an error', function (done) {
  118. this.addAffiliation.yields()
  119. this.UserUpdater.addAffiliationForNewUser(
  120. this.stubbedUser._id,
  121. this.newEmail,
  122. error => {
  123. expect(error).not.to.exist
  124. sinon.assert.calledOnce(this.UserUpdater.updateUser)
  125. sinon.assert.calledWithMatch(
  126. this.UserUpdater.updateUser,
  127. { _id: this.stubbedUser._id, 'emails.email': this.newEmail },
  128. { $unset: { 'emails.$.affiliationUnchecked': 1 } }
  129. )
  130. done()
  131. }
  132. )
  133. })
  134. })
  135. describe('changeEmailAddress', function () {
  136. beforeEach(function () {
  137. this.auditLog = {
  138. initiatorId: 'abc123',
  139. ipAddress: '0:0:0:0',
  140. }
  141. this.UserGetter.getUserEmail.callsArgWith(1, null, this.stubbedUser.email)
  142. this.UserUpdater.addEmailAddress = sinon.stub().callsArgWith(4)
  143. this.UserUpdater.setDefaultEmailAddress = sinon.stub().yields()
  144. this.UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
  145. })
  146. it('change email', function (done) {
  147. this.UserUpdater.changeEmailAddress(
  148. this.stubbedUser._id,
  149. this.newEmail,
  150. this.auditLog,
  151. err => {
  152. expect(err).not.to.exist
  153. this.UserUpdater.addEmailAddress
  154. .calledWith(this.stubbedUser._id, this.newEmail, {}, this.auditLog)
  155. .should.equal(true)
  156. this.UserUpdater.setDefaultEmailAddress
  157. .calledWith(
  158. this.stubbedUser._id,
  159. this.newEmail,
  160. true,
  161. this.auditLog,
  162. true
  163. )
  164. .should.equal(true)
  165. this.UserUpdater.removeEmailAddress
  166. .calledWith(this.stubbedUser._id, this.stubbedUser.email)
  167. .should.equal(true)
  168. done()
  169. }
  170. )
  171. })
  172. it('validates email', function (done) {
  173. this.UserUpdater.changeEmailAddress(
  174. this.stubbedUser._id,
  175. 'foo',
  176. this.auditLog,
  177. err => {
  178. expect(err).to.exist
  179. done()
  180. }
  181. )
  182. })
  183. it('handle error', function (done) {
  184. this.UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
  185. this.UserUpdater.changeEmailAddress(
  186. this.stubbedUser._id,
  187. this.newEmail,
  188. this.auditLog,
  189. err => {
  190. expect(err).to.exist
  191. done()
  192. }
  193. )
  194. })
  195. })
  196. describe('addEmailAddress', function () {
  197. beforeEach(function () {
  198. this.UserGetter.promises.ensureUniqueEmailAddress = sinon
  199. .stub()
  200. .resolves()
  201. this.UserUpdater.promises.updateUser = sinon.stub().resolves()
  202. })
  203. it('add email', function (done) {
  204. this.UserUpdater.addEmailAddress(
  205. this.stubbedUser._id,
  206. this.newEmail,
  207. {},
  208. { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
  209. err => {
  210. this.UserGetter.promises.ensureUniqueEmailAddress.called.should.equal(
  211. true
  212. )
  213. expect(err).to.not.exist
  214. const reversedHostname = this.newEmail
  215. .split('@')[1]
  216. .split('')
  217. .reverse()
  218. .join('')
  219. this.UserUpdater.promises.updateUser
  220. .calledWith(this.stubbedUser._id, {
  221. $push: {
  222. emails: {
  223. email: this.newEmail,
  224. createdAt: sinon.match.date,
  225. reversedHostname,
  226. },
  227. },
  228. })
  229. .should.equal(true)
  230. done()
  231. }
  232. )
  233. })
  234. it('add affiliation', function (done) {
  235. const affiliationOptions = {
  236. university: { id: 1 },
  237. role: 'Prof',
  238. department: 'Math',
  239. }
  240. this.UserUpdater.addEmailAddress(
  241. this.stubbedUser._id,
  242. this.newEmail,
  243. affiliationOptions,
  244. { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
  245. err => {
  246. expect(err).not.to.exist
  247. this.InstitutionsAPI.promises.addAffiliation.calledOnce.should.equal(
  248. true
  249. )
  250. const { args } = this.InstitutionsAPI.promises.addAffiliation.lastCall
  251. args[0].should.equal(this.stubbedUser._id)
  252. args[1].should.equal(this.newEmail)
  253. args[2].should.equal(affiliationOptions)
  254. done()
  255. }
  256. )
  257. })
  258. it('handle affiliation error', function (done) {
  259. this.InstitutionsAPI.promises.addAffiliation.rejects(new Error('nope'))
  260. this.UserUpdater.addEmailAddress(
  261. this.stubbedUser._id,
  262. this.newEmail,
  263. {},
  264. { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
  265. err => {
  266. expect(err).to.exist
  267. this.UserUpdater.promises.updateUser.called.should.equal(false)
  268. done()
  269. }
  270. )
  271. })
  272. it('validates email', function (done) {
  273. this.UserUpdater.addEmailAddress(
  274. this.stubbedUser._id,
  275. 'bar',
  276. {},
  277. { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
  278. err => {
  279. expect(err).to.exist
  280. done()
  281. }
  282. )
  283. })
  284. it('updates the audit log', function (done) {
  285. this.ip = '127:0:0:0'
  286. this.UserUpdater.addEmailAddress(
  287. this.stubbedUser._id,
  288. this.newEmail,
  289. {},
  290. { initiatorId: this.stubbedUser._id, ipAddress: this.ip },
  291. error => {
  292. expect(error).to.not.exist
  293. this.InstitutionsAPI.promises.addAffiliation.calledOnce.should.equal(
  294. true
  295. )
  296. const { args } = this.UserAuditLogHandler.promises.addEntry.lastCall
  297. expect(args[0]).to.equal(this.stubbedUser._id)
  298. expect(args[1]).to.equal('add-email')
  299. expect(args[2]).to.equal(this.stubbedUser._id)
  300. expect(args[3]).to.equal(this.ip)
  301. expect(args[4]).to.deep.equal({
  302. newSecondaryEmail: this.newEmail,
  303. })
  304. done()
  305. }
  306. )
  307. })
  308. describe('errors', function () {
  309. describe('via UserAuditLogHandler', function () {
  310. const anError = new Error('oops')
  311. beforeEach(function () {
  312. this.UserAuditLogHandler.promises.addEntry.throws(anError)
  313. })
  314. it('should not add email and should return error', function (done) {
  315. this.UserUpdater.addEmailAddress(
  316. this.stubbedUser._id,
  317. this.newEmail,
  318. {},
  319. { initiatorId: this.stubbedUser._id, ipAddress: '127:0:0:0' },
  320. error => {
  321. expect(error).to.exist
  322. expect(error).to.equal(anError)
  323. expect(this.UserUpdater.promises.updateUser).to.not.have.been
  324. .called
  325. done()
  326. }
  327. )
  328. })
  329. })
  330. })
  331. })
  332. describe('removeEmailAddress', function () {
  333. beforeEach(function () {
  334. this.UserUpdater.updateUser = sinon
  335. .stub()
  336. .callsArgWith(2, null, { nMatched: 1 })
  337. })
  338. it('remove email', function (done) {
  339. this.UserUpdater.removeEmailAddress(
  340. this.stubbedUser._id,
  341. this.newEmail,
  342. err => {
  343. expect(err).not.to.exist
  344. this.UserUpdater.updateUser
  345. .calledWith(
  346. { _id: this.stubbedUser._id, email: { $ne: this.newEmail } },
  347. { $pull: { emails: { email: this.newEmail } } }
  348. )
  349. .should.equal(true)
  350. done()
  351. }
  352. )
  353. })
  354. it('remove affiliation', function (done) {
  355. this.UserUpdater.removeEmailAddress(
  356. this.stubbedUser._id,
  357. this.newEmail,
  358. err => {
  359. expect(err).not.to.exist
  360. this.removeAffiliation.calledOnce.should.equal(true)
  361. const { args } = this.removeAffiliation.lastCall
  362. args[0].should.equal(this.stubbedUser._id)
  363. args[1].should.equal(this.newEmail)
  364. done()
  365. }
  366. )
  367. })
  368. it('refresh features', function (done) {
  369. this.UserUpdater.removeEmailAddress(
  370. this.stubbedUser._id,
  371. this.newEmail,
  372. err => {
  373. expect(err).not.to.exist
  374. sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
  375. done()
  376. }
  377. )
  378. })
  379. it('handle error', function (done) {
  380. this.UserUpdater.updateUser = sinon
  381. .stub()
  382. .callsArgWith(2, new Error('nope'))
  383. this.UserUpdater.removeEmailAddress(
  384. this.stubbedUser._id,
  385. this.newEmail,
  386. err => {
  387. expect(err).to.exist
  388. done()
  389. }
  390. )
  391. })
  392. it('handle missed update', function (done) {
  393. this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
  394. this.UserUpdater.removeEmailAddress(
  395. this.stubbedUser._id,
  396. this.newEmail,
  397. err => {
  398. expect(err).to.exist
  399. done()
  400. }
  401. )
  402. })
  403. it('handle affiliation error', function (done) {
  404. this.removeAffiliation.callsArgWith(2, new Error('nope'))
  405. this.UserUpdater.removeEmailAddress(
  406. this.stubbedUser._id,
  407. this.newEmail,
  408. err => {
  409. expect(err).to.exist
  410. this.UserUpdater.updateUser.called.should.equal(false)
  411. done()
  412. }
  413. )
  414. })
  415. it('validates email', function (done) {
  416. this.UserUpdater.removeEmailAddress(this.stubbedUser._id, 'baz', err => {
  417. expect(err).to.exist
  418. done()
  419. })
  420. })
  421. })
  422. describe('setDefaultEmailAddress', function () {
  423. beforeEach(function () {
  424. this.auditLog = {
  425. initiatorId: this.stubbedUser,
  426. ipAddress: '0:0:0:0',
  427. }
  428. this.stubbedUser.emails = [
  429. {
  430. email: this.newEmail,
  431. confirmedAt: new Date(),
  432. },
  433. ]
  434. this.UserGetter.promises.getUser.resolves(this.stubbedUser)
  435. this.NewsletterManager.promises.changeEmail.callsArgWith(2, null)
  436. this.RecurlyWrapper.promises.updateAccountEmailAddress.resolves()
  437. })
  438. it('set default', function (done) {
  439. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
  440. this.UserUpdater.setDefaultEmailAddress(
  441. this.stubbedUser._id,
  442. this.newEmail,
  443. false,
  444. this.auditLog,
  445. err => {
  446. expect(err).not.to.exist
  447. this.UserUpdater.promises.updateUser
  448. .calledWith(
  449. { _id: this.stubbedUser._id, 'emails.email': this.newEmail },
  450. { $set: { email: this.newEmail } }
  451. )
  452. .should.equal(true)
  453. done()
  454. }
  455. )
  456. })
  457. it('set changed the email in newsletter', function (done) {
  458. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
  459. this.UserUpdater.setDefaultEmailAddress(
  460. this.stubbedUser._id,
  461. this.newEmail,
  462. false,
  463. this.auditLog,
  464. err => {
  465. expect(err).not.to.exist
  466. this.NewsletterManager.promises.changeEmail
  467. .calledWith(this.stubbedUser, this.newEmail)
  468. .should.equal(true)
  469. this.RecurlyWrapper.promises.updateAccountEmailAddress
  470. .calledWith(this.stubbedUser._id, this.newEmail)
  471. .should.equal(true)
  472. done()
  473. }
  474. )
  475. })
  476. it('handle error', function (done) {
  477. this.UserUpdater.promises.updateUser = sinon.stub().rejects(Error('nope'))
  478. this.UserUpdater.setDefaultEmailAddress(
  479. this.stubbedUser._id,
  480. this.newEmail,
  481. false,
  482. this.auditLog,
  483. err => {
  484. expect(err).to.exist
  485. done()
  486. }
  487. )
  488. })
  489. it('handle missed update', function (done) {
  490. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
  491. this.UserUpdater.setDefaultEmailAddress(
  492. this.stubbedUser._id,
  493. this.newEmail,
  494. false,
  495. this.auditLog,
  496. err => {
  497. expect(err).to.exist
  498. done()
  499. }
  500. )
  501. })
  502. it('validates email', function (done) {
  503. this.UserUpdater.setDefaultEmailAddress(
  504. this.stubbedUser._id,
  505. '.edu',
  506. false,
  507. this.auditLog,
  508. err => {
  509. expect(err).to.exist
  510. done()
  511. }
  512. )
  513. })
  514. it('updates audit log', function (done) {
  515. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
  516. this.UserUpdater.setDefaultEmailAddress(
  517. this.stubbedUser._id,
  518. this.newEmail,
  519. false,
  520. this.auditLog,
  521. error => {
  522. expect(error).to.not.exist
  523. expect(
  524. this.UserAuditLogHandler.promises.addEntry
  525. ).to.have.been.calledWith(
  526. this.stubbedUser._id,
  527. 'change-primary-email',
  528. this.auditLog.initiatorId,
  529. this.auditLog.ipAddress,
  530. {
  531. newPrimaryEmail: this.newEmail,
  532. oldPrimaryEmail: this.stubbedUser.email,
  533. }
  534. )
  535. done()
  536. }
  537. )
  538. })
  539. it('blocks email update if audit log returns an error', function (done) {
  540. this.UserUpdater.promises.updateUser = sinon.stub()
  541. this.UserAuditLogHandler.promises.addEntry.rejects(new Error('oops'))
  542. this.UserUpdater.setDefaultEmailAddress(
  543. this.stubbedUser._id,
  544. this.newEmail,
  545. false,
  546. this.auditLog,
  547. error => {
  548. expect(error).to.exist
  549. expect(this.UserUpdater.promises.updateUser).to.not.have.been.called
  550. done()
  551. }
  552. )
  553. })
  554. describe('when email not confirmed', function () {
  555. beforeEach(function () {
  556. this.stubbedUser.emails = [
  557. {
  558. email: this.newEmail,
  559. confirmedAt: null,
  560. },
  561. ]
  562. this.UserUpdater.promises.updateUser = sinon.stub()
  563. })
  564. it('should callback with error', function () {
  565. this.UserUpdater.setDefaultEmailAddress(
  566. this.stubbedUser._id,
  567. this.newEmail,
  568. false,
  569. this.auditLog,
  570. error => {
  571. expect(error).to.exist
  572. expect(error.name).to.equal('UnconfirmedEmailError')
  573. this.UserUpdater.promises.updateUser.callCount.should.equal(0)
  574. this.NewsletterManager.promises.changeEmail.callCount.should.equal(
  575. 0
  576. )
  577. }
  578. )
  579. })
  580. })
  581. describe('when email does not belong to user', function () {
  582. beforeEach(function () {
  583. this.stubbedUser.emails = []
  584. this.UserGetter.promises.getUser.resolves(this.stubbedUser)
  585. this.UserUpdater.promises.updateUser = sinon.stub()
  586. })
  587. it('should callback with error', function () {
  588. this.UserUpdater.setDefaultEmailAddress(
  589. this.stubbedUser._id,
  590. this.newEmail,
  591. false,
  592. this.auditLog,
  593. error => {
  594. expect(error).to.exist
  595. expect(error.name).to.equal('Error')
  596. this.UserUpdater.promises.updateUser.callCount.should.equal(0)
  597. this.NewsletterManager.promises.changeEmail.callCount.should.equal(
  598. 0
  599. )
  600. }
  601. )
  602. })
  603. })
  604. describe('security alert', function () {
  605. it('should be sent to old and new email when sendSecurityAlert=true', function (done) {
  606. // this.UserGetter.promises.getUser.resolves(this.stubbedUser)
  607. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
  608. this.UserUpdater.setDefaultEmailAddress(
  609. this.stubbedUser._id,
  610. this.newEmail,
  611. false,
  612. this.auditLog,
  613. true,
  614. error => {
  615. expect(error).to.not.exist
  616. this.EmailHandler.promises.sendEmail.callCount.should.equal(2)
  617. const toOldEmailAlert = this.EmailHandler.promises.sendEmail
  618. .firstCall
  619. expect(toOldEmailAlert.args[0]).to.equal('securityAlert')
  620. const toNewEmailAlert = this.EmailHandler.promises.sendEmail
  621. .lastCall
  622. expect(toOldEmailAlert.args[1].to).to.equal(this.stubbedUser.email)
  623. expect(toNewEmailAlert.args[0]).to.equal('securityAlert')
  624. expect(toNewEmailAlert.args[1].to).to.equal(this.newEmail)
  625. done()
  626. }
  627. )
  628. })
  629. describe('errors', function () {
  630. const anError = new Error('oops')
  631. describe('EmailHandler', function () {
  632. beforeEach(function () {
  633. this.EmailHandler.promises.sendEmail.rejects(anError)
  634. this.UserUpdater.promises.updateUser = sinon
  635. .stub()
  636. .resolves({ n: 1 })
  637. })
  638. it('should log but not pass back the error', function (done) {
  639. this.UserUpdater.setDefaultEmailAddress(
  640. this.stubbedUser._id,
  641. this.newEmail,
  642. false,
  643. this.auditLog,
  644. true,
  645. error => {
  646. expect(error).to.not.exist
  647. const loggerCall = this.logger.error.firstCall
  648. expect(loggerCall.args[0]).to.deep.equal({
  649. error: anError,
  650. userId: this.stubbedUser._id,
  651. })
  652. expect(loggerCall.args[1]).to.contain(
  653. 'could not send security alert email when primary email changed'
  654. )
  655. done()
  656. }
  657. )
  658. })
  659. })
  660. })
  661. })
  662. })
  663. describe('confirmEmail', function () {
  664. beforeEach(function () {
  665. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
  666. })
  667. it('should update the email record', function (done) {
  668. this.UserUpdater.confirmEmail(
  669. this.stubbedUser._id,
  670. this.stubbedUserEmail,
  671. err => {
  672. expect(err).not.to.exist
  673. this.UserUpdater.promises.updateUser
  674. .calledWith(
  675. {
  676. _id: this.stubbedUser._id,
  677. 'emails.email': this.stubbedUserEmail,
  678. },
  679. {
  680. $set: {
  681. 'emails.$.reconfirmedAt': new Date(),
  682. },
  683. $min: {
  684. 'emails.$.confirmedAt': new Date(),
  685. },
  686. }
  687. )
  688. .should.equal(true)
  689. done()
  690. }
  691. )
  692. })
  693. it('add affiliation', function (done) {
  694. this.UserUpdater.confirmEmail(
  695. this.stubbedUser._id,
  696. this.newEmail,
  697. err => {
  698. expect(err).not.to.exist
  699. this.InstitutionsAPI.promises.addAffiliation.calledOnce.should.equal(
  700. true
  701. )
  702. sinon.assert.calledWith(
  703. this.InstitutionsAPI.promises.addAffiliation,
  704. this.stubbedUser._id,
  705. this.newEmail,
  706. { confirmedAt: new Date() }
  707. )
  708. done()
  709. }
  710. )
  711. })
  712. it('handle error', function (done) {
  713. this.UserUpdater.promises.updateUser = sinon
  714. .stub()
  715. .throws(new Error('nope'))
  716. this.UserUpdater.confirmEmail(
  717. this.stubbedUser._id,
  718. this.newEmail,
  719. err => {
  720. expect(err).to.exist
  721. done()
  722. }
  723. )
  724. })
  725. it('handle missed update', function (done) {
  726. this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
  727. this.UserUpdater.confirmEmail(
  728. this.stubbedUser._id,
  729. this.newEmail,
  730. err => {
  731. expect(err).to.exist
  732. done()
  733. }
  734. )
  735. })
  736. it('validates email', function (done) {
  737. this.UserUpdater.confirmEmail(this.stubbedUser._id, '@', err => {
  738. expect(err).to.exist
  739. done()
  740. })
  741. })
  742. it('handle affiliation error', function (done) {
  743. this.InstitutionsAPI.promises.addAffiliation.throws(Error('nope'))
  744. this.UserUpdater.confirmEmail(
  745. this.stubbedUser._id,
  746. this.newEmail,
  747. err => {
  748. expect(err).to.exist
  749. this.UserUpdater.promises.updateUser.called.should.equal(false)
  750. done()
  751. }
  752. )
  753. })
  754. it('refresh features', function (done) {
  755. this.UserUpdater.confirmEmail(
  756. this.stubbedUser._id,
  757. this.newEmail,
  758. err => {
  759. expect(err).not.to.exist
  760. sinon.assert.calledWith(
  761. this.FeaturesUpdater.promises.refreshFeatures,
  762. this.stubbedUser._id
  763. )
  764. done()
  765. }
  766. )
  767. })
  768. })
  769. })