notifications.test.tsx 37 KB

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