PrimaryEmailCheckTests.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.js'
  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(
  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.fetch('/project')
  68. expect(response.status).to.equal(200)
  69. })
  70. })
  71. describe('when the user has signed for longer than the email check expiration period', function () {
  72. beforeEach(async function () {
  73. const time = Date.now() - Settings.primary_email_check_expiration * 2
  74. await UserHelper.updateUser(userHelper.user._id, {
  75. $set: { lastPrimaryEmailCheck: new Date(time) },
  76. })
  77. })
  78. it("shouldn't be redirected from project list to the primary email check page", async function () {
  79. const response = await userHelper.fetch('/project')
  80. expect(response.status).to.equal(200)
  81. })
  82. })
  83. })
  84. describe('redirections in SAAS', function () {
  85. before(async function () {
  86. if (!Features.hasFeature('saas')) {
  87. this.skip()
  88. }
  89. })
  90. describe('when the user has signed up recently', function () {
  91. it("shouldn't be redirected from project list to the primary email check page", async function () {
  92. const response = await userHelper.fetch('/project')
  93. expect(response.status).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.fetch(
  97. '/user/emails/primary-email-check'
  98. )
  99. expect(response.status).to.equal(302)
  100. expect(response.headers.get('location')).to.equal(
  101. UserHelper.url('/project').toString()
  102. )
  103. })
  104. })
  105. describe('when the user has checked their email recently', function () {
  106. beforeEach(async function () {
  107. const time = Date.now() - Settings.primary_email_check_expiration * 0.5
  108. await UserHelper.updateUser(userHelper.user._id, {
  109. $set: { lastPrimaryEmailCheck: new Date(time) },
  110. })
  111. })
  112. it("shouldn't be redirected from project list to the primary email check page", async function () {
  113. const response = await userHelper.fetch('/project')
  114. expect(response.status).to.equal(200)
  115. })
  116. it('should be redirected from the primary email check page to the project list', async function () {
  117. const response = await userHelper.fetch(
  118. '/user/emails/primary-email-check'
  119. )
  120. expect(response.status).to.equal(302)
  121. expect(response.headers.get('location')).to.equal(
  122. UserHelper.url('/project').toString()
  123. )
  124. })
  125. })
  126. describe('when the user has confirmed their primary email recently', function () {
  127. beforeEach(async function () {
  128. // the user should check again their email according to `lastPrimaryEmailCheck` timestamp, but the behaviour is
  129. // overridden by email confirmation
  130. const time = Date.now() - Settings.primary_email_check_expiration * 2
  131. await UserHelper.updateUser(userHelper.user._id, {
  132. $set: { lastPrimaryEmailCheck: new Date(time) },
  133. })
  134. await userHelper.confirmEmail(
  135. userHelper.user._id,
  136. userHelper.user.email
  137. )
  138. })
  139. it("shouldn't be redirected from project list to the primary email check page", async function () {
  140. const response = await userHelper.fetch('/project')
  141. expect(response.status).to.equal(200)
  142. })
  143. it('should be redirected from the primary email check page to the project list', async function () {
  144. const response = await userHelper.fetch(
  145. '/user/emails/primary-email-check'
  146. )
  147. expect(response.status).to.equal(302)
  148. expect(response.headers.get('location')).to.equal(
  149. UserHelper.url('/project').toString()
  150. )
  151. })
  152. })
  153. describe('when the user has signed for longer than the email check expiration period', function () {
  154. beforeEach(async function () {
  155. const time = Date.now() - Settings.primary_email_check_expiration * 2
  156. await UserHelper.updateUser(userHelper.user._id, {
  157. $set: { lastPrimaryEmailCheck: new Date(time) },
  158. })
  159. })
  160. it('should be redirected from project list to the primary email check page', async function () {
  161. const response = await userHelper.fetch('/project')
  162. expect(response.status).to.equal(302)
  163. expect(response.headers.get('location')).to.equal(
  164. UserHelper.url('/user/emails/primary-email-check').toString()
  165. )
  166. })
  167. it('can visit the primary email check page', async function () {
  168. const response = await userHelper.fetch(
  169. '/user/emails/primary-email-check'
  170. )
  171. expect(response.status).to.equal(200)
  172. })
  173. })
  174. })
  175. describe('when user checks their primary email address', function () {
  176. let checkResponse
  177. beforeEach(async function () {
  178. // make sure the user requires checking their primary email address
  179. const time = Date.now() - Settings.primary_email_check_expiration * 2
  180. await UserHelper.updateUser(userHelper.user._id, {
  181. $set: { lastPrimaryEmailCheck: new Date(time) },
  182. })
  183. })
  184. describe('when the user has a secondary email address', function () {
  185. before(async function () {
  186. if (!Features.hasFeature('saas')) {
  187. this.skip()
  188. }
  189. })
  190. beforeEach(async function () {
  191. await userHelper.confirmEmail(
  192. userHelper.user._id,
  193. userHelper.user.email
  194. )
  195. await userHelper.addEmailAndConfirm(
  196. userHelper.user._id,
  197. 'secondary@overleaf.com'
  198. )
  199. checkResponse = await userHelper.fetch(
  200. '/user/emails/primary-email-check',
  201. { method: 'POST' }
  202. )
  203. })
  204. it('should be redirected to the project list page', function () {
  205. expect(checkResponse.status).to.equal(302)
  206. expect(checkResponse.headers.get('location')).to.equal(
  207. UserHelper.url('/project').toString()
  208. )
  209. })
  210. })
  211. describe('when the user has an institutional email and no secondary', function () {
  212. before(async function () {
  213. if (!Features.hasFeature('saas')) {
  214. this.skip()
  215. }
  216. if (!Features.hasFeature('saml')) {
  217. this.skip()
  218. }
  219. })
  220. beforeEach(async function () {
  221. MockV1Api.createInstitution({
  222. name: 'Exampe Institution',
  223. hostname: 'example.com',
  224. licence: 'pro_plus',
  225. confirmed: true,
  226. })
  227. MockV1Api.addAffiliation(userHelper.user._id, userHelper.user.email)
  228. })
  229. it('should be redirected to the add secondary email page', async function () {
  230. const response = await userHelper.fetch(
  231. '/user/emails/primary-email-check',
  232. { method: 'POST' }
  233. )
  234. expect(response.status).to.equal(302)
  235. expect(response.headers.get('location')).to.equal(
  236. UserHelper.url('/user/emails/add-secondary').toString()
  237. )
  238. })
  239. })
  240. describe('when the user is a managed user', function () {
  241. beforeEach(async function () {
  242. const adminUser = await UserHelper.createUser()
  243. this.subscription = new Subscription({
  244. adminId: adminUser._id,
  245. memberIds: [userHelper.user._id],
  246. groupPlan: true,
  247. planCode: 'group_professional_5_enterprise',
  248. })
  249. await this.subscription.ensureExists()
  250. await this.subscription.enableManagedUsers()
  251. })
  252. it('should be redirected to the project list page', async function () {
  253. const response = await userHelper.fetch(
  254. '/user/emails/primary-email-check',
  255. { method: 'POST' }
  256. )
  257. expect(response.status).to.equal(302)
  258. expect(response.headers.get('location')).to.equal(
  259. UserHelper.url('/project').toString()
  260. )
  261. })
  262. })
  263. })
  264. describe('when user has checked their primary email address', function () {
  265. beforeEach(async function () {
  266. const time = Date.now() - Settings.primary_email_check_expiration * 2
  267. await UserHelper.updateUser(userHelper.user._id, {
  268. $set: { lastPrimaryEmailCheck: new Date(time) },
  269. })
  270. await userHelper.fetch('/user/emails/primary-email-check', {
  271. method: 'POST',
  272. })
  273. })
  274. it("shouldn't be redirected from project list to the primary email check page any longer", async function () {
  275. const response = await userHelper.fetch('/project')
  276. expect(response.status).to.equal(200)
  277. })
  278. it('visiting the primary email check page should redirect to the project list page', async function () {
  279. const response = await userHelper.fetch(
  280. '/user/emails/primary-email-check'
  281. )
  282. expect(response.status).to.equal(302)
  283. expect(response.headers.get('location')).to.equal(
  284. UserHelper.url('/project').toString()
  285. )
  286. })
  287. })
  288. })