PrimaryEmailCheckTests.js 5.9 KB

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