PrimaryEmailCheckTests.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import UserHelper from './helpers/UserHelper.mjs'
  2. import Settings from '@overleaf/settings'
  3. import { expect } from 'chai'
  4. import Features from '../../../app/src/infrastructure/Features.mjs'
  5. import MockV1ApiClass from './mocks/MockV1Api.mjs'
  6. import SubscriptionHelper from './helpers/Subscription.mjs'
  7. const Subscription = SubscriptionHelper.promises
  8. describe('PrimaryEmailCheck', function () {
  9. let userHelper
  10. let MockV1Api
  11. before(function () {
  12. MockV1Api = MockV1ApiClass.instance()
  13. })
  14. beforeEach(async function () {
  15. userHelper = await UserHelper.createUser()
  16. userHelper = await UserHelper.loginUser(
  17. userHelper.getDefaultEmailPassword()
  18. )
  19. })
  20. describe('redirections in Overleaf Community Edition/Server Pro', function () {
  21. before(async function () {
  22. if (Features.hasFeature('saas')) {
  23. this.skip()
  24. }
  25. })
  26. describe('when the user has signed up recently', function () {
  27. it("shouldn't be redirected from project list to the primary email check page", async function () {
  28. const response = await userHelper.fetch('/project')
  29. expect(response.status).to.equal(200)
  30. })
  31. it('should be redirected from the primary email check page to the project list', async function () {
  32. const response = await userHelper.fetch(
  33. '/user/emails/primary-email-check'
  34. )
  35. expect(response.status).to.equal(302)
  36. expect(response.headers.get('location')).to.equal(
  37. UserHelper.url('/project').toString()
  38. )
  39. })
  40. })
  41. describe('when the user has checked their email recently', function () {
  42. beforeEach(async function () {
  43. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  44. await UserHelper.updateUser(userHelper.user._id, {
  45. $set: { lastPrimaryEmailCheck: new Date(time) },
  46. })
  47. })
  48. it("shouldn't be redirected from project list to the primary email check page", async function () {
  49. const response = await userHelper.fetch('/project')
  50. expect(response.status).to.equal(200)
  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(userHelper.user.email)
  62. })
  63. it("shouldn't be redirected from project list to the primary email check page", async function () {
  64. const response = await userHelper.fetch('/project')
  65. expect(response.status).to.equal(200)
  66. })
  67. })
  68. describe('when the user has signed for longer than the email check expiration period', function () {
  69. beforeEach(async function () {
  70. const time = Date.now() - Settings.primary_email_check_expiration * 2
  71. await UserHelper.updateUser(userHelper.user._id, {
  72. $set: { lastPrimaryEmailCheck: new Date(time) },
  73. })
  74. })
  75. it("shouldn't be redirected from project list to the primary email check page", async function () {
  76. const response = await userHelper.fetch('/project')
  77. expect(response.status).to.equal(200)
  78. })
  79. })
  80. })
  81. describe('redirections in SAAS', function () {
  82. before(async function () {
  83. if (!Features.hasFeature('saas')) {
  84. this.skip()
  85. }
  86. })
  87. describe('when the user has signed up recently', function () {
  88. it("shouldn't be redirected from project list to the primary email check page", async function () {
  89. const response = await userHelper.fetch('/project')
  90. expect(response.status).to.equal(200)
  91. })
  92. it('should be redirected from the primary email check page to the project list', async function () {
  93. const response = await userHelper.fetch(
  94. '/user/emails/primary-email-check'
  95. )
  96. expect(response.status).to.equal(302)
  97. expect(response.headers.get('location')).to.equal(
  98. UserHelper.url('/project').toString()
  99. )
  100. })
  101. })
  102. describe('when the user has checked their email recently', function () {
  103. beforeEach(async function () {
  104. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  105. await UserHelper.updateUser(userHelper.user._id, {
  106. $set: { lastPrimaryEmailCheck: new Date(time) },
  107. })
  108. })
  109. it("shouldn't be redirected from project list to the primary email check page", async function () {
  110. const response = await userHelper.fetch('/project')
  111. expect(response.status).to.equal(200)
  112. })
  113. it('should be redirected from the primary email check page to the project list', async function () {
  114. const response = await userHelper.fetch(
  115. '/user/emails/primary-email-check'
  116. )
  117. expect(response.status).to.equal(302)
  118. expect(response.headers.get('location')).to.equal(
  119. UserHelper.url('/project').toString()
  120. )
  121. })
  122. })
  123. describe('when the user has confirmed their primary email recently', function () {
  124. beforeEach(async function () {
  125. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  126. // overridden by email confirmation
  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. await userHelper.confirmEmail(userHelper.user.email)
  132. })
  133. it("shouldn't be redirected from project list to the primary email check page", async function () {
  134. const response = await userHelper.fetch('/project')
  135. expect(response.status).to.equal(200)
  136. })
  137. it('should be redirected from the primary email check page to the project list', async function () {
  138. const response = await userHelper.fetch(
  139. '/user/emails/primary-email-check'
  140. )
  141. expect(response.status).to.equal(302)
  142. expect(response.headers.get('location')).to.equal(
  143. UserHelper.url('/project').toString()
  144. )
  145. })
  146. })
  147. describe('when the user has signed for longer than the email check expiration period', function () {
  148. beforeEach(async function () {
  149. const time = Date.now() - Settings.primary_email_check_expiration * 2
  150. await UserHelper.updateUser(userHelper.user._id, {
  151. $set: { lastPrimaryEmailCheck: new Date(time) },
  152. })
  153. })
  154. it('should be redirected from project list to the primary email check page', async function () {
  155. const response = await userHelper.fetch('/project')
  156. expect(response.status).to.equal(302)
  157. expect(response.headers.get('location')).to.equal(
  158. UserHelper.url('/user/emails/primary-email-check').toString()
  159. )
  160. })
  161. it('can visit the primary email check page', async function () {
  162. const response = await userHelper.fetch(
  163. '/user/emails/primary-email-check'
  164. )
  165. expect(response.status).to.equal(200)
  166. })
  167. })
  168. })
  169. describe('when user checks their primary email address', function () {
  170. let checkResponse
  171. beforeEach(async function () {
  172. // make sure the user requires checking their primary email address
  173. const time = Date.now() - Settings.primary_email_check_expiration * 2
  174. await UserHelper.updateUser(userHelper.user._id, {
  175. $set: { lastPrimaryEmailCheck: new Date(time) },
  176. })
  177. })
  178. describe('when the user has a secondary email address', function () {
  179. before(async function () {
  180. if (!Features.hasFeature('saas')) {
  181. this.skip()
  182. }
  183. })
  184. beforeEach(async function () {
  185. await userHelper.confirmEmail(userHelper.user.email)
  186. await userHelper.addEmailAndConfirm('secondary@overleaf.com')
  187. checkResponse = await userHelper.fetch(
  188. '/user/emails/primary-email-check',
  189. { method: 'POST' }
  190. )
  191. })
  192. it('should be redirected to the project list page', function () {
  193. expect(checkResponse.status).to.equal(302)
  194. expect(checkResponse.headers.get('location')).to.equal(
  195. UserHelper.url('/project').toString()
  196. )
  197. })
  198. })
  199. describe('when the user has an institutional email and no secondary', function () {
  200. before(async function () {
  201. if (!Features.hasFeature('saas')) {
  202. this.skip()
  203. }
  204. if (!Features.hasFeature('saml')) {
  205. this.skip()
  206. }
  207. })
  208. beforeEach(async function () {
  209. MockV1Api.createInstitution({
  210. name: 'Exampe Institution',
  211. hostname: 'example.com',
  212. licence: 'pro_plus',
  213. confirmed: true,
  214. })
  215. MockV1Api.addAffiliation(userHelper.user._id, userHelper.user.email)
  216. })
  217. it('should be redirected to the add secondary email page', async function () {
  218. const response = await userHelper.fetch(
  219. '/user/emails/primary-email-check',
  220. { method: 'POST' }
  221. )
  222. expect(response.status).to.equal(302)
  223. expect(response.headers.get('location')).to.equal(
  224. UserHelper.url('/user/emails/add-secondary').toString()
  225. )
  226. })
  227. })
  228. describe('when the user is a managed user', function () {
  229. beforeEach(async function () {
  230. const adminUser = await UserHelper.createUser()
  231. this.subscription = new Subscription({
  232. adminId: adminUser._id,
  233. memberIds: [userHelper.user._id],
  234. groupPlan: true,
  235. planCode: 'group_professional_5_enterprise',
  236. })
  237. await this.subscription.ensureExists()
  238. await this.subscription.enableManagedUsers()
  239. })
  240. it('should be redirected to the project list page', async function () {
  241. const response = await userHelper.fetch(
  242. '/user/emails/primary-email-check',
  243. { method: 'POST' }
  244. )
  245. expect(response.status).to.equal(302)
  246. expect(response.headers.get('location')).to.equal(
  247. UserHelper.url('/project').toString()
  248. )
  249. })
  250. })
  251. })
  252. describe('when user has checked their primary email address', function () {
  253. beforeEach(async function () {
  254. const time = Date.now() - Settings.primary_email_check_expiration * 2
  255. await UserHelper.updateUser(userHelper.user._id, {
  256. $set: { lastPrimaryEmailCheck: new Date(time) },
  257. })
  258. await userHelper.fetch('/user/emails/primary-email-check', {
  259. method: 'POST',
  260. })
  261. })
  262. it("shouldn't be redirected from project list to the primary email check page any longer", async function () {
  263. const response = await userHelper.fetch('/project')
  264. expect(response.status).to.equal(200)
  265. })
  266. it('visiting the primary email check page should redirect to the project list page', async function () {
  267. const response = await userHelper.fetch(
  268. '/user/emails/primary-email-check'
  269. )
  270. expect(response.status).to.equal(302)
  271. expect(response.headers.get('location')).to.equal(
  272. UserHelper.url('/project').toString()
  273. )
  274. })
  275. })
  276. })