emails-section-add-new-email.test.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. import {
  2. render,
  3. screen,
  4. fireEvent,
  5. waitForElementToBeRemoved,
  6. within,
  7. } from '@testing-library/react'
  8. import userEvent from '@testing-library/user-event'
  9. import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
  10. import { expect } from 'chai'
  11. import fetchMock from 'fetch-mock'
  12. import { UserEmailData } from '../../../../../../types/user-email'
  13. import { Affiliation } from '../../../../../../types/affiliation'
  14. import withMarkup from '../../../../helpers/with-markup'
  15. import getMeta from '@/utils/meta'
  16. import { clearDomainCache } from '../../../../../../frontend/js/features/settings/components/emails/add-email/input'
  17. const userEmailData: UserEmailData & { affiliation: Affiliation } = {
  18. affiliation: {
  19. cachedConfirmedAt: null,
  20. cachedEntitlement: null,
  21. cachedLastDayToReconfirm: null,
  22. cachedPastReconfirmDate: false,
  23. cachedReconfirmedAt: null,
  24. department: 'Art History',
  25. institution: {
  26. commonsAccount: false,
  27. writefullCommonsAccount: false,
  28. confirmed: true,
  29. id: 1,
  30. isUniversity: true,
  31. maxConfirmationMonths: null,
  32. name: 'Overleaf',
  33. ssoEnabled: false,
  34. ssoBeta: false,
  35. },
  36. inReconfirmNotificationPeriod: false,
  37. inferred: false,
  38. licence: 'pro_plus',
  39. pastReconfirmDate: false,
  40. portal: { slug: '', templates_count: 1 },
  41. role: 'Reader',
  42. },
  43. email: 'baz@overleaf.com',
  44. default: false,
  45. }
  46. const institutionDomainData = [
  47. {
  48. university: {
  49. id: 1234,
  50. ssoEnabled: true,
  51. name: 'Auto Complete University',
  52. },
  53. hostname: 'autocomplete.edu',
  54. confirmed: true,
  55. },
  56. ] as const
  57. function resetFetchMock() {
  58. fetchMock.removeRoutes().clearHistory()
  59. fetchMock.get('express:/institutions/domains', [])
  60. }
  61. async function confirmCodeForEmail(email: string) {
  62. await screen.findByText(
  63. `Enter the 6-digit confirmation code sent to ${email}.`
  64. )
  65. const inputCode = screen.getByLabelText(/6-digit confirmation code/i)
  66. fireEvent.change(inputCode, { target: { value: '123456' } })
  67. const submitCodeBtn = screen.getByRole<HTMLButtonElement>('button', {
  68. name: 'Confirm',
  69. })
  70. fireEvent.click(submitCodeBtn)
  71. await waitForElementToBeRemoved(() =>
  72. screen.getByRole('button', { name: /confirming/i })
  73. )
  74. }
  75. describe('<EmailsSection />', function () {
  76. beforeEach(function () {
  77. Object.assign(getMeta('ol-ExposedSettings'), {
  78. hasAffiliationsFeature: true,
  79. hasSamlFeature: true,
  80. samlInitPath: 'saml/init',
  81. })
  82. fetchMock.removeRoutes().clearHistory()
  83. })
  84. afterEach(function () {
  85. resetFetchMock()
  86. clearDomainCache()
  87. })
  88. it('renders "add another email" button', async function () {
  89. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  90. render(<EmailsSection />)
  91. await screen.findByRole('button', { name: /add another email/i })
  92. })
  93. it('renders input', async function () {
  94. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  95. render(<EmailsSection />)
  96. await fetchMock.callHistory.flush(true)
  97. const button = await screen.findByRole<HTMLButtonElement>('button', {
  98. name: /add another email/i,
  99. })
  100. fireEvent.click(button)
  101. await screen.findByLabelText(/email/i, { selector: 'input' })
  102. })
  103. it('renders "Start adding your address" until a valid email is typed', async function () {
  104. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  105. fetchMock.get(`/institutions/domains?hostname=email.com&limit=1`, 200)
  106. fetchMock.get(`/institutions/domains?hostname=email&limit=1`, 200)
  107. render(<EmailsSection />)
  108. await fetchMock.callHistory.flush(true)
  109. const button = await screen.findByRole<HTMLButtonElement>('button', {
  110. name: /add another email/i,
  111. })
  112. fireEvent.click(button)
  113. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  114. // initially the text is displayed and the "add email" button disabled
  115. screen.getByText('Start by adding your email address.')
  116. expect(
  117. screen.getByRole<HTMLButtonElement>('button', {
  118. name: /add new email/i,
  119. }).disabled
  120. ).to.be.true
  121. // no changes while writing the email address
  122. fireEvent.change(input, {
  123. target: { value: 'partial@email' },
  124. })
  125. screen.getByText('Start by adding your email address.')
  126. expect(
  127. screen.getByRole<HTMLButtonElement>('button', {
  128. name: /add new email/i,
  129. }).disabled
  130. ).to.be.true
  131. // the text is removed when the complete email address is typed, and the "add button" is reenabled
  132. fireEvent.change(input, {
  133. target: { value: 'valid@email.com' },
  134. })
  135. expect(screen.queryByText('Start by adding your email address.')).to.be.null
  136. expect(
  137. screen.getByRole<HTMLButtonElement>('button', {
  138. name: /add new email/i,
  139. }).disabled
  140. ).to.be.false
  141. })
  142. it('renders "add new email" button', async function () {
  143. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  144. render(<EmailsSection />)
  145. const button = await screen.findByRole<HTMLButtonElement>('button', {
  146. name: /add another email/i,
  147. })
  148. fireEvent.click(button)
  149. screen.getByRole('button', { name: /add new email/i })
  150. })
  151. it('prevent users from adding new emails when the limit is reached', async function () {
  152. const emails = []
  153. for (let i = 0; i < 10; i++) {
  154. emails.push({ email: `bar${i}@overleaf.com` })
  155. }
  156. fetchMock.get('/user/emails?ensureAffiliation=true', emails)
  157. render(<EmailsSection />)
  158. const findByTextWithMarkup = withMarkup(screen.findByText)
  159. await findByTextWithMarkup(
  160. 'You can have a maximum of 10 email addresses on this account. To add another email address, please delete an existing one.'
  161. )
  162. expect(screen.queryByRole('button', { name: /add another email/i })).to.not
  163. .exist
  164. })
  165. it('adds new email address', async function () {
  166. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  167. render(<EmailsSection />)
  168. const addAnotherEmailBtn = await screen.findByRole<HTMLButtonElement>(
  169. 'button',
  170. { name: /add another email/i }
  171. )
  172. await fetchMock.callHistory.flush(true)
  173. resetFetchMock()
  174. fetchMock
  175. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  176. .post('/user/emails/secondary', 200)
  177. .post('/user/emails/confirm-secondary', 200)
  178. fireEvent.click(addAnotherEmailBtn)
  179. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  180. fireEvent.change(input, {
  181. target: { value: userEmailData.email },
  182. })
  183. const submitBtn = screen.getByRole<HTMLButtonElement>('button', {
  184. name: /add new email/i,
  185. })
  186. expect(submitBtn.disabled).to.be.false
  187. fireEvent.click(submitBtn)
  188. expect(submitBtn.disabled).to.be.true
  189. await waitForElementToBeRemoved(() =>
  190. screen.getByRole('button', {
  191. name: /Loading/i,
  192. })
  193. )
  194. await confirmCodeForEmail(userEmailData.email)
  195. await screen.findByText(userEmailData.email)
  196. })
  197. it('fails to add add new email address', async function () {
  198. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  199. render(<EmailsSection />)
  200. const addAnotherEmailBtn = await screen.findByRole<HTMLButtonElement>(
  201. 'button',
  202. { name: /add another email/i }
  203. )
  204. await fetchMock.callHistory.flush(true)
  205. resetFetchMock()
  206. fetchMock
  207. .get('/user/emails?ensureAffiliation=true', [])
  208. .post('/user/emails/secondary', 400)
  209. fireEvent.click(addAnotherEmailBtn)
  210. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  211. fireEvent.change(input, {
  212. target: { value: userEmailData.email },
  213. })
  214. const submitBtn = screen.getByRole<HTMLButtonElement>('button', {
  215. name: /add new email/i,
  216. })
  217. expect(submitBtn.disabled).to.be.false
  218. fireEvent.click(submitBtn)
  219. expect(submitBtn.disabled).to.be.true
  220. await screen.findByText(
  221. /Invalid Request. Please correct the data and try again./i
  222. )
  223. expect(submitBtn).to.not.be.null
  224. expect(submitBtn.disabled).to.be.false
  225. })
  226. it('can link email address to an existing SSO institution', async function () {
  227. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  228. render(<EmailsSection />)
  229. const button = await screen.findByRole<HTMLButtonElement>('button', {
  230. name: /add another email/i,
  231. })
  232. await fetchMock.callHistory.flush(true)
  233. fetchMock.removeRoutes().clearHistory()
  234. fetchMock.get('express:/institutions/domains', institutionDomainData)
  235. await userEvent.click(button)
  236. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  237. fireEvent.change(input, {
  238. target: { value: 'user@autocomplete.edu' },
  239. })
  240. await screen.findByRole('button', { name: 'Link accounts and add email' })
  241. })
  242. it('adds new email address with existing institution and custom departments', async function () {
  243. const country = 'Germany'
  244. const customDepartment = 'Custom department'
  245. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  246. render(<EmailsSection />)
  247. const button = await screen.findByRole<HTMLButtonElement>('button', {
  248. name: /add another email/i,
  249. })
  250. await fetchMock.callHistory.flush(true)
  251. resetFetchMock()
  252. await userEvent.click(button)
  253. await userEvent.type(
  254. screen.getByLabelText(/email/i, { selector: 'input' }),
  255. userEmailData.email
  256. )
  257. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  258. const universityInput = screen.getByRole<HTMLInputElement>('combobox', {
  259. name: /university/i,
  260. })
  261. expect(universityInput.disabled).to.be.true
  262. fetchMock.get('/institutions/list?country_code=de', [
  263. {
  264. id: userEmailData.affiliation.institution.id,
  265. name: userEmailData.affiliation.institution.name,
  266. country_code: 'de',
  267. departments: [customDepartment],
  268. },
  269. ])
  270. // Select the country from dropdown
  271. await userEvent.type(
  272. screen.getByRole('combobox', {
  273. name: /country/i,
  274. }),
  275. country
  276. )
  277. await userEvent.click(screen.getByText(country))
  278. expect(universityInput.disabled).to.be.false
  279. await fetchMock.callHistory.flush(true)
  280. resetFetchMock()
  281. // Select the university from dropdown
  282. await userEvent.click(universityInput)
  283. await userEvent.click(
  284. await screen.findByText(userEmailData.affiliation.institution.name)
  285. )
  286. const roleInput = screen.getByRole('combobox', { name: /role/i })
  287. await userEvent.type(roleInput, userEmailData.affiliation.role!)
  288. const departmentInput = screen.getByRole('combobox', {
  289. name: /department/i,
  290. })
  291. await userEvent.click(departmentInput)
  292. await userEvent.click(screen.getByText(customDepartment))
  293. const userEmailDataCopy = {
  294. ...userEmailData,
  295. affiliation: {
  296. ...userEmailData.affiliation,
  297. department: customDepartment,
  298. },
  299. }
  300. fetchMock
  301. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  302. .post('/user/emails/secondary', 200)
  303. .post('/user/emails/confirm-secondary', 200)
  304. await userEvent.click(
  305. screen.getByRole('button', {
  306. name: /add new email/i,
  307. })
  308. )
  309. const request = fetchMock.callHistory.calls(/\/user\/emails/).at(0)
  310. expect(
  311. JSON.parse(request?.options.body?.toString() || '{}')
  312. ).to.deep.include({
  313. email: userEmailData.email,
  314. university: {
  315. id: userEmailData.affiliation?.institution.id,
  316. },
  317. role: userEmailData.affiliation?.role,
  318. department: customDepartment,
  319. })
  320. await screen.findByText(
  321. `Enter the 6-digit confirmation code sent to ${userEmailData.email}.`
  322. )
  323. await confirmCodeForEmail(userEmailData.email)
  324. await screen.findByText(userEmailData.affiliation.role!, { exact: false })
  325. await screen.findByText(customDepartment, { exact: false })
  326. })
  327. it('autocompletes institution name', async function () {
  328. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  329. render(<EmailsSection />)
  330. const button = await screen.findByRole<HTMLButtonElement>('button', {
  331. name: /add another email/i,
  332. })
  333. await fetchMock.callHistory.flush(true)
  334. resetFetchMock()
  335. fetchMock.get('/institutions/list?country_code=de', [
  336. {
  337. id: 1,
  338. name: 'University of Bonn',
  339. },
  340. {
  341. id: 2,
  342. name: 'Bochum institute of Science',
  343. },
  344. ])
  345. // open "add new email" section and click "let us know" to open the Country/University form
  346. await userEvent.click(button)
  347. await userEvent.type(
  348. screen.getByLabelText(/email/i, { selector: 'input' }),
  349. userEmailData.email
  350. )
  351. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  352. // select a country
  353. const countryInput = screen.getByRole<HTMLInputElement>('combobox', {
  354. name: /country/i,
  355. })
  356. await userEvent.click(countryInput)
  357. await userEvent.type(countryInput, 'Germ')
  358. await userEvent.click(await screen.findByText('Germany'))
  359. // match several universities on initial typing
  360. const universityInput = screen.getByRole<HTMLInputElement>('combobox', {
  361. name: /university/i,
  362. })
  363. await userEvent.click(universityInput)
  364. await userEvent.type(universityInput, 'bo')
  365. await screen.findByText('University of Bonn')
  366. await screen.findByText('Bochum institute of Science')
  367. // match a single university when typing to refine the search
  368. await userEvent.type(universityInput, 'nn')
  369. await screen.findByText('University of Bonn')
  370. expect(screen.queryByText('Bochum institute of Science')).to.be.null
  371. })
  372. it('adds new email address without existing institution', async function () {
  373. const country = 'Germany'
  374. const countryCode = 'de'
  375. const newUniversity = 'Abcdef'
  376. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  377. render(<EmailsSection />)
  378. const button = await screen.findByRole<HTMLButtonElement>('button', {
  379. name: /add another email/i,
  380. })
  381. await fetchMock.callHistory.flush(true)
  382. resetFetchMock()
  383. await userEvent.click(button)
  384. await userEvent.type(
  385. screen.getByLabelText(/email/i, { selector: 'input' }),
  386. userEmailData.email
  387. )
  388. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  389. const universityInput = screen.getByRole<HTMLInputElement>('combobox', {
  390. name: /university/i,
  391. })
  392. expect(universityInput.disabled).to.be.true
  393. fetchMock.get(/\/institutions\/list/, [
  394. {
  395. id: userEmailData.affiliation.institution.id,
  396. name: userEmailData.affiliation.institution.name,
  397. country_code: 'de',
  398. departments: [],
  399. },
  400. ])
  401. // Select the country from dropdown
  402. await userEvent.type(
  403. screen.getByRole('combobox', {
  404. name: /country/i,
  405. }),
  406. country
  407. )
  408. await userEvent.click(screen.getByText(country))
  409. expect(universityInput.disabled).to.be.false
  410. await fetchMock.callHistory.flush(true)
  411. resetFetchMock()
  412. // Enter the university manually
  413. await userEvent.type(universityInput, newUniversity)
  414. const roleInput = screen.getByRole('combobox', { name: /role/i })
  415. await userEvent.type(roleInput, userEmailData.affiliation.role!)
  416. const departmentInput = screen.getByRole('combobox', {
  417. name: /department/i,
  418. })
  419. await userEvent.type(departmentInput, userEmailData.affiliation.department!)
  420. const userEmailDataCopy = {
  421. ...userEmailData,
  422. affiliation: {
  423. ...userEmailData.affiliation,
  424. institution: {
  425. ...userEmailData.affiliation.institution,
  426. name: newUniversity,
  427. },
  428. },
  429. }
  430. fetchMock
  431. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  432. .post('/user/emails/secondary', 200)
  433. .post('/user/emails/confirm-secondary', 200)
  434. await userEvent.click(
  435. screen.getByRole('button', {
  436. name: /add new email/i,
  437. })
  438. )
  439. await confirmCodeForEmail(userEmailData.email)
  440. const request = fetchMock.callHistory.calls(/\/user\/emails/).at(0)
  441. expect(
  442. JSON.parse(request?.options.body?.toString() || '{}')
  443. ).to.deep.include({
  444. email: userEmailData.email,
  445. university: {
  446. name: newUniversity,
  447. country_code: countryCode,
  448. },
  449. role: userEmailData.affiliation?.role,
  450. department: userEmailData.affiliation?.department,
  451. })
  452. await screen.findByText(userEmailData.email)
  453. await screen.findByText(newUniversity)
  454. await screen.findByText(userEmailData.affiliation.role!, { exact: false })
  455. await screen.findByText(userEmailData.affiliation.department!, {
  456. exact: false,
  457. })
  458. })
  459. it('shows country, university, role and department fields based on whether `change` was clicked or not', async function () {
  460. const institutionDomainDataCopy = [
  461. {
  462. ...institutionDomainData[0],
  463. university: {
  464. ...institutionDomainData[0].university,
  465. ssoEnabled: false,
  466. },
  467. },
  468. ]
  469. const hostnameFirstChar = institutionDomainDataCopy[0].hostname.charAt(0)
  470. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  471. render(<EmailsSection />)
  472. const button = await screen.findByRole<HTMLButtonElement>('button', {
  473. name: /add another email/i,
  474. })
  475. await fetchMock.callHistory.flush(true)
  476. fetchMock.removeRoutes().clearHistory()
  477. fetchMock.get(
  478. `/institutions/domains?hostname=${hostnameFirstChar}&limit=1`,
  479. institutionDomainDataCopy
  480. )
  481. await userEvent.click(button)
  482. await userEvent.type(
  483. screen.getByLabelText(/email/i, { selector: 'input' }),
  484. `user@${hostnameFirstChar}`
  485. )
  486. await userEvent.keyboard('{Tab}')
  487. await fetchMock.callHistory.flush(true)
  488. fetchMock.removeRoutes().clearHistory()
  489. expect(
  490. screen.queryByRole('combobox', {
  491. name: /country/i,
  492. })
  493. ).to.be.null
  494. expect(
  495. screen.queryByRole('combobox', {
  496. name: /university/i,
  497. })
  498. ).to.be.null
  499. screen.getByRole('combobox', {
  500. name: /role/i,
  501. })
  502. screen.getByRole('combobox', {
  503. name: /department/i,
  504. })
  505. await userEvent.click(screen.getByRole('button', { name: /change/i }))
  506. screen.getByRole('combobox', {
  507. name: /country/i,
  508. })
  509. screen.getByRole('combobox', {
  510. name: /university/i,
  511. })
  512. expect(
  513. screen.queryByRole('combobox', {
  514. name: /role/i,
  515. })
  516. ).to.be.null
  517. expect(
  518. screen.queryByRole('combobox', {
  519. name: /department/i,
  520. })
  521. ).to.be.null
  522. })
  523. it('displays institution name with change button when autocompleted and adds new record', async function () {
  524. const institutionDomainDataCopy = [
  525. {
  526. ...institutionDomainData[0],
  527. university: {
  528. ...institutionDomainData[0].university,
  529. ssoEnabled: false,
  530. },
  531. },
  532. ]
  533. const hostnameFirstChar = institutionDomainDataCopy[0].hostname.charAt(0)
  534. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  535. render(<EmailsSection />)
  536. const button = await screen.findByRole<HTMLButtonElement>('button', {
  537. name: /add another email/i,
  538. })
  539. await fetchMock.callHistory.flush(true)
  540. fetchMock.removeRoutes().clearHistory()
  541. fetchMock.get(
  542. `/institutions/domains?hostname=${hostnameFirstChar}&limit=1`,
  543. institutionDomainDataCopy
  544. )
  545. await userEvent.click(button)
  546. await userEvent.type(
  547. screen.getByLabelText(/email/i, { selector: 'input' }),
  548. `user@${hostnameFirstChar}`
  549. )
  550. await userEvent.keyboard('{Tab}')
  551. await fetchMock.callHistory.flush(true)
  552. fetchMock.removeRoutes().clearHistory()
  553. screen.getByText(institutionDomainDataCopy[0].university.name)
  554. const userEmailDataCopy = {
  555. ...userEmailData,
  556. affiliation: {
  557. ...userEmailData.affiliation,
  558. institution: {
  559. ...userEmailData.affiliation.institution,
  560. name: institutionDomainDataCopy[0].university.name,
  561. },
  562. },
  563. }
  564. fetchMock
  565. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  566. .post('/user/emails/secondary', 200)
  567. .post('/user/emails/confirm-secondary', 200)
  568. await userEvent.type(
  569. screen.getByRole('combobox', { name: /role/i }),
  570. userEmailData.affiliation.role!
  571. )
  572. await userEvent.type(
  573. screen.getByRole('combobox', { name: /department/i }),
  574. userEmailData.affiliation.department!
  575. )
  576. await userEvent.click(
  577. screen.getByRole('button', {
  578. name: /add new email/i,
  579. })
  580. )
  581. await confirmCodeForEmail('user@autocomplete.edu')
  582. await fetchMock.callHistory.flush(true)
  583. fetchMock.removeRoutes().clearHistory()
  584. screen.getByText(userEmailDataCopy.affiliation.institution.name, {
  585. exact: false,
  586. })
  587. screen.getByText(userEmailDataCopy.affiliation.role!, { exact: false })
  588. screen.getByText(userEmailDataCopy.affiliation.department!, {
  589. exact: false,
  590. })
  591. })
  592. describe('when domain is captured by a group', function () {
  593. describe('and managed users is not enabled', function () {
  594. beforeEach(async function () {
  595. await fetchMock.callHistory.flush(true)
  596. fetchMock.removeRoutes().clearHistory()
  597. const institution = {
  598. university: {
  599. id: 1234,
  600. ssoEnabled: false,
  601. name: 'Auto Complete University',
  602. },
  603. hostname: 'autocomplete.edu',
  604. confirmed: true,
  605. group: {
  606. domainCaptureEnabled: true,
  607. ssoConfig: {
  608. enabled: true,
  609. },
  610. },
  611. }
  612. fetchMock.get('express:/institutions/domains', [institution])
  613. })
  614. it('can add email address via SSO', async function () {
  615. // note: this UI is a WIP
  616. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  617. render(<EmailsSection />)
  618. const button = await screen.findByRole<HTMLButtonElement>('button', {
  619. name: /add another email/i,
  620. })
  621. await userEvent.click(button)
  622. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  623. fireEvent.change(input, {
  624. target: { value: 'user@autocomplete.edu' },
  625. })
  626. await screen.findByText('This feature is currently unavailable.')
  627. })
  628. })
  629. describe('and managed users is enabled', function () {
  630. beforeEach(async function () {
  631. await fetchMock.callHistory.flush(true)
  632. fetchMock.removeRoutes().clearHistory()
  633. const institution = {
  634. university: {
  635. id: 1234,
  636. ssoEnabled: false,
  637. name: 'Auto Complete University',
  638. },
  639. hostname: 'autocomplete.edu',
  640. confirmed: true,
  641. group: {
  642. domainCaptureEnabled: true,
  643. managedUsersEnabled: true,
  644. ssoConfig: {
  645. enabled: true,
  646. },
  647. },
  648. }
  649. fetchMock.get('express:/institutions/domains', [institution])
  650. })
  651. it('renders error', async function () {
  652. // note: this UI is a WIP
  653. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  654. render(<EmailsSection />)
  655. const button = await screen.findByRole<HTMLButtonElement>('button', {
  656. name: /add another email/i,
  657. })
  658. await userEvent.click(button)
  659. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  660. fireEvent.change(input, {
  661. target: { value: 'user@autocomplete.edu' },
  662. })
  663. const notification = await screen.findByRole('alert')
  664. within(notification).getByText(
  665. 'Your company email address has been registered under a verified domain, and cannot be added as a secondary email.',
  666. { exact: false }
  667. )
  668. })
  669. })
  670. describe('if Commons SSO then enabled, that takes priority over group UI', function () {
  671. // we shouldn't have SSO config in v1 and in v2 but adding test to ensure Commons takes priority
  672. beforeEach(async function () {
  673. await fetchMock.callHistory.flush(true)
  674. fetchMock.removeRoutes().clearHistory()
  675. const institution = {
  676. university: {
  677. id: 1234,
  678. ssoEnabled: true,
  679. name: 'Auto Complete University',
  680. },
  681. hostname: 'autocomplete.edu',
  682. confirmed: true,
  683. group: {
  684. domainCaptureEnabled: true,
  685. ssoConfig: {
  686. enabled: true,
  687. },
  688. },
  689. }
  690. fetchMock.get('express:/institutions/domains', [institution])
  691. })
  692. it('renders Commons UI', async function () {
  693. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  694. render(<EmailsSection />)
  695. const button = await screen.findByRole<HTMLButtonElement>('button', {
  696. name: /add another email/i,
  697. })
  698. await userEvent.click(button)
  699. const input = screen.getByLabelText(/email/i, { selector: 'input' })
  700. fireEvent.change(input, {
  701. target: { value: 'user@autocomplete.edu' },
  702. })
  703. await screen.findByRole('button', {
  704. name: 'Link accounts and add email',
  705. })
  706. })
  707. })
  708. })
  709. })