PrimaryEmailCheckTests.js 11 KB

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