notifications.test.tsx 35 KB

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