PrimaryEmailCheckTests.js 6.6 KB

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