project-sharing.spec.ts 16 KB

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