share-project-modal.test.jsx 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { screen, fireEvent, render, waitFor } from '@testing-library/react'
  4. import fetchMock from 'fetch-mock'
  5. import userEvent from '@testing-library/user-event'
  6. import ShareProjectModal from '../../../../../frontend/js/features/share-project-modal/components/share-project-modal'
  7. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  8. import {
  9. EditorProviders,
  10. USER_EMAIL,
  11. USER_ID,
  12. } from '../../../helpers/editor-providers'
  13. import { location } from '@/shared/components/location'
  14. import useScopeValue from '@/shared/hooks/use-scope-value'
  15. async function changePrivilegeLevel(screen, { current, next }) {
  16. const select = screen.getByDisplayValue(current)
  17. fireEvent.click(select)
  18. const option = screen.getByRole('option', {
  19. name: next,
  20. })
  21. fireEvent.click(option)
  22. }
  23. describe('<ShareProjectModal/>', function () {
  24. const project = {
  25. _id: 'test-project',
  26. name: 'Test Project',
  27. features: {
  28. collaborators: 10,
  29. compileGroup: 'standard',
  30. },
  31. owner: {
  32. _id: USER_ID,
  33. email: USER_EMAIL,
  34. },
  35. members: [],
  36. invites: [],
  37. }
  38. const contacts = [
  39. // user with edited name
  40. {
  41. type: 'user',
  42. email: 'test-user@example.com',
  43. first_name: 'Test',
  44. last_name: 'User',
  45. name: 'Test User',
  46. },
  47. // user with default name (email prefix)
  48. {
  49. type: 'user',
  50. email: 'test@example.com',
  51. first_name: 'test',
  52. },
  53. // no last name
  54. {
  55. type: 'user',
  56. first_name: 'Eratosthenes',
  57. email: 'eratosthenes@example.com',
  58. },
  59. // more users
  60. {
  61. type: 'user',
  62. first_name: 'Claudius',
  63. last_name: 'Ptolemy',
  64. email: 'ptolemy@example.com',
  65. },
  66. {
  67. type: 'user',
  68. first_name: 'Abd al-Rahman',
  69. last_name: 'Al-Sufi',
  70. email: 'al-sufi@example.com',
  71. },
  72. {
  73. type: 'user',
  74. first_name: 'Nicolaus',
  75. last_name: 'Copernicus',
  76. email: 'copernicus@example.com',
  77. },
  78. ]
  79. const modalProps = {
  80. show: true,
  81. handleHide: sinon.stub(),
  82. }
  83. beforeEach(function () {
  84. this.locationWrapperSandbox = sinon.createSandbox()
  85. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  86. fetchMock.get('/user/contacts', { contacts })
  87. window.metaAttributesCache.set('ol-user', { allowedFreeTrial: true })
  88. window.metaAttributesCache.set('ol-showUpgradePrompt', true)
  89. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  90. })
  91. afterEach(function () {
  92. this.locationWrapperSandbox.restore()
  93. fetchMock.removeRoutes().clearHistory()
  94. })
  95. it('renders the modal', async function () {
  96. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  97. scope: { project },
  98. })
  99. await screen.findByText('Share Project')
  100. })
  101. it('calls handleHide when a Close button is pressed', async function () {
  102. const handleHide = sinon.stub()
  103. renderWithEditorContext(
  104. <ShareProjectModal {...modalProps} handleHide={handleHide} />,
  105. { scope: { project } }
  106. )
  107. const [headerCloseButton, footerCloseButton] = await screen.findAllByRole(
  108. 'button',
  109. { name: 'Close' }
  110. )
  111. await userEvent.click(headerCloseButton)
  112. await userEvent.click(footerCloseButton)
  113. expect(handleHide.callCount).to.equal(2)
  114. })
  115. it('handles access level "private"', async function () {
  116. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  117. scope: { project: { ...project, publicAccesLevel: 'private' } },
  118. })
  119. await screen.findByText('Link sharing is off')
  120. await screen.findByRole('button', { name: 'Turn on link sharing' })
  121. expect(screen.queryByText('Anyone with this link can view this project')).to
  122. .be.null
  123. expect(screen.queryByText('Anyone with this link can edit this project')).to
  124. .be.null
  125. })
  126. it('handles access level "tokenBased"', async function () {
  127. const tokens = {
  128. readAndWrite: '6862414195fwtbrtrdtskb',
  129. readAndWritePrefix: '6862414195',
  130. readOnly: 'wrnjfzkysqkr',
  131. readAndWriteHashPrefix: 'taEVki',
  132. readOnlyHashPrefix: 'j2xYbL',
  133. }
  134. fetchMock.get(`/project/${project._id}/tokens`, tokens)
  135. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  136. scope: { project: { ...project, publicAccesLevel: 'tokenBased' } },
  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. screen.getByText(
  145. `https://www.test-overleaf.com/${tokens.readAndWrite}#${tokens.readAndWriteHashPrefix}`
  146. )
  147. screen.getByText(
  148. `https://www.test-overleaf.com/read/${tokens.readOnly}#${tokens.readOnlyHashPrefix}`
  149. )
  150. })
  151. it('handles legacy access level "readAndWrite"', async function () {
  152. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  153. scope: { project: { ...project, publicAccesLevel: 'readAndWrite' } },
  154. })
  155. await screen.findByText(
  156. 'This project is public and can be edited by anyone with the URL.'
  157. )
  158. await screen.findByRole('button', { name: 'Make private' })
  159. })
  160. it('handles legacy access level "readOnly"', async function () {
  161. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  162. scope: { project: { ...project, publicAccesLevel: 'readOnly' } },
  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('displays actions for project-owners', async function () {
  170. fetchMock.get(`/project/${project._id}/tokens`, {})
  171. const invites = [
  172. {
  173. _id: 'invited-author',
  174. email: 'invited-author@example.com',
  175. privileges: 'readAndWrite',
  176. },
  177. ]
  178. // render as project owner: actions should be present
  179. render(
  180. <EditorProviders
  181. scope={{
  182. project: {
  183. ...project,
  184. invites,
  185. publicAccesLevel: 'tokenBased',
  186. },
  187. }}
  188. >
  189. <ShareProjectModal {...modalProps} />
  190. </EditorProviders>
  191. )
  192. await screen.findByRole('button', { name: 'Turn off link sharing' })
  193. await screen.findByRole('button', { name: 'Resend' })
  194. })
  195. it('hides actions from non-project-owners when link sharing on', async function () {
  196. const invites = [
  197. {
  198. _id: 'invited-author',
  199. email: 'invited-author@example.com',
  200. privileges: 'readAndWrite',
  201. },
  202. ]
  203. render(
  204. <EditorProviders
  205. scope={{
  206. project: {
  207. ...project,
  208. invites,
  209. publicAccesLevel: 'tokenBased',
  210. },
  211. }}
  212. user={{
  213. id: 'non-project-owner',
  214. email: 'non-project-owner@example.com',
  215. }}
  216. >
  217. <ShareProjectModal {...modalProps} />
  218. </EditorProviders>
  219. )
  220. await screen.findByText(
  221. 'To change access permissions, please ask the project owner'
  222. )
  223. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  224. .be.null
  225. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  226. .null
  227. expect(screen.queryByRole('button', { name: 'Resend' })).to.be.null
  228. })
  229. it('hides actions from non-project-owners when link sharing off', async function () {
  230. const invites = [
  231. {
  232. _id: 'invited-author',
  233. email: 'invited-author@example.com',
  234. privileges: 'readAndWrite',
  235. },
  236. ]
  237. render(
  238. <EditorProviders
  239. scope={{
  240. project: {
  241. ...project,
  242. invites,
  243. publicAccesLevel: 'private',
  244. },
  245. }}
  246. user={{
  247. id: 'non-project-owner',
  248. email: 'non-project-owner@example.com',
  249. }}
  250. >
  251. <ShareProjectModal {...modalProps} />
  252. </EditorProviders>
  253. )
  254. await screen.findByText(
  255. 'To add more collaborators or turn on link sharing, please ask the project owner'
  256. )
  257. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  258. .be.null
  259. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  260. .null
  261. expect(screen.queryByRole('button', { name: 'Resend' })).to.be.null
  262. })
  263. it('only shows read-only token link to restricted token members', async function () {
  264. window.metaAttributesCache.set('ol-isRestrictedTokenMember', true)
  265. fetchMock.get(`/project/${project._id}/tokens`, {})
  266. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  267. isRestrictedTokenMember: true,
  268. scope: { project: { ...project, publicAccesLevel: 'tokenBased' } },
  269. })
  270. // no buttons
  271. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  272. .null
  273. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  274. .be.null
  275. // only read-only token link
  276. await screen.findByText('Anyone with this link can view this project')
  277. expect(screen.queryByText('Anyone with this link can edit this project')).to
  278. .be.null
  279. })
  280. it('displays project members and invites', async function () {
  281. const members = [
  282. {
  283. _id: 'member-author',
  284. email: 'member-author@example.com',
  285. privileges: 'readAndWrite',
  286. },
  287. {
  288. _id: 'member-viewer',
  289. email: 'member-viewer@example.com',
  290. privileges: 'readOnly',
  291. },
  292. ]
  293. const invites = [
  294. {
  295. _id: 'invited-author',
  296. email: 'invited-author@example.com',
  297. privileges: 'readAndWrite',
  298. },
  299. {
  300. _id: 'invited-viewer',
  301. email: 'invited-viewer@example.com',
  302. privileges: 'readOnly',
  303. },
  304. ]
  305. fetchMock.get(`/project/${project._id}/tokens`, {})
  306. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  307. scope: {
  308. project: {
  309. ...project,
  310. members,
  311. invites,
  312. publicAccesLevel: 'tokenBased',
  313. },
  314. },
  315. })
  316. const projectOwnerEmail = USER_EMAIL
  317. await screen.findByText(projectOwnerEmail)
  318. expect(screen.queryAllByText(projectOwnerEmail)).to.have.length(1)
  319. expect(screen.queryAllByText('member-author@example.com')).to.have.length(1)
  320. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  321. expect(screen.queryAllByText('invited-author@example.com')).to.have.length(
  322. 1
  323. )
  324. expect(screen.queryAllByText('invited-viewer@example.com')).to.have.length(
  325. 1
  326. )
  327. expect(screen.queryAllByText('Invite not yet accepted.')).to.have.length(
  328. invites.length
  329. )
  330. expect(screen.queryAllByRole('button', { name: 'Resend' })).to.have.length(
  331. invites.length
  332. )
  333. })
  334. it('resends an invite', async function () {
  335. fetchMock.get(`/project/${project._id}/tokens`, {})
  336. fetchMock.postOnce(
  337. 'express:/project/:projectId/invite/:inviteId/resend',
  338. 204
  339. )
  340. const invites = [
  341. {
  342. _id: 'invited-author',
  343. email: 'invited-author@example.com',
  344. privileges: 'readAndWrite',
  345. },
  346. ]
  347. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  348. scope: {
  349. project: {
  350. ...project,
  351. invites,
  352. publicAccesLevel: 'tokenBased',
  353. },
  354. },
  355. })
  356. const [, closeButton] = screen.getAllByRole('button', {
  357. name: 'Close',
  358. })
  359. const resendButton = screen.getByRole('button', { name: 'Resend' })
  360. fireEvent.click(resendButton)
  361. await waitFor(() => expect(closeButton.disabled).to.be.true)
  362. expect(fetchMock.callHistory.done()).to.be.true
  363. await waitFor(() => expect(closeButton.disabled).to.be.false)
  364. })
  365. it('revokes an invite', async function () {
  366. fetchMock.get(`/project/${project._id}/tokens`, {})
  367. fetchMock.deleteOnce('express:/project/:projectId/invite/:inviteId', 204)
  368. const invites = [
  369. {
  370. _id: 'invited-author',
  371. email: 'invited-author@example.com',
  372. privileges: 'readAndWrite',
  373. },
  374. ]
  375. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  376. scope: {
  377. project: {
  378. ...project,
  379. invites,
  380. publicAccesLevel: 'tokenBased',
  381. },
  382. },
  383. })
  384. const [, closeButton] = screen.getAllByRole('button', {
  385. name: 'Close',
  386. })
  387. const revokeButton = screen.getByRole('button', { name: 'Revoke' })
  388. fireEvent.click(revokeButton)
  389. await waitFor(() => expect(closeButton.disabled).to.be.true)
  390. expect(fetchMock.callHistory.done()).to.be.true
  391. await waitFor(() => expect(closeButton.disabled).to.be.false)
  392. })
  393. it('changes member privileges to read + write', async function () {
  394. fetchMock.get(`/project/${project._id}/tokens`, {})
  395. fetchMock.putOnce('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(<ShareProjectModal {...modalProps} />, {
  404. scope: {
  405. project: {
  406. ...project,
  407. members,
  408. publicAccesLevel: 'tokenBased',
  409. },
  410. },
  411. })
  412. const [, closeButton] = await screen.getAllByRole('button', {
  413. name: 'Close',
  414. })
  415. expect(
  416. await screen.findAllByText('member-viewer@example.com')
  417. ).to.have.length(1)
  418. await changePrivilegeLevel(screen, { current: 'Viewer', next: 'Editor' })
  419. await waitFor(() => expect(closeButton.disabled).to.be.true)
  420. const { body } = fetchMock.callHistory.calls().at(-1).options
  421. expect(JSON.parse(body)).to.deep.equal({ privilegeLevel: 'readAndWrite' })
  422. expect(fetchMock.callHistory.done()).to.be.true
  423. await waitFor(() => expect(closeButton.disabled).to.be.false)
  424. })
  425. it('removes a member from the project', async function () {
  426. fetchMock.get(`/project/${project._id}/tokens`, {})
  427. fetchMock.deleteOnce('express:/project/:projectId/users/:userId', 204)
  428. const members = [
  429. {
  430. _id: 'member-viewer',
  431. email: 'member-viewer@example.com',
  432. privileges: 'readOnly',
  433. },
  434. ]
  435. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  436. scope: {
  437. project: {
  438. ...project,
  439. members,
  440. publicAccesLevel: 'tokenBased',
  441. },
  442. },
  443. })
  444. expect(
  445. await screen.findAllByText('member-viewer@example.com')
  446. ).to.have.length(1)
  447. await changePrivilegeLevel(screen, {
  448. current: 'Viewer',
  449. next: 'Remove access',
  450. })
  451. const removeButton = screen.getByRole('button', {
  452. name: 'Change',
  453. })
  454. fireEvent.click(removeButton)
  455. const url = fetchMock.callHistory.calls().at(-1).url
  456. expect(url).to.equal(
  457. 'https://www.test-overleaf.com/project/test-project/users/member-viewer'
  458. )
  459. expect(fetchMock.callHistory.done()).to.be.true
  460. })
  461. it('changes member privileges to owner with confirmation', async function () {
  462. fetchMock.get(`/project/${project._id}/tokens`, {})
  463. fetchMock.postOnce('express:/project/:projectId/transfer-ownership', 204)
  464. const members = [
  465. {
  466. _id: 'member-viewer',
  467. email: 'member-viewer@example.com',
  468. privileges: 'readOnly',
  469. },
  470. ]
  471. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  472. scope: {
  473. project: {
  474. ...project,
  475. members,
  476. publicAccesLevel: 'tokenBased',
  477. },
  478. },
  479. })
  480. await screen.findByText('member-viewer@example.com')
  481. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  482. await changePrivilegeLevel(screen, {
  483. current: 'Viewer',
  484. next: 'Make owner',
  485. })
  486. screen.getByText((_, node) => {
  487. return (
  488. node.textContent ===
  489. 'Are you sure you want to make member-viewer@example.com the owner of Test Project?'
  490. )
  491. })
  492. const confirmButton = screen.getByRole('button', {
  493. name: 'Change owner',
  494. })
  495. fireEvent.click(confirmButton)
  496. await waitFor(() => expect(confirmButton.disabled).to.be.true)
  497. const { body } = fetchMock.callHistory.calls().at(-1).options
  498. expect(JSON.parse(body)).to.deep.equal({ user_id: 'member-viewer' })
  499. expect(fetchMock.callHistory.done()).to.be.true
  500. })
  501. it('sends invites to input email addresses', async function () {
  502. fetchMock.get(`/project/${project._id}/tokens`, {})
  503. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  504. scope: {
  505. project: {
  506. ...project,
  507. publicAccesLevel: 'tokenBased',
  508. },
  509. },
  510. })
  511. const [inputElement] = await screen.findAllByLabelText('Add people')
  512. // loading contacts
  513. await waitFor(() => {
  514. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  515. })
  516. // displaying a list of matching contacts
  517. inputElement.focus()
  518. fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
  519. await screen.findByText(/ptolemy@example.com/)
  520. // sending invitations
  521. fetchMock.post(
  522. 'express:/project/:projectId/invite',
  523. ({ args: [, req] }) => {
  524. const data = JSON.parse(req.body)
  525. if (data.email === 'a@b.c') {
  526. return {
  527. status: 400,
  528. body: { errorReason: 'invalid_email' },
  529. }
  530. }
  531. return {
  532. status: 200,
  533. body: {
  534. invite: {
  535. ...data,
  536. _id: data.email,
  537. },
  538. },
  539. }
  540. }
  541. )
  542. fireEvent.paste(inputElement, {
  543. clipboardData: {
  544. getData: () =>
  545. `test@example.com; foo@example.com
  546. bar@example.com, a@b.c`,
  547. },
  548. })
  549. const user = userEvent.setup()
  550. await user.click(screen.getByTestId('add-collaborator-select'))
  551. await user.click(screen.getByText('Viewer'))
  552. const submitButton = screen.getByRole('button', { name: 'Invite' })
  553. await userEvent.click(submitButton)
  554. let calls
  555. await waitFor(
  556. () => {
  557. calls = fetchMock.callHistory.calls(
  558. 'express:/project/:projectId/invite'
  559. )
  560. expect(calls).to.have.length(4)
  561. },
  562. { timeout: 5000 } // allow time for delay between each request
  563. )
  564. expect(calls[0].args[1].body).to.equal(
  565. JSON.stringify({ email: 'test@example.com', privileges: 'readOnly' })
  566. )
  567. expect(calls[1].args[1].body).to.equal(
  568. JSON.stringify({ email: 'foo@example.com', privileges: 'readOnly' })
  569. )
  570. expect(calls[2].args[1].body).to.equal(
  571. JSON.stringify({ email: 'bar@example.com', privileges: 'readOnly' })
  572. )
  573. expect(calls[3].args[1].body).to.equal(
  574. JSON.stringify({ email: 'a@b.c', privileges: 'readOnly' })
  575. )
  576. // error from the last invite
  577. screen.getByText('An email address is invalid')
  578. })
  579. it('displays a message when the collaborator limit is reached', async function () {
  580. fetchMock.get(`/project/${project._id}/tokens`, {})
  581. fetchMock.post(
  582. '/event/paywall-prompt',
  583. {},
  584. { body: { 'paywall-type': 'project-sharing' } }
  585. )
  586. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  587. scope: {
  588. project: {
  589. ...project,
  590. publicAccesLevel: 'tokenBased',
  591. features: {
  592. collaborators: 0,
  593. compileGroup: 'standard',
  594. trackChangesVisible: true,
  595. },
  596. },
  597. },
  598. })
  599. await screen.findByText('Add more collaborators')
  600. const user = userEvent.setup()
  601. await user.click(screen.getByTestId('add-collaborator-select'))
  602. const editorOption = screen.getByText('Editor').closest('button')
  603. const reviewerOption = screen.getByText('Reviewer').closest('button')
  604. const viewerOption = screen.getByText('Viewer').closest('button')
  605. expect(editorOption.classList.contains('disabled')).to.be.true
  606. expect(reviewerOption.classList.contains('disabled')).to.be.true
  607. expect(viewerOption.classList.contains('disabled')).to.be.false
  608. screen.getByText(
  609. /Upgrade to add more collaborators and access collaboration features like track changes and full project history/
  610. )
  611. })
  612. it('counts reviewers towards the collaborator limit', async function () {
  613. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  614. scope: {
  615. project: {
  616. ...project,
  617. features: {
  618. collaborators: 1,
  619. trackChangesVisible: true,
  620. },
  621. members: [
  622. {
  623. _id: 'reviewer-id',
  624. email: 'reviewer@example.com',
  625. privileges: 'review',
  626. },
  627. ],
  628. },
  629. },
  630. })
  631. await screen.findByText('Add more collaborators')
  632. const user = userEvent.setup()
  633. await user.click(screen.getByTestId('add-collaborator-select'))
  634. const editorOption = screen.getByText('Editor').closest('button')
  635. const reviewerOption = screen.getByText('Reviewer').closest('button')
  636. const viewerOption = screen.getByText('Viewer').closest('button')
  637. expect(editorOption.classList.contains('disabled')).to.be.true
  638. expect(reviewerOption.classList.contains('disabled')).to.be.true
  639. expect(viewerOption.classList.contains('disabled')).to.be.false
  640. screen.getByText(
  641. /Upgrade to add more collaborators and access collaboration features like track changes and full project history/
  642. )
  643. })
  644. it('handles server error responses', async function () {
  645. fetchMock.get(`/project/${project._id}/tokens`, {})
  646. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  647. scope: {
  648. project: {
  649. ...project,
  650. publicAccesLevel: 'tokenBased',
  651. },
  652. },
  653. })
  654. // loading contacts
  655. await waitFor(() => {
  656. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  657. })
  658. const [inputElement] = await screen.findAllByLabelText('Add people')
  659. const submitButton = screen.getByRole('button', { name: 'Invite' })
  660. const respondWithError = async function (errorReason) {
  661. fireEvent.focus(inputElement)
  662. fireEvent.change(inputElement, {
  663. target: { value: 'invited-author-1@example.com' },
  664. })
  665. fireEvent.blur(inputElement)
  666. fetchMock.postOnce('express:/project/:projectId/invite', {
  667. status: 400,
  668. body: { errorReason },
  669. })
  670. expect(submitButton.disabled).to.be.false
  671. await userEvent.click(submitButton)
  672. await fetchMock.callHistory.flush(true)
  673. expect(fetchMock.callHistory.done()).to.be.true
  674. }
  675. await respondWithError('cannot_invite_non_user')
  676. await screen.findByText(
  677. `Can’t send invite. Recipient must already have an Overleaf account`
  678. )
  679. await respondWithError('cannot_verify_user_not_robot')
  680. await screen.findByText(
  681. `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.`
  682. )
  683. await respondWithError('cannot_invite_self')
  684. await screen.findByText(`Can’t send invite to yourself`)
  685. await respondWithError('invalid_email')
  686. await screen.findByText(`An email address is invalid`)
  687. await respondWithError('too_many_requests')
  688. await screen.findByText(
  689. `Too many requests were received in a short space of time. Please wait for a few moments and try again.`
  690. )
  691. })
  692. it('handles switching between access levels', async function () {
  693. fetchMock.get(`/project/${project._id}/tokens`, {})
  694. fetchMock.post('express:/project/:projectId/settings/admin', 204)
  695. let setPublicAccessLevel = function () {}
  696. function WrappedModal() {
  697. setPublicAccessLevel = useScopeValue('project.publicAccesLevel')[1]
  698. return <ShareProjectModal {...modalProps} />
  699. }
  700. renderWithEditorContext(<WrappedModal />, {
  701. scope: {
  702. project: { ...project, publicAccesLevel: 'private' },
  703. },
  704. })
  705. await screen.findByText('Link sharing is off')
  706. const enableButton = await screen.findByRole('button', {
  707. name: 'Turn on link sharing',
  708. })
  709. fireEvent.click(enableButton)
  710. await waitFor(() => expect(enableButton.disabled).to.be.true)
  711. const { body: tokenBody } = fetchMock.callHistory.calls().at(-1).options
  712. expect(JSON.parse(tokenBody)).to.deep.equal({
  713. publicAccessLevel: 'tokenBased',
  714. })
  715. // NOTE: the project data is usually updated via the websocket connection
  716. // but we can't do that so we're doing it via the scope value store (this
  717. // will be via the project context when this value has been migrated)
  718. setPublicAccessLevel('tokenBased')
  719. await screen.findByText('Link sharing is on')
  720. const disableButton = await screen.findByRole('button', {
  721. name: 'Turn off link sharing',
  722. })
  723. fireEvent.click(disableButton)
  724. await waitFor(() => expect(disableButton.disabled).to.be.true)
  725. const { body: privateBody } = fetchMock.callHistory.calls().at(-1).options
  726. expect(JSON.parse(privateBody)).to.deep.equal({
  727. publicAccessLevel: 'private',
  728. })
  729. setPublicAccessLevel('private')
  730. await screen.findByText('Link sharing is off')
  731. })
  732. it('avoids selecting unmatched contact', async function () {
  733. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  734. scope: { project },
  735. })
  736. const [inputElement] = await screen.findAllByLabelText('Add people')
  737. // Wait for contacts to load
  738. await waitFor(() => {
  739. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  740. })
  741. // Enter a prefix that matches a contact
  742. inputElement.focus()
  743. fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
  744. // The matching contact should now be present and selected
  745. await screen.findByRole('option', {
  746. name: `Claudius Ptolemy <ptolemy@example.com>`,
  747. selected: true,
  748. })
  749. // Keep entering text so the contact no longer matches
  750. fireEvent.change(inputElement, {
  751. target: { value: 'ptolemy.new@example.com' },
  752. })
  753. // The matching contact should no longer be present
  754. expect(
  755. screen.queryByRole('option', {
  756. name: `Claudius Ptolemy <ptolemy@example.com>`,
  757. })
  758. ).to.be.null
  759. // No items should be added yet
  760. expect(screen.queryByRole('button', { name: 'Remove' })).to.be.null
  761. // Pressing Tab should add the entered item
  762. fireEvent.keyDown(inputElement, { key: 'Tab', code: 'Tab' })
  763. await waitFor(() => {
  764. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  765. 1
  766. )
  767. })
  768. // Blurring the input should not add another contact
  769. fireEvent.blur(inputElement)
  770. await waitFor(() => {
  771. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  772. 1
  773. )
  774. })
  775. })
  776. it('selects contact by typing the entire email and blurring the input', async function () {
  777. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  778. scope: { project },
  779. })
  780. const [inputElement] = await screen.findAllByLabelText('Add people')
  781. // Wait for contacts to load
  782. await waitFor(() => {
  783. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  784. })
  785. // Enter a prefix that matches a contact
  786. await userEvent.type(inputElement, 'ptolemy@example.com')
  787. // The matching contact should now be present and selected
  788. await screen.findByRole('option', {
  789. name: `Claudius Ptolemy <ptolemy@example.com>`,
  790. selected: true,
  791. })
  792. // No items should be added yet
  793. expect(screen.queryByRole('button', { name: /Remove/ })).to.be.null
  794. // Click anywhere on the form to blur the input
  795. await userEvent.click(screen.getByRole('dialog'))
  796. // The contact should be added
  797. await waitFor(() => {
  798. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  799. 1
  800. )
  801. })
  802. })
  803. it('selects contact by typing a partial email and selecting the suggestion', async function () {
  804. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  805. scope: { project },
  806. })
  807. const [inputElement] = await screen.findAllByLabelText('Add people')
  808. // Wait for contacts to load
  809. await waitFor(() => {
  810. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  811. })
  812. // Enter a prefix that matches a contact
  813. await userEvent.type(inputElement, 'pto')
  814. // The matching contact should now be present and selected
  815. await userEvent.click(
  816. screen.getByRole('option', {
  817. name: `Claudius Ptolemy <ptolemy@example.com>`,
  818. selected: true,
  819. })
  820. )
  821. // Click anywhere on the form to blur the input
  822. await userEvent.click(screen.getByRole('dialog'))
  823. // The contact should be added
  824. await waitFor(() => {
  825. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  826. 1
  827. )
  828. })
  829. })
  830. it('allows an email address to be selected, removed, then re-added', async function () {
  831. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  832. scope: { project },
  833. })
  834. const [inputElement] = await screen.findAllByLabelText('Add people')
  835. // Wait for contacts to load
  836. await waitFor(() => {
  837. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  838. })
  839. // Enter a prefix that matches a contact
  840. await userEvent.type(inputElement, 'pto')
  841. // Select the suggested contact
  842. await userEvent.click(
  843. screen.getByRole('option', {
  844. name: `Claudius Ptolemy <ptolemy@example.com>`,
  845. selected: true,
  846. })
  847. )
  848. // Click anywhere on the form to blur the input
  849. await userEvent.click(screen.getByRole('dialog'))
  850. // Remove the just-added collaborator
  851. await userEvent.click(screen.getByRole('button', { name: /Remove/ }))
  852. // Remove button should now be gone
  853. expect(screen.queryByRole('button', { name: /Remove/ })).to.be.null
  854. // Add the same collaborator again
  855. await userEvent.type(inputElement, 'pto')
  856. // Click the suggested contact again
  857. await userEvent.click(
  858. screen.getByRole('option', {
  859. name: `Claudius Ptolemy <ptolemy@example.com>`,
  860. selected: true,
  861. })
  862. )
  863. // Click anywhere on the form to blur the input
  864. await userEvent.click(screen.getByRole('dialog'))
  865. // The contact should be added
  866. await waitFor(() => {
  867. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  868. 1
  869. )
  870. })
  871. })
  872. })