project-sharing.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. createProject,
  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(function () {
  29. projectName = getSpamSafeProjectName()
  30. ;({ recompile, waitForCompile } = prepareWaitForNextCompileSlot())
  31. setupTestProject()
  32. })
  33. beforeEach(() => {
  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. createProject(projectName)
  43. })
  44. // Add chat message
  45. cy.findByRole('button', { name: 'Chat' }).click()
  46. // wait for lazy loading of the chat pane
  47. cy.findByText('Send your first message to your collaborators')
  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 and logs' }).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 and logs' }).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('button', { 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' }) // wait for lazy loading
  148. cy.findByRole('button', { name: 'Chat' }).should('not.exist')
  149. }
  150. function expectNoHistoryAccess() {
  151. cy.findByRole('button', { name: 'Layout' }) // 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(projectName, email, 'Viewer')
  216. })
  217. it('should grant the collaborator read access', () => {
  218. expectFullReadOnlyAccess()
  219. expectProjectDashboardEntry()
  220. })
  221. })
  222. describe('read only', () => {
  223. const email = 'collaborator-ro@example.com'
  224. ensureUserExists({ email })
  225. beforeWithReRunOnTestRetry(function () {
  226. login('user@example.com')
  227. shareProjectByEmailAndAcceptInviteViaDash(projectName, email, 'Viewer')
  228. })
  229. it('should grant the collaborator read access', () => {
  230. login(email)
  231. openProjectByName(projectName)
  232. expectFullReadOnlyAccess()
  233. expectProjectDashboardEntry()
  234. })
  235. })
  236. describe('read and write', () => {
  237. const email = 'collaborator-rw@example.com'
  238. ensureUserExists({ email })
  239. beforeWithReRunOnTestRetry(function () {
  240. login('user@example.com')
  241. shareProjectByEmailAndAcceptInviteViaDash(projectName, email, 'Editor')
  242. })
  243. it('should grant the collaborator write access', () => {
  244. login(email)
  245. openProjectByName(projectName)
  246. expectFullReadAndWriteAccess()
  247. expectEditAuthoredAs('You')
  248. expectProjectDashboardEntry()
  249. })
  250. })
  251. describe('token access', () => {
  252. describe('logged in', () => {
  253. describe('read only', () => {
  254. const email = 'collaborator-link-ro@example.com'
  255. ensureUserExists({ email })
  256. it('should grant restricted read access', () => {
  257. login(email)
  258. openProjectViaLinkSharingAsUser(
  259. linkSharingReadOnly,
  260. projectName,
  261. email
  262. )
  263. expectRestrictedReadOnlyAccess()
  264. expectProjectDashboardEntry()
  265. })
  266. })
  267. describe('read and write', () => {
  268. const email = 'collaborator-link-rw@example.com'
  269. ensureUserExists({ email })
  270. it('should grant full write access', () => {
  271. login(email)
  272. openProjectViaLinkSharingAsUser(
  273. linkSharingReadAndWrite,
  274. projectName,
  275. email
  276. )
  277. expectFullReadAndWriteAccess()
  278. expectEditAuthoredAs('You')
  279. expectProjectDashboardEntry()
  280. })
  281. })
  282. })
  283. describe('with OVERLEAF_ALLOW_PUBLIC_ACCESS=false', () => {
  284. describe('wrap startup', () => {
  285. startWith({
  286. pro: true,
  287. vars: {
  288. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'false',
  289. },
  290. withDataDir: true,
  291. })
  292. it('should block access', () => {
  293. expectNoAccess()
  294. })
  295. })
  296. describe('with OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING=true', () => {
  297. startWith({
  298. pro: true,
  299. vars: {
  300. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'false',
  301. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  302. },
  303. withDataDir: true,
  304. })
  305. it('should block access', () => {
  306. expectNoAccess()
  307. })
  308. })
  309. })
  310. describe('with OVERLEAF_ALLOW_PUBLIC_ACCESS=true', () => {
  311. describe('wrap startup', () => {
  312. startWith({
  313. pro: true,
  314. vars: {
  315. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  316. },
  317. withDataDir: true,
  318. })
  319. it('should grant read access with read link', () => {
  320. openProjectViaLinkSharingAsAnon(linkSharingReadOnly)
  321. expectRestrictedReadOnlyAccess()
  322. })
  323. it('should prompt for login with write link', () => {
  324. cy.visit(linkSharingReadAndWrite)
  325. cy.url().should('match', /\/login/)
  326. })
  327. })
  328. describe('with OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING=true', () => {
  329. startWith({
  330. pro: true,
  331. vars: {
  332. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  333. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  334. },
  335. withDataDir: true,
  336. })
  337. it('should grant read access with read link', () => {
  338. openProjectViaLinkSharingAsAnon(linkSharingReadOnly)
  339. expectRestrictedReadOnlyAccess()
  340. })
  341. it('should grant write access with write link', () => {
  342. openProjectViaLinkSharingAsAnon(linkSharingReadAndWrite)
  343. expectAnonymousReadAndWriteAccess()
  344. expectEditAuthoredAs('Anonymous')
  345. })
  346. })
  347. })
  348. describe('with OVERLEAF_DISABLE_LINK_SHARING=true', () => {
  349. const email = 'collaborator-email@example.com'
  350. ensureUserExists({ email })
  351. const invitedEmail = 'invited-email@example.com'
  352. ensureUserExists({ email: invitedEmail })
  353. const retainedViewerEmail = 'collaborator-retained-viewer@example.com'
  354. ensureUserExists({ email: retainedViewerEmail })
  355. const retainedEditorEmail = 'collaborator-retained-editor@example.com'
  356. ensureUserExists({ email: retainedEditorEmail })
  357. // Link-sharing urls have to be created before disabling link sharing.
  358. // We use the `beforeEach` hook to reload the server with link sharing
  359. // disabled **after** the initial setup which happens in the `before`
  360. // block. The `before` hook always runs prior to the `beforeEach` hook.
  361. // Set up retained access before disabling link sharing
  362. before(function () {
  363. // Set up retained viewer access
  364. login(retainedViewerEmail)
  365. openProjectViaLinkSharingAsUser(
  366. linkSharingReadOnly,
  367. projectName,
  368. retainedViewerEmail
  369. )
  370. // Set up retained editor access
  371. login(retainedEditorEmail)
  372. openProjectViaLinkSharingAsUser(
  373. linkSharingReadAndWrite,
  374. projectName,
  375. retainedEditorEmail
  376. )
  377. })
  378. beforeEach(function () {
  379. this.timeout(STARTUP_TIMEOUT) // Increase timeout for server reload
  380. return cy.wrap(
  381. reloadWith({
  382. pro: true,
  383. vars: {
  384. OVERLEAF_ALLOW_PUBLIC_ACCESS: 'true',
  385. OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING: 'true',
  386. OVERLEAF_DISABLE_LINK_SHARING: 'true',
  387. },
  388. withDataDir: true,
  389. }),
  390. { timeout: STARTUP_TIMEOUT }
  391. )
  392. })
  393. it('should not display link sharing in the sharing modal', () => {
  394. login('user@example.com')
  395. openProjectByName(projectName)
  396. cy.findByText('Share').click()
  397. cy.findByText('Turn on link sharing').should('not.exist')
  398. })
  399. it('should block new access to read-only link shared projects', () => {
  400. login(email)
  401. // Test read-only link returns 404
  402. cy.request({
  403. url: linkSharingReadOnly,
  404. failOnStatusCode: false,
  405. }).then(response => {
  406. expect(response.status).to.eq(404)
  407. })
  408. })
  409. it('should block new access to read-write link shared projects', () => {
  410. login(email)
  411. // Test read-write link returns 404
  412. cy.request({
  413. url: linkSharingReadAndWrite,
  414. failOnStatusCode: false,
  415. }).then(response => {
  416. expect(response.status).to.eq(404)
  417. })
  418. })
  419. it('should continue to allow email sharing', () => {
  420. login('user@example.com')
  421. shareProjectByEmailAndAcceptInviteViaEmail(
  422. projectName,
  423. invitedEmail,
  424. 'Viewer'
  425. )
  426. expectFullReadOnlyAccess()
  427. expectProjectDashboardEntry()
  428. })
  429. it('should retain read-only access when project was joined via link before link sharing was turned off', () => {
  430. login(retainedViewerEmail)
  431. openProjectByName(projectName)
  432. expectRestrictedReadOnlyAccess()
  433. expectProjectDashboardEntry()
  434. })
  435. it('should retain read-write access when project was joined via link before link sharing was turned off', () => {
  436. login(retainedEditorEmail)
  437. openProjectByName(projectName)
  438. expectFullReadAndWriteAccess()
  439. expectProjectDashboardEntry()
  440. })
  441. })
  442. })
  443. })