UserEmailsTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. const { expect } = require('chai')
  2. const async = require('async')
  3. const User = require('./helpers/User')
  4. const UserHelper = require('./helpers/UserHelper')
  5. const { db, ObjectId } = require('../../../app/src/infrastructure/mongojs')
  6. const MockV1Api = require('./helpers/MockV1Api')
  7. const expectErrorResponse = require('./helpers/expectErrorResponse')
  8. describe('UserEmails', function() {
  9. beforeEach(function(done) {
  10. this.timeout(20000)
  11. this.user = new User()
  12. this.user.login(done)
  13. })
  14. describe('confirming an email', function() {
  15. it('should confirm the email', function(done) {
  16. let token = null
  17. async.series(
  18. [
  19. cb => {
  20. this.user.request(
  21. {
  22. method: 'POST',
  23. url: '/user/emails',
  24. json: {
  25. email: 'newly-added-email@example.com'
  26. }
  27. },
  28. (error, response, body) => {
  29. expect(error).to.not.exist
  30. expect(response.statusCode).to.equal(204)
  31. cb()
  32. }
  33. )
  34. },
  35. cb => {
  36. this.user.request(
  37. { url: '/user/emails', json: true },
  38. (error, response, body) => {
  39. expect(error).to.not.exist
  40. expect(response.statusCode).to.equal(200)
  41. expect(body[0].confirmedAt).to.not.exist
  42. expect(body[1].confirmedAt).to.not.exist
  43. cb()
  44. }
  45. )
  46. },
  47. cb => {
  48. db.tokens.find(
  49. {
  50. use: 'email_confirmation',
  51. 'data.user_id': this.user._id,
  52. usedAt: { $exists: false }
  53. },
  54. (error, tokens) => {
  55. expect(error).to.not.exist
  56. // There should only be one confirmation token at the moment
  57. expect(tokens.length).to.equal(1)
  58. expect(tokens[0].data.email).to.equal(
  59. 'newly-added-email@example.com'
  60. )
  61. expect(tokens[0].data.user_id).to.equal(this.user._id)
  62. ;({ token } = tokens[0])
  63. cb()
  64. }
  65. )
  66. },
  67. cb => {
  68. this.user.request(
  69. {
  70. method: 'POST',
  71. url: '/user/emails/confirm',
  72. json: {
  73. token
  74. }
  75. },
  76. (error, response, body) => {
  77. expect(error).to.not.exist
  78. expect(response.statusCode).to.equal(200)
  79. cb()
  80. }
  81. )
  82. },
  83. cb => {
  84. this.user.request(
  85. { url: '/user/emails', json: true },
  86. (error, response, body) => {
  87. expect(error).to.not.exist
  88. expect(response.statusCode).to.equal(200)
  89. expect(body[0].confirmedAt).to.not.exist
  90. expect(body[1].confirmedAt).to.exist
  91. cb()
  92. }
  93. )
  94. },
  95. cb => {
  96. db.tokens.find(
  97. {
  98. use: 'email_confirmation',
  99. 'data.user_id': this.user._id,
  100. usedAt: { $exists: false }
  101. },
  102. (error, tokens) => {
  103. expect(error).to.not.exist
  104. // Token should be deleted after use
  105. expect(tokens.length).to.equal(0)
  106. cb()
  107. }
  108. )
  109. }
  110. ],
  111. done
  112. )
  113. })
  114. it('should not allow confirmation of the email if the user has changed', function(done) {
  115. let token1 = null
  116. let token2 = null
  117. this.user2 = new User()
  118. this.email = 'duplicate-email@example.com'
  119. async.series(
  120. [
  121. cb => this.user2.login(cb),
  122. cb => {
  123. // Create email for first user
  124. this.user.request(
  125. {
  126. method: 'POST',
  127. url: '/user/emails',
  128. json: { email: this.email }
  129. },
  130. cb
  131. )
  132. },
  133. cb => {
  134. db.tokens.find(
  135. {
  136. use: 'email_confirmation',
  137. 'data.user_id': this.user._id,
  138. usedAt: { $exists: false }
  139. },
  140. (error, tokens) => {
  141. expect(error).to.not.exist
  142. // There should only be one confirmation token at the moment
  143. expect(tokens.length).to.equal(1)
  144. expect(tokens[0].data.email).to.equal(this.email)
  145. expect(tokens[0].data.user_id).to.equal(this.user._id)
  146. token1 = tokens[0].token
  147. cb()
  148. }
  149. )
  150. },
  151. cb => {
  152. // Delete the email from the first user
  153. this.user.request(
  154. {
  155. method: 'POST',
  156. url: '/user/emails/delete',
  157. json: { email: this.email }
  158. },
  159. cb
  160. )
  161. },
  162. cb => {
  163. // Create email for second user
  164. this.user2.request(
  165. {
  166. method: 'POST',
  167. url: '/user/emails',
  168. json: { email: this.email }
  169. },
  170. cb
  171. )
  172. },
  173. cb => {
  174. // Original confirmation token should no longer work
  175. this.user.request(
  176. {
  177. method: 'POST',
  178. url: '/user/emails/confirm',
  179. json: {
  180. token: token1
  181. }
  182. },
  183. (error, response, body) => {
  184. expect(error).to.not.exist
  185. expect(response.statusCode).to.equal(404)
  186. cb()
  187. }
  188. )
  189. },
  190. cb => {
  191. db.tokens.find(
  192. {
  193. use: 'email_confirmation',
  194. 'data.user_id': this.user2._id,
  195. usedAt: { $exists: false }
  196. },
  197. (error, tokens) => {
  198. expect(error).to.not.exist
  199. // The first token has been used, so this should be token2 now
  200. expect(tokens.length).to.equal(1)
  201. expect(tokens[0].data.email).to.equal(this.email)
  202. expect(tokens[0].data.user_id).to.equal(this.user2._id)
  203. token2 = tokens[0].token
  204. cb()
  205. }
  206. )
  207. },
  208. cb => {
  209. // Second user should be able to confirm the email
  210. this.user2.request(
  211. {
  212. method: 'POST',
  213. url: '/user/emails/confirm',
  214. json: {
  215. token: token2
  216. }
  217. },
  218. (error, response, body) => {
  219. expect(error).to.not.exist
  220. expect(response.statusCode).to.equal(200)
  221. cb()
  222. }
  223. )
  224. },
  225. cb => {
  226. this.user2.request(
  227. { url: '/user/emails', json: true },
  228. (error, response, body) => {
  229. expect(error).to.not.exist
  230. expect(response.statusCode).to.equal(200)
  231. expect(body[0].confirmedAt).to.not.exist
  232. expect(body[1].confirmedAt).to.exist
  233. cb()
  234. }
  235. )
  236. }
  237. ],
  238. done
  239. )
  240. })
  241. })
  242. describe('with an expired token', function() {
  243. it('should not confirm the email', function(done) {
  244. let token = null
  245. async.series(
  246. [
  247. cb => {
  248. this.user.request(
  249. {
  250. method: 'POST',
  251. url: '/user/emails',
  252. json: {
  253. email: (this.email = 'expired-token-email@example.com')
  254. }
  255. },
  256. (error, response, body) => {
  257. expect(error).to.not.exist
  258. expect(response.statusCode).to.equal(204)
  259. cb()
  260. }
  261. )
  262. },
  263. cb => {
  264. db.tokens.find(
  265. {
  266. use: 'email_confirmation',
  267. 'data.user_id': this.user._id,
  268. usedAt: { $exists: false }
  269. },
  270. (error, tokens) => {
  271. expect(error).to.not.exist
  272. // There should only be one confirmation token at the moment
  273. expect(tokens.length).to.equal(1)
  274. expect(tokens[0].data.email).to.equal(this.email)
  275. expect(tokens[0].data.user_id).to.equal(this.user._id)
  276. ;({ token } = tokens[0])
  277. cb()
  278. }
  279. )
  280. },
  281. cb => {
  282. db.tokens.update(
  283. {
  284. token
  285. },
  286. {
  287. $set: {
  288. expiresAt: new Date(Date.now() - 1000000)
  289. }
  290. },
  291. cb
  292. )
  293. },
  294. cb => {
  295. this.user.request(
  296. {
  297. method: 'POST',
  298. url: '/user/emails/confirm',
  299. json: {
  300. token
  301. }
  302. },
  303. (error, response, body) => {
  304. expect(error).to.not.exist
  305. expect(response.statusCode).to.equal(404)
  306. cb()
  307. }
  308. )
  309. }
  310. ],
  311. done
  312. )
  313. })
  314. })
  315. describe('resending the confirmation', function() {
  316. it('should generate a new token', function(done) {
  317. async.series(
  318. [
  319. cb => {
  320. this.user.request(
  321. {
  322. method: 'POST',
  323. url: '/user/emails',
  324. json: {
  325. email: 'reconfirmation-email@example.com'
  326. }
  327. },
  328. (error, response, body) => {
  329. expect(error).to.not.exist
  330. expect(response.statusCode).to.equal(204)
  331. cb()
  332. }
  333. )
  334. },
  335. cb => {
  336. db.tokens.find(
  337. {
  338. use: 'email_confirmation',
  339. 'data.user_id': this.user._id,
  340. usedAt: { $exists: false }
  341. },
  342. (error, tokens) => {
  343. expect(error).to.not.exist
  344. // There should only be one confirmation token at the moment
  345. expect(tokens.length).to.equal(1)
  346. expect(tokens[0].data.email).to.equal(
  347. 'reconfirmation-email@example.com'
  348. )
  349. expect(tokens[0].data.user_id).to.equal(this.user._id)
  350. cb()
  351. }
  352. )
  353. },
  354. cb => {
  355. this.user.request(
  356. {
  357. method: 'POST',
  358. url: '/user/emails/resend_confirmation',
  359. json: {
  360. email: 'reconfirmation-email@example.com'
  361. }
  362. },
  363. (error, response, body) => {
  364. expect(error).to.not.exist
  365. expect(response.statusCode).to.equal(200)
  366. cb()
  367. }
  368. )
  369. },
  370. cb => {
  371. db.tokens.find(
  372. {
  373. use: 'email_confirmation',
  374. 'data.user_id': this.user._id,
  375. usedAt: { $exists: false }
  376. },
  377. (error, tokens) => {
  378. expect(error).to.not.exist
  379. // There should be two tokens now
  380. expect(tokens.length).to.equal(2)
  381. expect(tokens[0].data.email).to.equal(
  382. 'reconfirmation-email@example.com'
  383. )
  384. expect(tokens[0].data.user_id).to.equal(this.user._id)
  385. expect(tokens[1].data.email).to.equal(
  386. 'reconfirmation-email@example.com'
  387. )
  388. expect(tokens[1].data.user_id).to.equal(this.user._id)
  389. cb()
  390. }
  391. )
  392. }
  393. ],
  394. done
  395. )
  396. })
  397. it('should create a new token if none exists', function(done) {
  398. // This should only be for users that have sign up with their main
  399. // emails before the confirmation system existed
  400. async.series(
  401. [
  402. cb => {
  403. db.tokens.remove(
  404. {
  405. use: 'email_confirmation',
  406. 'data.user_id': this.user._id,
  407. usedAt: { $exists: false }
  408. },
  409. cb
  410. )
  411. },
  412. cb => {
  413. this.user.request(
  414. {
  415. method: 'POST',
  416. url: '/user/emails/resend_confirmation',
  417. json: {
  418. email: this.user.email
  419. }
  420. },
  421. (error, response, body) => {
  422. expect(error).to.not.exist
  423. expect(response.statusCode).to.equal(200)
  424. cb()
  425. }
  426. )
  427. },
  428. cb => {
  429. db.tokens.find(
  430. {
  431. use: 'email_confirmation',
  432. 'data.user_id': this.user._id,
  433. usedAt: { $exists: false }
  434. },
  435. (error, tokens) => {
  436. expect(error).to.not.exist
  437. // There should still only be one confirmation token
  438. expect(tokens.length).to.equal(1)
  439. expect(tokens[0].data.email).to.equal(this.user.email)
  440. expect(tokens[0].data.user_id).to.equal(this.user._id)
  441. cb()
  442. }
  443. )
  444. }
  445. ],
  446. done
  447. )
  448. })
  449. it("should not allow reconfirmation if the email doesn't match the user", function(done) {
  450. async.series(
  451. [
  452. cb => {
  453. this.user.request(
  454. {
  455. method: 'POST',
  456. url: '/user/emails/resend_confirmation',
  457. json: {
  458. email: 'non-matching-email@example.com'
  459. }
  460. },
  461. (error, response, body) => {
  462. expect(error).to.not.exist
  463. expect(response.statusCode).to.equal(422)
  464. cb()
  465. }
  466. )
  467. },
  468. cb => {
  469. db.tokens.find(
  470. {
  471. use: 'email_confirmation',
  472. 'data.user_id': this.user._id,
  473. usedAt: { $exists: false }
  474. },
  475. (error, tokens) => {
  476. expect(error).to.not.exist
  477. expect(tokens.length).to.equal(0)
  478. cb()
  479. }
  480. )
  481. }
  482. ],
  483. done
  484. )
  485. })
  486. })
  487. describe('setting a default email', function() {
  488. it('should update confirmed emails for users not in v1', function(done) {
  489. async.series(
  490. [
  491. cb => {
  492. this.user.request(
  493. {
  494. method: 'POST',
  495. url: '/user/emails',
  496. json: {
  497. email: 'new-confirmed-default@example.com'
  498. }
  499. },
  500. (error, response, body) => {
  501. expect(error).to.not.exist
  502. expect(response.statusCode).to.equal(204)
  503. cb()
  504. }
  505. )
  506. },
  507. cb => {
  508. // Mark the email as confirmed
  509. db.users.update(
  510. {
  511. 'emails.email': 'new-confirmed-default@example.com'
  512. },
  513. {
  514. $set: {
  515. 'emails.$.confirmedAt': new Date()
  516. }
  517. },
  518. cb
  519. )
  520. },
  521. cb => {
  522. this.user.request(
  523. {
  524. method: 'POST',
  525. url: '/user/emails/default',
  526. json: {
  527. email: 'new-confirmed-default@example.com'
  528. }
  529. },
  530. (error, response, body) => {
  531. expect(error).to.not.exist
  532. expect(response.statusCode).to.equal(200)
  533. cb()
  534. }
  535. )
  536. },
  537. cb => {
  538. this.user.request(
  539. { url: '/user/emails', json: true },
  540. (error, response, body) => {
  541. expect(error).to.not.exist
  542. expect(response.statusCode).to.equal(200)
  543. expect(body[0].confirmedAt).to.not.exist
  544. expect(body[0].default).to.equal(false)
  545. expect(body[1].confirmedAt).to.exist
  546. expect(body[1].default).to.equal(true)
  547. cb()
  548. }
  549. )
  550. }
  551. ],
  552. done
  553. )
  554. })
  555. it('should not allow changing unconfirmed emails in v1', function(done) {
  556. async.series(
  557. [
  558. cb => {
  559. db.users.update(
  560. {
  561. _id: ObjectId(this.user._id)
  562. },
  563. {
  564. $set: {
  565. 'overleaf.id': 42
  566. }
  567. },
  568. cb
  569. )
  570. },
  571. cb => {
  572. this.user.request(
  573. {
  574. method: 'POST',
  575. url: '/user/emails',
  576. json: {
  577. email: 'new-unconfirmed-default@example.com'
  578. }
  579. },
  580. (error, response, body) => {
  581. expect(error).to.not.exist
  582. expect(response.statusCode).to.equal(204)
  583. cb()
  584. }
  585. )
  586. },
  587. cb => {
  588. this.user.request(
  589. {
  590. method: 'POST',
  591. url: '/user/emails/default',
  592. json: {
  593. email: 'new-unconfirmed-default@example.com'
  594. }
  595. },
  596. (error, response, body) => {
  597. expect(error).to.not.exist
  598. expect(response.statusCode).to.equal(409)
  599. cb()
  600. }
  601. )
  602. },
  603. cb => {
  604. this.user.request(
  605. { url: '/user/emails', json: true },
  606. (error, response, body) => {
  607. expect(error).to.not.exist
  608. expect(body[0].default).to.equal(true)
  609. expect(body[1].default).to.equal(false)
  610. cb()
  611. }
  612. )
  613. }
  614. ],
  615. done
  616. )
  617. })
  618. it('should not update the email in v1', function(done) {
  619. async.series(
  620. [
  621. cb => {
  622. db.users.update(
  623. {
  624. _id: ObjectId(this.user._id)
  625. },
  626. {
  627. $set: {
  628. 'overleaf.id': 42
  629. }
  630. },
  631. cb
  632. )
  633. },
  634. cb => {
  635. this.user.request(
  636. {
  637. method: 'POST',
  638. url: '/user/emails',
  639. json: {
  640. email: 'new-confirmed-default-in-v1@example.com'
  641. }
  642. },
  643. (error, response, body) => {
  644. expect(error).to.not.exist
  645. expect(response.statusCode).to.equal(204)
  646. cb()
  647. }
  648. )
  649. },
  650. cb => {
  651. // Mark the email as confirmed
  652. db.users.update(
  653. {
  654. 'emails.email': 'new-confirmed-default-in-v1@example.com'
  655. },
  656. {
  657. $set: {
  658. 'emails.$.confirmedAt': new Date()
  659. }
  660. },
  661. cb
  662. )
  663. },
  664. cb => {
  665. this.user.request(
  666. {
  667. method: 'POST',
  668. url: '/user/emails/default',
  669. json: {
  670. email: 'new-confirmed-default-in-v1@example.com'
  671. }
  672. },
  673. (error, response, body) => {
  674. expect(error).to.not.exist
  675. expect(response.statusCode).to.equal(200)
  676. cb()
  677. }
  678. )
  679. }
  680. ],
  681. error => {
  682. expect(error).to.not.exist
  683. expect(MockV1Api.updateEmail.callCount).to.equal(0)
  684. done()
  685. }
  686. )
  687. })
  688. it('should not return an error if the email exists in v1', function(done) {
  689. MockV1Api.existingEmails.push('exists-in-v1@example.com')
  690. async.series(
  691. [
  692. cb => {
  693. db.users.update(
  694. {
  695. _id: ObjectId(this.user._id)
  696. },
  697. {
  698. $set: {
  699. 'overleaf.id': 42
  700. }
  701. },
  702. cb
  703. )
  704. },
  705. cb => {
  706. this.user.request(
  707. {
  708. method: 'POST',
  709. url: '/user/emails',
  710. json: {
  711. email: 'exists-in-v1@example.com'
  712. }
  713. },
  714. (error, response, body) => {
  715. expect(error).to.not.exist
  716. expect(response.statusCode).to.equal(204)
  717. cb()
  718. }
  719. )
  720. },
  721. cb => {
  722. // Mark the email as confirmed
  723. db.users.update(
  724. {
  725. 'emails.email': 'exists-in-v1@example.com'
  726. },
  727. {
  728. $set: {
  729. 'emails.$.confirmedAt': new Date()
  730. }
  731. },
  732. cb
  733. )
  734. },
  735. cb => {
  736. this.user.request(
  737. {
  738. method: 'POST',
  739. url: '/user/emails/default',
  740. json: {
  741. email: 'exists-in-v1@example.com'
  742. }
  743. },
  744. (error, response, body) => {
  745. expect(error).to.not.exist
  746. expect(response.statusCode).to.equal(200)
  747. cb()
  748. }
  749. )
  750. },
  751. cb => {
  752. this.user.request(
  753. { url: '/user/emails', json: true },
  754. (error, response, body) => {
  755. expect(error).to.not.exist
  756. expect(body[0].default).to.equal(false)
  757. expect(body[1].default).to.equal(true)
  758. cb()
  759. }
  760. )
  761. }
  762. ],
  763. done
  764. )
  765. })
  766. })
  767. describe('when not logged in', function() {
  768. beforeEach(function(done) {
  769. this.anonymous = new User()
  770. this.anonymous.getCsrfToken(done)
  771. })
  772. it('should return a plain 403 when setting the email', function(done) {
  773. this.anonymous.request(
  774. {
  775. method: 'POST',
  776. url: '/user/emails',
  777. json: {
  778. email: 'newly-added-email@example.com'
  779. }
  780. },
  781. (error, response, body) => {
  782. if (error) {
  783. return done(error)
  784. }
  785. expectErrorResponse.requireLogin.json(response, body)
  786. done()
  787. }
  788. )
  789. })
  790. })
  791. describe('secondary email', function() {
  792. let newEmail, userHelper, userId, user
  793. beforeEach(async function() {
  794. newEmail = 'a-new-email@overleaf.com'
  795. userHelper = new UserHelper()
  796. userHelper = await UserHelper.createUser()
  797. userHelper = await UserHelper.loginUser({
  798. email: userHelper.getDefaultEmail(),
  799. password: userHelper.getDefaultPassword()
  800. })
  801. userId = userHelper.user._id
  802. await userHelper.request.post({
  803. form: {
  804. email: newEmail
  805. },
  806. simple: false,
  807. uri: '/user/emails'
  808. })
  809. userHelper = await UserHelper.getUser(userId)
  810. user = userHelper.user
  811. })
  812. it('should add the email', async function() {
  813. expect(user.emails[1].email).to.equal(newEmail)
  814. })
  815. it('should add to the user audit log', async function() {
  816. expect(typeof user.auditLog[0].initiatorId).to.equal('object')
  817. expect(user.auditLog[0].initiatorId).to.deep.equal(user._id)
  818. expect(user.auditLog[0].info.newSecondaryEmail).to.equal(newEmail)
  819. expect(user.auditLog[0].ip).to.equal(this.user.request.ip)
  820. })
  821. })
  822. })