notifications.test.tsx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 an institutional email address to claim your features/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. })
  534. describe('getEmailDeletionDate', function () {
  535. beforeEach(async function () {
  536. window.metaAttributesCache.set('ol-userEmails', [
  537. confirmedUserData,
  538. untrustedUserData,
  539. ])
  540. this.clock = sinon.useFakeTimers(new Date('2025-07-01').getTime())
  541. })
  542. afterEach(function () {
  543. this.clock.restore()
  544. })
  545. it('returns deletion date for unconfirmed email within notification window', function () {
  546. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  547. const signUpDate = '2022-01-01' // Before cutoff '2025-03-03'
  548. const emailDeletionDate = getEmailDeletionDate(
  549. unconfirmedUserData,
  550. signUpDate
  551. )
  552. const dateOptions: Intl.DateTimeFormatOptions = {
  553. month: 'long',
  554. day: 'numeric',
  555. year: 'numeric',
  556. }
  557. expect(emailDeletionDate).to.equal(
  558. new Date('2025-09-03').toLocaleDateString(undefined, dateOptions)
  559. )
  560. })
  561. it('returns false for primary email', function () {
  562. const primaryUserData = { ...unconfirmedUserData, default: true }
  563. const signUpDate = '2022-01-01'
  564. const emailDeletionDate = getEmailDeletionDate(
  565. primaryUserData,
  566. signUpDate
  567. )
  568. expect(emailDeletionDate).to.be.false
  569. })
  570. it('returns false for already confirmed email', function () {
  571. window.metaAttributesCache.set('ol-userEmails', [confirmedUserData])
  572. const signUpDate = '2022-01-01'
  573. const emailDeletionDate = getEmailDeletionDate(
  574. confirmedUserData,
  575. signUpDate
  576. )
  577. expect(emailDeletionDate).to.be.false
  578. })
  579. })
  580. describe('<ConfirmEmail/>', function () {
  581. beforeEach(async function () {
  582. Object.assign(getMeta('ol-ExposedSettings'), {
  583. emailConfirmationDisabled: false,
  584. })
  585. window.metaAttributesCache.set('ol-userEmails', [
  586. confirmedUserData,
  587. untrustedUserData,
  588. ])
  589. window.metaAttributesCache.set(
  590. 'ol-usersBestSubscription',
  591. freeSubscription
  592. )
  593. window.metaAttributesCache.set('ol-user', {
  594. signUpDate: new Date('2024-01-01').toISOString(),
  595. })
  596. })
  597. afterEach(function () {
  598. fetchMock.removeRoutes().clearHistory()
  599. })
  600. function testUnconfirmedNotification(
  601. userEmails: any[],
  602. isPrimary: boolean
  603. ) {
  604. it(`sends unconfirmed notification email successfully when email is ${isPrimary ? 'primary' : 'secondary'}`, async function () {
  605. window.metaAttributesCache.set('ol-userEmails', userEmails)
  606. renderWithinProjectListProvider(ConfirmEmail)
  607. await fetchMock.callHistory.flush(true)
  608. fetchMock.post('/user/emails/send-confirmation-code', 200)
  609. const email = userEmails[0].email
  610. const alert = await screen.findByRole('alert')
  611. if (isPrimary) {
  612. expect(alert.textContent).to.contain(
  613. `Please confirm your primary email address ${email}. To edit it, go to `
  614. )
  615. } else {
  616. expect(alert.textContent).to.contain(
  617. `Please confirm your secondary email address ${email}. To edit it, go to `
  618. )
  619. }
  620. expect(
  621. screen
  622. .getByRole('button', { name: 'Send confirmation code' })
  623. .classList.contains('button-loading')
  624. ).to.be.false
  625. expect(screen.queryByRole('dialog')).to.be.null
  626. const sendCodeButton = await screen.findByRole('button', {
  627. name: 'Send confirmation code',
  628. })
  629. fireEvent.click(sendCodeButton)
  630. await screen.findByRole('dialog')
  631. expect(fetchMock.callHistory.called()).to.be.true
  632. })
  633. }
  634. testUnconfirmedNotification(
  635. [{ email: 'baz@overleaf.com', default: true }],
  636. true
  637. )
  638. testUnconfirmedNotification(
  639. [{ email: 'baz@overleaf.com', default: false }],
  640. false
  641. )
  642. it('sends untrusted notification email successfully', async function () {
  643. window.metaAttributesCache.set('ol-userEmails', [untrustedUserData])
  644. renderWithinProjectListProvider(ConfirmEmail)
  645. await fetchMock.callHistory.flush(true)
  646. fetchMock.post('/user/emails/send-confirmation-code', 200)
  647. const email = untrustedUserData.email
  648. const alert = await screen.findByRole('alert')
  649. expect(alert.textContent).to.contain(
  650. `To enhance the security of your Overleaf account, please reconfirm your secondary email address ${email}.`
  651. )
  652. const resendButton = screen.getByRole('button', {
  653. name: 'Send confirmation code',
  654. })
  655. fireEvent.click(resendButton)
  656. await screen.findByRole('dialog')
  657. expect(fetchMock.callHistory.called()).to.be.true
  658. })
  659. it('fails to send', async function () {
  660. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  661. renderWithinProjectListProvider(ConfirmEmail)
  662. await fetchMock.callHistory.flush(true)
  663. fetchMock.post('/user/emails/send-confirmation-code', 500)
  664. const resendButtons = await screen.findAllByRole('button', {
  665. name: 'Send confirmation code',
  666. })
  667. const resendButton = resendButtons[0]
  668. fireEvent.click(resendButton)
  669. await fetchMock.callHistory.flush(true)
  670. await screen.findByRole('dialog')
  671. expect(fetchMock.callHistory.called()).to.be.true
  672. screen.getByText(/something went wrong/i)
  673. })
  674. for (const subscription of [freeSubscription, individualSubscription]) {
  675. it(`shows commons notification for commons account when user is on ${subscription.type} plan`, async function () {
  676. window.metaAttributesCache.set('ol-userEmails', [
  677. unconfirmedCommonsUserData,
  678. ])
  679. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  680. renderWithinProjectListProvider(ConfirmEmail)
  681. await fetchMock.callHistory.flush(true)
  682. const alert = await screen.findByRole('alert')
  683. const email = unconfirmedCommonsUserData.email
  684. expect(alert.textContent).to.contain(
  685. 'You are one step away from accessing Overleaf Professional features'
  686. )
  687. expect(alert.textContent).to.contain(
  688. `Overleaf has an Overleaf subscription. Click the confirmation link sent to ${email} to upgrade to Overleaf Professional`
  689. )
  690. })
  691. }
  692. for (const subscription of [groupSubscription, commonsSubscription]) {
  693. it(`shows default notification for commons account when user is on ${subscription.type} plan`, async function () {
  694. window.metaAttributesCache.set('ol-userEmails', [
  695. unconfirmedCommonsUserData,
  696. ])
  697. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  698. renderWithinProjectListProvider(ConfirmEmail)
  699. await fetchMock.callHistory.flush(true)
  700. const alert = await screen.findByRole('alert')
  701. const email = unconfirmedCommonsUserData.email
  702. const isPrimary = unconfirmedCommonsUserData.default
  703. if (isPrimary) {
  704. expect(alert.textContent).to.contain(
  705. `Please confirm your primary email address ${email}.`
  706. )
  707. } else {
  708. expect(alert.textContent).to.contain(
  709. `Please confirm your secondary email address ${email}.`
  710. )
  711. }
  712. })
  713. }
  714. })
  715. describe('<Affiliation/>', function () {
  716. beforeEach(function () {
  717. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  718. this.locationWrapperSandbox = sinon.createSandbox()
  719. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  720. fetchMock.removeRoutes().clearHistory()
  721. })
  722. afterEach(function () {
  723. this.locationWrapperSandbox.restore()
  724. fetchMock.removeRoutes().clearHistory()
  725. })
  726. it('shows reconfirm message with SSO disabled', async function () {
  727. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  728. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  729. ])
  730. render(<ReconfirmationInfo />)
  731. screen.getByRole('alert')
  732. screen.getByText(
  733. /take a moment to confirm your institutional email address/i
  734. )
  735. const removeLink = screen.getByRole('link', { name: /remove it/i })
  736. expect(removeLink.getAttribute('href')).to.equal(
  737. `/user/settings?remove=${professionalUserData.email}`
  738. )
  739. const learnMore = screen.getByRole('link', { name: /learn more/i })
  740. expect(learnMore.getAttribute('href')).to.equal(
  741. '/learn/how-to/Institutional_Email_Reconfirmation'
  742. )
  743. const sendReconfirmationMock = fetchMock.post(
  744. '/user/emails/send-confirmation-code',
  745. 200
  746. )
  747. fireEvent.click(
  748. screen.getByRole('button', { name: 'Send confirmation code' })
  749. )
  750. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  751. // Sometimes we need to wait for useWaitForI18n to be ready
  752. await screen.findByText(
  753. /Enter the 6-digit code sent to foo@overleaf.com/i
  754. )
  755. expect(sendReconfirmationMock.callHistory.called()).to.be.true
  756. fireEvent.click(
  757. screen.getByRole('button', { name: /resend confirmation code/i })
  758. )
  759. await waitForElementToBeRemoved(() =>
  760. screen.getByText('Resending confirmation code')
  761. )
  762. expect(sendReconfirmationMock.callHistory.calls()).to.have.lengthOf(2)
  763. })
  764. it('shows reconfirm message with SSO enabled', async function () {
  765. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  766. merge(cloneDeep(professionalUserData), {
  767. affiliation: { institution: { ssoEnabled: true } },
  768. }),
  769. ])
  770. render(<ReconfirmationInfo />)
  771. fireEvent.click(
  772. screen.getByRole('button', { name: /confirm affiliation/i })
  773. )
  774. sinon.assert.calledOnce(this.locationWrapperStub.assign)
  775. sinon.assert.calledWithMatch(
  776. this.locationWrapperStub.assign,
  777. `${exposedSettings.samlInitPath}?university_id=${professionalUserData.affiliation.institution.id}&reconfirm=/project`
  778. )
  779. })
  780. it('shows success reconfirmation message', function () {
  781. window.metaAttributesCache.set('ol-userEmails', [
  782. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  783. ])
  784. window.metaAttributesCache.set('ol-reconfirmedViaSAML', 'Saml Provider')
  785. const { rerender } = render(<ReconfirmationInfo />)
  786. screen.getByRole('alert')
  787. screen.getByText(professionalUserData.affiliation!.institution!.name!)
  788. screen.getByText(/affiliation is confirmed/i)
  789. window.metaAttributesCache.set('ol-reconfirmedViaSAML', '')
  790. rerender(<ReconfirmationInfo />)
  791. expect(screen.queryByRole('alert')).to.be.null
  792. })
  793. })
  794. describe('<GroupsAndEnterpriseBanner />', function () {
  795. beforeEach(function () {
  796. localStorage.clear()
  797. fetchMock.removeRoutes().clearHistory()
  798. // at least one project is required to show some notifications
  799. const projects = [{}] as Project[]
  800. fetchMock.post(/\/api\/project/, {
  801. status: 200,
  802. body: {
  803. projects,
  804. totalSize: projects.length,
  805. },
  806. })
  807. window.metaAttributesCache.set(
  808. 'ol-groupsAndEnterpriseBannerVariant',
  809. 'on-premise'
  810. )
  811. window.metaAttributesCache.set('ol-inactiveTutorials', '[]')
  812. })
  813. afterEach(function () {
  814. fetchMock.removeRoutes().clearHistory()
  815. })
  816. it('does not show the banner for users that are in group or are affiliated', async function () {
  817. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', false)
  818. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  819. await fetchMock.callHistory.flush(true)
  820. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  821. })
  822. it('shows the banner for users that have dismissed the previous banners', async function () {
  823. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  824. localStorage.setItem('has_dismissed_groups_and_enterprise_banner', true)
  825. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  826. await fetchMock.callHistory.flush(true)
  827. await screen.findByRole('link', { name: 'Contact sales' })
  828. })
  829. 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 () {
  830. const dismissed = new Date()
  831. dismissed.setDate(dismissed.getDate() - 29) // 29 days
  832. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  833. localStorage.setItem(
  834. 'has_dismissed_groups_and_enterprise_banner',
  835. dismissed
  836. )
  837. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  838. await fetchMock.callHistory.flush(true)
  839. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  840. })
  841. it('shows the banner for users who have not dismissed the repeat appearance', async function () {
  842. window.metaAttributesCache.set(
  843. 'ol-inactiveTutorials',
  844. '["groups-enterprise-banner"]'
  845. )
  846. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  847. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  848. await fetchMock.callHistory.flush(true)
  849. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  850. })
  851. it('does not show the banner for users with both inactive tutorials', async function () {
  852. window.metaAttributesCache.set(
  853. 'ol-inactiveTutorials',
  854. '["groups-enterprise-banner", "groups-enterprise-banner-repeat"]'
  855. )
  856. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  857. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  858. await fetchMock.callHistory.flush(true)
  859. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  860. })
  861. describe('users that are not in group and are not affiliated', function () {
  862. beforeEach(function () {
  863. localStorage.clear()
  864. fetchMock.removeRoutes().clearHistory()
  865. // at least one project is required to show some notifications
  866. const projects = [{}] as Project[]
  867. fetchMock.post(/\/api\/project/, {
  868. status: 200,
  869. body: {
  870. projects,
  871. totalSize: projects.length,
  872. },
  873. })
  874. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  875. })
  876. afterEach(function () {
  877. fetchMock.removeRoutes().clearHistory()
  878. })
  879. after(function () {
  880. localStorage.clear()
  881. })
  882. it('will show the correct text for the `on-premise` variant', async function () {
  883. window.metaAttributesCache.set(
  884. 'ol-groupsAndEnterpriseBannerVariant',
  885. 'on-premise'
  886. )
  887. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  888. await fetchMock.callHistory.flush(true)
  889. await screen.findByText(
  890. '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.'
  891. )
  892. const link = screen.getByRole('link', { name: 'Contact sales' })
  893. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-2`)
  894. })
  895. it('will show the correct text for the `FOMO` variant', async function () {
  896. window.metaAttributesCache.set(
  897. 'ol-groupsAndEnterpriseBannerVariant',
  898. 'FOMO'
  899. )
  900. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  901. await fetchMock.callHistory.flush(true)
  902. await screen.findByText(
  903. 'Why do Fortune 500 companies and top research institutions trust Overleaf to streamline their collaboration? Get in touch to learn more.'
  904. )
  905. const link = screen.getByRole('link', { name: 'Contact sales' })
  906. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-4`)
  907. })
  908. })
  909. })
  910. describe('GroupSsoSetupSuccess', function () {
  911. it('shows group SSO linked notification', function () {
  912. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', true)
  913. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  914. screen.getByText('Success! Single sign-on is all set up for you.')
  915. })
  916. it('does not show group SSO linked notification', function () {
  917. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', false)
  918. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  919. expect(
  920. screen.queryByText('Success! Single sign-on is all set up for you.')
  921. ).to.be.null
  922. })
  923. it('shows group SSO linked notification when joining via domain capture', function () {
  924. const groupName = 'Group Name'
  925. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', true)
  926. window.metaAttributesCache.set('ol-viaDomainCapture', true)
  927. window.metaAttributesCache.set('ol-joinedGroupName', groupName)
  928. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  929. const alert = screen.getByRole('alert')
  930. expect(alert.textContent).to.contain(
  931. `You’ve joined the ${groupName} group. SSO is enabled.`
  932. )
  933. })
  934. })
  935. })