PrimaryEmailCheckTests.js 6.5 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. 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.fetch('/project' + SPLIT_TEST_QUERY)
  41. expect(response.status).to.equal(200)
  42. })
  43. it('should be redirected from the primary email check page to the project list', async function () {
  44. const response = await userHelper.fetch(
  45. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY
  46. )
  47. expect(response.status).to.equal(302)
  48. expect(response.headers.get('location')).to.equal(
  49. UserHelper.url('/project').toString()
  50. )
  51. })
  52. })
  53. describe('when the user has checked their email recently', function () {
  54. beforeEach(async function () {
  55. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  56. await UserHelper.updateUser(userHelper.user._id, {
  57. $set: { lastPrimaryEmailCheck: new Date(time) },
  58. })
  59. })
  60. it("shouldn't be redirected from project list to the primary email check page", async function () {
  61. const response = await userHelper.fetch('/project' + SPLIT_TEST_QUERY)
  62. expect(response.status).to.equal(200)
  63. })
  64. it('should be redirected from the primary email check page to the project list', async function () {
  65. const response = await userHelper.fetch(
  66. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY
  67. )
  68. expect(response.status).to.equal(302)
  69. expect(response.headers.get('location')).to.equal(
  70. UserHelper.url('/project').toString()
  71. )
  72. })
  73. })
  74. describe('when the user has confirmed their primary email recently', function () {
  75. beforeEach(async function () {
  76. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  77. // overridden by email confirmation
  78. const time = Date.now() - Settings.primary_email_check_expiration * 2
  79. await UserHelper.updateUser(userHelper.user._id, {
  80. $set: { lastPrimaryEmailCheck: new Date(time) },
  81. })
  82. await userHelper.confirmEmail(
  83. userHelper.user._id,
  84. userHelper.user.email
  85. )
  86. })
  87. it("shouldn't be redirected from project list to the primary email check page", async function () {
  88. const response = await userHelper.fetch('/project' + SPLIT_TEST_QUERY)
  89. expect(response.status).to.equal(200)
  90. })
  91. it('should be redirected from the primary email check page to the project list', async function () {
  92. const response = await userHelper.fetch(
  93. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY
  94. )
  95. expect(response.status).to.equal(302)
  96. expect(response.headers.get('location')).to.equal(
  97. UserHelper.url('/project').toString()
  98. )
  99. })
  100. })
  101. describe('when the user has signed for longer than the email check expiration period', function () {
  102. beforeEach(async function () {
  103. const time = Date.now() - Settings.primary_email_check_expiration * 2
  104. await UserHelper.updateUser(userHelper.user._id, {
  105. $set: { lastPrimaryEmailCheck: new Date(time) },
  106. })
  107. })
  108. it('should be redirected from project list to the primary email check page', async function () {
  109. const response = await userHelper.fetch('/project' + SPLIT_TEST_QUERY)
  110. expect(response.status).to.equal(302)
  111. expect(response.headers.get('location')).to.equal(
  112. UserHelper.url('/user/emails/primary-email-check').toString()
  113. )
  114. })
  115. it('can visit the primary email check page', async function () {
  116. const response = await userHelper.fetch(
  117. '/user/emails/primary-email-check'
  118. )
  119. expect(response.status).to.equal(200)
  120. })
  121. })
  122. })
  123. describe('when user checks their primary email address', function () {
  124. let checkResponse
  125. beforeEach(async function () {
  126. // make sure the user requires checking their primary email address
  127. const time = Date.now() - Settings.primary_email_check_expiration * 2
  128. await UserHelper.updateUser(userHelper.user._id, {
  129. $set: { lastPrimaryEmailCheck: new Date(time) },
  130. })
  131. checkResponse = await userHelper.fetch(
  132. '/user/emails/primary-email-check' + SPLIT_TEST_QUERY,
  133. { method: 'POST' }
  134. )
  135. })
  136. it('should be redirected to the project list page', function () {
  137. expect(checkResponse.status).to.equal(302)
  138. expect(checkResponse.headers.get('location')).to.equal(
  139. UserHelper.url('/project').toString()
  140. )
  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.fetch('/project' + SPLIT_TEST_QUERY)
  144. expect(response.status).to.equal(200)
  145. })
  146. it('visiting the primary email check page should redirect to the project list page', async function () {
  147. const response = await userHelper.fetch(
  148. '/user/emails/primary-email-check'
  149. )
  150. expect(response.status).to.equal(302)
  151. expect(response.headers.get('location')).to.equal(
  152. UserHelper.url('/project').toString()
  153. )
  154. })
  155. })
  156. })