notifications.test.tsx 32 KB

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