notifications.test.tsx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import {
  4. fireEvent,
  5. render,
  6. screen,
  7. waitForElementToBeRemoved,
  8. within,
  9. } from '@testing-library/react'
  10. import fetchMock from 'fetch-mock'
  11. import { merge, cloneDeep } from 'lodash'
  12. import {
  13. professionalUserData,
  14. unconfirmedUserData,
  15. untrustedUserData,
  16. unconfirmedCommonsUserData,
  17. confirmedUserData,
  18. } from '../../settings/fixtures/test-user-email-data'
  19. import {
  20. notificationDropboxDuplicateProjectNames,
  21. notificationGroupInviteDefault,
  22. notificationIPMatchedAffiliation,
  23. notificationProjectInvite,
  24. notificationTPDSFileLimit,
  25. notificationsInstitution,
  26. } from '../fixtures/notifications-data'
  27. import Common from '../../../../../frontend/js/features/project-list/components/notifications/groups/common'
  28. import Institution from '../../../../../frontend/js/features/project-list/components/notifications/groups/institution'
  29. import ConfirmEmail, {
  30. getEmailDeletionDate,
  31. } from '../../../../../frontend/js/features/project-list/components/notifications/groups/confirm-email'
  32. import ReconfirmationInfo from '../../../../../frontend/js/features/project-list/components/notifications/groups/affiliation/reconfirmation-info'
  33. import { ProjectListProvider } from '../../../../../frontend/js/features/project-list/context/project-list-context'
  34. import { SplitTestProvider } from '@/shared/context/split-test-context'
  35. import {
  36. Notification,
  37. Institution as InstitutionType,
  38. } from '../../../../../types/project/dashboard/notification'
  39. import { DeepPartial } from '../../../../../types/utils'
  40. import { Project } from '../../../../../types/project/dashboard/api'
  41. import GroupsAndEnterpriseBanner from '../../../../../frontend/js/features/project-list/components/notifications/groups-and-enterprise-banner'
  42. import GroupSsoSetupSuccess from '../../../../../frontend/js/features/project-list/components/notifications/groups/group-sso-setup-success'
  43. import localStorage from '@/infrastructure/local-storage'
  44. import { location } from '@/shared/components/location'
  45. import {
  46. commonsSubscription,
  47. freeSubscription,
  48. groupSubscription,
  49. individualSubscription,
  50. } from '../fixtures/user-subscriptions'
  51. import getMeta from '@/utils/meta'
  52. const renderWithinProjectListProvider = (
  53. Component: React.ComponentType<React.PropsWithChildren>
  54. ) => {
  55. render(<Component />, {
  56. wrapper: ({ children }) => (
  57. <ProjectListProvider>
  58. <SplitTestProvider>
  59. <ul className="list-unstyled">{children}</ul>
  60. </SplitTestProvider>
  61. </ProjectListProvider>
  62. ),
  63. })
  64. }
  65. describe('<UserNotifications />', function () {
  66. const exposedSettings = {
  67. samlInitPath: '/fakeSaml/',
  68. appName: 'Overleaf',
  69. }
  70. beforeEach(function () {
  71. fetchMock.removeRoutes().clearHistory()
  72. // at least one project is required to show some notifications
  73. const projects = [{}] as Project[]
  74. fetchMock.post(/\/api\/project/, {
  75. status: 200,
  76. body: {
  77. projects,
  78. totalSize: projects.length,
  79. },
  80. })
  81. })
  82. afterEach(function () {
  83. fetchMock.removeRoutes().clearHistory()
  84. })
  85. describe('<Common>', function () {
  86. beforeEach(function () {
  87. window.metaAttributesCache.set('ol-user', {})
  88. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  89. })
  90. afterEach(function () {
  91. fetchMock.removeRoutes().clearHistory()
  92. })
  93. it('accepts project invite', async function () {
  94. const reconfiguredNotification: DeepPartial<Notification> = {
  95. _id: 1,
  96. templateKey: 'notification_project_invite',
  97. }
  98. window.metaAttributesCache.set('ol-notifications', [
  99. merge(cloneDeep(notificationProjectInvite), reconfiguredNotification),
  100. ])
  101. renderWithinProjectListProvider(Common)
  102. await fetchMock.callHistory.flush(true)
  103. const deleteMock = fetchMock.delete(
  104. `/notifications/${reconfiguredNotification._id}`,
  105. 200
  106. )
  107. const acceptMock = fetchMock.post(
  108. `/project/${notificationProjectInvite.messageOpts.projectId}/invite/token/${notificationProjectInvite.messageOpts.token}/accept`,
  109. 200
  110. )
  111. screen.getByRole('alert')
  112. screen.getByText(/would like you to join/i)
  113. const joinBtn = screen.getByRole('button', {
  114. name: /join project/i,
  115. }) as HTMLButtonElement
  116. expect(joinBtn.disabled).to.be.false
  117. fireEvent.click(joinBtn)
  118. expect(joinBtn.disabled).to.be.true
  119. await waitForElementToBeRemoved(() =>
  120. screen.getByRole('button', { name: /joining/i })
  121. )
  122. expect(acceptMock.callHistory.called()).to.be.true
  123. await screen.findByText(/joined/i)
  124. expect(screen.queryByRole('button', { name: /join project/i })).to.be.null
  125. const openProject = screen.getByRole('link', { name: /open project/i })
  126. expect(openProject.getAttribute('href')).to.equal(
  127. `/project/${notificationProjectInvite.messageOpts.projectId}`
  128. )
  129. const closeBtn = screen.getByRole('button', { name: /close/i })
  130. fireEvent.click(closeBtn)
  131. expect(deleteMock.callHistory.called()).to.be.true
  132. expect(screen.queryByRole('alert')).to.be.null
  133. })
  134. it('fails to accept project invite', async function () {
  135. const reconfiguredNotification: DeepPartial<Notification> = {
  136. _id: 1,
  137. templateKey: 'notification_project_invite',
  138. }
  139. window.metaAttributesCache.set('ol-notifications', [
  140. merge(cloneDeep(notificationProjectInvite), reconfiguredNotification),
  141. ])
  142. renderWithinProjectListProvider(Common)
  143. await fetchMock.callHistory.flush(true)
  144. fetchMock.post(
  145. `/project/${notificationProjectInvite.messageOpts.projectId}/invite/token/${notificationProjectInvite.messageOpts.token}/accept`,
  146. 500
  147. )
  148. screen.getByRole('alert')
  149. screen.getByText(/would like you to join/i)
  150. const joinBtn = screen.getByRole('button', {
  151. name: /join project/i,
  152. }) as HTMLButtonElement
  153. fireEvent.click(joinBtn)
  154. await waitForElementToBeRemoved(() =>
  155. screen.getByRole('button', { name: /joining/i })
  156. )
  157. expect(fetchMock.callHistory.called()).to.be.true
  158. screen.getByRole('button', { name: /join project/i })
  159. expect(screen.queryByRole('button', { name: /open project/i })).to.be.null
  160. })
  161. it('shows WFH2020', async function () {
  162. const reconfiguredNotification: DeepPartial<Notification> = {
  163. _id: 1,
  164. templateKey: 'wfh_2020_upgrade_offer',
  165. }
  166. window.metaAttributesCache.set('ol-notifications', [
  167. merge(reconfiguredNotification),
  168. ])
  169. renderWithinProjectListProvider(Common)
  170. await fetchMock.callHistory.flush(true)
  171. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  172. screen.getByRole('alert')
  173. screen.getByText(/your free WFH2020 upgrade came to an end on/i)
  174. const viewLink = screen.getByRole('link', { name: /view/i })
  175. expect(viewLink.getAttribute('href')).to.equal(
  176. 'https://www.overleaf.com/events/wfh2020'
  177. )
  178. const closeBtn = screen.getByRole('button', { name: /close/i })
  179. fireEvent.click(closeBtn)
  180. expect(fetchMock.callHistory.called()).to.be.true
  181. expect(screen.queryByRole('alert')).to.be.null
  182. })
  183. it('shows IP matched affiliation with SSO enabled', async function () {
  184. const reconfiguredNotification: DeepPartial<Notification> = {
  185. _id: 1,
  186. templateKey: 'notification_ip_matched_affiliation',
  187. messageOpts: { ssoEnabled: true },
  188. }
  189. window.metaAttributesCache.set('ol-notifications', [
  190. merge(
  191. cloneDeep(notificationIPMatchedAffiliation),
  192. reconfiguredNotification
  193. ),
  194. ])
  195. renderWithinProjectListProvider(Common)
  196. await fetchMock.callHistory.flush(true)
  197. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  198. screen.getByRole('alert')
  199. screen.getByText(/it looks like you’re at/i)
  200. screen.getByText(/you can now log in through your institution/i)
  201. screen.getByText(
  202. /link an institutional email address to your account to get started/i
  203. )
  204. const findOutMore = screen.getByRole('link', { name: /find out more/i })
  205. expect(findOutMore.getAttribute('href')).to.equal(
  206. 'https://www.overleaf.com/learn/how-to/Institutional_Login'
  207. )
  208. const linkAccount = screen.getByRole('link', { name: /link account/i })
  209. expect(linkAccount.getAttribute('href')).to.equal(
  210. `${exposedSettings.samlInitPath}?university_id=${notificationIPMatchedAffiliation.messageOpts.institutionId}&auto=/project`
  211. )
  212. const closeBtn = screen.getByRole('button', { name: /close/i })
  213. fireEvent.click(closeBtn)
  214. expect(fetchMock.callHistory.called()).to.be.true
  215. expect(screen.queryByRole('alert')).to.be.null
  216. })
  217. it('shows IP matched affiliation with SSO disabled', async function () {
  218. const reconfiguredNotification: DeepPartial<Notification> = {
  219. _id: 1,
  220. templateKey: 'notification_ip_matched_affiliation',
  221. messageOpts: { ssoEnabled: false },
  222. }
  223. window.metaAttributesCache.set('ol-notifications', [
  224. merge(
  225. cloneDeep(notificationIPMatchedAffiliation),
  226. reconfiguredNotification
  227. ),
  228. ])
  229. renderWithinProjectListProvider(Common)
  230. await fetchMock.callHistory.flush(true)
  231. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  232. screen.getByRole('alert')
  233. screen.getByText(/it looks like you’re at/i)
  234. screen.getByText(/did you know that/i)
  235. screen.getByText(
  236. /add an institutional email address to claim your features/i
  237. )
  238. const addAffiliation = screen.getByRole('link', {
  239. name: /add affiliation/i,
  240. })
  241. expect(addAffiliation.getAttribute('href')).to.equal(`/user/settings`)
  242. const closeBtn = screen.getByRole('button', { name: /close/i })
  243. fireEvent.click(closeBtn)
  244. expect(fetchMock.callHistory.called()).to.be.true
  245. expect(screen.queryByRole('alert')).to.be.null
  246. })
  247. it('shows tpds file limit', async function () {
  248. const reconfiguredNotification: DeepPartial<Notification> = {
  249. templateKey: 'notification_tpds_file_limit',
  250. }
  251. window.metaAttributesCache.set('ol-notifications', [
  252. merge(cloneDeep(notificationTPDSFileLimit), reconfiguredNotification),
  253. ])
  254. renderWithinProjectListProvider(Common)
  255. await fetchMock.callHistory.flush(true)
  256. screen.getByRole('alert')
  257. screen.getByText(/file limit/i)
  258. screen.getByText(/You can't add more files to the project or sync it/i)
  259. const accountSettings = screen.getByRole('link', {
  260. name: /Open project/i,
  261. })
  262. expect(accountSettings.getAttribute('href')).to.equal('/project/123')
  263. const closeBtn = screen.getByRole('button', { name: /close/i })
  264. fireEvent.click(closeBtn)
  265. expect(screen.queryByRole('alert')).to.be.null
  266. })
  267. it('shows dropbox duplicate project names warning', async function () {
  268. const reconfiguredNotification: DeepPartial<Notification> = {
  269. _id: 1,
  270. templateKey: 'notification_dropbox_duplicate_project_names',
  271. }
  272. window.metaAttributesCache.set('ol-notifications', [
  273. merge(
  274. cloneDeep(notificationDropboxDuplicateProjectNames),
  275. reconfiguredNotification
  276. ),
  277. ])
  278. renderWithinProjectListProvider(Common)
  279. await fetchMock.callHistory.flush(true)
  280. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  281. screen.getByRole('alert')
  282. screen.getByText(/you have more than one project called/i)
  283. screen.getByText(/make your project names unique/i)
  284. const learnMore = screen.getByRole('link', { name: /learn more/i })
  285. expect(learnMore.getAttribute('href')).to.equal(
  286. '/learn/how-to/Dropbox_Synchronization#Troubleshooting'
  287. )
  288. const closeBtn = screen.getByRole('button', { name: /close/i })
  289. fireEvent.click(closeBtn)
  290. expect(fetchMock.callHistory.called()).to.be.true
  291. expect(screen.queryByRole('alert')).to.be.null
  292. })
  293. it('shows dropbox unlinked tue to lapsed reconfirmation', async function () {
  294. const reconfiguredNotification: DeepPartial<Notification> = {
  295. _id: 1,
  296. templateKey:
  297. 'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
  298. }
  299. window.metaAttributesCache.set('ol-notifications', [
  300. merge(
  301. cloneDeep(notificationDropboxDuplicateProjectNames),
  302. reconfiguredNotification
  303. ),
  304. ])
  305. renderWithinProjectListProvider(Common)
  306. await fetchMock.callHistory.flush(true)
  307. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  308. screen.getByRole('alert')
  309. screen.getByText(/your Dropbox account has been unlinked/i)
  310. screen.getByText(
  311. /confirm you are still at the institution and on their license, or upgrade your account in order to relink your dropbox account/i
  312. )
  313. const learnMore = screen.getByRole('link', { name: /learn more/i })
  314. expect(learnMore.getAttribute('href')).to.equal(
  315. '/learn/how-to/Institutional_Email_Reconfirmation'
  316. )
  317. const closeBtn = screen.getByRole('button', { name: /close/i })
  318. fireEvent.click(closeBtn)
  319. expect(fetchMock.callHistory.called()).to.be.true
  320. expect(screen.queryByRole('alert')).to.be.null
  321. })
  322. it('shows non specific notification', async function () {
  323. const reconfiguredNotification: DeepPartial<Notification> = {
  324. _id: 1,
  325. html: 'unspecific message',
  326. }
  327. window.metaAttributesCache.set('ol-notifications', [
  328. reconfiguredNotification,
  329. ])
  330. renderWithinProjectListProvider(Common)
  331. await fetchMock.callHistory.flush(true)
  332. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  333. screen.getByRole('alert')
  334. screen.getByText(reconfiguredNotification.html!)
  335. const closeBtn = screen.getByRole('button', { name: /close/i })
  336. fireEvent.click(closeBtn)
  337. expect(fetchMock.callHistory.called()).to.be.true
  338. expect(screen.queryByRole('alert')).to.be.null
  339. })
  340. describe('<GroupInvitation />', function () {
  341. describe('without existing personal subscription', function () {
  342. it('shows group invitation notification for user without personal subscription', async function () {
  343. const notificationGroupInvite: DeepPartial<Notification> = {
  344. _id: 1,
  345. templateKey: 'notification_group_invitation',
  346. }
  347. window.metaAttributesCache.set('ol-notifications', [
  348. merge(
  349. cloneDeep(notificationGroupInviteDefault),
  350. notificationGroupInvite
  351. ),
  352. ])
  353. renderWithinProjectListProvider(Common)
  354. await fetchMock.callHistory.flush(true)
  355. fetchMock.delete(`/notifications/${notificationGroupInvite._id}`, 200)
  356. screen.getByRole('alert')
  357. screen.getByText('inviter@overleaf.com')
  358. screen.getByText(
  359. /has invited you to join a group subscription on Overleaf/
  360. )
  361. screen.getByRole('button', { name: 'Join now' })
  362. screen.getByRole('button', { name: /close/i })
  363. })
  364. describe('with existing personal subscription', function () {
  365. it('shows group invitation notification for user with personal subscription', async function () {
  366. const notificationGroupInvite: DeepPartial<Notification> = {
  367. _id: 1,
  368. templateKey: 'notification_group_invitation',
  369. }
  370. window.metaAttributesCache.set('ol-notifications', [
  371. merge(
  372. cloneDeep(notificationGroupInviteDefault),
  373. notificationGroupInvite
  374. ),
  375. ])
  376. window.metaAttributesCache.set(
  377. 'ol-hasIndividualRecurlySubscription',
  378. true
  379. )
  380. renderWithinProjectListProvider(Common)
  381. await fetchMock.callHistory.flush(true)
  382. fetchMock.delete(
  383. `/notifications/${notificationGroupInvite._id}`,
  384. 200
  385. )
  386. screen.getByRole('alert')
  387. screen.getByText(
  388. /inviter@overleaf.com has invited you to join a group Overleaf subscription. If you join this group, you may not need your individual subscription. Would you like to cancel it/
  389. )
  390. screen.getByRole('button', { name: 'Not now' })
  391. screen.getByRole('button', { name: 'Cancel my subscription' })
  392. })
  393. })
  394. })
  395. })
  396. })
  397. describe('<Institution>', function () {
  398. beforeEach(function () {
  399. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  400. fetchMock.removeRoutes().clearHistory()
  401. })
  402. afterEach(function () {
  403. fetchMock.removeRoutes().clearHistory()
  404. })
  405. it('shows sso available', function () {
  406. const institution: DeepPartial<InstitutionType> = {
  407. templateKey: 'notification_institution_sso_available',
  408. }
  409. window.metaAttributesCache.set('ol-notificationsInstitution', [
  410. { ...notificationsInstitution, ...institution },
  411. ])
  412. render(<Institution />)
  413. screen.getByRole('alert')
  414. screen.getByText(/you can now link/i)
  415. screen.getByText(/doing this will allow you to log in/i)
  416. const learnMore = screen.getByRole('link', { name: /learn more/i })
  417. expect(learnMore.getAttribute('href')).to.equal(
  418. '/learn/how-to/Institutional_Login'
  419. )
  420. const action = screen.getByRole('link', { name: /link account/i })
  421. expect(action.getAttribute('href')).to.equal(
  422. `${exposedSettings.samlInitPath}?university_id=${notificationsInstitution.institutionId}&auto=/project&email=${notificationsInstitution.email}`
  423. )
  424. })
  425. it('shows sso linked', function () {
  426. const institution: DeepPartial<InstitutionType> = {
  427. _id: 1,
  428. templateKey: 'notification_institution_sso_linked',
  429. }
  430. window.metaAttributesCache.set('ol-notificationsInstitution', [
  431. { ...notificationsInstitution, ...institution },
  432. ])
  433. render(<Institution />)
  434. fetchMock.delete(`/notifications/${institution._id}`, 200)
  435. screen.getByRole('alert')
  436. screen.getByText(/has been linked to your/i)
  437. const closeBtn = screen.getByRole('button', { name: /close/i })
  438. fireEvent.click(closeBtn)
  439. expect(fetchMock.callHistory.called()).to.be.true
  440. expect(screen.queryByRole('alert')).to.be.null
  441. })
  442. it('shows sso non canonical', function () {
  443. const institution: DeepPartial<InstitutionType> = {
  444. _id: 1,
  445. templateKey: 'notification_institution_sso_non_canonical',
  446. }
  447. window.metaAttributesCache.set('ol-notificationsInstitution', [
  448. { ...notificationsInstitution, ...institution },
  449. ])
  450. render(<Institution />)
  451. fetchMock.delete(`/notifications/${institution._id}`, 200)
  452. screen.getByRole('alert')
  453. screen.getByText(/you’ve tried to log in with/i)
  454. screen.getByText(
  455. /in order to match your institutional metadata, your account is associated with/i
  456. )
  457. const closeBtn = screen.getByRole('button', { name: /close/i })
  458. fireEvent.click(closeBtn)
  459. expect(fetchMock.callHistory.called()).to.be.true
  460. expect(screen.queryByRole('alert')).to.be.null
  461. })
  462. it('shows sso already registered', function () {
  463. const institution: DeepPartial<InstitutionType> = {
  464. _id: 1,
  465. templateKey: 'notification_institution_sso_already_registered',
  466. }
  467. window.metaAttributesCache.set('ol-notificationsInstitution', [
  468. { ...notificationsInstitution, ...institution },
  469. ])
  470. render(<Institution />)
  471. fetchMock.delete(`/notifications/${institution._id}`, 200)
  472. screen.getByRole('alert')
  473. screen.getByText(/which is already registered with/i)
  474. const action = screen.getByRole('link', { name: /find out more/i })
  475. expect(action.getAttribute('href')).to.equal(
  476. '/learn/how-to/Institutional_Login'
  477. )
  478. const closeBtn = screen.getByRole('button', { name: /close/i })
  479. fireEvent.click(closeBtn)
  480. expect(fetchMock.callHistory.called()).to.be.true
  481. expect(screen.queryByRole('alert')).to.be.null
  482. })
  483. it('shows sso error', function () {
  484. const institution: DeepPartial<InstitutionType> = {
  485. templateKey: 'notification_institution_sso_error',
  486. error: {
  487. message: 'fake message',
  488. tryAgain: true,
  489. },
  490. }
  491. window.metaAttributesCache.set('ol-notificationsInstitution', [
  492. { ...notificationsInstitution, ...institution },
  493. ])
  494. render(<Institution />)
  495. screen.getByRole('alert')
  496. screen.getByText(/something went wrong/i)
  497. screen.getByText(institution.error!.message!)
  498. screen.getByText(/please try again/i)
  499. const closeBtn = screen.getByRole('button', { name: /close/i })
  500. fireEvent.click(closeBtn)
  501. expect(screen.queryByRole('alert')).to.be.null
  502. })
  503. })
  504. describe('getEmailDeletionDate', function () {
  505. beforeEach(async function () {
  506. window.metaAttributesCache.set('ol-userEmails', [
  507. confirmedUserData,
  508. untrustedUserData,
  509. ])
  510. this.clock = sinon.useFakeTimers(new Date('2025-07-01').getTime())
  511. })
  512. afterEach(function () {
  513. this.clock.restore()
  514. })
  515. it('returns deletion date for unconfirmed email within notification window', function () {
  516. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  517. const signUpDate = '2022-01-01' // Before cutoff '2025-03-03'
  518. const emailDeletionDate = getEmailDeletionDate(
  519. unconfirmedUserData,
  520. signUpDate
  521. )
  522. expect(emailDeletionDate).to.equal(
  523. new Date('2025-09-03').toLocaleDateString()
  524. )
  525. })
  526. it('returns false for primary email', function () {
  527. const primaryUserData = { ...unconfirmedUserData, default: true }
  528. const signUpDate = '2022-01-01'
  529. const emailDeletionDate = getEmailDeletionDate(
  530. primaryUserData,
  531. signUpDate
  532. )
  533. expect(emailDeletionDate).to.be.false
  534. })
  535. it('returns false for already confirmed email', function () {
  536. window.metaAttributesCache.set('ol-userEmails', [confirmedUserData])
  537. const signUpDate = '2022-01-01'
  538. const emailDeletionDate = getEmailDeletionDate(
  539. confirmedUserData,
  540. signUpDate
  541. )
  542. expect(emailDeletionDate).to.be.false
  543. })
  544. })
  545. describe('<ConfirmEmail/>', function () {
  546. beforeEach(async function () {
  547. Object.assign(getMeta('ol-ExposedSettings'), {
  548. emailConfirmationDisabled: false,
  549. })
  550. window.metaAttributesCache.set('ol-userEmails', [
  551. confirmedUserData,
  552. untrustedUserData,
  553. ])
  554. window.metaAttributesCache.set(
  555. 'ol-usersBestSubscription',
  556. freeSubscription
  557. )
  558. window.metaAttributesCache.set('ol-user', {
  559. signUpDate: new Date('2024-01-01').toISOString(),
  560. })
  561. })
  562. afterEach(function () {
  563. fetchMock.removeRoutes().clearHistory()
  564. })
  565. function testUnconfirmedNotification(
  566. userEmails: any[],
  567. isPrimary: boolean
  568. ) {
  569. it(`sends unconfirmed notification email successfully when email is ${isPrimary ? 'primary' : 'secondary'}`, async function () {
  570. window.metaAttributesCache.set('ol-userEmails', userEmails)
  571. renderWithinProjectListProvider(ConfirmEmail)
  572. await fetchMock.callHistory.flush(true)
  573. fetchMock.post('/user/emails/resend_confirmation', 200)
  574. const email = userEmails[0].email
  575. const notificationBody = await screen.findByTestId(
  576. 'pro-notification-body'
  577. )
  578. if (isPrimary) {
  579. expect(notificationBody.textContent).to.contain(
  580. `Please confirm your primary email address ${email} by clicking on the link in the confirmation email.`
  581. )
  582. } else {
  583. expect(notificationBody.textContent).to.contain(
  584. `Please confirm your secondary email address ${email} by clicking on the link in the confirmation email.`
  585. )
  586. }
  587. const resendButton = screen.getByRole('button', { name: /resend/i })
  588. fireEvent.click(resendButton)
  589. await waitForElementToBeRemoved(() =>
  590. screen.queryByRole('button', { name: /resend/i })
  591. )
  592. expect(fetchMock.callHistory.called()).to.be.true
  593. expect(screen.queryByRole('alert')).to.be.null
  594. })
  595. }
  596. testUnconfirmedNotification(
  597. [{ email: 'baz@overleaf.com', default: true }],
  598. true
  599. )
  600. testUnconfirmedNotification(
  601. [{ email: 'baz@overleaf.com', default: false }],
  602. false
  603. )
  604. it('sends untrusted notification email successfully', async function () {
  605. window.metaAttributesCache.set('ol-userEmails', [untrustedUserData])
  606. renderWithinProjectListProvider(ConfirmEmail)
  607. await fetchMock.callHistory.flush(true)
  608. fetchMock.post('/user/emails/resend_confirmation', 200)
  609. const email = untrustedUserData.email
  610. const notificationBody = await screen.findByTestId(
  611. 'not-trusted-notification-body'
  612. )
  613. expect(notificationBody.textContent).to.contain(
  614. `To enhance the security of your Overleaf account, please reconfirm your secondary email address ${email}.`
  615. )
  616. const resendButton = screen.getByRole('button', { name: /resend/i })
  617. fireEvent.click(resendButton)
  618. await waitForElementToBeRemoved(() =>
  619. screen.getByRole('button', { name: /resend/i })
  620. )
  621. expect(fetchMock.callHistory.called()).to.be.true
  622. expect(screen.queryByRole('alert')).to.be.null
  623. })
  624. it('fails to send', async function () {
  625. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  626. renderWithinProjectListProvider(ConfirmEmail)
  627. await fetchMock.callHistory.flush(true)
  628. fetchMock.post('/user/emails/resend_confirmation', 500)
  629. const resendButtons = await screen.findAllByRole('button', {
  630. name: /resend/i,
  631. })
  632. const resendButton = resendButtons[0]
  633. fireEvent.click(resendButton)
  634. const notificationBody = screen.getByTestId('pro-notification-body')
  635. await waitForElementToBeRemoved(() =>
  636. within(notificationBody).getByTestId(
  637. 'loading-resending-confirmation-email'
  638. )
  639. )
  640. expect(fetchMock.callHistory.called()).to.be.true
  641. screen.getByText(/something went wrong/i)
  642. })
  643. for (const subscription of [freeSubscription, individualSubscription]) {
  644. it(`shows commons notification for commons account when user is on ${subscription.type} plan`, async function () {
  645. window.metaAttributesCache.set('ol-userEmails', [
  646. unconfirmedCommonsUserData,
  647. ])
  648. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  649. renderWithinProjectListProvider(ConfirmEmail)
  650. await fetchMock.callHistory.flush(true)
  651. const alert = await screen.findByRole('alert')
  652. const email = unconfirmedCommonsUserData.email
  653. const notificationBody = within(alert).getByTestId('notification-body')
  654. expect(notificationBody.textContent).to.contain(
  655. 'You are one step away from accessing Overleaf Professional features'
  656. )
  657. expect(notificationBody.textContent).to.contain(
  658. `Overleaf has an Overleaf subscription. Click the confirmation link sent to ${email} to upgrade to Overleaf Professional`
  659. )
  660. })
  661. }
  662. for (const subscription of [groupSubscription, commonsSubscription]) {
  663. it(`shows default notification for commons account when user is on ${subscription.type} plan`, async function () {
  664. window.metaAttributesCache.set('ol-userEmails', [
  665. unconfirmedCommonsUserData,
  666. ])
  667. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  668. renderWithinProjectListProvider(ConfirmEmail)
  669. await fetchMock.callHistory.flush(true)
  670. const alert = await screen.findByRole('alert')
  671. const email = unconfirmedCommonsUserData.email
  672. const notificationBody = within(alert).getByTestId(
  673. 'pro-notification-body'
  674. )
  675. const isPrimary = unconfirmedCommonsUserData.default
  676. if (isPrimary) {
  677. expect(notificationBody.textContent).to.contain(
  678. `Please confirm your primary email address ${email} by clicking on the link in the confirmation email`
  679. )
  680. } else {
  681. expect(notificationBody.textContent).to.contain(
  682. `Please confirm your secondary email address ${email} by clicking on the link in the confirmation email`
  683. )
  684. }
  685. })
  686. }
  687. })
  688. describe('<Affiliation/>', function () {
  689. beforeEach(function () {
  690. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  691. this.locationWrapperSandbox = sinon.createSandbox()
  692. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  693. fetchMock.removeRoutes().clearHistory()
  694. })
  695. afterEach(function () {
  696. this.locationWrapperSandbox.restore()
  697. fetchMock.removeRoutes().clearHistory()
  698. })
  699. it('shows reconfirm message with SSO disabled', async function () {
  700. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  701. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  702. ])
  703. render(<ReconfirmationInfo />)
  704. screen.getByRole('alert')
  705. screen.getByText(
  706. /take a moment to confirm your institutional email address/i
  707. )
  708. const removeLink = screen.getByRole('link', { name: /remove it/i })
  709. expect(removeLink.getAttribute('href')).to.equal(
  710. `/user/settings?remove=${professionalUserData.email}`
  711. )
  712. const learnMore = screen.getByRole('link', { name: /learn more/i })
  713. expect(learnMore.getAttribute('href')).to.equal(
  714. '/learn/how-to/Institutional_Email_Reconfirmation'
  715. )
  716. const sendReconfirmationMock = fetchMock.post(
  717. '/user/emails/send-reconfirmation',
  718. 200
  719. )
  720. fireEvent.click(
  721. screen.getByRole('button', { name: /confirm affiliation/i })
  722. )
  723. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  724. screen.getByText(/check your email inbox to confirm/i)
  725. expect(screen.queryByRole('button', { name: /confirm affiliation/i })).to
  726. .be.null
  727. expect(screen.queryByRole('link', { name: /remove it/i })).to.be.null
  728. expect(screen.queryByRole('link', { name: /learn more/i })).to.be.null
  729. expect(sendReconfirmationMock.callHistory.called()).to.be.true
  730. fireEvent.click(
  731. screen.getByRole('button', { name: /resend confirmation email/i })
  732. )
  733. await waitForElementToBeRemoved(() => screen.getByText('Sending…'))
  734. expect(sendReconfirmationMock.callHistory.calls()).to.have.lengthOf(2)
  735. })
  736. it('shows reconfirm message with SSO enabled', async function () {
  737. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  738. merge(cloneDeep(professionalUserData), {
  739. affiliation: { institution: { ssoEnabled: true } },
  740. }),
  741. ])
  742. render(<ReconfirmationInfo />)
  743. fireEvent.click(
  744. screen.getByRole('button', { name: /confirm affiliation/i })
  745. )
  746. sinon.assert.calledOnce(this.locationWrapperStub.assign)
  747. sinon.assert.calledWithMatch(
  748. this.locationWrapperStub.assign,
  749. `${exposedSettings.samlInitPath}?university_id=${professionalUserData.affiliation.institution.id}&reconfirm=/project`
  750. )
  751. })
  752. it('shows success reconfirmation message', function () {
  753. window.metaAttributesCache.set('ol-userEmails', [
  754. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  755. ])
  756. window.metaAttributesCache.set('ol-reconfirmedViaSAML', 'Saml Provider')
  757. const { rerender } = render(<ReconfirmationInfo />)
  758. screen.getByRole('alert')
  759. screen.getByText(professionalUserData.affiliation!.institution!.name!)
  760. screen.getByText(/affiliation is confirmed/i)
  761. window.metaAttributesCache.set('ol-reconfirmedViaSAML', '')
  762. rerender(<ReconfirmationInfo />)
  763. expect(screen.queryByRole('alert')).to.be.null
  764. })
  765. })
  766. describe('<GroupsAndEnterpriseBanner />', function () {
  767. beforeEach(function () {
  768. localStorage.clear()
  769. fetchMock.removeRoutes().clearHistory()
  770. // at least one project is required to show some notifications
  771. const projects = [{}] as Project[]
  772. fetchMock.post(/\/api\/project/, {
  773. status: 200,
  774. body: {
  775. projects,
  776. totalSize: projects.length,
  777. },
  778. })
  779. window.metaAttributesCache.set(
  780. 'ol-groupsAndEnterpriseBannerVariant',
  781. 'on-premise'
  782. )
  783. })
  784. afterEach(function () {
  785. fetchMock.removeRoutes().clearHistory()
  786. })
  787. it('does not show the banner for users that are in group or are affiliated', async function () {
  788. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', false)
  789. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  790. await fetchMock.callHistory.flush(true)
  791. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  792. })
  793. it('shows the banner for users that have dismissed the previous banners', async function () {
  794. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  795. localStorage.setItem('has_dismissed_groups_and_enterprise_banner', true)
  796. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  797. await fetchMock.callHistory.flush(true)
  798. await screen.findByRole('link', { name: 'Contact sales' })
  799. })
  800. it('shows the banner for users that have dismissed the banner more than 30 days ago', async function () {
  801. const dismissed = new Date()
  802. dismissed.setDate(dismissed.getDate() - 31) // 31 days
  803. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  804. localStorage.setItem(
  805. 'has_dismissed_groups_and_enterprise_banner',
  806. dismissed
  807. )
  808. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  809. await fetchMock.callHistory.flush(true)
  810. await screen.findByRole('link', { name: 'Contact sales' })
  811. })
  812. it('does not show the banner for users that have dismissed the banner within the last 30 days', async function () {
  813. const dismissed = new Date()
  814. dismissed.setDate(dismissed.getDate() - 29) // 29 days
  815. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  816. localStorage.setItem(
  817. 'has_dismissed_groups_and_enterprise_banner',
  818. dismissed
  819. )
  820. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  821. await fetchMock.callHistory.flush(true)
  822. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  823. })
  824. describe('users that are not in group and are not affiliated', function () {
  825. beforeEach(function () {
  826. localStorage.clear()
  827. fetchMock.removeRoutes().clearHistory()
  828. // at least one project is required to show some notifications
  829. const projects = [{}] as Project[]
  830. fetchMock.post(/\/api\/project/, {
  831. status: 200,
  832. body: {
  833. projects,
  834. totalSize: projects.length,
  835. },
  836. })
  837. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  838. })
  839. afterEach(function () {
  840. fetchMock.removeRoutes().clearHistory()
  841. })
  842. after(function () {
  843. localStorage.clear()
  844. })
  845. it('will show the correct text for the `on-premise` variant', async function () {
  846. window.metaAttributesCache.set(
  847. 'ol-groupsAndEnterpriseBannerVariant',
  848. 'on-premise'
  849. )
  850. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  851. await fetchMock.callHistory.flush(true)
  852. await screen.findByText(
  853. 'Overleaf On-Premises: Does your company want to keep its data within its firewall? Overleaf offers Server Pro, an on-premises solution for companies. Get in touch to learn more.'
  854. )
  855. const link = screen.getByRole('link', { name: 'Contact sales' })
  856. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-2`)
  857. })
  858. it('will show the correct text for the `FOMO` variant', async function () {
  859. window.metaAttributesCache.set(
  860. 'ol-groupsAndEnterpriseBannerVariant',
  861. 'FOMO'
  862. )
  863. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  864. await fetchMock.callHistory.flush(true)
  865. await screen.findByText(
  866. 'Why do Fortune 500 companies and top research institutions trust Overleaf to streamline their collaboration? Get in touch to learn more.'
  867. )
  868. const link = screen.getByRole('link', { name: 'Contact sales' })
  869. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-4`)
  870. })
  871. })
  872. })
  873. describe('GroupSsoSetupSuccess', function () {
  874. it('shows group SSO linked notification', function () {
  875. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', true)
  876. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  877. screen.getByText('Success! Single sign-on is all set up for you.')
  878. })
  879. it('does not show group SSO linked notification', function () {
  880. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', false)
  881. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  882. expect(
  883. screen.queryByText('Success! Single sign-on is all set up for you.')
  884. ).to.be.null
  885. })
  886. })
  887. })