notifications.test.tsx 37 KB

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