project-sharing.spec.ts 14 KB

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