PrimaryEmailCheckTests.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. beforeEach(async function () {
  8. userHelper = await UserHelper.createUser()
  9. userHelper = await UserHelper.loginUser(
  10. userHelper.getDefaultEmailPassword()
  11. )
  12. })
  13. describe('redirections in Server CE/Pro', function () {
  14. before(async function () {
  15. if (Features.hasFeature('saas')) {
  16. this.skip()
  17. }
  18. })
  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. })
  46. describe('when the user has confirmed their primary email recently', function () {
  47. beforeEach(async function () {
  48. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  49. // overridden by email confirmation
  50. const time = Date.now() - Settings.primary_email_check_expiration * 2
  51. await UserHelper.updateUser(userHelper.user._id, {
  52. $set: { lastPrimaryEmailCheck: new Date(time) },
  53. })
  54. await userHelper.confirmEmail(
  55. userHelper.user._id,
  56. userHelper.user.email
  57. )
  58. })
  59. it("shouldn't be redirected from project list to the primary email check page", async function () {
  60. const response = await userHelper.fetch('/project')
  61. expect(response.status).to.equal(200)
  62. })
  63. })
  64. describe('when the user has signed for longer than the email check expiration period', function () {
  65. beforeEach(async function () {
  66. const time = Date.now() - Settings.primary_email_check_expiration * 2
  67. await UserHelper.updateUser(userHelper.user._id, {
  68. $set: { lastPrimaryEmailCheck: new Date(time) },
  69. })
  70. })
  71. it("shouldn't be redirected from project list to the primary email check page", async function () {
  72. const response = await userHelper.fetch('/project')
  73. expect(response.status).to.equal(200)
  74. })
  75. })
  76. })
  77. describe('redirections in SAAS', function () {
  78. before(async function () {
  79. if (!Features.hasFeature('saas')) {
  80. this.skip()
  81. }
  82. })
  83. describe('when the user has signed up recently', function () {
  84. it("shouldn't be redirected from project list to the primary email check page", async function () {
  85. const response = await userHelper.fetch('/project')
  86. expect(response.status).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.fetch(
  90. '/user/emails/primary-email-check'
  91. )
  92. expect(response.status).to.equal(302)
  93. expect(response.headers.get('location')).to.equal(
  94. UserHelper.url('/project').toString()
  95. )
  96. })
  97. })
  98. describe('when the user has checked their email recently', function () {
  99. beforeEach(async function () {
  100. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  101. await UserHelper.updateUser(userHelper.user._id, {
  102. $set: { lastPrimaryEmailCheck: new Date(time) },
  103. })
  104. })
  105. it("shouldn't be redirected from project list to the primary email check page", async function () {
  106. const response = await userHelper.fetch('/project')
  107. expect(response.status).to.equal(200)
  108. })
  109. it('should be redirected from the primary email check page to the project list', async function () {
  110. const response = await userHelper.fetch(
  111. '/user/emails/primary-email-check'
  112. )
  113. expect(response.status).to.equal(302)
  114. expect(response.headers.get('location')).to.equal(
  115. UserHelper.url('/project').toString()
  116. )
  117. })
  118. })
  119. describe('when the user has confirmed their primary email recently', function () {
  120. beforeEach(async function () {
  121. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  122. // overridden by email confirmation
  123. const time = Date.now() - Settings.primary_email_check_expiration * 2
  124. await UserHelper.updateUser(userHelper.user._id, {
  125. $set: { lastPrimaryEmailCheck: new Date(time) },
  126. })
  127. await userHelper.confirmEmail(
  128. userHelper.user._id,
  129. userHelper.user.email
  130. )
  131. })
  132. it("shouldn't be redirected from project list to the primary email check page", async function () {
  133. const response = await userHelper.fetch('/project')
  134. expect(response.status).to.equal(200)
  135. })
  136. it('should be redirected from the primary email check page to the project list', async function () {
  137. const response = await userHelper.fetch(
  138. '/user/emails/primary-email-check'
  139. )
  140. expect(response.status).to.equal(302)
  141. expect(response.headers.get('location')).to.equal(
  142. UserHelper.url('/project').toString()
  143. )
  144. })
  145. })
  146. describe('when the user has signed for longer than the email check expiration period', function () {
  147. beforeEach(async function () {
  148. const time = Date.now() - Settings.primary_email_check_expiration * 2
  149. await UserHelper.updateUser(userHelper.user._id, {
  150. $set: { lastPrimaryEmailCheck: new Date(time) },
  151. })
  152. })
  153. it('should be redirected from project list to the primary email check page', async function () {
  154. const response = await userHelper.fetch('/project')
  155. expect(response.status).to.equal(302)
  156. expect(response.headers.get('location')).to.equal(
  157. UserHelper.url('/user/emails/primary-email-check').toString()
  158. )
  159. })
  160. it('can visit the primary email check page', async function () {
  161. const response = await userHelper.fetch(
  162. '/user/emails/primary-email-check'
  163. )
  164. expect(response.status).to.equal(200)
  165. })
  166. })
  167. })
  168. describe('when user checks their primary email address', function () {
  169. let checkResponse
  170. beforeEach(async function () {
  171. // make sure the user requires checking their primary email address
  172. const time = Date.now() - Settings.primary_email_check_expiration * 2
  173. await UserHelper.updateUser(userHelper.user._id, {
  174. $set: { lastPrimaryEmailCheck: new Date(time) },
  175. })
  176. checkResponse = await userHelper.fetch(
  177. '/user/emails/primary-email-check',
  178. { method: 'POST' }
  179. )
  180. })
  181. it('should be redirected to the project list page', function () {
  182. expect(checkResponse.status).to.equal(302)
  183. expect(checkResponse.headers.get('location')).to.equal(
  184. UserHelper.url('/project').toString()
  185. )
  186. })
  187. it("shouldn't be redirected from project list to the primary email check page any longer", async function () {
  188. const response = await userHelper.fetch('/project')
  189. expect(response.status).to.equal(200)
  190. })
  191. it('visiting the primary email check page should redirect to the project list page', async function () {
  192. const response = await userHelper.fetch(
  193. '/user/emails/primary-email-check'
  194. )
  195. expect(response.status).to.equal(302)
  196. expect(response.headers.get('location')).to.equal(
  197. UserHelper.url('/project').toString()
  198. )
  199. })
  200. })
  201. })