notifications.test.tsx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. await waitForElementToBeRemoved(() => screen.getByText('Loading'))
  122. expect(acceptMock.called()).to.be.true
  123. screen.getByText(/joined/i)
  124. expect(screen.queryByRole('button', { name: /join project/i })).to.be.null
  125. const openProject = screen.getByRole('button', { name: /open project/i })
  126. expect(openProject.getAttribute('href')).to.equal(
  127. `/project/${notificationProjectInvite.messageOpts.projectId}`
  128. )
  129. const closeBtn = screen.getByRole('button', { name: /close/i })
  130. fireEvent.click(closeBtn)
  131. expect(deleteMock.called()).to.be.true
  132. expect(screen.queryByRole('alert')).to.be.null
  133. })
  134. it('fails to accept project invite', async function () {
  135. const reconfiguredNotification: DeepPartial<Notification> = {
  136. _id: 1,
  137. templateKey: 'notification_project_invite',
  138. }
  139. window.metaAttributesCache.set('ol-notifications', [
  140. merge(cloneDeep(notificationProjectInvite), reconfiguredNotification),
  141. ])
  142. renderWithinProjectListProvider(Common)
  143. await fetchMock.flush(true)
  144. fetchMock.post(
  145. `project/${notificationProjectInvite.messageOpts.projectId}/invite/token/${notificationProjectInvite.messageOpts.token}/accept`,
  146. 500
  147. )
  148. screen.getByRole('alert')
  149. screen.getByText(/would like you to join/i)
  150. const joinBtn = screen.getByRole('button', {
  151. name: /join project/i,
  152. }) as HTMLButtonElement
  153. fireEvent.click(joinBtn)
  154. await waitForElementToBeRemoved(() =>
  155. screen.getByRole('button', { name: /loading/i })
  156. )
  157. expect(fetchMock.called()).to.be.true
  158. screen.getByRole('button', { name: /join project/i })
  159. expect(screen.queryByRole('button', { name: /open project/i })).to.be.null
  160. })
  161. it('shows WFH2020', async function () {
  162. const reconfiguredNotification: DeepPartial<Notification> = {
  163. _id: 1,
  164. templateKey: 'wfh_2020_upgrade_offer',
  165. }
  166. window.metaAttributesCache.set('ol-notifications', [
  167. merge(reconfiguredNotification),
  168. ])
  169. renderWithinProjectListProvider(Common)
  170. await fetchMock.flush(true)
  171. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  172. screen.getByRole('alert')
  173. screen.getByText(/your free WFH2020 upgrade came to an end on/i)
  174. const viewLink = screen.getByRole('button', { name: /view/i })
  175. expect(viewLink.getAttribute('href')).to.equal(
  176. 'https://www.overleaf.com/events/wfh2020'
  177. )
  178. const closeBtn = screen.getByRole('button', { name: /close/i })
  179. fireEvent.click(closeBtn)
  180. expect(fetchMock.called()).to.be.true
  181. expect(screen.queryByRole('alert')).to.be.null
  182. })
  183. it('shows IP matched affiliation with SSO enabled', async function () {
  184. const reconfiguredNotification: DeepPartial<Notification> = {
  185. _id: 1,
  186. templateKey: 'notification_ip_matched_affiliation',
  187. messageOpts: { ssoEnabled: true },
  188. }
  189. window.metaAttributesCache.set('ol-notifications', [
  190. merge(
  191. cloneDeep(notificationIPMatchedAffiliation),
  192. reconfiguredNotification
  193. ),
  194. ])
  195. renderWithinProjectListProvider(Common)
  196. await fetchMock.flush(true)
  197. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  198. screen.getByRole('alert')
  199. screen.getByText(/it looks like you’re at/i)
  200. screen.getByText(/you can now log in through your institution/i)
  201. screen.getByText(
  202. /link an institutional email address to your account to get started/i
  203. )
  204. const findOutMore = screen.getByRole('link', { name: /find out more/i })
  205. expect(findOutMore.getAttribute('href')).to.equal(
  206. 'https://www.overleaf.com/learn/how-to/Institutional_Login'
  207. )
  208. const linkAccount = screen.getByRole('button', { name: /link account/i })
  209. expect(linkAccount.getAttribute('href')).to.equal(
  210. `${exposedSettings.samlInitPath}?university_id=${notificationIPMatchedAffiliation.messageOpts.institutionId}&auto=/project`
  211. )
  212. const closeBtn = screen.getByRole('button', { name: /close/i })
  213. fireEvent.click(closeBtn)
  214. expect(fetchMock.called()).to.be.true
  215. expect(screen.queryByRole('alert')).to.be.null
  216. })
  217. it('shows IP matched affiliation with SSO disabled', async function () {
  218. const reconfiguredNotification: DeepPartial<Notification> = {
  219. _id: 1,
  220. templateKey: 'notification_ip_matched_affiliation',
  221. messageOpts: { ssoEnabled: false },
  222. }
  223. window.metaAttributesCache.set('ol-notifications', [
  224. merge(
  225. cloneDeep(notificationIPMatchedAffiliation),
  226. reconfiguredNotification
  227. ),
  228. ])
  229. renderWithinProjectListProvider(Common)
  230. await fetchMock.flush(true)
  231. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  232. screen.getByRole('alert')
  233. screen.getByText(/it looks like you’re at/i)
  234. screen.getByText(/did you know that/i)
  235. screen.getByText(
  236. /add an institutional email address to claim your features/i
  237. )
  238. const addAffiliation = screen.getByRole('button', {
  239. name: /add affiliation/i,
  240. })
  241. expect(addAffiliation.getAttribute('href')).to.equal(`/user/settings`)
  242. const closeBtn = screen.getByRole('button', { name: /close/i })
  243. fireEvent.click(closeBtn)
  244. expect(fetchMock.called()).to.be.true
  245. expect(screen.queryByRole('alert')).to.be.null
  246. })
  247. it('shows tpds file limit', async function () {
  248. const reconfiguredNotification: DeepPartial<Notification> = {
  249. templateKey: 'notification_tpds_file_limit',
  250. }
  251. window.metaAttributesCache.set('ol-notifications', [
  252. merge(cloneDeep(notificationTPDSFileLimit), reconfiguredNotification),
  253. ])
  254. renderWithinProjectListProvider(Common)
  255. await fetchMock.flush(true)
  256. screen.getByRole('alert')
  257. screen.getByText(/file limit/i)
  258. screen.getByText(/You can't add more files to the project or sync it/i)
  259. const accountSettings = screen.getByRole('button', {
  260. name: /Open project/i,
  261. })
  262. expect(accountSettings.getAttribute('href')).to.equal('/project/123')
  263. const closeBtn = screen.getByRole('button', { name: /close/i })
  264. fireEvent.click(closeBtn)
  265. expect(screen.queryByRole('alert')).to.be.null
  266. })
  267. it('shows dropbox duplicate project names warning', async function () {
  268. const reconfiguredNotification: DeepPartial<Notification> = {
  269. _id: 1,
  270. templateKey: 'notification_dropbox_duplicate_project_names',
  271. }
  272. window.metaAttributesCache.set('ol-notifications', [
  273. merge(
  274. cloneDeep(notificationDropboxDuplicateProjectNames),
  275. reconfiguredNotification
  276. ),
  277. ])
  278. renderWithinProjectListProvider(Common)
  279. await fetchMock.flush(true)
  280. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  281. screen.getByRole('alert')
  282. screen.getByText(/you have more than one project called/i)
  283. screen.getByText(/make your project names unique/i)
  284. const learnMore = screen.getByRole('link', { name: /learn more/i })
  285. expect(learnMore.getAttribute('href')).to.equal(
  286. '/learn/how-to/Dropbox_Synchronization#Troubleshooting'
  287. )
  288. const closeBtn = screen.getByRole('button', { name: /close/i })
  289. fireEvent.click(closeBtn)
  290. expect(fetchMock.called()).to.be.true
  291. expect(screen.queryByRole('alert')).to.be.null
  292. })
  293. it('shows dropbox unlinked tue to lapsed reconfirmation', async function () {
  294. const reconfiguredNotification: DeepPartial<Notification> = {
  295. _id: 1,
  296. templateKey:
  297. 'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
  298. }
  299. window.metaAttributesCache.set('ol-notifications', [
  300. merge(
  301. cloneDeep(notificationDropboxDuplicateProjectNames),
  302. reconfiguredNotification
  303. ),
  304. ])
  305. renderWithinProjectListProvider(Common)
  306. await fetchMock.flush(true)
  307. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  308. screen.getByRole('alert')
  309. screen.getByText(/your Dropbox account has been unlinked/i)
  310. screen.getByText(
  311. /confirm you are still at the institution and on their license, or upgrade your account in order to relink your dropbox account/i
  312. )
  313. const learnMore = screen.getByRole('link', { name: /learn more/i })
  314. expect(learnMore.getAttribute('href')).to.equal(
  315. '/learn/how-to/Institutional_Email_Reconfirmation'
  316. )
  317. const closeBtn = screen.getByRole('button', { name: /close/i })
  318. fireEvent.click(closeBtn)
  319. expect(fetchMock.called()).to.be.true
  320. expect(screen.queryByRole('alert')).to.be.null
  321. })
  322. it('shows non specific notification', async function () {
  323. const reconfiguredNotification: DeepPartial<Notification> = {
  324. _id: 1,
  325. html: 'unspecific message',
  326. }
  327. window.metaAttributesCache.set('ol-notifications', [
  328. reconfiguredNotification,
  329. ])
  330. renderWithinProjectListProvider(Common)
  331. await fetchMock.flush(true)
  332. fetchMock.delete(`/notifications/${reconfiguredNotification._id}`, 200)
  333. screen.getByRole('alert')
  334. screen.getByText(reconfiguredNotification.html!)
  335. const closeBtn = screen.getByRole('button', { name: /close/i })
  336. fireEvent.click(closeBtn)
  337. expect(fetchMock.called()).to.be.true
  338. expect(screen.queryByRole('alert')).to.be.null
  339. })
  340. describe('<GroupInvitation />', function () {
  341. describe('without existing personal subscription', function () {
  342. it('shows group invitation notification for user without personal subscription', async function () {
  343. const notificationGroupInvite: DeepPartial<Notification> = {
  344. _id: 1,
  345. templateKey: 'notification_group_invitation',
  346. }
  347. window.metaAttributesCache.set('ol-notifications', [
  348. merge(
  349. cloneDeep(notificationGroupInviteDefault),
  350. notificationGroupInvite
  351. ),
  352. ])
  353. renderWithinProjectListProvider(Common)
  354. await fetchMock.flush(true)
  355. fetchMock.delete(`/notifications/${notificationGroupInvite._id}`, 200)
  356. screen.getByRole('alert')
  357. screen.getByText('inviter@overleaf.com')
  358. screen.getByText(
  359. /has invited you to join a group subscription on Overleaf/
  360. )
  361. screen.getByRole('button', { name: 'Join now' })
  362. screen.getByRole('button', { name: /close/i })
  363. })
  364. describe('with existing personal subscription', function () {
  365. it('shows group invitation notification for user with personal subscription', async function () {
  366. const notificationGroupInvite: DeepPartial<Notification> = {
  367. _id: 1,
  368. templateKey: 'notification_group_invitation',
  369. }
  370. window.metaAttributesCache.set('ol-notifications', [
  371. merge(
  372. cloneDeep(notificationGroupInviteDefault),
  373. notificationGroupInvite
  374. ),
  375. ])
  376. window.metaAttributesCache.set(
  377. 'ol-hasIndividualRecurlySubscription',
  378. true
  379. )
  380. renderWithinProjectListProvider(Common)
  381. await fetchMock.flush(true)
  382. fetchMock.delete(
  383. `/notifications/${notificationGroupInvite._id}`,
  384. 200
  385. )
  386. screen.getByRole('alert')
  387. screen.getByText(
  388. /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/
  389. )
  390. screen.getByRole('button', { name: 'Not now' })
  391. screen.getByRole('button', { name: 'Cancel my subscription' })
  392. })
  393. })
  394. })
  395. })
  396. })
  397. describe('<Institution>', function () {
  398. beforeEach(function () {
  399. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  400. fetchMock.reset()
  401. })
  402. afterEach(function () {
  403. fetchMock.reset()
  404. })
  405. it('shows sso available', function () {
  406. const institution: DeepPartial<InstitutionType> = {
  407. templateKey: 'notification_institution_sso_available',
  408. }
  409. window.metaAttributesCache.set('ol-notificationsInstitution', [
  410. { ...notificationsInstitution, ...institution },
  411. ])
  412. render(<Institution />)
  413. screen.getByRole('alert')
  414. screen.getByText(/you can now link/i)
  415. screen.getByText(/doing this will allow you to log in/i)
  416. const learnMore = screen.getByRole('link', { name: /learn more/i })
  417. expect(learnMore.getAttribute('href')).to.equal(
  418. '/learn/how-to/Institutional_Login'
  419. )
  420. const action = screen.getByRole('button', { name: /link account/i })
  421. expect(action.getAttribute('href')).to.equal(
  422. `${exposedSettings.samlInitPath}?university_id=${notificationsInstitution.institutionId}&auto=/project&email=${notificationsInstitution.email}`
  423. )
  424. })
  425. it('shows sso linked', function () {
  426. const institution: DeepPartial<InstitutionType> = {
  427. _id: 1,
  428. templateKey: 'notification_institution_sso_linked',
  429. }
  430. window.metaAttributesCache.set('ol-notificationsInstitution', [
  431. { ...notificationsInstitution, ...institution },
  432. ])
  433. render(<Institution />)
  434. fetchMock.delete(`/notifications/${institution._id}`, 200)
  435. screen.getByRole('alert')
  436. screen.getByText(/has been linked to your/i)
  437. const closeBtn = screen.getByRole('button', { name: /close/i })
  438. fireEvent.click(closeBtn)
  439. expect(fetchMock.called()).to.be.true
  440. expect(screen.queryByRole('alert')).to.be.null
  441. })
  442. it('shows sso non canonical', function () {
  443. const institution: DeepPartial<InstitutionType> = {
  444. _id: 1,
  445. templateKey: 'notification_institution_sso_non_canonical',
  446. }
  447. window.metaAttributesCache.set('ol-notificationsInstitution', [
  448. { ...notificationsInstitution, ...institution },
  449. ])
  450. render(<Institution />)
  451. fetchMock.delete(`/notifications/${institution._id}`, 200)
  452. screen.getByRole('alert')
  453. screen.getByText(/you’ve tried to log in with/i)
  454. screen.getByText(
  455. /in order to match your institutional metadata, your account is associated with/i
  456. )
  457. const closeBtn = screen.getByRole('button', { name: /close/i })
  458. fireEvent.click(closeBtn)
  459. expect(fetchMock.called()).to.be.true
  460. expect(screen.queryByRole('alert')).to.be.null
  461. })
  462. it('shows sso already registered', function () {
  463. const institution: DeepPartial<InstitutionType> = {
  464. _id: 1,
  465. templateKey: 'notification_institution_sso_already_registered',
  466. }
  467. window.metaAttributesCache.set('ol-notificationsInstitution', [
  468. { ...notificationsInstitution, ...institution },
  469. ])
  470. render(<Institution />)
  471. fetchMock.delete(`/notifications/${institution._id}`, 200)
  472. screen.getByRole('alert')
  473. screen.getByText(/which is already registered with/i)
  474. const action = screen.getByRole('button', { name: /find out more/i })
  475. expect(action.getAttribute('href')).to.equal(
  476. '/learn/how-to/Institutional_Login'
  477. )
  478. const closeBtn = screen.getByRole('button', { name: /close/i })
  479. fireEvent.click(closeBtn)
  480. expect(fetchMock.called()).to.be.true
  481. expect(screen.queryByRole('alert')).to.be.null
  482. })
  483. it('shows sso error', function () {
  484. const institution: DeepPartial<InstitutionType> = {
  485. templateKey: 'notification_institution_sso_error',
  486. error: {
  487. message: 'fake message',
  488. tryAgain: true,
  489. },
  490. }
  491. window.metaAttributesCache.set('ol-notificationsInstitution', [
  492. { ...notificationsInstitution, ...institution },
  493. ])
  494. render(<Institution />)
  495. screen.getByRole('alert')
  496. screen.getByText(/something went wrong/i)
  497. screen.getByText(institution.error!.message!)
  498. screen.getByText(/please try again/i)
  499. const closeBtn = screen.getByRole('button', { name: /close/i })
  500. fireEvent.click(closeBtn)
  501. expect(screen.queryByRole('alert')).to.be.null
  502. })
  503. })
  504. describe('<ConfirmEmail/>', function () {
  505. beforeEach(async function () {
  506. Object.assign(getMeta('ol-ExposedSettings'), {
  507. emailConfirmationDisabled: false,
  508. })
  509. window.metaAttributesCache.set('ol-userEmails', [unconfirmedUserData])
  510. window.metaAttributesCache.set(
  511. 'ol-usersBestSubscription',
  512. freeSubscription
  513. )
  514. })
  515. afterEach(function () {
  516. fetchMock.reset()
  517. })
  518. it('sends successfully', async function () {
  519. renderWithinProjectListProvider(ConfirmEmail)
  520. await fetchMock.flush(true)
  521. fetchMock.post('/user/emails/resend_confirmation', 200)
  522. const email = unconfirmedUserData.email
  523. const notificationBody = screen.getByTestId('pro-notification-body')
  524. expect(notificationBody.textContent).to.contain(
  525. `Please confirm your email ${email} by clicking on the link in the confirmation email`
  526. )
  527. const resendButton = screen.getByRole('button', { name: /resend/i })
  528. fireEvent.click(resendButton)
  529. expect(screen.queryByRole('button', { name: /resend/i })).to.be.null
  530. await waitForElementToBeRemoved(() =>
  531. screen.getByText(/resending confirmation email/i)
  532. )
  533. expect(fetchMock.called()).to.be.true
  534. expect(screen.queryByRole('alert')).to.be.null
  535. })
  536. it('fails to send', async function () {
  537. renderWithinProjectListProvider(ConfirmEmail)
  538. await fetchMock.flush(true)
  539. fetchMock.post('/user/emails/resend_confirmation', 500)
  540. const resendButton = screen.getByRole('button', { name: /resend/i })
  541. fireEvent.click(resendButton)
  542. await waitForElementToBeRemoved(() =>
  543. screen.getByText(/resending confirmation email/i)
  544. )
  545. expect(fetchMock.called()).to.be.true
  546. screen.getByText(/something went wrong/i)
  547. })
  548. for (const subscription of [freeSubscription, individualSubscription]) {
  549. it(`shows commons notification for commons account when user is on ${subscription.type} plan`, async function () {
  550. window.metaAttributesCache.set('ol-userEmails', [
  551. unconfirmedCommonsUserData,
  552. ])
  553. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  554. renderWithinProjectListProvider(ConfirmEmail)
  555. await fetchMock.flush(true)
  556. const alert = screen.getByRole('alert')
  557. const email = unconfirmedCommonsUserData.email
  558. const notificationBody = within(alert).getByTestId('notification-body')
  559. expect(notificationBody.textContent).to.contain(
  560. 'You are one step away from accessing Overleaf Professional features'
  561. )
  562. expect(notificationBody.textContent).to.contain(
  563. `Overleaf has an Overleaf subscription. Click the confirmation link sent to ${email} to upgrade to Overleaf Professional`
  564. )
  565. })
  566. }
  567. for (const subscription of [groupSubscription, commonsSubscription]) {
  568. it(`shows default notification for commons account when user is on ${subscription.type} plan`, async function () {
  569. window.metaAttributesCache.set('ol-userEmails', [
  570. unconfirmedCommonsUserData,
  571. ])
  572. window.metaAttributesCache.set('ol-usersBestSubscription', subscription)
  573. renderWithinProjectListProvider(ConfirmEmail)
  574. await fetchMock.flush(true)
  575. const alert = screen.getByRole('alert')
  576. const email = unconfirmedCommonsUserData.email
  577. const notificationBody = within(alert).getByTestId(
  578. 'pro-notification-body'
  579. )
  580. expect(notificationBody.textContent).to.contain(
  581. `Please confirm your email ${email} by clicking on the link in the confirmation email`
  582. )
  583. })
  584. }
  585. })
  586. describe('<Affiliation/>', function () {
  587. let assignStub: sinon.SinonStub
  588. beforeEach(function () {
  589. Object.assign(getMeta('ol-ExposedSettings'), exposedSettings)
  590. assignStub = sinon.stub()
  591. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  592. assign: assignStub,
  593. replace: sinon.stub(),
  594. reload: sinon.stub(),
  595. setHash: 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. })