share-project-modal.test.jsx 30 KB

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