PrimaryEmailCheckTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const UserHelper = require('./helpers/UserHelper')
  2. const Settings = require('@overleaf/settings')
  3. const { expect } = require('chai')
  4. const SplitTestManager = require('../../../app/src/Features/SplitTests/SplitTestManager')
  5. // While the split test is in progress this must be appended to URLs during tests
  6. const SPLIT_TEST_QUERY = '?primary-email-check=active'
  7. describe('PrimaryEmailCheck', function () {
  8. let userHelper
  9. // Create the primary-email-check split test because this is now required for the query string override to work. See
  10. // https://github.com/overleaf/internal/pull/7545#discussion_r848575736
  11. before(async function () {
  12. await SplitTestManager.createSplitTest('primary-email-check', {
  13. active: true,
  14. analyticsEnabled: true,
  15. phase: 'release',
  16. variants: [
  17. {
  18. name: 'active',
  19. rolloutPercent: 0,
  20. },
  21. ],
  22. })
  23. })
  24. beforeEach(async function () {
  25. userHelper = await UserHelper.createUser()
  26. userHelper = await UserHelper.loginUser(
  27. userHelper.getDefaultEmailPassword()
  28. )
  29. })
  30. describe('redirections', function () {
  31. describe('when the user has signed up recently', function () {
  32. it("shouldn't be redirected from project list to the primary email check page", async function () {
  33. const response = await userHelper.request.get(
  34. '/project' + SPLIT_TEST_QUERY
  35. )
  36. expect(response.statusCode).to.equal(200)
  37. })
  38. it('should be redirected from the primary email check page to the project list', async function () {
  39. const response = await userHelper.request.get(
  40. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY,
  41. { simple: false }
  42. )
  43. expect(response.statusCode).to.equal(302)
  44. expect(response.headers.location).to.equal('/project')
  45. })
  46. })
  47. describe('when the user has checked their email recently', function () {
  48. beforeEach(async function () {
  49. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  50. await UserHelper.updateUser(userHelper.user._id, {
  51. $set: { lastPrimaryEmailCheck: new Date(time) },
  52. })
  53. })
  54. it("shouldn't be redirected from project list to the primary email check page", async function () {
  55. const response = await userHelper.request.get(
  56. '/project' + SPLIT_TEST_QUERY
  57. )
  58. expect(response.statusCode).to.equal(200)
  59. })
  60. it('should be redirected from the primary email check page to the project list', async function () {
  61. const response = await userHelper.request.get(
  62. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY,
  63. { simple: false }
  64. )
  65. expect(response.statusCode).to.equal(302)
  66. expect(response.headers.location).to.equal('/project')
  67. })
  68. })
  69. describe('when the user has confirmed their primary email recently', function () {
  70. beforeEach(async function () {
  71. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  72. // overridden by email confirmation
  73. const time = Date.now() - Settings.primary_email_check_expiration * 2
  74. await UserHelper.updateUser(userHelper.user._id, {
  75. $set: { lastPrimaryEmailCheck: new Date(time) },
  76. })
  77. await userHelper.confirmEmail(
  78. userHelper.user._id,
  79. userHelper.user.email
  80. )
  81. })
  82. it("shouldn't be redirected from project list to the primary email check page", async function () {
  83. const response = await userHelper.request.get(
  84. '/project' + SPLIT_TEST_QUERY
  85. )
  86. expect(response.statusCode).to.equal(200)
  87. })
  88. it('should be redirected from the primary email check page to the project list', async function () {
  89. const response = await userHelper.request.get(
  90. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY,
  91. { simple: false }
  92. )
  93. expect(response.statusCode).to.equal(302)
  94. expect(response.headers.location).to.equal('/project')
  95. })
  96. })
  97. describe('when the user has signed for longer than the email check expiration period', function () {
  98. beforeEach(async function () {
  99. const time = Date.now() - Settings.primary_email_check_expiration * 2
  100. await UserHelper.updateUser(userHelper.user._id, {
  101. $set: { lastPrimaryEmailCheck: new Date(time) },
  102. })
  103. })
  104. it('should be redirected from project list to the primary email check page', async function () {
  105. const response = await userHelper.request.get(
  106. '/project' + SPLIT_TEST_QUERY,
  107. { simple: false }
  108. )
  109. expect(response.statusCode).to.equal(302)
  110. expect(response.headers.location).to.equal(
  111. '/user/emails/primary-email-check'
  112. )
  113. })
  114. it('can visit the primary email check page', async function () {
  115. const response = await userHelper.request.get(
  116. '/user/emails/primary-email-check'
  117. )
  118. expect(response.statusCode).to.equal(200)
  119. })
  120. })
  121. })
  122. describe('when user checks their primary email address', function () {
  123. let checkResponse
  124. beforeEach(async function () {
  125. // make sure the user requires checking their primary email address
  126. const time = Date.now() - Settings.primary_email_check_expiration * 2
  127. await UserHelper.updateUser(userHelper.user._id, {
  128. $set: { lastPrimaryEmailCheck: new Date(time) },
  129. })
  130. checkResponse = await userHelper.request.post(
  131. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY,
  132. {
  133. form: {},
  134. simple: false,
  135. }
  136. )
  137. })
  138. it('should be redirected to the project list page', function () {
  139. expect(checkResponse.statusCode).to.equal(302)
  140. expect(checkResponse.headers.location).to.equal('/project')
  141. })
  142. it("shouldn't be redirected from project list to the primary email check page any longer", async function () {
  143. const response = await userHelper.request.get(
  144. '/project' + SPLIT_TEST_QUERY
  145. )
  146. expect(response.statusCode).to.equal(200)
  147. })
  148. it('visiting the primary email check page should redirect to the project list page', async function () {
  149. const response = await userHelper.request.get(
  150. '/user/emails/primary-email-check',
  151. { simple: false }
  152. )
  153. expect(response.statusCode).to.equal(302)
  154. expect(response.headers.location).to.equal('/project')
  155. })
  156. })
  157. })