notifications.test.tsx 33 KB

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