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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. import {
  2. render,
  3. screen,
  4. fireEvent,
  5. waitForElementToBeRemoved,
  6. } from '@testing-library/react'
  7. import userEvent from '@testing-library/user-event'
  8. import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
  9. import { expect } from 'chai'
  10. import fetchMock from 'fetch-mock'
  11. import { UserEmailData } from '../../../../../../types/user-email'
  12. import { Affiliation } from '../../../../../../types/affiliation'
  13. const userEmailData: UserEmailData & { affiliation: Affiliation } = {
  14. affiliation: {
  15. cachedConfirmedAt: null,
  16. cachedEntitlement: null,
  17. cachedLastDayToReconfirm: null,
  18. cachedPastReconfirmDate: false,
  19. cachedReconfirmedAt: null,
  20. department: 'Art History',
  21. institution: {
  22. commonsAccount: false,
  23. confirmed: true,
  24. id: 1,
  25. isUniversity: true,
  26. maxConfirmationMonths: null,
  27. name: 'Overleaf',
  28. ssoEnabled: false,
  29. ssoBeta: false,
  30. },
  31. inReconfirmNotificationPeriod: false,
  32. inferred: false,
  33. licence: 'pro_plus',
  34. pastReconfirmDate: false,
  35. portal: { slug: '', templates_count: 1 },
  36. role: 'Reader',
  37. },
  38. email: 'baz@overleaf.com',
  39. default: false,
  40. }
  41. const institutionDomainData = [
  42. {
  43. university: {
  44. id: 1234,
  45. ssoEnabled: true,
  46. name: 'Auto Complete University',
  47. },
  48. hostname: 'autocomplete.edu',
  49. confirmed: true,
  50. },
  51. ] as const
  52. function resetFetchMock() {
  53. fetchMock.reset()
  54. fetchMock.get('express:/institutions/domains', [])
  55. }
  56. describe('<EmailsSection />', function () {
  57. beforeEach(function () {
  58. window.metaAttributesCache.set('ol-ExposedSettings', {
  59. hasAffiliationsFeature: true,
  60. hasSamlFeature: true,
  61. samlInitPath: 'saml/init',
  62. })
  63. fetchMock.reset()
  64. })
  65. afterEach(function () {
  66. window.metaAttributesCache = new Map()
  67. resetFetchMock()
  68. })
  69. it('renders "add another email" button', async function () {
  70. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  71. render(<EmailsSection />)
  72. await fetchMock.flush(true)
  73. screen.getByRole('button', { name: /add another email/i })
  74. })
  75. it('renders input', async function () {
  76. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  77. render(<EmailsSection />)
  78. const addAnotherEmailBtn = (await screen.findByRole('button', {
  79. name: /add another email/i,
  80. })) as HTMLButtonElement
  81. fireEvent.click(addAnotherEmailBtn)
  82. screen.getByLabelText(/email/i)
  83. })
  84. it('renders "Start adding your address" until a valid email is typed', async function () {
  85. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  86. fetchMock.get(`/institutions/domains?hostname=email.com&limit=1`, 200)
  87. fetchMock.get(`/institutions/domains?hostname=email&limit=1`, 200)
  88. render(<EmailsSection />)
  89. const addAnotherEmailBtn = (await screen.findByRole('button', {
  90. name: /add another email/i,
  91. })) as HTMLButtonElement
  92. fireEvent.click(addAnotherEmailBtn)
  93. const input = screen.getByLabelText(/email/i)
  94. // initially the text is displayed and the "add email" button disabled
  95. screen.getByText('Start by adding your email address.')
  96. expect(
  97. (
  98. screen.getByRole('button', {
  99. name: /add new email/i,
  100. }) as HTMLButtonElement
  101. ).disabled
  102. ).to.be.true
  103. // no changes while writing the email address
  104. fireEvent.change(input, {
  105. target: { value: 'partial@email' },
  106. })
  107. screen.getByText('Start by adding your email address.')
  108. expect(
  109. (
  110. screen.getByRole('button', {
  111. name: /add new email/i,
  112. }) as HTMLButtonElement
  113. ).disabled
  114. ).to.be.true
  115. // the text is removed when the complete email address is typed, and the "add button" is reenabled
  116. fireEvent.change(input, {
  117. target: { value: 'valid@email.com' },
  118. })
  119. expect(screen.queryByText('Start by adding your email address.')).to.be.null
  120. expect(
  121. (
  122. screen.getByRole('button', {
  123. name: /add new email/i,
  124. }) as HTMLButtonElement
  125. ).disabled
  126. ).to.be.false
  127. })
  128. it('renders "add new email" button', async function () {
  129. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  130. render(<EmailsSection />)
  131. const addAnotherEmailBtn = (await screen.findByRole('button', {
  132. name: /add another email/i,
  133. })) as HTMLButtonElement
  134. fireEvent.click(addAnotherEmailBtn)
  135. screen.getByRole('button', { name: /add new email/i })
  136. })
  137. it('adds new email address', async function () {
  138. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  139. render(<EmailsSection />)
  140. await fetchMock.flush(true)
  141. resetFetchMock()
  142. fetchMock
  143. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  144. .post('/user/emails', 200)
  145. const addAnotherEmailBtn = await screen.findByRole('button', {
  146. name: /add another email/i,
  147. })
  148. fireEvent.click(addAnotherEmailBtn)
  149. const input = screen.getByLabelText(/email/i)
  150. fireEvent.change(input, {
  151. target: { value: userEmailData.email },
  152. })
  153. const submitBtn = screen.getByRole('button', {
  154. name: /add new email/i,
  155. }) as HTMLButtonElement
  156. expect(submitBtn.disabled).to.be.false
  157. fireEvent.click(submitBtn)
  158. expect(submitBtn.disabled).to.be.true
  159. await waitForElementToBeRemoved(() =>
  160. screen.getByRole('button', {
  161. name: /add new email/i,
  162. })
  163. )
  164. screen.getByText(userEmailData.email)
  165. })
  166. it('fails to add add new email address', async function () {
  167. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  168. render(<EmailsSection />)
  169. await fetchMock.flush(true)
  170. resetFetchMock()
  171. fetchMock
  172. .get('/user/emails?ensureAffiliation=true', [])
  173. .post('/user/emails', 400)
  174. const addAnotherEmailBtn = screen.getByRole('button', {
  175. name: /add another email/i,
  176. })
  177. fireEvent.click(addAnotherEmailBtn)
  178. const input = screen.getByLabelText(/email/i)
  179. fireEvent.change(input, {
  180. target: { value: userEmailData.email },
  181. })
  182. const submitBtn = screen.getByRole('button', {
  183. name: /add new email/i,
  184. }) as HTMLButtonElement
  185. expect(submitBtn.disabled).to.be.false
  186. fireEvent.click(submitBtn)
  187. expect(submitBtn.disabled).to.be.true
  188. await screen.findByText(
  189. /Invalid Request. Please correct the data and try again./i
  190. )
  191. expect(submitBtn).to.not.be.null
  192. expect(submitBtn.disabled).to.be.false
  193. })
  194. it('can link email address to an existing SSO institution', async function () {
  195. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  196. render(<EmailsSection />)
  197. await fetchMock.flush(true)
  198. fetchMock.reset()
  199. fetchMock.get('express:/institutions/domains', institutionDomainData)
  200. await userEvent.click(
  201. screen.getByRole('button', {
  202. name: /add another email/i,
  203. })
  204. )
  205. const input = screen.getByLabelText(/email/i)
  206. fireEvent.change(input, {
  207. target: { value: 'user@autocomplete.edu' },
  208. })
  209. await screen.findByRole('button', { name: 'Link Accounts and Add Email' })
  210. })
  211. it('adds new email address with existing institution and custom departments', async function () {
  212. const country = 'Germany'
  213. const customDepartment = 'Custom department'
  214. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  215. render(<EmailsSection />)
  216. await fetchMock.flush(true)
  217. resetFetchMock()
  218. await userEvent.click(
  219. screen.getByRole('button', {
  220. name: /add another email/i,
  221. })
  222. )
  223. await userEvent.type(screen.getByLabelText(/email/i), userEmailData.email)
  224. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  225. const universityInput = screen.getByRole('textbox', {
  226. name: /university/i,
  227. }) as HTMLInputElement
  228. expect(universityInput.disabled).to.be.true
  229. fetchMock.get(/\/institutions\/list/, [
  230. {
  231. id: userEmailData.affiliation.institution.id,
  232. name: userEmailData.affiliation.institution.name,
  233. country_code: 'de',
  234. departments: [customDepartment],
  235. },
  236. ])
  237. // Select the country from dropdown
  238. await userEvent.type(
  239. screen.getByRole('textbox', {
  240. name: /country/i,
  241. }),
  242. country
  243. )
  244. await userEvent.click(screen.getByText(country))
  245. expect(universityInput.disabled).to.be.false
  246. await fetchMock.flush(true)
  247. resetFetchMock()
  248. // Select the university from dropdown
  249. await userEvent.click(universityInput)
  250. await userEvent.click(
  251. screen.getByText(userEmailData.affiliation.institution.name)
  252. )
  253. const roleInput = screen.getByRole('textbox', { name: /role/i })
  254. await userEvent.type(roleInput, userEmailData.affiliation.role!)
  255. const departmentInput = screen.getByRole('textbox', { name: /department/i })
  256. await userEvent.click(departmentInput)
  257. await userEvent.click(screen.getByText(customDepartment))
  258. const userEmailDataCopy = {
  259. ...userEmailData,
  260. affiliation: {
  261. ...userEmailData.affiliation,
  262. department: customDepartment,
  263. },
  264. }
  265. fetchMock
  266. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  267. .post(/\/user\/emails/, 200)
  268. await userEvent.click(
  269. screen.getByRole('button', {
  270. name: /add new email/i,
  271. })
  272. )
  273. const [[, request]] = fetchMock.calls(/\/user\/emails/)
  274. expect(JSON.parse(request?.body?.toString() || '{}')).to.deep.equal({
  275. email: userEmailData.email,
  276. university: {
  277. id: userEmailData.affiliation?.institution.id,
  278. },
  279. role: userEmailData.affiliation?.role,
  280. department: customDepartment,
  281. })
  282. screen.getByText(userEmailData.email)
  283. screen.getByText(userEmailData.affiliation.institution.name)
  284. screen.getByText(userEmailData.affiliation.role!, { exact: false })
  285. screen.getByText(customDepartment, { exact: false })
  286. })
  287. it('autocompletes institution name', async function () {
  288. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  289. render(<EmailsSection />)
  290. await fetchMock.flush(true)
  291. resetFetchMock()
  292. fetchMock.get(/\/institutions\/list/, [
  293. {
  294. id: 1,
  295. name: 'University of Bonn',
  296. },
  297. {
  298. id: 2,
  299. name: 'Bochum institute of Science',
  300. },
  301. ])
  302. // open "add new email" section and click "let us know" to open the Country/University form
  303. await userEvent.click(
  304. screen.getByRole('button', {
  305. name: /add another email/i,
  306. })
  307. )
  308. await userEvent.type(screen.getByLabelText(/email/i), userEmailData.email)
  309. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  310. // select a country
  311. const countryInput = screen.getByRole('textbox', {
  312. name: /country/i,
  313. }) as HTMLInputElement
  314. await userEvent.click(countryInput)
  315. await userEvent.type(countryInput, 'Germ')
  316. await userEvent.click(await screen.findByText('Germany'))
  317. // match several universities on initial typing
  318. const universityInput = screen.getByRole('textbox', {
  319. name: /university/i,
  320. }) as HTMLInputElement
  321. await userEvent.click(universityInput)
  322. await userEvent.type(universityInput, 'bo')
  323. screen.getByText('University of Bonn')
  324. screen.getByText('Bochum institute of Science')
  325. // match a single university when typing to refine the search
  326. await userEvent.type(universityInput, 'nn')
  327. screen.getByText('University of Bonn')
  328. expect(screen.queryByText('Bochum institute of Science')).to.be.null
  329. })
  330. it('adds new email address without existing institution', async function () {
  331. const country = 'Germany'
  332. const countryCode = 'de'
  333. const newUniversity = 'Abcdef'
  334. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  335. render(<EmailsSection />)
  336. await fetchMock.flush(true)
  337. resetFetchMock()
  338. await userEvent.click(
  339. screen.getByRole('button', {
  340. name: /add another email/i,
  341. })
  342. )
  343. await userEvent.type(screen.getByLabelText(/email/i), userEmailData.email)
  344. await userEvent.click(screen.getByRole('button', { name: /let us know/i }))
  345. const universityInput = screen.getByRole('textbox', {
  346. name: /university/i,
  347. }) as HTMLInputElement
  348. expect(universityInput.disabled).to.be.true
  349. fetchMock.get(/\/institutions\/list/, [
  350. {
  351. id: userEmailData.affiliation.institution.id,
  352. name: userEmailData.affiliation.institution.name,
  353. country_code: 'de',
  354. departments: [],
  355. },
  356. ])
  357. // Select the country from dropdown
  358. await userEvent.type(
  359. screen.getByRole('textbox', {
  360. name: /country/i,
  361. }),
  362. country
  363. )
  364. await userEvent.click(screen.getByText(country))
  365. expect(universityInput.disabled).to.be.false
  366. await fetchMock.flush(true)
  367. resetFetchMock()
  368. // Enter the university manually
  369. await userEvent.type(universityInput, newUniversity)
  370. const roleInput = screen.getByRole('textbox', { name: /role/i })
  371. await userEvent.type(roleInput, userEmailData.affiliation.role!)
  372. const departmentInput = screen.getByRole('textbox', { name: /department/i })
  373. await userEvent.type(departmentInput, userEmailData.affiliation.department!)
  374. const userEmailDataCopy = {
  375. ...userEmailData,
  376. affiliation: {
  377. ...userEmailData.affiliation,
  378. institution: {
  379. ...userEmailData.affiliation.institution,
  380. name: newUniversity,
  381. },
  382. },
  383. }
  384. fetchMock
  385. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  386. .post(/\/user\/emails/, 200)
  387. await userEvent.click(
  388. screen.getByRole('button', {
  389. name: /add new email/i,
  390. })
  391. )
  392. const [[, request]] = fetchMock.calls(/\/user\/emails/)
  393. expect(JSON.parse(request?.body?.toString() || '{}')).to.deep.equal({
  394. email: userEmailData.email,
  395. university: {
  396. name: newUniversity,
  397. country_code: countryCode,
  398. },
  399. role: userEmailData.affiliation?.role,
  400. department: userEmailData.affiliation?.department,
  401. })
  402. screen.getByText(userEmailData.email)
  403. screen.getByText(newUniversity)
  404. screen.getByText(userEmailData.affiliation.role!, { exact: false })
  405. screen.getByText(userEmailData.affiliation.department!, { exact: false })
  406. })
  407. it('shows country, university, role and department fields based on whether `change` was clicked or not', async function () {
  408. const institutionDomainDataCopy = [
  409. {
  410. ...institutionDomainData[0],
  411. university: {
  412. ...institutionDomainData[0].university,
  413. ssoEnabled: false,
  414. },
  415. },
  416. ]
  417. const hostnameFirstChar = institutionDomainDataCopy[0].hostname.charAt(0)
  418. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  419. render(<EmailsSection />)
  420. await fetchMock.flush(true)
  421. fetchMock.reset()
  422. fetchMock.get(
  423. `/institutions/domains?hostname=${hostnameFirstChar}&limit=1`,
  424. institutionDomainDataCopy
  425. )
  426. await userEvent.click(
  427. screen.getByRole('button', {
  428. name: /add another email/i,
  429. })
  430. )
  431. await userEvent.type(
  432. screen.getByLabelText(/email/i),
  433. `user@${hostnameFirstChar}`
  434. )
  435. await userEvent.keyboard('{Tab}')
  436. await fetchMock.flush(true)
  437. fetchMock.reset()
  438. expect(
  439. screen.queryByRole('textbox', {
  440. name: /country/i,
  441. })
  442. ).to.be.null
  443. expect(
  444. screen.queryByRole('textbox', {
  445. name: /university/i,
  446. })
  447. ).to.be.null
  448. screen.getByRole('textbox', {
  449. name: /role/i,
  450. })
  451. screen.getByRole('textbox', {
  452. name: /department/i,
  453. })
  454. await userEvent.click(screen.getByRole('button', { name: /change/i }))
  455. screen.getByRole('textbox', {
  456. name: /country/i,
  457. })
  458. screen.getByRole('textbox', {
  459. name: /university/i,
  460. })
  461. expect(
  462. screen.queryByRole('textbox', {
  463. name: /role/i,
  464. })
  465. ).to.be.null
  466. expect(
  467. screen.queryByRole('textbox', {
  468. name: /department/i,
  469. })
  470. ).to.be.null
  471. })
  472. it('displays institution name with change button when autocompleted and adds new record', async function () {
  473. const institutionDomainDataCopy = [
  474. {
  475. ...institutionDomainData[0],
  476. university: {
  477. ...institutionDomainData[0].university,
  478. ssoEnabled: false,
  479. },
  480. },
  481. ]
  482. const hostnameFirstChar = institutionDomainDataCopy[0].hostname.charAt(0)
  483. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  484. render(<EmailsSection />)
  485. await fetchMock.flush(true)
  486. fetchMock.reset()
  487. fetchMock.get(
  488. `/institutions/domains?hostname=${hostnameFirstChar}&limit=1`,
  489. institutionDomainDataCopy
  490. )
  491. await userEvent.click(
  492. screen.getByRole('button', {
  493. name: /add another email/i,
  494. })
  495. )
  496. await userEvent.type(
  497. screen.getByLabelText(/email/i),
  498. `user@${hostnameFirstChar}`
  499. )
  500. await userEvent.keyboard('{Tab}')
  501. await fetchMock.flush(true)
  502. fetchMock.reset()
  503. screen.getByText(institutionDomainDataCopy[0].university.name)
  504. const userEmailDataCopy = {
  505. ...userEmailData,
  506. affiliation: {
  507. ...userEmailData.affiliation,
  508. institution: {
  509. ...userEmailData.affiliation.institution,
  510. name: institutionDomainDataCopy[0].university.name,
  511. },
  512. },
  513. }
  514. fetchMock
  515. .get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  516. .post('/user/emails', 200)
  517. await userEvent.type(
  518. screen.getByRole('textbox', { name: /role/i }),
  519. userEmailData.affiliation.role!
  520. )
  521. await userEvent.type(
  522. screen.getByRole('textbox', { name: /department/i }),
  523. userEmailData.affiliation.department!
  524. )
  525. await userEvent.click(
  526. screen.getByRole('button', {
  527. name: /add new email/i,
  528. })
  529. )
  530. await fetchMock.flush(true)
  531. fetchMock.reset()
  532. screen.getByText(userEmailDataCopy.affiliation.institution.name, {
  533. exact: false,
  534. })
  535. screen.getByText(userEmailDataCopy.affiliation.role!, { exact: false })
  536. screen.getByText(userEmailDataCopy.affiliation.department!, {
  537. exact: false,
  538. })
  539. })
  540. })