share-project-modal.test.tsx 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { screen, Screen, fireEvent, waitFor } from '@testing-library/react'
  4. import fetchMock, { CallLog } 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. makeProjectProvider,
  10. projectDefaults,
  11. USER_EMAIL,
  12. USER_ID,
  13. } from '../../../helpers/editor-providers'
  14. import { location } from '@/shared/components/location'
  15. import { useProjectContext } from '@/shared/context/project-context'
  16. import {
  17. ProjectMember,
  18. ProjectMetadata,
  19. } from '@/shared/context/types/project-metadata'
  20. import { UserId } from '@ol-types/user'
  21. import { PublicAccessLevel } from '@ol-types/public-access-level'
  22. async function changePrivilegeLevel(
  23. screen: Screen,
  24. { current, next }: { current: string; next: string }
  25. ) {
  26. const select = screen.getByDisplayValue(current)
  27. fireEvent.click(select)
  28. const option = screen.getByRole('option', {
  29. name: next,
  30. })
  31. fireEvent.click(option)
  32. }
  33. const testProjectOverrides: Partial<ProjectMetadata> = {
  34. _id: 'test-project',
  35. name: 'Test Project',
  36. features: {
  37. collaborators: 10,
  38. compileGroup: 'standard',
  39. },
  40. owner: {
  41. _id: USER_ID,
  42. email: USER_EMAIL,
  43. first_name: 'Test',
  44. last_name: 'Owner',
  45. privileges: 'owner',
  46. signUpDate: new Date('2025-07-07').toISOString(),
  47. },
  48. }
  49. const shareModalProjectDefaults: ProjectMetadata = Object.assign(
  50. {},
  51. projectDefaults,
  52. testProjectOverrides
  53. )
  54. function createContextProps(projectOverrides?: Partial<ProjectMetadata>) {
  55. const project = Object.assign({}, shareModalProjectDefaults, projectOverrides)
  56. return { providers: { ProjectProvider: makeProjectProvider(project) } }
  57. }
  58. describe('<ShareProjectModal/>', function () {
  59. const contacts = [
  60. // user with edited name
  61. {
  62. type: 'user',
  63. email: 'test-user@example.com',
  64. first_name: 'Test',
  65. last_name: 'User',
  66. name: 'Test User',
  67. },
  68. // user with default name (email prefix)
  69. {
  70. type: 'user',
  71. email: 'test@example.com',
  72. first_name: 'test',
  73. },
  74. // no last name
  75. {
  76. type: 'user',
  77. first_name: 'Eratosthenes',
  78. email: 'eratosthenes@example.com',
  79. },
  80. // more users
  81. {
  82. type: 'user',
  83. first_name: 'Claudius',
  84. last_name: 'Ptolemy',
  85. email: 'ptolemy@example.com',
  86. },
  87. {
  88. type: 'user',
  89. first_name: 'Abd al-Rahman',
  90. last_name: 'Al-Sufi',
  91. email: 'al-sufi@example.com',
  92. },
  93. {
  94. type: 'user',
  95. first_name: 'Nicolaus',
  96. last_name: 'Copernicus',
  97. email: 'copernicus@example.com',
  98. },
  99. ]
  100. const modalProps = {
  101. show: true,
  102. handleHide: sinon.stub(),
  103. handleOpen: sinon.stub(),
  104. }
  105. beforeEach(function () {
  106. this.locationWrapperSandbox = sinon.createSandbox()
  107. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  108. fetchMock.get('/user/contacts', { contacts })
  109. window.metaAttributesCache.set('ol-user', { allowedFreeTrial: true })
  110. window.metaAttributesCache.set('ol-showUpgradePrompt', true)
  111. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  112. })
  113. afterEach(function () {
  114. this.locationWrapperSandbox.restore()
  115. fetchMock.removeRoutes().clearHistory()
  116. })
  117. it('renders the modal', async function () {
  118. renderWithEditorContext(
  119. <ShareProjectModal {...modalProps} />,
  120. createContextProps()
  121. )
  122. await screen.findByText('Share Project')
  123. })
  124. it('calls handleHide when a Close button is pressed', async function () {
  125. const handleHide = sinon.stub()
  126. renderWithEditorContext(
  127. <ShareProjectModal {...modalProps} handleHide={handleHide} />,
  128. createContextProps()
  129. )
  130. const closeButton = screen.getByRole('button', { name: 'Close dialog' })
  131. await userEvent.click(closeButton)
  132. expect(handleHide.callCount).to.equal(1)
  133. })
  134. it('handles access level "private"', async function () {
  135. renderWithEditorContext(
  136. <ShareProjectModal {...modalProps} />,
  137. createContextProps({ publicAccessLevel: 'private' })
  138. )
  139. await screen.findByText('Link sharing is off')
  140. await screen.findByRole('button', { name: 'Turn on link sharing' })
  141. expect(screen.queryByText('Anyone with this link can view this project')).to
  142. .be.null
  143. expect(screen.queryByText('Anyone with this link can edit this project')).to
  144. .be.null
  145. })
  146. it('handles access level "tokenBased"', async function () {
  147. const tokens = {
  148. readAndWrite: '6862414195fwtbrtrdtskb',
  149. readAndWritePrefix: '6862414195',
  150. readOnly: 'wrnjfzkysqkr',
  151. readAndWriteHashPrefix: 'taEVki',
  152. readOnlyHashPrefix: 'j2xYbL',
  153. }
  154. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, tokens)
  155. renderWithEditorContext(
  156. <ShareProjectModal {...modalProps} />,
  157. createContextProps({ publicAccessLevel: 'tokenBased' })
  158. )
  159. await screen.findByText('Link sharing is on')
  160. await screen.findByRole('button', { name: 'Turn off link sharing' })
  161. expect(screen.queryByText('Anyone with this link can view this project'))
  162. .not.to.be.null
  163. expect(screen.queryByText('Anyone with this link can edit this project'))
  164. .not.to.be.null
  165. screen.getByText(
  166. `https://www.test-overleaf.com/${tokens.readAndWrite}#${tokens.readAndWriteHashPrefix}`
  167. )
  168. screen.getByText(
  169. `https://www.test-overleaf.com/read/${tokens.readOnly}#${tokens.readOnlyHashPrefix}`
  170. )
  171. })
  172. it('handles legacy access level "readAndWrite"', async function () {
  173. renderWithEditorContext(
  174. <ShareProjectModal {...modalProps} />,
  175. createContextProps({ publicAccessLevel: 'readAndWrite' })
  176. )
  177. await screen.findByText(
  178. 'This project is public and can be edited by anyone with the URL.'
  179. )
  180. await screen.findByRole('button', { name: 'Make private' })
  181. })
  182. it('handles legacy access level "readOnly"', async function () {
  183. renderWithEditorContext(
  184. <ShareProjectModal {...modalProps} />,
  185. createContextProps({ publicAccessLevel: 'readOnly' })
  186. )
  187. await screen.findByText(
  188. 'This project is public and can be viewed but not edited by anyone with the URL'
  189. )
  190. await screen.findByRole('button', { name: 'Make private' })
  191. })
  192. it('displays actions for project-owners', async function () {
  193. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  194. const invites: ProjectMember[] = [
  195. {
  196. _id: 'invited-author' as UserId,
  197. email: 'invited-author@example.com',
  198. privileges: 'readAndWrite',
  199. first_name: 'Invited',
  200. last_name: 'Author',
  201. },
  202. ]
  203. // render as project owner: actions should be present
  204. renderWithEditorContext(
  205. <ShareProjectModal {...modalProps} />,
  206. createContextProps({ publicAccessLevel: 'tokenBased', invites })
  207. )
  208. await screen.findByRole('button', { name: 'Turn off link sharing' })
  209. await screen.findByRole('button', { name: 'Resend' })
  210. })
  211. it('hides actions from non-project-owners when link sharing on', async function () {
  212. const invites: ProjectMember[] = [
  213. {
  214. _id: 'invited-author' as UserId,
  215. email: 'invited-author@example.com',
  216. privileges: 'readAndWrite',
  217. first_name: 'Invited',
  218. last_name: 'Author',
  219. },
  220. ]
  221. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  222. ...createContextProps({ publicAccessLevel: 'tokenBased', invites }),
  223. user: {
  224. id: 'non-project-owner' as UserId,
  225. email: 'non-project-owner@example.com',
  226. },
  227. })
  228. await screen.findByText(
  229. 'To change access permissions, please ask the project owner'
  230. )
  231. expect(screen.queryByRole('button', { name: 'Turn off link sharing' })).to
  232. .be.null
  233. expect(screen.queryByRole('button', { name: 'Turn on link sharing' })).to.be
  234. .null
  235. expect(screen.queryByRole('button', { name: 'Resend' })).to.be.null
  236. })
  237. it('hides actions from non-project-owners when link sharing off', async function () {
  238. const invites: ProjectMember[] = [
  239. {
  240. _id: 'invited-author' as UserId,
  241. email: 'invited-author@example.com',
  242. privileges: 'readAndWrite',
  243. first_name: 'Invited',
  244. last_name: 'Author',
  245. },
  246. ]
  247. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  248. ...createContextProps({ publicAccessLevel: 'private', invites }),
  249. user: {
  250. id: 'non-project-owner' as UserId,
  251. email: 'non-project-owner@example.com',
  252. },
  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/${shareModalProjectDefaults._id}/tokens`, {})
  266. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  267. ...createContextProps({ publicAccessLevel: 'private' }),
  268. isRestrictedTokenMember: true,
  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: ProjectMember[] = [
  282. {
  283. _id: 'member-author' as UserId,
  284. email: 'member-author@example.com',
  285. privileges: 'readAndWrite',
  286. first_name: 'Member',
  287. last_name: 'Author',
  288. },
  289. {
  290. _id: 'member-viewer' as UserId,
  291. email: 'member-viewer@example.com',
  292. privileges: 'readOnly',
  293. first_name: 'Member',
  294. last_name: 'Viewer',
  295. },
  296. ]
  297. const invites: ProjectMember[] = [
  298. {
  299. _id: 'invited-author' as UserId,
  300. email: 'invited-author@example.com',
  301. privileges: 'readAndWrite',
  302. first_name: 'Invited',
  303. last_name: 'Author',
  304. },
  305. {
  306. _id: 'invited-viewer' as UserId,
  307. email: 'invited-viewer@example.com',
  308. privileges: 'readOnly',
  309. first_name: 'Invited',
  310. last_name: 'Viewer',
  311. },
  312. ]
  313. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  314. renderWithEditorContext(
  315. <ShareProjectModal {...modalProps} />,
  316. createContextProps({ publicAccessLevel: 'tokenBased', members, invites })
  317. )
  318. const projectOwnerEmail = USER_EMAIL
  319. await screen.findByText(projectOwnerEmail)
  320. expect(screen.queryAllByText(projectOwnerEmail)).to.have.length(1)
  321. expect(screen.queryAllByText('member-author@example.com')).to.have.length(1)
  322. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  323. expect(screen.queryAllByText('invited-author@example.com')).to.have.length(
  324. 1
  325. )
  326. expect(screen.queryAllByText('invited-viewer@example.com')).to.have.length(
  327. 1
  328. )
  329. expect(screen.queryAllByText('Invite not yet accepted.')).to.have.length(
  330. invites.length
  331. )
  332. expect(screen.queryAllByRole('button', { name: 'Resend' })).to.have.length(
  333. invites.length
  334. )
  335. })
  336. it('resends an invite', async function () {
  337. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  338. fetchMock.postOnce(
  339. 'express:/project/:projectId/invite/:inviteId/resend',
  340. 204
  341. )
  342. const invites: ProjectMember[] = [
  343. {
  344. _id: 'invited-author' as UserId,
  345. email: 'invited-author@example.com',
  346. privileges: 'readAndWrite',
  347. first_name: 'Invited',
  348. last_name: 'Author',
  349. },
  350. ]
  351. renderWithEditorContext(
  352. <ShareProjectModal {...modalProps} />,
  353. createContextProps({ publicAccessLevel: 'tokenBased', invites })
  354. )
  355. const closeButton = screen.getByRole('button', {
  356. name: 'Close',
  357. })
  358. const resendButton = screen.getByRole('button', { name: 'Resend' })
  359. fireEvent.click(resendButton)
  360. await waitFor(
  361. () => expect((closeButton as HTMLButtonElement).disabled).to.be.true
  362. )
  363. expect(fetchMock.callHistory.done()).to.be.true
  364. await waitFor(
  365. () => expect((closeButton as HTMLButtonElement).disabled).to.be.false
  366. )
  367. })
  368. it('revokes an invite', async function () {
  369. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  370. fetchMock.deleteOnce('express:/project/:projectId/invite/:inviteId', 204)
  371. const invites: ProjectMember[] = [
  372. {
  373. _id: 'invited-author' as UserId,
  374. email: 'invited-author@example.com',
  375. privileges: 'readAndWrite',
  376. first_name: 'Invited',
  377. last_name: 'Author',
  378. },
  379. ]
  380. renderWithEditorContext(
  381. <ShareProjectModal {...modalProps} />,
  382. createContextProps({ publicAccessLevel: 'tokenBased', invites })
  383. )
  384. const closeButton = screen.getByRole('button', {
  385. name: 'Close',
  386. })
  387. const revokeButton = screen.getByRole('button', { name: 'Revoke' })
  388. fireEvent.click(revokeButton)
  389. await waitFor(
  390. () => expect((closeButton as HTMLButtonElement).disabled).to.be.true
  391. )
  392. expect(fetchMock.callHistory.done()).to.be.true
  393. await waitFor(
  394. () => expect((closeButton as HTMLButtonElement).disabled).to.be.false
  395. )
  396. })
  397. it('changes member privileges to read + write', async function () {
  398. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  399. fetchMock.putOnce('express:/project/:projectId/users/:userId', 204)
  400. const members: ProjectMember[] = [
  401. {
  402. _id: 'member-viewer' as UserId,
  403. email: 'member-viewer@example.com',
  404. privileges: 'readOnly',
  405. first_name: 'Member',
  406. last_name: 'Viewer',
  407. },
  408. ]
  409. renderWithEditorContext(
  410. <ShareProjectModal {...modalProps} />,
  411. createContextProps({ publicAccessLevel: 'tokenBased', members })
  412. )
  413. const closeButton = screen.getByRole('button', {
  414. name: 'Close',
  415. })
  416. expect(
  417. await screen.findAllByText('member-viewer@example.com')
  418. ).to.have.length(1)
  419. await changePrivilegeLevel(screen, { current: 'Viewer', next: 'Editor' })
  420. await waitFor(
  421. () => expect((closeButton as HTMLButtonElement).disabled).to.be.true
  422. )
  423. const body = fetchMock.callHistory.calls().at(-1)?.options?.body
  424. expect(JSON.parse(body as string)).to.deep.equal({
  425. privilegeLevel: 'readAndWrite',
  426. })
  427. expect(fetchMock.callHistory.done()).to.be.true
  428. await waitFor(
  429. () => expect((closeButton as HTMLButtonElement).disabled).to.be.false
  430. )
  431. })
  432. it('removes a member from the project', async function () {
  433. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  434. fetchMock.deleteOnce('express:/project/:projectId/users/:userId', 204)
  435. const members: ProjectMember[] = [
  436. {
  437. _id: 'member-viewer' as UserId,
  438. email: 'member-viewer@example.com',
  439. privileges: 'readOnly',
  440. first_name: 'Member',
  441. last_name: 'Viewer',
  442. },
  443. ]
  444. renderWithEditorContext(
  445. <ShareProjectModal {...modalProps} />,
  446. createContextProps({ publicAccessLevel: 'tokenBased', members })
  447. )
  448. expect(
  449. await screen.findAllByText('member-viewer@example.com')
  450. ).to.have.length(1)
  451. await changePrivilegeLevel(screen, {
  452. current: 'Viewer',
  453. next: 'Remove access',
  454. })
  455. const removeButton = screen.getByRole('button', {
  456. name: 'Confirm',
  457. })
  458. fireEvent.click(removeButton)
  459. const url = fetchMock.callHistory.calls().at(-1)?.url
  460. expect(url).to.equal(
  461. 'https://www.test-overleaf.com/project/test-project/users/member-viewer'
  462. )
  463. expect(fetchMock.callHistory.done()).to.be.true
  464. })
  465. it('changes member privileges to owner with confirmation', async function () {
  466. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  467. fetchMock.postOnce('express:/project/:projectId/transfer-ownership', 204)
  468. const members: ProjectMember[] = [
  469. {
  470. _id: 'member-viewer' as UserId,
  471. email: 'member-viewer@example.com',
  472. privileges: 'readOnly',
  473. first_name: 'Member',
  474. last_name: 'Viewer',
  475. },
  476. ]
  477. renderWithEditorContext(
  478. <ShareProjectModal {...modalProps} />,
  479. createContextProps({ publicAccessLevel: 'tokenBased', members })
  480. )
  481. await screen.findByText('member-viewer@example.com')
  482. expect(screen.queryAllByText('member-viewer@example.com')).to.have.length(1)
  483. await changePrivilegeLevel(screen, {
  484. current: 'Viewer',
  485. next: 'Make owner',
  486. })
  487. screen.getByText((_, node) => {
  488. return (
  489. node !== null &&
  490. node.textContent ===
  491. 'Are you sure you want to make member-viewer@example.com the owner of Test Project?'
  492. )
  493. })
  494. const confirmButton: HTMLButtonElement = screen.getByRole('button', {
  495. name: 'Change owner',
  496. })
  497. fireEvent.click(confirmButton)
  498. await waitFor(() => expect(confirmButton.disabled).to.be.true)
  499. const body = fetchMock.callHistory.calls().at(-1)?.options?.body
  500. expect(JSON.parse(body as string)).to.deep.equal({
  501. user_id: 'member-viewer',
  502. })
  503. expect(fetchMock.callHistory.done()).to.be.true
  504. })
  505. it('sends invites to input email addresses', async function () {
  506. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  507. renderWithEditorContext(
  508. <ShareProjectModal {...modalProps} />,
  509. createContextProps({ publicAccessLevel: 'tokenBased' })
  510. )
  511. const [inputElement] = await screen.findAllByLabelText('Add email address')
  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 as { body: string }).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: CallLog[] = []
  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] as { body: string }).body).to.equal(
  565. JSON.stringify({ email: 'test@example.com', privileges: 'readOnly' })
  566. )
  567. expect((calls[1].args[1] as { body: string }).body).to.equal(
  568. JSON.stringify({ email: 'foo@example.com', privileges: 'readOnly' })
  569. )
  570. expect((calls[2].args[1] as { body: string }).body).to.equal(
  571. JSON.stringify({ email: 'bar@example.com', privileges: 'readOnly' })
  572. )
  573. expect((calls[3].args[1] as { body: string }).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/${shareModalProjectDefaults._id}/tokens`, {})
  581. fetchMock.post(
  582. '/event/paywall-prompt',
  583. {},
  584. { body: { 'paywall-type': 'project-sharing' } }
  585. )
  586. renderWithEditorContext(
  587. <ShareProjectModal {...modalProps} />,
  588. createContextProps({
  589. publicAccessLevel: 'tokenBased',
  590. features: {
  591. collaborators: 0,
  592. compileGroup: 'standard',
  593. trackChangesVisible: true,
  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 higher AI allowance, track changes, and full project history/
  608. )
  609. })
  610. it('counts reviewers towards the collaborator limit', async function () {
  611. renderWithEditorContext(
  612. <ShareProjectModal {...modalProps} />,
  613. createContextProps({
  614. features: {
  615. collaborators: 1,
  616. trackChangesVisible: true,
  617. },
  618. members: [
  619. {
  620. _id: 'reviewer-id' as UserId,
  621. email: 'reviewer@example.com',
  622. privileges: 'review',
  623. first_name: 'Reviewer',
  624. last_name: 'LastName',
  625. },
  626. ],
  627. })
  628. )
  629. await screen.findByText('Add more collaborators')
  630. const user = userEvent.setup()
  631. await user.click(screen.getByTestId('add-collaborator-select'))
  632. const editorOption = screen.getByText('Editor').closest('button')
  633. const reviewerOption = screen.getByText('Reviewer').closest('button')
  634. const viewerOption = screen.getByText('Viewer').closest('button')
  635. expect(editorOption?.classList.contains('disabled')).to.be.true
  636. expect(reviewerOption?.classList.contains('disabled')).to.be.true
  637. expect(viewerOption?.classList.contains('disabled')).to.be.false
  638. screen.getByText(
  639. /Upgrade to add more collaborators and access higher AI allowance, track changes, and full project history/
  640. )
  641. })
  642. it('handles server error responses', async function () {
  643. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  644. renderWithEditorContext(
  645. <ShareProjectModal {...modalProps} />,
  646. createContextProps({
  647. publicAccessLevel: 'tokenBased',
  648. })
  649. )
  650. // loading contacts
  651. await waitFor(() => {
  652. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  653. })
  654. const [inputElement] = await screen.findAllByLabelText('Add email address')
  655. const submitButton: HTMLButtonElement = screen.getByRole('button', {
  656. name: 'Invite',
  657. })
  658. const respondWithError = async function (errorReason: string) {
  659. fireEvent.focus(inputElement)
  660. fireEvent.change(inputElement, {
  661. target: { value: 'invited-author-1@example.com' },
  662. })
  663. fireEvent.blur(inputElement)
  664. fetchMock.postOnce('express:/project/:projectId/invite', {
  665. status: 400,
  666. body: { errorReason },
  667. })
  668. expect(submitButton.disabled).to.be.false
  669. await userEvent.click(submitButton)
  670. await fetchMock.callHistory.flush(true)
  671. expect(fetchMock.callHistory.done()).to.be.true
  672. }
  673. await respondWithError('cannot_invite_non_user')
  674. await screen.findByText(
  675. `Can’t send invite. Recipient must already have an Overleaf account`
  676. )
  677. await respondWithError('cannot_verify_user_not_robot')
  678. await screen.findByText(
  679. `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.`
  680. )
  681. await respondWithError('cannot_invite_self')
  682. await screen.findByText(`Can’t send invite to yourself`)
  683. await respondWithError('invalid_email')
  684. await screen.findByText(`An email address is invalid`)
  685. await respondWithError('too_many_requests')
  686. await screen.findByText(
  687. `Too many requests were received in a short space of time. Please wait for a few moments and try again.`
  688. )
  689. })
  690. it('handles switching between access levels', async function () {
  691. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  692. fetchMock.post('express:/project/:projectId/settings/admin', 204)
  693. let setPublicAccessLevel = function (_: PublicAccessLevel) {}
  694. function WrappedModal() {
  695. const { updateProject } = useProjectContext()
  696. setPublicAccessLevel = (publicAccessLevel: PublicAccessLevel) => {
  697. updateProject({ publicAccessLevel })
  698. }
  699. return <ShareProjectModal {...modalProps} />
  700. }
  701. renderWithEditorContext(
  702. <WrappedModal />,
  703. createContextProps({
  704. publicAccessLevel: 'private',
  705. })
  706. )
  707. await screen.findByText('Link sharing is off')
  708. const enableButton: HTMLButtonElement = await screen.findByRole('button', {
  709. name: 'Turn on link sharing',
  710. })
  711. fireEvent.click(enableButton)
  712. await waitFor(() => expect(enableButton.disabled).to.be.true)
  713. const tokenBody = fetchMock.callHistory.calls().at(-1)?.options.body
  714. expect(JSON.parse(tokenBody as string)).to.deep.equal({
  715. publicAccessLevel: 'tokenBased',
  716. })
  717. // NOTE: the project data is usually updated via the websocket connection
  718. // but we can't do that so we're doing it via the project context, which is
  719. // exposed in a hacky way here
  720. setPublicAccessLevel('tokenBased')
  721. await screen.findByText('Link sharing is on')
  722. const disableButton: HTMLButtonElement = await screen.findByRole('button', {
  723. name: 'Turn off link sharing',
  724. })
  725. fireEvent.click(disableButton)
  726. await waitFor(() => expect(disableButton.disabled).to.be.true)
  727. const privateBody = fetchMock.callHistory.calls().at(-1)?.options.body
  728. expect(JSON.parse(privateBody as string)).to.deep.equal({
  729. publicAccessLevel: 'private',
  730. })
  731. setPublicAccessLevel('private')
  732. await screen.findByText('Link sharing is off')
  733. })
  734. it('avoids selecting unmatched contact', async function () {
  735. renderWithEditorContext(
  736. <ShareProjectModal {...modalProps} />,
  737. createContextProps()
  738. )
  739. const [inputElement] = await screen.findAllByLabelText('Add email address')
  740. // Wait for contacts to load
  741. await waitFor(() => {
  742. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  743. })
  744. // Enter a prefix that matches a contact
  745. inputElement.focus()
  746. fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
  747. // The matching contact should now be present and selected
  748. await screen.findByRole('option', {
  749. name: `Claudius Ptolemy <ptolemy@example.com>`,
  750. selected: true,
  751. })
  752. // Keep entering text so the contact no longer matches
  753. fireEvent.change(inputElement, {
  754. target: { value: 'ptolemy.new@example.com' },
  755. })
  756. // The matching contact should no longer be present
  757. expect(
  758. screen.queryByRole('option', {
  759. name: `Claudius Ptolemy <ptolemy@example.com>`,
  760. })
  761. ).to.be.null
  762. // No items should be added yet
  763. expect(screen.queryByRole('button', { name: 'Remove' })).to.be.null
  764. // Pressing Tab should add the entered item
  765. fireEvent.keyDown(inputElement, { key: 'Tab', code: 'Tab' })
  766. const collaborators = await waitFor(() => {
  767. return screen.getAllByRole('button', { name: /Remove/ })
  768. })
  769. expect(collaborators).to.not.be.null
  770. // Blurring the input should not add another contact
  771. fireEvent.blur(inputElement)
  772. await waitFor(() => {
  773. expect(screen.getAllByRole('button', { name: /Remove/ }).length).to.eql(
  774. collaborators.length
  775. )
  776. })
  777. })
  778. it('selects contact by typing the entire email and blurring the input', async function () {
  779. renderWithEditorContext(
  780. <ShareProjectModal {...modalProps} />,
  781. createContextProps()
  782. )
  783. const [inputElement] = await screen.findAllByLabelText('Add email address')
  784. // Wait for contacts to load
  785. await waitFor(() => {
  786. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  787. })
  788. // Enter a prefix that matches a contact
  789. await userEvent.type(inputElement, 'ptolemy@example.com')
  790. // The matching contact should now be present and selected
  791. await screen.findByRole('option', {
  792. name: `Claudius Ptolemy <ptolemy@example.com>`,
  793. selected: true,
  794. })
  795. // No items should be added yet
  796. expect(screen.queryByRole('button', { name: /Remove/ })).to.be.null
  797. // Click anywhere on the form to blur the input
  798. await userEvent.click(screen.getByRole('dialog'))
  799. // The contact should be added
  800. await waitFor(() => {
  801. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  802. 1
  803. )
  804. })
  805. })
  806. it('selects contact by typing a partial email and selecting the suggestion', async function () {
  807. renderWithEditorContext(
  808. <ShareProjectModal {...modalProps} />,
  809. createContextProps()
  810. )
  811. const [inputElement] = await screen.findAllByLabelText('Add email address')
  812. // Wait for contacts to load
  813. await waitFor(() => {
  814. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  815. })
  816. // Enter a prefix that matches a contact
  817. await userEvent.type(inputElement, 'pto')
  818. // The matching contact should now be present and selected
  819. await userEvent.click(
  820. screen.getByRole('option', {
  821. name: `Claudius Ptolemy <ptolemy@example.com>`,
  822. selected: true,
  823. })
  824. )
  825. // Click anywhere on the form to blur the input
  826. await userEvent.click(screen.getByRole('dialog'))
  827. // The contact should be added
  828. await waitFor(() => {
  829. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  830. 1
  831. )
  832. })
  833. })
  834. it('re-selects the same suggestion after removing it', async function () {
  835. renderWithEditorContext(
  836. <ShareProjectModal {...modalProps} />,
  837. createContextProps()
  838. )
  839. const [inputElement] = await screen.findAllByLabelText('Add email address')
  840. await waitFor(() => {
  841. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  842. })
  843. await userEvent.type(inputElement, 'pto')
  844. await userEvent.click(
  845. screen.getByRole('option', {
  846. name: `Claudius Ptolemy <ptolemy@example.com>`,
  847. })
  848. )
  849. const removeButton = await screen.findByRole('button', { name: /Remove/ })
  850. await userEvent.click(removeButton)
  851. await waitFor(() => {
  852. expect(screen.queryByRole('button', { name: /remove/i })).to.be.null
  853. })
  854. await userEvent.click(inputElement)
  855. await userEvent.click(
  856. await screen.findByRole('option', {
  857. name: `Claudius Ptolemy <ptolemy@example.com>`,
  858. })
  859. )
  860. await waitFor(() => {
  861. expect(screen.getAllByRole('button', { name: /remove/i })).to.have.length(
  862. 1
  863. )
  864. })
  865. })
  866. describe('sharing-updates feature flag enabled', function () {
  867. beforeEach(function () {
  868. window.metaAttributesCache.set('ol-splitTestVariants', {
  869. 'sharing-updates': 'enabled',
  870. })
  871. })
  872. afterEach(function () {
  873. window.metaAttributesCache.delete('ol-splitTestVariants')
  874. })
  875. it('sets "Via sharing links (legacy)" when `publicAccessLevel` is `tokenBased`', async function () {
  876. fetchMock.get(`/project/${shareModalProjectDefaults._id}/tokens`, {})
  877. renderWithEditorContext(
  878. <ShareProjectModal {...modalProps} />,
  879. createContextProps({ publicAccessLevel: 'tokenBased' })
  880. )
  881. await screen.findByText('Via sharing links (legacy)')
  882. })
  883. it('sets "Only invited people" when sharing-link returns 404', async function () {
  884. fetchMock.get(
  885. `/project/${shareModalProjectDefaults._id}/sharing-link`,
  886. 404
  887. )
  888. renderWithEditorContext(
  889. <ShareProjectModal {...modalProps} />,
  890. createContextProps({ publicAccessLevel: 'private' })
  891. )
  892. await screen.findByText('Only invited people')
  893. })
  894. it('sets "Anyone with the link" when sharing-link returns a link without `subscriptionId`', async function () {
  895. fetchMock.get(`/project/${shareModalProjectDefaults._id}/sharing-link`, {
  896. _id: 'link-id',
  897. token: 'abc123',
  898. privileges: 'readOnly',
  899. })
  900. renderWithEditorContext(
  901. <ShareProjectModal {...modalProps} />,
  902. createContextProps({ publicAccessLevel: 'private' })
  903. )
  904. await screen.findByText('Anyone with the link')
  905. })
  906. it('sets "Anyone in your group with the link" when sharing-link returns a link with subscriptionId', async function () {
  907. fetchMock.get(`/project/${shareModalProjectDefaults._id}/sharing-link`, {
  908. _id: 'link-id',
  909. token: 'abc123',
  910. privileges: 'readOnly',
  911. subscriptionId: 'sub-123',
  912. })
  913. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  914. ...createContextProps({ publicAccessLevel: 'private' }),
  915. user: {
  916. id: USER_ID,
  917. email: USER_EMAIL,
  918. activeProfessionalGroupSubscriptions: [{ _id: 'sub-123' }],
  919. },
  920. })
  921. await screen.findByText('Anyone in your group with the link')
  922. })
  923. describe('invited people count', function () {
  924. beforeEach(function () {
  925. fetchMock.get(
  926. `/project/${shareModalProjectDefaults._id}/sharing-link`,
  927. 404
  928. )
  929. })
  930. it('shows "No one invited yet" when the owner is the only person', async function () {
  931. renderWithEditorContext(
  932. <ShareProjectModal {...modalProps} />,
  933. createContextProps({ publicAccessLevel: 'private' })
  934. )
  935. await screen.findByText('No one invited yet')
  936. expect(screen.queryByText('1 person invited')).to.be.null
  937. })
  938. it('shows the invited people count, including the owner, when there are collaborators', async function () {
  939. const members: ProjectMember[] = [
  940. {
  941. _id: 'member-author' as UserId,
  942. email: 'member-author@example.com',
  943. privileges: 'readAndWrite',
  944. first_name: 'Member',
  945. last_name: 'Author',
  946. },
  947. {
  948. _id: 'member-viewer' as UserId,
  949. email: 'member-viewer@example.com',
  950. privileges: 'readOnly',
  951. first_name: 'Member',
  952. last_name: 'Viewer',
  953. },
  954. ]
  955. renderWithEditorContext(
  956. <ShareProjectModal {...modalProps} />,
  957. createContextProps({ publicAccessLevel: 'private', members })
  958. )
  959. await screen.findByText('3 people invited')
  960. expect(screen.queryByText('No one invited yet')).to.be.null
  961. })
  962. })
  963. describe('copy link button', function () {
  964. let clipboardWriteTextStub: sinon.SinonStub
  965. beforeEach(function () {
  966. clipboardWriteTextStub = sinon.stub().resolves()
  967. Object.defineProperty(navigator, 'clipboard', {
  968. value: { writeText: clipboardWriteTextStub },
  969. configurable: true,
  970. writable: true,
  971. })
  972. })
  973. afterEach(function () {
  974. window.metaAttributesCache.delete('ol-splitTestVariants')
  975. delete (navigator as any).clipboard
  976. })
  977. it('shows a disabled copy sharing link button when access is "Only invited people"', async function () {
  978. fetchMock.get('express:/project/:projectId/sharing-link', 404)
  979. renderWithEditorContext(
  980. <ShareProjectModal {...modalProps} />,
  981. createContextProps()
  982. )
  983. const copyButton: HTMLButtonElement = await screen.findByRole(
  984. 'button',
  985. {
  986. name: /copy sharing link/i,
  987. }
  988. )
  989. expect(copyButton.disabled).to.be.true
  990. })
  991. it('enables the copy sharing link button when access is "Anyone with the link" and copies the correct URL on click', async function () {
  992. const sharingLinkToken = 'abc123token'
  993. fetchMock.get('express:/project/:projectId/sharing-link', {
  994. _id: 'invite-id',
  995. token: sharingLinkToken,
  996. privileges: 'readAndWrite',
  997. })
  998. renderWithEditorContext(
  999. <ShareProjectModal {...modalProps} />,
  1000. createContextProps()
  1001. )
  1002. const copyButton: HTMLButtonElement = await screen.findByRole(
  1003. 'button',
  1004. {
  1005. name: /copy sharing link/i,
  1006. }
  1007. )
  1008. expect(copyButton.disabled).to.be.false
  1009. await userEvent.click(copyButton)
  1010. expect(clipboardWriteTextStub.calledOnce).to.be.true
  1011. expect(clipboardWriteTextStub.firstCall.args[0]).to.equal(
  1012. `${window.location.origin}/project/${shareModalProjectDefaults._id}/share#${sharingLinkToken}`
  1013. )
  1014. await screen.findByText(/link copied/i)
  1015. })
  1016. })
  1017. })
  1018. it('allows an email address to be selected, removed, then re-added', async function () {
  1019. renderWithEditorContext(
  1020. <ShareProjectModal {...modalProps} />,
  1021. createContextProps()
  1022. )
  1023. const [inputElement] = await screen.findAllByLabelText('Add email address')
  1024. // Wait for contacts to load
  1025. await waitFor(() => {
  1026. expect(fetchMock.callHistory.called('express:/user/contacts')).to.be.true
  1027. })
  1028. // Enter a prefix that matches a contact
  1029. await userEvent.type(inputElement, 'pto')
  1030. // Select the suggested contact
  1031. await userEvent.click(
  1032. screen.getByRole('option', {
  1033. name: `Claudius Ptolemy <ptolemy@example.com>`,
  1034. selected: true,
  1035. })
  1036. )
  1037. // Click anywhere on the form to blur the input
  1038. await userEvent.click(screen.getByRole('dialog'))
  1039. // Remove the just-added collaborator
  1040. await userEvent.click(screen.getByRole('button', { name: /Remove/ }))
  1041. // Remove button should now be gone
  1042. expect(screen.queryByRole('button', { name: /Remove/ })).to.be.null
  1043. // Add the same collaborator again
  1044. await userEvent.type(inputElement, 'pto')
  1045. // Click the suggested contact again
  1046. await userEvent.click(
  1047. screen.getByRole('option', {
  1048. name: `Claudius Ptolemy <ptolemy@example.com>`,
  1049. selected: true,
  1050. })
  1051. )
  1052. // Click anywhere on the form to blur the input
  1053. await userEvent.click(screen.getByRole('dialog'))
  1054. // The contact should be added
  1055. await waitFor(() => {
  1056. expect(screen.getAllByRole('button', { name: /Remove/ })).to.have.length(
  1057. 1
  1058. )
  1059. })
  1060. })
  1061. it('does not show the "Give feedback" link when the "sharing-updates" feature flag is disabled', async function () {
  1062. renderWithEditorContext(
  1063. <ShareProjectModal {...modalProps} />,
  1064. createContextProps()
  1065. )
  1066. expect(screen.queryByRole('link', { name: 'Give feedback' })).to.be.null
  1067. })
  1068. describe('with "sharing-updates" feature flag', function () {
  1069. beforeEach(function () {
  1070. window.metaAttributesCache.set('ol-splitTestVariants', {
  1071. 'sharing-updates': 'enabled',
  1072. })
  1073. })
  1074. afterEach(function () {
  1075. window.metaAttributesCache.set('ol-splitTestVariants', {})
  1076. window.metaAttributesCache.set('ol-splitTestInfo', {})
  1077. delete (navigator as any).clipboard
  1078. })
  1079. it('disables the invite button when no email is entered', async function () {
  1080. renderWithEditorContext(
  1081. <ShareProjectModal {...modalProps} />,
  1082. createContextProps({ publicAccessLevel: 'tokenBased' })
  1083. )
  1084. const inviteButton = (await screen.findByRole('button', {
  1085. name: /invite/i,
  1086. })) as HTMLButtonElement
  1087. expect(inviteButton.disabled).to.be.true
  1088. })
  1089. it('enables the invite button once a valid email is typed', async function () {
  1090. renderWithEditorContext(
  1091. <ShareProjectModal {...modalProps} />,
  1092. createContextProps({ publicAccessLevel: 'tokenBased' })
  1093. )
  1094. const inviteButton = (await screen.findByRole('button', {
  1095. name: /invite/i,
  1096. })) as HTMLButtonElement
  1097. expect(inviteButton.disabled).to.be.true
  1098. const inputElement = await screen.findByTestId('collaborator-email-input')
  1099. fireEvent.change(inputElement, {
  1100. target: { value: 'new@example.com' },
  1101. })
  1102. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1103. })
  1104. it('shows a validation error and disables invite button for an invalid email format', async function () {
  1105. renderWithEditorContext(
  1106. <ShareProjectModal {...modalProps} />,
  1107. createContextProps({ publicAccessLevel: 'tokenBased' })
  1108. )
  1109. const inputElement = await screen.findByTestId('collaborator-email-input')
  1110. fireEvent.change(inputElement, { target: { value: 'invalid@' } })
  1111. fireEvent.blur(inputElement)
  1112. await screen.findByText('Email addresses must be a valid format.')
  1113. const inviteButton = screen.getByRole('button', {
  1114. name: /invite/i,
  1115. }) as HTMLButtonElement
  1116. expect(inviteButton.disabled).to.be.true
  1117. })
  1118. it('shows a validation error and disables invite button when email already has access', async function () {
  1119. const members: ProjectMember[] = [
  1120. {
  1121. _id: 'existing-member' as UserId,
  1122. email: 'member@example.com',
  1123. privileges: 'readAndWrite',
  1124. first_name: 'Existing',
  1125. last_name: 'Member',
  1126. },
  1127. ]
  1128. renderWithEditorContext(
  1129. <ShareProjectModal {...modalProps} />,
  1130. createContextProps({ publicAccessLevel: 'tokenBased', members })
  1131. )
  1132. const inputElement = await screen.findByTestId('collaborator-email-input')
  1133. fireEvent.change(inputElement, {
  1134. target: { value: 'member@example.com' },
  1135. })
  1136. fireEvent.blur(inputElement)
  1137. await screen.findByText('Only add people who don’t yet have access.')
  1138. const inviteButton = screen.getByRole('button', {
  1139. name: /invite/i,
  1140. }) as HTMLButtonElement
  1141. expect(inviteButton.disabled).to.be.true
  1142. })
  1143. it('shows "invitations sent" message after a successful invite', async function () {
  1144. fetchMock.post('express:/project/:projectId/invite', {
  1145. status: 200,
  1146. body: {
  1147. invite: {
  1148. _id: 'new-invite',
  1149. email: 'new@example.com',
  1150. privileges: 'readAndWrite',
  1151. },
  1152. },
  1153. })
  1154. renderWithEditorContext(
  1155. <ShareProjectModal {...modalProps} />,
  1156. createContextProps({ publicAccessLevel: 'tokenBased' })
  1157. )
  1158. const inputElement = await screen.findByTestId('collaborator-email-input')
  1159. fireEvent.change(inputElement, { target: { value: 'new@example.com' } })
  1160. fireEvent.blur(inputElement)
  1161. const inviteButton = (await screen.findByRole('button', {
  1162. name: /invite/i,
  1163. })) as HTMLButtonElement
  1164. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1165. await userEvent.click(inviteButton)
  1166. await screen.findByText('Invitation(s) sent.')
  1167. })
  1168. it('shows a generic error and no success message when an invite fails (e.g. collaborator limit reached)', async function () {
  1169. fetchMock.post('express:/project/:projectId/invite', {
  1170. status: 200,
  1171. body: {
  1172. invite: null,
  1173. },
  1174. })
  1175. renderWithEditorContext(
  1176. <ShareProjectModal {...modalProps} />,
  1177. createContextProps({ publicAccessLevel: 'tokenBased' })
  1178. )
  1179. const inputElement = await screen.findByTestId('collaborator-email-input')
  1180. fireEvent.change(inputElement, { target: { value: 'new@example.com' } })
  1181. fireEvent.blur(inputElement)
  1182. const inviteButton = (await screen.findByRole('button', {
  1183. name: /invite/i,
  1184. })) as HTMLButtonElement
  1185. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1186. await userEvent.click(inviteButton)
  1187. await screen.findByText('Sorry, something went wrong')
  1188. expect(screen.queryByText('Invitation(s) sent.')).to.be.null
  1189. })
  1190. it('clears "successActionMessage" when invitations are sent', async function () {
  1191. Object.defineProperty(navigator, 'clipboard', {
  1192. value: { writeText: sinon.stub().resolves() },
  1193. configurable: true,
  1194. writable: true,
  1195. })
  1196. const sharingLinkToken = 'abc123token'
  1197. fetchMock.get('express:/project/:projectId/sharing-link', {
  1198. _id: 'invite-id',
  1199. token: sharingLinkToken,
  1200. privileges: 'readAndWrite',
  1201. })
  1202. fetchMock.post('express:/project/:projectId/invite', {
  1203. status: 200,
  1204. body: {
  1205. invite: {
  1206. _id: 'new-invite',
  1207. email: 'new@example.com',
  1208. privileges: 'readAndWrite',
  1209. },
  1210. },
  1211. })
  1212. renderWithEditorContext(
  1213. <ShareProjectModal {...modalProps} />,
  1214. createContextProps()
  1215. )
  1216. const copyButton: HTMLButtonElement = await screen.findByRole('button', {
  1217. name: /copy sharing link/i,
  1218. })
  1219. expect(copyButton.disabled).to.be.false
  1220. await userEvent.click(copyButton)
  1221. await screen.findByText(/link copied/i)
  1222. const inputElement = screen.getByTestId('collaborator-email-input')
  1223. fireEvent.change(inputElement, { target: { value: 'new@example.com' } })
  1224. fireEvent.blur(inputElement)
  1225. const inviteButton = (await screen.findByRole('button', {
  1226. name: /invite/i,
  1227. })) as HTMLButtonElement
  1228. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1229. await userEvent.click(inviteButton)
  1230. await screen.findByText('Invitation(s) sent.')
  1231. expect(screen.queryByText(/link copied/i)).to.be.null
  1232. })
  1233. it('clears "invitations sent" message when input is changed', async function () {
  1234. fetchMock.post('express:/project/:projectId/invite', {
  1235. status: 200,
  1236. body: {
  1237. invite: {
  1238. _id: 'new-invite',
  1239. email: 'new@example.com',
  1240. privileges: 'readAndWrite',
  1241. },
  1242. },
  1243. })
  1244. renderWithEditorContext(
  1245. <ShareProjectModal {...modalProps} />,
  1246. createContextProps()
  1247. )
  1248. const inputElement = await screen.findByTestId('collaborator-email-input')
  1249. fireEvent.change(inputElement, { target: { value: 'new@example.com' } })
  1250. fireEvent.blur(inputElement)
  1251. const inviteButton = (await screen.findByRole('button', {
  1252. name: /invite/i,
  1253. })) as HTMLButtonElement
  1254. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1255. await userEvent.click(inviteButton)
  1256. await screen.findByText('Invitation(s) sent.')
  1257. fireEvent.change(inputElement, { target: { value: 'a' } })
  1258. await waitFor(
  1259. () => expect(screen.queryByText('Invitation(s) sent.')).to.be.null
  1260. )
  1261. })
  1262. it('clears "invitations sent" message when a selected item is removed', async function () {
  1263. fetchMock.post('express:/project/:projectId/invite', {
  1264. status: 200,
  1265. body: {
  1266. invite: {
  1267. _id: 'new-invite',
  1268. email: 'new@example.com',
  1269. privileges: 'readAndWrite',
  1270. },
  1271. },
  1272. })
  1273. renderWithEditorContext(
  1274. <ShareProjectModal {...modalProps} />,
  1275. createContextProps()
  1276. )
  1277. const inputElement = await screen.findByTestId('collaborator-email-input')
  1278. fireEvent.change(inputElement, { target: { value: 'new@example.com' } })
  1279. fireEvent.blur(inputElement)
  1280. const inviteButton = (await screen.findByRole('button', {
  1281. name: /invite/i,
  1282. })) as HTMLButtonElement
  1283. await waitFor(() => expect(inviteButton.disabled).to.be.false)
  1284. await userEvent.click(inviteButton)
  1285. await screen.findByText('Invitation(s) sent.')
  1286. fireEvent.change(inputElement, {
  1287. target: { value: 'another@example.com' },
  1288. })
  1289. fireEvent.blur(inputElement)
  1290. const removeButton = await screen.findByRole('button', {
  1291. name: /remove/i,
  1292. })
  1293. await userEvent.click(removeButton)
  1294. await waitFor(
  1295. () => expect(screen.queryByText('Invitation(s) sent.')).to.be.null
  1296. )
  1297. })
  1298. it('shows the "Give feedback" link for the project owner', async function () {
  1299. renderWithEditorContext(
  1300. <ShareProjectModal {...modalProps} />,
  1301. createContextProps()
  1302. )
  1303. await screen.findByRole('link', { name: 'Give feedback' })
  1304. })
  1305. it('does not show the "Give feedback" link for non-owners', async function () {
  1306. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  1307. ...createContextProps(),
  1308. user: {
  1309. id: 'non-project-owner' as UserId,
  1310. email: 'non-project-owner@example.com',
  1311. },
  1312. })
  1313. expect(screen.queryByRole('link', { name: 'Give feedback' })).to.be.null
  1314. })
  1315. describe('"Give feedback" link URL based on subscription plan', function () {
  1316. it('links to the professional feedback URL when the user has a professional group plan', async function () {
  1317. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  1318. ...createContextProps(),
  1319. user: {
  1320. id: USER_ID,
  1321. email: USER_EMAIL,
  1322. isProfessionalGroupPlan: true,
  1323. },
  1324. })
  1325. const feedbackLink = await screen.findByRole('link', {
  1326. name: 'Give feedback',
  1327. })
  1328. expect(feedbackLink.getAttribute('href')).to.equal(
  1329. 'https://forms.gle/rz1JDMuNajWG4ZY49'
  1330. )
  1331. })
  1332. it('links to the standard feedback URL when the user does not have a professional group plan', async function () {
  1333. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  1334. ...createContextProps(),
  1335. user: {
  1336. id: USER_ID,
  1337. email: USER_EMAIL,
  1338. isProfessionalGroupPlan: false,
  1339. },
  1340. })
  1341. const feedbackLink = await screen.findByRole('link', {
  1342. name: 'Give feedback',
  1343. })
  1344. expect(feedbackLink.getAttribute('href')).to.equal(
  1345. 'https://forms.gle/WLEjzG4Ayp8zFscM9'
  1346. )
  1347. })
  1348. it('links to the Labs feedback URL when the "sharing-updates" feature is in the Labs phase', async function () {
  1349. window.metaAttributesCache.set('ol-splitTestInfo', {
  1350. 'sharing-updates': { phase: 'labs' },
  1351. })
  1352. renderWithEditorContext(<ShareProjectModal {...modalProps} />, {
  1353. ...createContextProps(),
  1354. user: {
  1355. id: USER_ID,
  1356. email: USER_EMAIL,
  1357. isProfessionalGroupPlan: false,
  1358. },
  1359. })
  1360. const feedbackLink = await screen.findByRole('link', {
  1361. name: 'Give feedback',
  1362. })
  1363. expect(feedbackLink.getAttribute('href')).to.equal(
  1364. 'https://docs.google.com/forms/d/e/1FAIpQLSeOsPzSw8lWLY310ZvR7BCK08v3Puc4JWFdV6K3m9QbsL2OSw/viewform'
  1365. )
  1366. })
  1367. })
  1368. })
  1369. })