PrimaryEmailCheckTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const UserHelper = require('./helpers/UserHelper')
  2. const Settings = require('@overleaf/settings')
  3. const { expect } = require('chai')
  4. const Features = require('../../../app/src/infrastructure/Features')
  5. describe('PrimaryEmailCheck', function () {
  6. let userHelper
  7. before(async function () {
  8. if (!Features.hasFeature('saas')) {
  9. this.skip()
  10. }
  11. })
  12. beforeEach(async function () {
  13. userHelper = await UserHelper.createUser()
  14. userHelper = await UserHelper.loginUser(
  15. userHelper.getDefaultEmailPassword()
  16. )
  17. })
  18. describe('redirections', function () {
  19. describe('when the user has signed up recently', function () {
  20. it("shouldn't be redirected from project list to the primary email check page", async function () {
  21. const response = await userHelper.fetch('/project')
  22. expect(response.status).to.equal(200)
  23. })
  24. it('should be redirected from the primary email check page to the project list', async function () {
  25. const response = await userHelper.fetch(
  26. '/user/emails/primary-email-check'
  27. )
  28. expect(response.status).to.equal(302)
  29. expect(response.headers.get('location')).to.equal(
  30. UserHelper.url('/project').toString()
  31. )
  32. })
  33. })
  34. describe('when the user has checked their email recently', function () {
  35. beforeEach(async function () {
  36. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  37. await UserHelper.updateUser(userHelper.user._id, {
  38. $set: { lastPrimaryEmailCheck: new Date(time) },
  39. })
  40. })
  41. it("shouldn't be redirected from project list to the primary email check page", async function () {
  42. const response = await userHelper.fetch('/project')
  43. expect(response.status).to.equal(200)
  44. })
  45. it('should be redirected from the primary email check page to the project list', async function () {
  46. const response = await userHelper.fetch(
  47. '/user/emails/primary-email-check'
  48. )
  49. expect(response.status).to.equal(302)
  50. expect(response.headers.get('location')).to.equal(
  51. UserHelper.url('/project').toString()
  52. )
  53. })
  54. })
  55. describe('when the user has confirmed their primary email recently', function () {
  56. beforeEach(async function () {
  57. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  58. // overridden by email confirmation
  59. const time = Date.now() - Settings.primary_email_check_expiration * 2
  60. await UserHelper.updateUser(userHelper.user._id, {
  61. $set: { lastPrimaryEmailCheck: new Date(time) },
  62. })
  63. await userHelper.confirmEmail(
  64. userHelper.user._id,
  65. userHelper.user.email
  66. )
  67. })
  68. it("shouldn't be redirected from project list to the primary email check page", async function () {
  69. const response = await userHelper.fetch('/project')
  70. expect(response.status).to.equal(200)
  71. })
  72. it('should be redirected from the primary email check page to the project list', async function () {
  73. const response = await userHelper.fetch(
  74. '/user/emails/primary-email-check'
  75. )
  76. expect(response.status).to.equal(302)
  77. expect(response.headers.get('location')).to.equal(
  78. UserHelper.url('/project').toString()
  79. )
  80. })
  81. })
  82. describe('when the user has signed for longer than the email check expiration period', function () {
  83. beforeEach(async function () {
  84. const time = Date.now() - Settings.primary_email_check_expiration * 2
  85. await UserHelper.updateUser(userHelper.user._id, {
  86. $set: { lastPrimaryEmailCheck: new Date(time) },
  87. })
  88. })
  89. it('should be redirected from project list to the primary email check page', async function () {
  90. const response = await userHelper.fetch('/project')
  91. expect(response.status).to.equal(302)
  92. expect(response.headers.get('location')).to.equal(
  93. UserHelper.url('/user/emails/primary-email-check').toString()
  94. )
  95. })
  96. it('can visit the primary email check page', async function () {
  97. const response = await userHelper.fetch(
  98. '/user/emails/primary-email-check'
  99. )
  100. expect(response.status).to.equal(200)
  101. })
  102. })
  103. })
  104. describe('when user checks their primary email address', function () {
  105. let checkResponse
  106. beforeEach(async function () {
  107. // make sure the user requires checking their primary email address
  108. const time = Date.now() - Settings.primary_email_check_expiration * 2
  109. await UserHelper.updateUser(userHelper.user._id, {
  110. $set: { lastPrimaryEmailCheck: new Date(time) },
  111. })
  112. checkResponse = await userHelper.fetch(
  113. '/user/emails/primary-email-check',
  114. { method: 'POST' }
  115. )
  116. })
  117. it('should be redirected to the project list page', function () {
  118. expect(checkResponse.status).to.equal(302)
  119. expect(checkResponse.headers.get('location')).to.equal(
  120. UserHelper.url('/project').toString()
  121. )
  122. })
  123. it("shouldn't be redirected from project list to the primary email check page any longer", async function () {
  124. const response = await userHelper.fetch('/project')
  125. expect(response.status).to.equal(200)
  126. })
  127. it('visiting the primary email check page should redirect to the project list page', async function () {
  128. const response = await userHelper.fetch(
  129. '/user/emails/primary-email-check'
  130. )
  131. expect(response.status).to.equal(302)
  132. expect(response.headers.get('location')).to.equal(
  133. UserHelper.url('/project').toString()
  134. )
  135. })
  136. })
  137. })