UserEmailsTests.js 24 KB

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