notifications.test.tsx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. screen.getByRole('alert')
  435. screen.getByText(/has been linked to your/i)
  436. const closeBtn = screen.getByRole('button', { name: /close/i })
  437. fireEvent.click(closeBtn)
  438. expect(fetchMock.callHistory.called()).to.be.true
  439. expect(screen.queryByRole('alert')).to.be.null
  440. })
  441. it('shows sso non canonical', function () {
  442. const institution: DeepPartial<InstitutionType> = {
  443. _id: 1,
  444. templateKey: 'notification_institution_sso_non_canonical',
  445. }
  446. window.metaAttributesCache.set('ol-notificationsInstitution', [
  447. { ...notificationsInstitution, ...institution },
  448. ])
  449. render(<Institution />)
  450. fetchMock.delete(`/notifications/${institution._id}`, 200)
  451. screen.getByRole('alert')
  452. screen.getByText(/you’ve tried to log in with/i)
  453. screen.getByText(
  454. /in order to match your institutional metadata, your account is associated with/i
  455. )
  456. const closeBtn = screen.getByRole('button', { name: /close/i })
  457. fireEvent.click(closeBtn)
  458. expect(fetchMock.callHistory.called()).to.be.true
  459. expect(screen.queryByRole('alert')).to.be.null
  460. })
  461. it('shows sso already registered', function () {
  462. const institution: DeepPartial<InstitutionType> = {
  463. _id: 1,
  464. templateKey: 'notification_institution_sso_already_registered',
  465. }
  466. window.metaAttributesCache.set('ol-notificationsInstitution', [
  467. { ...notificationsInstitution, ...institution },
  468. ])
  469. render(<Institution />)
  470. fetchMock.delete(`/notifications/${institution._id}`, 200)
  471. screen.getByRole('alert')
  472. screen.getByText(/which is already registered with/i)
  473. const action = screen.getByRole('link', { name: /find out more/i })
  474. expect(action.getAttribute('href')).to.equal(
  475. '/learn/how-to/Institutional_Login'
  476. )
  477. const closeBtn = screen.getByRole('button', { name: /close/i })
  478. fireEvent.click(closeBtn)
  479. expect(fetchMock.callHistory.called()).to.be.true
  480. expect(screen.queryByRole('alert')).to.be.null
  481. })
  482. it('shows sso error', function () {
  483. const institution: DeepPartial<InstitutionType> = {
  484. templateKey: 'notification_institution_sso_error',
  485. error: {
  486. message: 'fake message',
  487. tryAgain: true,
  488. },
  489. }
  490. window.metaAttributesCache.set('ol-notificationsInstitution', [
  491. { ...notificationsInstitution, ...institution },
  492. ])
  493. render(<Institution />)
  494. screen.getByRole('alert')
  495. screen.getByText(/something went wrong/i)
  496. screen.getByText(institution.error!.message!)
  497. screen.getByText(/please try again/i)
  498. const closeBtn = screen.getByRole('button', { name: /close/i })
  499. fireEvent.click(closeBtn)
  500. expect(screen.queryByRole('alert')).to.be.null
  501. })
  502. })
  503. describe('getEmailDeletionDate', function () {
  504. beforeEach(async function () {
  505. window.metaAttributesCache.set('ol-userEmails', [
  506. confirmedUserData,
  507. untrustedUserData,
  508. ])
  509. this.clock = sinon.useFakeTimers(new Date('2025-07-01').getTime())
  510. })
  511. afterEach(function () {
  512. this.clock.restore()
  513. })
  514. it('returns deletion date for unconfirmed email within notification window', function () {
  515. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  516. const signUpDate = '2022-01-01' // Before cutoff '2025-03-03'
  517. const emailDeletionDate = getEmailDeletionDate(
  518. unconfirmedUserData,
  519. signUpDate
  520. )
  521. const dateOptions: Intl.DateTimeFormatOptions = {
  522. month: 'long',
  523. day: 'numeric',
  524. year: 'numeric',
  525. }
  526. expect(emailDeletionDate).to.equal(
  527. new Date('2025-09-03').toLocaleDateString(undefined, dateOptions)
  528. )
  529. })
  530. it('returns false for primary email', function () {
  531. const primaryUserData = { ...unconfirmedUserData, default: true }
  532. const signUpDate = '2022-01-01'
  533. const emailDeletionDate = getEmailDeletionDate(
  534. primaryUserData,
  535. signUpDate
  536. )
  537. expect(emailDeletionDate).to.be.false
  538. })
  539. it('returns false for already confirmed email', function () {
  540. window.metaAttributesCache.set('ol-userEmails', [confirmedUserData])
  541. const signUpDate = '2022-01-01'
  542. const emailDeletionDate = getEmailDeletionDate(
  543. confirmedUserData,
  544. signUpDate
  545. )
  546. expect(emailDeletionDate).to.be.false
  547. })
  548. })
  549. describe('<ConfirmEmail/>', function () {
  550. beforeEach(async function () {
  551. Object.assign(getMeta('ol-ExposedSettings'), {
  552. emailConfirmationDisabled: false,
  553. })
  554. window.metaAttributesCache.set('ol-userEmails', [
  555. confirmedUserData,
  556. untrustedUserData,
  557. ])
  558. window.metaAttributesCache.set(
  559. 'ol-usersBestSubscription',
  560. freeSubscription
  561. )
  562. window.metaAttributesCache.set('ol-user', {
  563. signUpDate: new Date('2024-01-01').toISOString(),
  564. })
  565. })
  566. afterEach(function () {
  567. fetchMock.removeRoutes().clearHistory()
  568. })
  569. function testUnconfirmedNotification(
  570. userEmails: any[],
  571. isPrimary: boolean
  572. ) {
  573. it(`sends unconfirmed notification email successfully when email is ${isPrimary ? 'primary' : 'secondary'}`, async function () {
  574. window.metaAttributesCache.set('ol-userEmails', userEmails)
  575. renderWithinProjectListProvider(ConfirmEmail)
  576. await fetchMock.callHistory.flush(true)
  577. fetchMock.post('/user/emails/send-confirmation-code', 200)
  578. const email = userEmails[0].email
  579. const alert = await screen.findByRole('alert')
  580. if (isPrimary) {
  581. expect(alert.textContent).to.contain(
  582. `Please confirm your primary email address ${email}. To edit it, go to `
  583. )
  584. } else {
  585. expect(alert.textContent).to.contain(
  586. `Please confirm your secondary email address ${email}. To edit it, go to `
  587. )
  588. }
  589. expect(
  590. screen
  591. .getByRole('button', { name: 'Send confirmation code' })
  592. .classList.contains('button-loading')
  593. ).to.be.false
  594. expect(screen.queryByRole('dialog')).to.be.null
  595. const sendCodeButton = await screen.findByRole('button', {
  596. name: 'Send confirmation code',
  597. })
  598. fireEvent.click(sendCodeButton)
  599. await screen.findByRole('dialog')
  600. expect(fetchMock.callHistory.called()).to.be.true
  601. })
  602. }
  603. testUnconfirmedNotification(
  604. [{ email: 'baz@overleaf.com', default: true }],
  605. true
  606. )
  607. testUnconfirmedNotification(
  608. [{ email: 'baz@overleaf.com', default: false }],
  609. false
  610. )
  611. it('sends untrusted notification email successfully', async function () {
  612. window.metaAttributesCache.set('ol-userEmails', [untrustedUserData])
  613. renderWithinProjectListProvider(ConfirmEmail)
  614. await fetchMock.callHistory.flush(true)
  615. fetchMock.post('/user/emails/send-confirmation-code', 200)
  616. const email = untrustedUserData.email
  617. const alert = await screen.findByRole('alert')
  618. expect(alert.textContent).to.contain(
  619. `To enhance the security of your Overleaf account, please reconfirm your secondary email address ${email}.`
  620. )
  621. const resendButton = screen.getByRole('button', {
  622. name: 'Send confirmation code',
  623. })
  624. fireEvent.click(resendButton)
  625. await screen.findByRole('dialog')
  626. expect(fetchMock.callHistory.called()).to.be.true
  627. })
  628. it('fails to send', async function () {
  629. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  630. renderWithinProjectListProvider(ConfirmEmail)
  631. await fetchMock.callHistory.flush(true)
  632. fetchMock.post('/user/emails/send-confirmation-code', 500)
  633. const resendButtons = await screen.findAllByRole('button', {
  634. name: 'Send confirmation code',
  635. })
  636. const resendButton = resendButtons[0]
  637. fireEvent.click(resendButton)
  638. await screen.findByRole('dialog')
  639. expect(fetchMock.callHistory.called()).to.be.true
  640. screen.getByText(/something went wrong/i)
  641. })
  642. for (const subscription of [freeSubscription, individualSubscription]) {
  643. it(`shows commons notification for commons account when user is on ${subscription.type} plan`, async function () {
  644. window.metaAttributesCache.set('ol-userEmails', [
  645. unconfirmedCommonsUserData,
  646. ])
  647. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  648. renderWithinProjectListProvider(ConfirmEmail)
  649. await fetchMock.callHistory.flush(true)
  650. const alert = await screen.findByRole('alert')
  651. const email = unconfirmedCommonsUserData.email
  652. expect(alert.textContent).to.contain(
  653. 'You are one step away from accessing Overleaf Professional features'
  654. )
  655. expect(alert.textContent).to.contain(
  656. `Overleaf has an Overleaf subscription. Click the confirmation link sent to ${email} to upgrade to Overleaf Professional`
  657. )
  658. })
  659. }
  660. for (const subscription of [groupSubscription, commonsSubscription]) {
  661. it(`shows default notification for commons account when user is on ${subscription.type} plan`, async function () {
  662. window.metaAttributesCache.set('ol-userEmails', [
  663. unconfirmedCommonsUserData,
  664. ])
  665. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  666. renderWithinProjectListProvider(ConfirmEmail)
  667. await fetchMock.callHistory.flush(true)
  668. const alert = await screen.findByRole('alert')
  669. const email = unconfirmedCommonsUserData.email
  670. const isPrimary = unconfirmedCommonsUserData.default
  671. if (isPrimary) {
  672. expect(alert.textContent).to.contain(
  673. `Please confirm your primary email address ${email}.`
  674. )
  675. } else {
  676. expect(alert.textContent).to.contain(
  677. `Please confirm your secondary email address ${email}.`
  678. )
  679. }
  680. })
  681. }
  682. })
  683. describe('<Affiliation/>', function () {
  684. beforeEach(function () {
  685. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  686. this.locationWrapperSandbox = sinon.createSandbox()
  687. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  688. fetchMock.removeRoutes().clearHistory()
  689. })
  690. afterEach(function () {
  691. this.locationWrapperSandbox.restore()
  692. fetchMock.removeRoutes().clearHistory()
  693. })
  694. it('shows reconfirm message with SSO disabled', async function () {
  695. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  696. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  697. ])
  698. render(<ReconfirmationInfo />)
  699. screen.getByRole('alert')
  700. screen.getByText(
  701. /take a moment to confirm your institutional email address/i
  702. )
  703. const removeLink = screen.getByRole('link', { name: /remove it/i })
  704. expect(removeLink.getAttribute('href')).to.equal(
  705. `/user/settings?remove=${professionalUserData.email}`
  706. )
  707. const learnMore = screen.getByRole('link', { name: /learn more/i })
  708. expect(learnMore.getAttribute('href')).to.equal(
  709. '/learn/how-to/Institutional_Email_Reconfirmation'
  710. )
  711. const sendReconfirmationMock = fetchMock.post(
  712. '/user/emails/send-confirmation-code',
  713. 200
  714. )
  715. fireEvent.click(
  716. screen.getByRole('button', { name: 'Send confirmation code' })
  717. )
  718. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  719. screen.getByText(/Enter the 6-digit code sent to foo@overleaf.com/i)
  720. expect(sendReconfirmationMock.callHistory.called()).to.be.true
  721. fireEvent.click(
  722. screen.getByRole('button', { name: /resend confirmation code/i })
  723. )
  724. await waitForElementToBeRemoved(() =>
  725. screen.getByText('Resending confirmation code')
  726. )
  727. expect(sendReconfirmationMock.callHistory.calls()).to.have.lengthOf(2)
  728. })
  729. it('shows reconfirm message with SSO enabled', async function () {
  730. window.metaAttributesCache.set('ol-allInReconfirmNotificationPeriods', [
  731. merge(cloneDeep(professionalUserData), {
  732. affiliation: { institution: { ssoEnabled: true } },
  733. }),
  734. ])
  735. render(<ReconfirmationInfo />)
  736. fireEvent.click(
  737. screen.getByRole('button', { name: /confirm affiliation/i })
  738. )
  739. sinon.assert.calledOnce(this.locationWrapperStub.assign)
  740. sinon.assert.calledWithMatch(
  741. this.locationWrapperStub.assign,
  742. `${exposedSettings.samlInitPath}?university_id=${professionalUserData.affiliation.institution.id}&reconfirm=/project`
  743. )
  744. })
  745. it('shows success reconfirmation message', function () {
  746. window.metaAttributesCache.set('ol-userEmails', [
  747. { ...professionalUserData, samlProviderId: 'Saml Provider' },
  748. ])
  749. window.metaAttributesCache.set('ol-reconfirmedViaSAML', 'Saml Provider')
  750. const { rerender } = render(<ReconfirmationInfo />)
  751. screen.getByRole('alert')
  752. screen.getByText(professionalUserData.affiliation!.institution!.name!)
  753. screen.getByText(/affiliation is confirmed/i)
  754. window.metaAttributesCache.set('ol-reconfirmedViaSAML', '')
  755. rerender(<ReconfirmationInfo />)
  756. expect(screen.queryByRole('alert')).to.be.null
  757. })
  758. })
  759. describe('<GroupsAndEnterpriseBanner />', function () {
  760. beforeEach(function () {
  761. localStorage.clear()
  762. fetchMock.removeRoutes().clearHistory()
  763. // at least one project is required to show some notifications
  764. const projects = [{}] as Project[]
  765. fetchMock.post(/\/api\/project/, {
  766. status: 200,
  767. body: {
  768. projects,
  769. totalSize: projects.length,
  770. },
  771. })
  772. window.metaAttributesCache.set(
  773. 'ol-groupsAndEnterpriseBannerVariant',
  774. 'on-premise'
  775. )
  776. })
  777. afterEach(function () {
  778. fetchMock.removeRoutes().clearHistory()
  779. })
  780. it('does not show the banner for users that are in group or are affiliated', async function () {
  781. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', false)
  782. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  783. await fetchMock.callHistory.flush(true)
  784. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  785. })
  786. it('shows the banner for users that have dismissed the previous banners', async function () {
  787. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  788. localStorage.setItem('has_dismissed_groups_and_enterprise_banner', true)
  789. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  790. await fetchMock.callHistory.flush(true)
  791. await screen.findByRole('link', { name: 'Contact sales' })
  792. })
  793. it('shows the banner for users that have dismissed the banner more than 30 days ago', async function () {
  794. const dismissed = new Date()
  795. dismissed.setDate(dismissed.getDate() - 31) // 31 days
  796. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  797. localStorage.setItem(
  798. 'has_dismissed_groups_and_enterprise_banner',
  799. dismissed
  800. )
  801. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  802. await fetchMock.callHistory.flush(true)
  803. await screen.findByRole('link', { name: 'Contact sales' })
  804. })
  805. it('does not show the banner for users that have dismissed the banner within the last 30 days', async function () {
  806. const dismissed = new Date()
  807. dismissed.setDate(dismissed.getDate() - 29) // 29 days
  808. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  809. localStorage.setItem(
  810. 'has_dismissed_groups_and_enterprise_banner',
  811. dismissed
  812. )
  813. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  814. await fetchMock.callHistory.flush(true)
  815. expect(screen.queryByRole('link', { name: 'Contact sales' })).to.be.null
  816. })
  817. describe('users that are not in group and are not affiliated', function () {
  818. beforeEach(function () {
  819. localStorage.clear()
  820. fetchMock.removeRoutes().clearHistory()
  821. // at least one project is required to show some notifications
  822. const projects = [{}] as Project[]
  823. fetchMock.post(/\/api\/project/, {
  824. status: 200,
  825. body: {
  826. projects,
  827. totalSize: projects.length,
  828. },
  829. })
  830. window.metaAttributesCache.set('ol-showGroupsAndEnterpriseBanner', true)
  831. })
  832. afterEach(function () {
  833. fetchMock.removeRoutes().clearHistory()
  834. })
  835. after(function () {
  836. localStorage.clear()
  837. })
  838. it('will show the correct text for the `on-premise` variant', async function () {
  839. window.metaAttributesCache.set(
  840. 'ol-groupsAndEnterpriseBannerVariant',
  841. 'on-premise'
  842. )
  843. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  844. await fetchMock.callHistory.flush(true)
  845. await screen.findByText(
  846. '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.'
  847. )
  848. const link = screen.getByRole('link', { name: 'Contact sales' })
  849. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-2`)
  850. })
  851. it('will show the correct text for the `FOMO` variant', async function () {
  852. window.metaAttributesCache.set(
  853. 'ol-groupsAndEnterpriseBannerVariant',
  854. 'FOMO'
  855. )
  856. renderWithinProjectListProvider(GroupsAndEnterpriseBanner)
  857. await fetchMock.callHistory.flush(true)
  858. await screen.findByText(
  859. 'Why do Fortune 500 companies and top research institutions trust Overleaf to streamline their collaboration? Get in touch to learn more.'
  860. )
  861. const link = screen.getByRole('link', { name: 'Contact sales' })
  862. expect(link.getAttribute('href')).to.equal(`/for/contact-sales-4`)
  863. })
  864. })
  865. })
  866. describe('GroupSsoSetupSuccess', function () {
  867. it('shows group SSO linked notification', function () {
  868. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', true)
  869. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  870. screen.getByText('Success! Single sign-on is all set up for you.')
  871. })
  872. it('does not show group SSO linked notification', function () {
  873. window.metaAttributesCache.set('ol-groupSsoSetupSuccess', false)
  874. renderWithinProjectListProvider(GroupSsoSetupSuccess)
  875. expect(
  876. screen.queryByText('Success! Single sign-on is all set up for you.')
  877. ).to.be.null
  878. })
  879. })
  880. })