notifications.test.tsx 40 KB

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