share-project-modal.test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import React from 'react'
  4. import {
  5. screen,
  6. fireEvent,
  7. waitFor,
  8. waitForElementToBeRemoved,
  9. } from '@testing-library/react'
  10. import fetchMock from 'fetch-mock'
  11. import { get } from 'lodash'
  12. import ShareProjectModal from '../../../../../frontend/js/features/share-project-modal/components/share-project-modal'
  13. import {
  14. renderWithEditorContext,
  15. cleanUpContext,
  16. } from '../../../helpers/render-with-context'
  17. import * as locationModule from '../../../../../frontend/js/features/share-project-modal/utils/location'
  18. describe('<ShareProjectModal/>', function () {
  19. const project = {
  20. _id: 'test-project',
  21. name: 'Test Project',
  22. features: {
  23. collaborators: 10,
  24. },
  25. owner: {
  26. email: 'project-owner@example.com',
  27. },
  28. members: [],
  29. invites: [],
  30. }
  31. const contacts = [
  32. // user with edited name
  33. {
  34. type: 'user',
  35. email: 'test-user@example.com',
  36. first_name: 'Test',
  37. last_name: 'User',
  38. name: 'Test User',
  39. },
  40. // user with default name (email prefix)
  41. {
  42. type: 'user',
  43. email: 'test@example.com',
  44. first_name: 'test',
  45. },
  46. // no last name
  47. {
  48. type: 'user',
  49. first_name: 'Eratosthenes',
  50. email: 'eratosthenes@example.com',
  51. },
  52. // more users
  53. {
  54. type: 'user',
  55. first_name: 'Claudius',
  56. last_name: 'Ptolemy',
  57. email: 'ptolemy@example.com',
  58. },
  59. {
  60. type: 'user',
  61. first_name: 'Abd al-Rahman',
  62. last_name: 'Al-Sufi',
  63. email: 'al-sufi@example.com',
  64. },
  65. {
  66. type: 'user',
  67. first_name: 'Nicolaus',
  68. last_name: 'Copernicus',
  69. email: 'copernicus@example.com',
  70. },
  71. ]
  72. const ideWithProject = project => {
  73. const scope = { project }
  74. return {
  75. $scope: {
  76. $watch: (path, callback) => {
  77. callback(get(scope, path))
  78. return () => null
  79. },
  80. $applyAsync: () => {},
  81. ...scope,
  82. },
  83. }
  84. }
  85. const modalProps = {
  86. ide: ideWithProject(project),
  87. show: true,
  88. isAdmin: true,
  89. handleHide: sinon.stub(),
  90. }
  91. beforeEach(function () {
  92. fetchMock.get('/user/contacts', { contacts })
  93. })
  94. afterEach(function () {
  95. fetchMock.restore()
  96. cleanUpContext()
  97. })
  98. it('renders the modal', async function () {
  99. renderWithEditorContext(<ShareProjectModal {...modalProps} />)
  100. await screen.findByText('Share Project')
  101. })
  102. it('calls handleHide when a Close button is pressed', async function () {
  103. const handleHide = sinon.stub()
  104. renderWithEditorContext(
  105. <ShareProjectModal {...modalProps} handleHide={handleHide} />
  106. )
  107. const [
  108. headerCloseButton,
  109. footerCloseButton,
  110. ] = await screen.findAllByRole('button', { name: 'Close' })
  111. fireEvent.click(headerCloseButton)
  112. fireEvent.click(footerCloseButton)
  113. expect(handleHide.callCount).to.equal(2)
  114. })
  115. it('handles access level "private"', async function () {
  116. renderWithEditorContext(
  117. <ShareProjectModal
  118. {...modalProps}
  119. ide={ideWithProject({ ...project, publicAccesLevel: 'private' })}
  120. />
  121. )
  122. await screen.findByText(
  123. 'Link sharing is off, only invited users can view this project.'
  124. )
  125. await screen.findByRole('button', { name: 'Turn on link sharing' })
  126. expect(screen.queryByText('Anyone with this link can view this project')).to
  127. .be.null
  128. expect(screen.queryByText('Anyone with this link can edit this project')).to
  129. .be.null
  130. })
  131. it('handles access level "tokenBased"', async function () {
  132. renderWithEditorContext(
  133. <ShareProjectModal
  134. {...modalProps}
  135. ide={ideWithProject({ ...project, publicAccesLevel: 'tokenBased' })}
  136. />
  137. )
  138. await screen.findByText('Link sharing is on')
  139. await screen.findByRole('button', { name: 'Turn off link sharing' })
  140. expect(screen.queryByText('Anyone with this link can view this project'))
  141. .not.to.be.null
  142. expect(screen.queryByText('Anyone with this link can edit this project'))
  143. .not.to.be.null
  144. })
  145. it('handles legacy access level "readAndWrite"', async function () {
  146. renderWithEditorContext(
  147. <ShareProjectModal
  148. {...modalProps}
  149. ide={ideWithProject({ ...project, publicAccesLevel: 'readAndWrite' })}
  150. />
  151. )
  152. await screen.findByText(
  153. 'This project is public and can be edited by anyone with the URL.'
  154. )
  155. await screen.findByRole('button', { name: 'Make Private' })
  156. })
  157. it('handles legacy access level "readOnly"', async function () {
  158. renderWithEditorContext(
  159. <ShareProjectModal
  160. {...modalProps}
  161. ide={ideWithProject({ ...project, publicAccesLevel: 'readOnly' })}
  162. />
  163. )
  164. await screen.findByText(
  165. 'This project is public and can be viewed but not edited by anyone with the URL'
  166. )
  167. await screen.findByRole('button', { name: 'Make Private' })
  168. })
  169. it('hides actions from non-admins', async function () {
  170. const invites = [
  171. {
  172. _id: 'invited-author',
  173. email: 'invited-author@example.com',
  174. privileges: 'readAndWrite',
  175. },
  176. ]
  177. // render as admin: actions should be present
  178. const { rerender } = renderWithEditorContext(
  179. <ShareProjectModal
  180. {...modalProps}
  181. ide={ideWithProject({
  182. ...project,
  183. invites,
  184. publicAccesLevel: 'tokenBased',
  185. })}
  186. isAdmin
  187. />
  188. )
  189. await screen.findByRole('button', { name: 'Turn off link sharing' })
  190. await screen.findByRole('button', { name: 'Resend' })
  191. // render as non-admin (non-owner), link sharing on: actions should be missing and message should be present
  192. rerender(
  193. <ShareProjectModal
  194. {...modalProps}
  195. ide={ideWithProject({
  196. ...project,
  197. invites,
  198. publicAccesLevel: 'tokenBased',
  199. })}
  200. isAdmin={false}
  201. />
  202. )
  203. await screen.findByText(
  204. 'To change access permissions, please ask the project owner'
  205. )
  206. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  207. .be.null
  208. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  209. .null
  210. expect(screen.queryByRole('button', { name: 'Resend' })).to.be.null
  211. // render as non-admin (non-owner), link sharing off: actions should be missing and message should be present
  212. rerender(
  213. <ShareProjectModal
  214. {...modalProps}
  215. ide={ideWithProject({
  216. ...project,
  217. invites,
  218. publicAccesLevel: 'private',
  219. })}
  220. isAdmin={false}
  221. />
  222. )
  223. await screen.findByText(
  224. 'To add more collaborators or turn on link sharing, please ask the project owner'
  225. )
  226. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  227. .be.null
  228. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  229. .null
  230. expect(screen.queryByRole('button', { name: 'Resend' })).to.be.null
  231. })
  232. it('only shows read-only token link to restricted token members', async function () {
  233. renderWithEditorContext(
  234. <ShareProjectModal
  235. {...modalProps}
  236. ide={ideWithProject({ ...project, publicAccesLevel: 'tokenBased' })}
  237. />,
  238. { isRestrictedTokenMember: true }
  239. )
  240. // no buttons
  241. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  242. .null
  243. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  244. .be.null
  245. // only read-only token link
  246. await screen.findByText('Anyone with this link can view this project')
  247. expect(screen.queryByText('Anyone with this link can edit this project')).to
  248. .be.null
  249. })
  250. it('displays project members and invites', async function () {
  251. const members = [
  252. {
  253. _id: 'member-author',
  254. email: 'member-author@example.com',
  255. privileges: 'readAndWrite',
  256. },
  257. {
  258. _id: 'member-viewer',
  259. email: 'member-viewer@example.com',
  260. privileges: 'readOnly',
  261. },
  262. ]
  263. const invites = [
  264. {
  265. _id: 'invited-author',
  266. email: 'invited-author@example.com',
  267. privileges: 'readAndWrite',
  268. },
  269. {
  270. _id: 'invited-viewer',
  271. email: 'invited-viewer@example.com',
  272. privileges: 'readOnly',
  273. },
  274. ]
  275. renderWithEditorContext(
  276. <ShareProjectModal
  277. {...modalProps}
  278. ide={ideWithProject({
  279. ...project,
  280. members,
  281. invites,
  282. publicAccesLevel: 'tokenBased',
  283. })}
  284. />
  285. )
  286. expect(screen.queryAllByText('project-owner@example.com')).to.have.length(1)
  287. expect(screen.queryAllByText('member-author@example.com')).to.have.length(1)
  288. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  289. expect(screen.queryAllByText('invited-author@example.com')).to.have.length(
  290. 1
  291. )
  292. expect(screen.queryAllByText('invited-viewer@example.com')).to.have.length(
  293. 1
  294. )
  295. expect(screen.queryAllByText('Invite not yet accepted.')).to.have.length(
  296. invites.length
  297. )
  298. expect(screen.queryAllByRole('button', { name: 'Resend' })).to.have.length(
  299. invites.length
  300. )
  301. })
  302. it('resends an invite', async function () {
  303. fetchMock.postOnce(
  304. 'express:/project/:projectId/invite/:inviteId/resend',
  305. 204
  306. )
  307. const invites = [
  308. {
  309. _id: 'invited-author',
  310. email: 'invited-author@example.com',
  311. privileges: 'readAndWrite',
  312. },
  313. ]
  314. renderWithEditorContext(
  315. <ShareProjectModal
  316. {...modalProps}
  317. ide={ideWithProject({
  318. ...project,
  319. invites,
  320. publicAccesLevel: 'tokenBased',
  321. })}
  322. />
  323. )
  324. const [, closeButton] = screen.getAllByRole('button', {
  325. name: 'Close',
  326. })
  327. const resendButton = screen.getByRole('button', { name: 'Resend' })
  328. fireEvent.click(resendButton)
  329. await waitFor(() => expect(closeButton.disabled).to.be.true)
  330. expect(fetchMock.done()).to.be.true
  331. expect(closeButton.disabled).to.be.false
  332. })
  333. it('revokes an invite', async function () {
  334. fetchMock.deleteOnce('express:/project/:projectId/invite/:inviteId', 204)
  335. const invites = [
  336. {
  337. _id: 'invited-author',
  338. email: 'invited-author@example.com',
  339. privileges: 'readAndWrite',
  340. },
  341. ]
  342. renderWithEditorContext(
  343. <ShareProjectModal
  344. {...modalProps}
  345. ide={ideWithProject({
  346. ...project,
  347. invites,
  348. publicAccesLevel: 'tokenBased',
  349. })}
  350. />
  351. )
  352. const [, closeButton] = screen.getAllByRole('button', {
  353. name: 'Close',
  354. })
  355. const revokeButton = screen.getByRole('button', { name: 'Revoke' })
  356. fireEvent.click(revokeButton)
  357. await waitFor(() => expect(closeButton.disabled).to.be.true)
  358. expect(fetchMock.done()).to.be.true
  359. expect(closeButton.disabled).to.be.false
  360. })
  361. it('changes member privileges to read + write', async function () {
  362. fetchMock.putOnce('express:/project/:projectId/users/:userId', 204)
  363. const members = [
  364. {
  365. _id: 'member-viewer',
  366. email: 'member-viewer@example.com',
  367. privileges: 'readOnly',
  368. },
  369. ]
  370. renderWithEditorContext(
  371. <ShareProjectModal
  372. {...modalProps}
  373. ide={ideWithProject({
  374. ...project,
  375. members,
  376. publicAccesLevel: 'tokenBased',
  377. })}
  378. />
  379. )
  380. const [, closeButton] = await screen.getAllByRole('button', {
  381. name: 'Close',
  382. })
  383. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  384. const select = screen.getByDisplayValue('Read Only')
  385. await fireEvent.change(select, { target: { value: 'readAndWrite' } })
  386. const changeButton = screen.getByRole('button', { name: 'Change' })
  387. fireEvent.click(changeButton)
  388. await waitFor(() => expect(closeButton.disabled).to.be.true)
  389. const { body } = fetchMock.lastOptions()
  390. expect(JSON.parse(body)).to.deep.equal({ privilegeLevel: 'readAndWrite' })
  391. expect(fetchMock.done()).to.be.true
  392. expect(closeButton.disabled).to.be.false
  393. })
  394. it('removes a member from the project', async function () {
  395. fetchMock.deleteOnce('express:/project/:projectId/users/:userId', 204)
  396. const members = [
  397. {
  398. _id: 'member-viewer',
  399. email: 'member-viewer@example.com',
  400. privileges: 'readOnly',
  401. },
  402. ]
  403. renderWithEditorContext(
  404. <ShareProjectModal
  405. {...modalProps}
  406. ide={ideWithProject({
  407. ...project,
  408. members,
  409. publicAccesLevel: 'tokenBased',
  410. })}
  411. />
  412. )
  413. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  414. const removeButton = screen.getByRole('button', {
  415. name: 'Remove collaborator',
  416. })
  417. fireEvent.click(removeButton)
  418. const url = fetchMock.lastUrl()
  419. expect(url).to.equal('/project/test-project/users/member-viewer')
  420. expect(fetchMock.done()).to.be.true
  421. await waitForElementToBeRemoved(() =>
  422. screen.queryByText('member-viewer@example.com')
  423. )
  424. })
  425. it('changes member privileges to owner with confirmation', async function () {
  426. fetchMock.postOnce('express:/project/:projectId/transfer-ownership', 204)
  427. const members = [
  428. {
  429. _id: 'member-viewer',
  430. email: 'member-viewer@example.com',
  431. privileges: 'readOnly',
  432. },
  433. ]
  434. renderWithEditorContext(
  435. <ShareProjectModal
  436. {...modalProps}
  437. ide={ideWithProject({
  438. ...project,
  439. members,
  440. publicAccesLevel: 'tokenBased',
  441. })}
  442. />
  443. )
  444. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  445. const select = screen.getByDisplayValue('Read Only')
  446. fireEvent.change(select, { target: { value: 'owner' } })
  447. const changeButton = screen.getByRole('button', { name: 'Change' })
  448. await fireEvent.click(changeButton)
  449. screen.getByText((_, node) => {
  450. return (
  451. node.textContent ===
  452. 'Are you sure you want to make member-viewer@example.com the owner of Test Project?'
  453. )
  454. })
  455. const reloadStub = sinon.stub(locationModule, 'reload')
  456. const confirmButton = screen.getByRole('button', {
  457. name: 'Change owner',
  458. })
  459. fireEvent.click(confirmButton)
  460. await waitFor(() => expect(confirmButton.disabled).to.be.true)
  461. const { body } = fetchMock.lastOptions()
  462. expect(JSON.parse(body)).to.deep.equal({ user_id: 'member-viewer' })
  463. expect(fetchMock.done()).to.be.true
  464. expect(reloadStub.calledOnce).to.be.true
  465. reloadStub.restore()
  466. })
  467. it('sends invites to input email addresses', async function () {
  468. renderWithEditorContext(
  469. <ShareProjectModal
  470. {...modalProps}
  471. ide={ideWithProject({
  472. ...project,
  473. publicAccesLevel: 'tokenBased',
  474. })}
  475. />
  476. )
  477. const [inputElement] = await screen.findAllByLabelText(
  478. 'Share with your collaborators'
  479. )
  480. // loading contacts
  481. await waitFor(() => {
  482. expect(fetchMock.called('express:/user/contacts')).to.be.true
  483. })
  484. // displaying a list of matching contacts
  485. inputElement.focus()
  486. fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
  487. await screen.findByText(/ptolemy@example.com/)
  488. // sending invitations
  489. fetchMock.post('express:/project/:projectId/invite', (url, req) => {
  490. const data = JSON.parse(req.body)
  491. if (data.email === 'a@b.c') {
  492. return {
  493. status: 400,
  494. body: { errorReason: 'invalid_email' },
  495. }
  496. }
  497. return {
  498. status: 200,
  499. body: {
  500. invite: {
  501. ...data,
  502. _id: data.email,
  503. },
  504. },
  505. }
  506. })
  507. fireEvent.paste(inputElement, {
  508. clipboardData: {
  509. getData: () =>
  510. 'test@example.com, foo@example.com, bar@example.com, a@b.c',
  511. },
  512. })
  513. const privilegesElement = screen.getByDisplayValue('Can Edit')
  514. fireEvent.change(privilegesElement, { target: { value: 'readOnly' } })
  515. const submitButton = screen.getByRole('button', { name: 'Share' })
  516. submitButton.click()
  517. let calls
  518. await waitFor(
  519. () => {
  520. calls = fetchMock.calls('express:/project/:projectId/invite')
  521. expect(calls).to.have.length(4)
  522. },
  523. { timeout: 5000 } // allow time for delay between each request
  524. )
  525. expect(calls[0][1].body).to.equal(
  526. JSON.stringify({ email: 'test@example.com', privileges: 'readOnly' })
  527. )
  528. expect(calls[1][1].body).to.equal(
  529. JSON.stringify({ email: 'foo@example.com', privileges: 'readOnly' })
  530. )
  531. expect(calls[2][1].body).to.equal(
  532. JSON.stringify({ email: 'bar@example.com', privileges: 'readOnly' })
  533. )
  534. expect(calls[3][1].body).to.equal(
  535. JSON.stringify({ email: 'a@b.c', privileges: 'readOnly' })
  536. )
  537. // error from the last invite
  538. screen.getByText('An email address is invalid')
  539. })
  540. it('displays a message when the collaborator limit is reached', async function () {
  541. fetchMock.post('/event/project-sharing-paywall-prompt', {})
  542. renderWithEditorContext(
  543. <ShareProjectModal
  544. {...modalProps}
  545. ide={ideWithProject({
  546. ...project,
  547. publicAccesLevel: 'tokenBased',
  548. features: {
  549. collaborators: 0,
  550. },
  551. })}
  552. />,
  553. {
  554. user: {
  555. id: '123abd',
  556. allowedFreeTrial: true,
  557. },
  558. }
  559. )
  560. expect(screen.queryByLabelText('Share with your collaborators')).to.be.null
  561. screen.getByText(
  562. /You need to upgrade your account to add more collaborators/
  563. )
  564. })
  565. it('handles server error responses', async function () {
  566. renderWithEditorContext(
  567. <ShareProjectModal
  568. {...modalProps}
  569. ide={ideWithProject({
  570. ...project,
  571. publicAccesLevel: 'tokenBased',
  572. })}
  573. />
  574. )
  575. // loading contacts
  576. await waitFor(() => {
  577. expect(fetchMock.called('express:/user/contacts')).to.be.true
  578. })
  579. const [inputElement] = await screen.findAllByLabelText(
  580. 'Share with your collaborators'
  581. )
  582. const submitButton = screen.getByRole('button', { name: 'Share' })
  583. const respondWithError = async function (errorReason) {
  584. inputElement.focus()
  585. fireEvent.change(inputElement, {
  586. target: { value: 'invited-author-1@example.com' },
  587. })
  588. inputElement.blur()
  589. fetchMock.postOnce(
  590. 'express:/project/:projectId/invite',
  591. {
  592. status: 400,
  593. body: { errorReason },
  594. },
  595. { overwriteRoutes: true }
  596. )
  597. expect(submitButton.disabled).to.be.false
  598. submitButton.click()
  599. await fetchMock.flush(true)
  600. expect(fetchMock.done()).to.be.true
  601. }
  602. await respondWithError('cannot_invite_non_user')
  603. await screen.findByText(
  604. `Can't send invite. Recipient must already have a Overleaf account`
  605. )
  606. await respondWithError('cannot_verify_user_not_robot')
  607. await screen.findByText(
  608. `Sorry, we could not verify that you are not a robot. Please check that Google reCAPTCHA is not being blocked by an ad blocker or firewall.`
  609. )
  610. await respondWithError('cannot_invite_self')
  611. await screen.findByText(`Can't send invite to yourself`)
  612. await respondWithError('invalid_email')
  613. await screen.findByText(`An email address is invalid`)
  614. await respondWithError('too_many_requests')
  615. await screen.findByText(
  616. `Too many requests were received in a short space of time. Please wait for a few moments and try again.`
  617. )
  618. })
  619. it('handles switching between access levels', async function () {
  620. fetchMock.post('express:/project/:projectId/settings/admin', 204)
  621. const watchCallbacks = {}
  622. const ideWithProject = project => {
  623. return {
  624. $scope: {
  625. $watch: (path, callback, deep) => {
  626. watchCallbacks[path] = callback
  627. return () => {}
  628. },
  629. $applyAsync: () => {},
  630. project,
  631. },
  632. }
  633. }
  634. renderWithEditorContext(
  635. <ShareProjectModal
  636. {...modalProps}
  637. ide={ideWithProject({ ...project, publicAccesLevel: 'private' })}
  638. />
  639. )
  640. await screen.findByText(
  641. 'Link sharing is off, only invited users can view this project.'
  642. )
  643. const enableButton = await screen.findByRole('button', {
  644. name: 'Turn on link sharing',
  645. })
  646. fireEvent.click(enableButton)
  647. await waitFor(() => expect(enableButton.disabled).to.be.true)
  648. const { body: tokenBody } = fetchMock.lastOptions()
  649. expect(JSON.parse(tokenBody)).to.deep.equal({
  650. publicAccessLevel: 'tokenBased',
  651. })
  652. // NOTE: updating the scoped project data manually,
  653. // as the project data is usually updated via the websocket connection
  654. watchCallbacks.project({ ...project, publicAccesLevel: 'tokenBased' })
  655. await screen.findByText('Link sharing is on')
  656. const disableButton = await screen.findByRole('button', {
  657. name: 'Turn off link sharing',
  658. })
  659. fireEvent.click(disableButton)
  660. await waitFor(() => expect(disableButton.disabled).to.be.true)
  661. const { body: privateBody } = fetchMock.lastOptions()
  662. expect(JSON.parse(privateBody)).to.deep.equal({
  663. publicAccessLevel: 'private',
  664. })
  665. // NOTE: updating the scoped project data manually,
  666. // as the project data is usually updated via the websocket connection
  667. watchCallbacks.project({ ...project, publicAccesLevel: 'private' })
  668. await screen.findByText(
  669. 'Link sharing is off, only invited users can view this project.'
  670. )
  671. })
  672. it('avoids selecting unmatched contact', async function () {
  673. renderWithEditorContext(<ShareProjectModal {...modalProps} />)
  674. const [inputElement] = await screen.findAllByLabelText(
  675. 'Share with your collaborators'
  676. )
  677. // Wait for contacts to load
  678. await waitFor(() => {
  679. expect(fetchMock.called('express:/user/contacts')).to.be.true
  680. })
  681. // Enter a prefix that matches a contact
  682. inputElement.focus()
  683. fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
  684. // The matching contact should now be present and selected
  685. await screen.findByRole('option', {
  686. name: `Claudius Ptolemy <ptolemy@example.com>`,
  687. selected: true,
  688. })
  689. // Keep entering text so the contact no longer matches
  690. fireEvent.change(inputElement, {
  691. target: { value: 'ptolemy.new@example.com' },
  692. })
  693. // The matching contact should no longer be present
  694. expect(
  695. screen.queryByRole('option', {
  696. name: `Claudius Ptolemy <ptolemy@example.com>`,
  697. })
  698. ).to.be.null
  699. // No items should be added yet
  700. expect(screen.queryByRole('button', { name: 'Remove' })).to.be.null
  701. // Pressing Tab should add the entered item
  702. fireEvent.keyDown(inputElement, { key: 'Tab', code: 'Tab' })
  703. await waitFor(() => {
  704. expect(screen.getAllByRole('button', { name: 'Remove' })).to.have.length(
  705. 1
  706. )
  707. })
  708. // Blurring the input should not add another contact
  709. fireEvent.blur(inputElement)
  710. await waitFor(() => {
  711. expect(screen.getAllByRole('button', { name: 'Remove' })).to.have.length(
  712. 1
  713. )
  714. })
  715. })
  716. })