project-sharing.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. import { v4 as uuid } from 'uuid'
  2. import {
  3. isExcludedBySharding,
  4. startWith,
  5. reloadWith,
  6. STARTUP_TIMEOUT,
  7. } from './helpers/config'
  8. import { ensureUserExists, login } from './helpers/login'
  9. import {
  10. createProjectAndOpenInNewEditor,
  11. enableLinkSharing,
  12. getSpamSafeProjectName,
  13. openProjectByName,
  14. openProjectViaLinkSharingAsAnon,
  15. openProjectViaLinkSharingAsUser,
  16. shareProjectByEmailAndAcceptInviteViaDash,
  17. shareProjectByEmailAndAcceptInviteViaEmail,
  18. } from './helpers/project'
  19. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  20. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  21. describe('Project Sharing', function () {
  22. if (isExcludedBySharding('PRO_CUSTOM_4')) return
  23. ensureUserExists({ email: 'user@example.com' })
  24. startWith({ withDataDir: true, pro: true })
  25. let projectName: string
  26. let recompile: () => void
  27. let waitForCompile: (triggerCompile: () => void) => void
  28. beforeWithReRunOnTestRetry(() => {
  29. projectName = getSpamSafeProjectName()
  30. ;({ recompile, waitForCompile } = prepareWaitForNextCompileSlot())
  31. setupTestProject()
  32. })
  33. beforeEach(function () {
  34. // Always start with a fresh session
  35. cy.session([uuid()], () => {})
  36. })
  37. let linkSharingReadOnly: string
  38. let linkSharingReadAndWrite: string
  39. function setupTestProject() {
  40. login('user@example.com')
  41. waitForCompile(() => {
  42. createProjectAndOpenInNewEditor(projectName)
  43. })
  44. // Add chat message
  45. cy.findByRole('tab', { name: 'Chat' }).click()
  46. // wait for lazy loading of the chat pane
  47. cy.findByText('Start the conversation by saying hello or sharing an update')
  48. cy.get(
  49. 'textarea[placeholder="Send a message to your collaborators…"]'
  50. ).type('New Chat Message{enter}')
  51. // Get link sharing links
  52. enableLinkSharing().then(
  53. ({ linkSharingReadOnly: ro, linkSharingReadAndWrite: rw }) => {
  54. linkSharingReadAndWrite = rw
  55. linkSharingReadOnly = ro
  56. }
  57. )
  58. }
  59. function expectContentReadOnlyAccess() {
  60. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  61. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  62. 'contain.text',
  63. '\\maketitle'
  64. )
  65. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  66. 'have.attr',
  67. 'contenteditable',
  68. 'false'
  69. )
  70. }
  71. function expectContentWriteAccess() {
  72. const section = `Test Section ${uuid()}`
  73. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  74. // wait for the editor to finish loading
  75. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  76. 'contain.text',
  77. '\\maketitle'
  78. )
  79. // the editor should be writable
  80. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  81. 'have.attr',
  82. 'contenteditable',
  83. 'true'
  84. )
  85. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() => {
  86. cy.findByText('\\maketitle').parent().click()
  87. cy.findByText('\\maketitle').parent().type(`\n\\section{{}${section}}`)
  88. })
  89. // should have written
  90. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  91. 'contain.text',
  92. `\\section{${section}}`
  93. )
  94. // check PDF
  95. recompile()
  96. cy.findByRole('region', { name: 'PDF preview' }).within(() => {
  97. cy.findByLabelText(/Page.*1/i).should('be.visible')
  98. cy.findByText(projectName).should('be.visible')
  99. })
  100. cy.findByRole('region', { name: 'PDF preview' }).within(() => {
  101. cy.findByLabelText(/Page.*1/i).should('be.visible')
  102. cy.contains(section)
  103. })
  104. }
  105. function expectNoAccess() {
  106. // try read only access link
  107. cy.visit(linkSharingReadOnly)
  108. cy.url().should('match', /\/login/)
  109. // Cypress bugs: cypress resolves the link-sharing link outside the browser, and it carries over the hash of the link-sharing link to the login page redirect (bug 1).
  110. // Effectively, cypress then instructs the browser to change the page from /login#read-only-hash to /login#read-and-write-hash.
  111. // This is turn does not trigger a "page load", but rather just "scrolling", which in turn trips up the "page loaded" detection in cypress (bug 2).
  112. // Work around this by navigating away from the /login page in between checks.
  113. cy.visit('/user/password/reset')
  114. // try read and write access link
  115. cy.visit(linkSharingReadAndWrite)
  116. cy.url().should('match', /\/login/)
  117. }
  118. function expectChatAccess() {
  119. cy.findByRole('tab', { name: 'Chat' }).click()
  120. cy.findByText('New Chat Message')
  121. }
  122. function expectHistoryAccess() {
  123. cy.findByRole('button', { name: 'History' }).click()
  124. // The input is not clickable due to being visually hidden, click its label instead
  125. cy.findByRole('complementary', {
  126. name: 'Project history and labels',
  127. }).within(() => {
  128. cy.findByRole('group', {
  129. name: 'Show all of the project history or only labelled versions.',
  130. }).within(() => {
  131. cy.findByText('All history').click()
  132. })
  133. cy.findByRole('radio', { name: 'Labels' }).should('not.be.checked')
  134. cy.findByRole('radio', { name: 'All history' }).should('be.checked')
  135. })
  136. cy.findByText(/\\begin\{document}/)
  137. cy.findByRole('complementary', {
  138. name: 'Project history and labels',
  139. }).within(() => {
  140. cy.findAllByTestId('history-version-metadata-users')
  141. .last()
  142. .should('have.text', 'user')
  143. })
  144. cy.findByRole('button', { name: 'Back to editor' }).click()
  145. }
  146. function expectNoChatAccess() {
  147. cy.findByRole('button', { name: 'Layout options' }) // wait for lazy loading
  148. cy.findByRole('tab', { name: 'Chat' }).should('not.exist')
  149. }
  150. function expectNoHistoryAccess() {
  151. cy.findByRole('button', { name: 'Layout options' }) // wait for lazy loading
  152. cy.findByRole('button', { name: 'History' }).should('not.exist')
  153. }
  154. function expectCommentAccess() {
  155. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  156. 'contain.text',
  157. '\\maketitle'
  158. )
  159. cy.findByText('\\maketitle').parent().dblclick()
  160. cy.findByRole('button', { name: 'Add comment' }).should('be.visible')
  161. cy.findByRole('textbox', { name: 'Source Editor editing' }).click()
  162. }
  163. function expectNoCommentAccess() {
  164. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  165. 'contain.text',
  166. '\\maketitle'
  167. )
  168. cy.findByText('\\maketitle').parent().dblclick()
  169. cy.findByRole('button', { name: 'Add comment' }).should('not.exist')
  170. cy.findByRole('textbox', { name: 'Source Editor editing' }).click()
  171. }
  172. function expectFullReadOnlyAccess() {
  173. expectContentReadOnlyAccess()
  174. expectChatAccess()
  175. expectHistoryAccess()
  176. expectNoCommentAccess()
  177. }
  178. function expectRestrictedReadOnlyAccess() {
  179. expectContentReadOnlyAccess()
  180. expectNoChatAccess()
  181. expectNoHistoryAccess()
  182. expectNoCommentAccess()
  183. }
  184. function expectFullReadAndWriteAccess() {
  185. expectContentWriteAccess()
  186. expectChatAccess()
  187. expectHistoryAccess()
  188. expectCommentAccess()
  189. }
  190. function expectAnonymousReadAndWriteAccess() {
  191. expectContentWriteAccess()
  192. expectChatAccess()
  193. expectHistoryAccess()
  194. expectNoCommentAccess()
  195. }
  196. function expectProjectDashboardEntry() {
  197. cy.visit('/project')
  198. cy.findByText(projectName)
  199. }
  200. function expectEditAuthoredAs(author: string) {
  201. cy.findByRole('button', { name: 'History' }).click()
  202. cy.findByRole('complementary', {
  203. name: 'Project history and labels',
  204. }).within(() => {
  205. cy.findAllByTestId('history-version-metadata-users')
  206. .first()
  207. .should('contain.text', author) // might have other edits in the same group
  208. })
  209. }
  210. describe('via email', function () {
  211. const email = 'collaborator-email@example.com'
  212. ensureUserExists({ email })
  213. beforeEach(function () {
  214. login('user@example.com')
  215. shareProjectByEmailAndAcceptInviteViaEmail(
  216. projectName,
  217. email,
  218. 'Viewer',
  219. true
  220. )
  221. })
  222. it('should grant the collaborator read access', function () {
  223. expectFullReadOnlyAccess()
  224. expectProjectDashboardEntry()
  225. })
  226. })
  227. describe('read only', function () {
  228. const email = 'collaborator-ro@example.com'
  229. ensureUserExists({ email })
  230. beforeWithReRunOnTestRetry(() => {
  231. login('user@example.com')
  232. shareProjectByEmailAndAcceptInviteViaDash(
  233. projectName,
  234. email,
  235. 'Viewer',
  236. true
  237. )
  238. })
  239. it('should grant the collaborator read access', function () {
  240. login(email)
  241. openProjectByName(projectName, true)
  242. expectFullReadOnlyAccess()
  243. expectProjectDashboardEntry()
  244. })
  245. })
  246. describe('read and write', function () {
  247. const email = 'collaborator-rw@example.com'
  248. ensureUserExists({ email })
  249. beforeWithReRunOnTestRetry(() => {
  250. login('user@example.com')
  251. shareProjectByEmailAndAcceptInviteViaDash(
  252. projectName,
  253. email,
  254. 'Editor',
  255. true
  256. )
  257. })
  258. it('should grant the collaborator write access', function () {
  259. login(email)
  260. openProjectByName(projectName, true)
  261. expectFullReadAndWriteAccess()
  262. expectEditAuthoredAs('You')
  263. expectProjectDashboardEntry()
  264. })
  265. })
  266. describe('token access', function () {
  267. describe('logged in', function () {
  268. describe('read only', function () {
  269. const email = 'collaborator-link-ro@example.com'
  270. ensureUserExists({ email })
  271. it('should grant restricted read access', function () {
  272. login(email)
  273. openProjectViaLinkSharingAsUser(
  274. linkSharingReadOnly,
  275. projectName,
  276. email,
  277. true
  278. )
  279. expectRestrictedReadOnlyAccess()
  280. expectProjectDashboardEntry()
  281. })
  282. })
  283. describe('read and write', function () {
  284. const email = 'collaborator-link-rw@example.com'
  285. ensureUserExists({ email })
  286. it('should grant full write access', function () {
  287. login(email)
  288. openProjectViaLinkSharingAsUser(
  289. linkSharingReadAndWrite,
  290. projectName,
  291. email,
  292. true
  293. )
  294. expectFullReadAndWriteAccess()
  295. expectEditAuthoredAs('You')
  296. expectProjectDashboardEntry()
  297. })
  298. })
  299. })
  300. describe('with OVERLEAF_ALLOW_PUBLIC_ACCESS=false', function () {
  301. describe('wrap startup', function () {
  302. startWith({
  303. pro: true,
  304. vars: {
  305. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'false',
  306. },
  307. withDataDir: true,
  308. })
  309. it('should block access', function () {
  310. expectNoAccess()
  311. })
  312. })
  313. describe('with OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING=true', function () {
  314. startWith({
  315. pro: true,
  316. vars: {
  317. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'false',
  318. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  319. },
  320. withDataDir: true,
  321. })
  322. it('should block access', function () {
  323. expectNoAccess()
  324. })
  325. })
  326. })
  327. describe('with OVERLEAF_ALLOW_PUBLIC_ACCESS=true', function () {
  328. describe('wrap startup', function () {
  329. startWith({
  330. pro: true,
  331. vars: {
  332. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  333. },
  334. withDataDir: true,
  335. })
  336. it('should grant read access with read link', function () {
  337. openProjectViaLinkSharingAsAnon(linkSharingReadOnly, true)
  338. expectRestrictedReadOnlyAccess()
  339. })
  340. it('should prompt for login with write link', function () {
  341. cy.visit(linkSharingReadAndWrite)
  342. cy.url().should('match', /\/login/)
  343. })
  344. })
  345. describe('with OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING=true', function () {
  346. startWith({
  347. pro: true,
  348. vars: {
  349. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  350. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  351. },
  352. withDataDir: true,
  353. })
  354. it('should grant read access with read link', function () {
  355. openProjectViaLinkSharingAsAnon(linkSharingReadOnly, true)
  356. expectRestrictedReadOnlyAccess()
  357. })
  358. it('should grant write access with write link', function () {
  359. openProjectViaLinkSharingAsAnon(linkSharingReadAndWrite, true)
  360. expectAnonymousReadAndWriteAccess()
  361. expectEditAuthoredAs('Anonymous')
  362. })
  363. })
  364. })
  365. describe('with OVERLEAF_DISABLE_LINK_SHARING=true', function () {
  366. const email = 'collaborator-email@example.com'
  367. ensureUserExists({ email })
  368. const invitedEmail = 'invited-email@example.com'
  369. ensureUserExists({ email: invitedEmail })
  370. const retainedViewerEmail = 'collaborator-retained-viewer@example.com'
  371. ensureUserExists({ email: retainedViewerEmail })
  372. const retainedEditorEmail = 'collaborator-retained-editor@example.com'
  373. ensureUserExists({ email: retainedEditorEmail })
  374. // Link-sharing urls have to be created before disabling link sharing.
  375. // We use the `beforeEach` hook to reload the server with link sharing
  376. // disabled **after** the initial setup which happens in the `before`
  377. // block. The `before` hook always runs prior to the `beforeEach` hook.
  378. // Set up retained access before disabling link sharing
  379. before(function () {
  380. // Set up retained viewer access
  381. login(retainedViewerEmail)
  382. openProjectViaLinkSharingAsUser(
  383. linkSharingReadOnly,
  384. projectName,
  385. retainedViewerEmail,
  386. true
  387. )
  388. // Set up retained editor access
  389. login(retainedEditorEmail)
  390. openProjectViaLinkSharingAsUser(
  391. linkSharingReadAndWrite,
  392. projectName,
  393. retainedEditorEmail,
  394. true
  395. )
  396. })
  397. beforeEach(function () {
  398. this.timeout(STARTUP_TIMEOUT) // Increase timeout for server reload
  399. return cy.wrap(
  400. reloadWith({
  401. pro: true,
  402. vars: {
  403. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  404. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  405. OVERLEAF_DISABLE_LINK_SHARING: 'true',
  406. },
  407. withDataDir: true,
  408. }),
  409. { timeout: STARTUP_TIMEOUT }
  410. )
  411. })
  412. it('should not display link sharing in the sharing modal', function () {
  413. login('user@example.com')
  414. openProjectByName(projectName, true)
  415. cy.findByRole('navigation', {
  416. name: 'Project actions',
  417. })
  418. .findByRole('button', { name: 'Share' })
  419. .click()
  420. cy.findByRole('button', { name: 'Turn on link sharing' }).should(
  421. 'not.exist'
  422. )
  423. })
  424. it('should block new access to read-only link shared projects', function () {
  425. login(email)
  426. // Test read-only link returns 404
  427. cy.request({
  428. url: linkSharingReadOnly,
  429. failOnStatusCode: false,
  430. }).then(response => {
  431. expect(response.status).to.eq(404)
  432. })
  433. })
  434. it('should block new access to read-write link shared projects', function () {
  435. login(email)
  436. // Test read-write link returns 404
  437. cy.request({
  438. url: linkSharingReadAndWrite,
  439. failOnStatusCode: false,
  440. }).then(response => {
  441. expect(response.status).to.eq(404)
  442. })
  443. })
  444. it('should continue to allow email sharing', function () {
  445. login('user@example.com')
  446. shareProjectByEmailAndAcceptInviteViaEmail(
  447. projectName,
  448. invitedEmail,
  449. 'Viewer',
  450. true
  451. )
  452. expectFullReadOnlyAccess()
  453. expectProjectDashboardEntry()
  454. })
  455. it('should retain read-only access when project was joined via link before link sharing was turned off', function () {
  456. login(retainedViewerEmail)
  457. openProjectByName(projectName, true)
  458. expectRestrictedReadOnlyAccess()
  459. expectProjectDashboardEntry()
  460. })
  461. it('should retain read-write access when project was joined via link before link sharing was turned off', function () {
  462. login(retainedEditorEmail)
  463. openProjectByName(projectName, true)
  464. expectFullReadAndWriteAccess()
  465. expectProjectDashboardEntry()
  466. })
  467. })
  468. })
  469. })