project.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { login } from './login'
  2. import { openEmail } from './email'
  3. import { v4 as uuid } from 'uuid'
  4. export function createProject(
  5. name: string,
  6. {
  7. type = 'Blank project',
  8. newProjectButtonMatcher = /new project/i,
  9. open = true,
  10. }: {
  11. type?: 'Blank project' | 'Example project'
  12. newProjectButtonMatcher?: RegExp
  13. open?: boolean
  14. } = {}
  15. ): Cypress.Chainable<string> {
  16. cy.url().then(url => {
  17. if (!url.endsWith('/project')) {
  18. cy.visit('/project')
  19. }
  20. })
  21. const interceptId = uuid()
  22. let projectId = ''
  23. if (!open) {
  24. cy.then(() => {
  25. // Register intercept just before creating the project, otherwise we might
  26. // intercept a request from a prior createProject invocation.
  27. cy.intercept(
  28. { method: 'GET', url: /\/project\/[a-fA-F0-9]{24}$/, times: 1 },
  29. req => {
  30. projectId = req.url.split('/').pop()!
  31. // Redirect back to the project dashboard, effectively reload the page.
  32. req.redirect('/project')
  33. }
  34. ).as(interceptId)
  35. })
  36. }
  37. cy.findAllByRole('button').contains(newProjectButtonMatcher).click()
  38. // FIXME: This should only look in the left menu
  39. // The upgrading tests create projects in older versions of Server Pro which used different casing of the project type. Use case-insensitive match.
  40. cy.findAllByText(type, { exact: false }).first().click()
  41. cy.findByRole('dialog').within(() => {
  42. cy.get('input').type(name)
  43. cy.findByText('Create').click()
  44. })
  45. if (open) {
  46. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  47. waitForMainDocToLoad()
  48. return cy
  49. .url()
  50. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  51. .then(url => url.split('/').pop())
  52. } else {
  53. const alias = `@${interceptId}` // IDEs do not like computed values in cy.wait().
  54. cy.wait(alias)
  55. return cy.then(() => projectId)
  56. }
  57. }
  58. export function openProjectByName(projectName: string) {
  59. cy.visit('/project')
  60. cy.findByText(projectName).click()
  61. waitForMainDocToLoad()
  62. }
  63. export function openProjectById(projectId: string) {
  64. cy.visit(`/project/${projectId}`)
  65. waitForMainDocToLoad()
  66. }
  67. export function openProjectViaLinkSharingAsAnon(url: string) {
  68. cy.visit(url)
  69. waitForMainDocToLoad()
  70. }
  71. export function openProjectViaLinkSharingAsUser(
  72. url: string,
  73. projectName: string,
  74. email: string
  75. ) {
  76. cy.visit(url)
  77. cy.findByText(projectName) // wait for lazy loading
  78. cy.contains(`as ${email}`)
  79. cy.findByText('OK, join project').click()
  80. waitForMainDocToLoad()
  81. }
  82. export function openProjectViaInviteNotification(projectName: string) {
  83. cy.visit('/project')
  84. cy.findByText(projectName)
  85. .parent()
  86. .parent()
  87. .within(() => {
  88. cy.findByText('Join Project').click()
  89. })
  90. cy.findByText('Open Project').click()
  91. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  92. waitForMainDocToLoad()
  93. }
  94. function shareProjectByEmail(
  95. projectName: string,
  96. email: string,
  97. level: 'Viewer' | 'Editor'
  98. ) {
  99. openProjectByName(projectName)
  100. cy.findByText('Share').click()
  101. cy.findByRole('dialog').within(() => {
  102. cy.findByLabelText('Add email address', { selector: 'input' }).type(
  103. `${email},`
  104. )
  105. cy.findByLabelText('Add email address', { selector: 'input' })
  106. .parents('form')
  107. .within(() => {
  108. cy.findByTestId('add-collaborator-select')
  109. .click()
  110. .then(() => {
  111. cy.findByText(level).click()
  112. })
  113. })
  114. cy.findByText('Invite').click({ force: true })
  115. cy.findByText('Invite not yet accepted.')
  116. })
  117. }
  118. export function shareProjectByEmailAndAcceptInviteViaDash(
  119. projectName: string,
  120. email: string,
  121. level: 'Viewer' | 'Editor'
  122. ) {
  123. shareProjectByEmail(projectName, email, level)
  124. login(email)
  125. openProjectViaInviteNotification(projectName)
  126. }
  127. export function getSpamSafeProjectName() {
  128. while (true) {
  129. // Move from hex/16 to base64/64 possible characters per char in string
  130. const name = Buffer.from(uuid().replaceAll('-', ''), 'hex')
  131. .toString('base64')
  132. .slice(0, 10)
  133. const nDigits = (name.match(/\d/g) || []).length
  134. if (nDigits < 6) return name
  135. }
  136. }
  137. export function shareProjectByEmailAndAcceptInviteViaEmail(
  138. projectName: string,
  139. email: string,
  140. level: 'Viewer' | 'Editor'
  141. ) {
  142. shareProjectByEmail(projectName, email, level)
  143. login(email)
  144. openEmail(projectName, frame => {
  145. frame.contains('View project').then(a => {
  146. cy.log(
  147. 'bypass target=_blank and navigate current browser tab/cypress-iframe to project invite'
  148. )
  149. cy.visit(a.attr('href')!)
  150. })
  151. })
  152. cy.url().should('match', /\/project\/[a-f0-9]+\/invite\/token\/[a-f0-9]+/)
  153. cy.findByText(/user would like you to join/)
  154. cy.contains(new RegExp(`You are accepting this invite as ${email}`))
  155. cy.findByText('Join Project').click()
  156. waitForMainDocToLoad()
  157. }
  158. export function enableLinkSharing() {
  159. let linkSharingReadOnly: string
  160. let linkSharingReadAndWrite: string
  161. const origin = new URL(Cypress.config().baseUrl!).origin
  162. waitForMainDocToLoad()
  163. cy.findByText('Share').click()
  164. cy.findByText('Turn on link sharing').click()
  165. cy.findByText('Anyone with this link can view this project')
  166. .next()
  167. .should('contain.text', origin + '/read')
  168. .then(el => {
  169. linkSharingReadOnly = el.text()
  170. })
  171. cy.findByText('Anyone with this link can edit this project')
  172. .next()
  173. .should('contain.text', origin + '/')
  174. .then(el => {
  175. linkSharingReadAndWrite = el.text()
  176. })
  177. return cy.then(() => {
  178. return { linkSharingReadOnly, linkSharingReadAndWrite }
  179. })
  180. }
  181. export function waitForMainDocToLoad() {
  182. cy.log('Wait for main doc to load; it will steal the focus after loading')
  183. cy.get('.cm-content').should('contain.text', 'Introduction')
  184. }
  185. export function openFile(fileName: string, waitFor: string) {
  186. // force: The file-tree pane is too narrow to display the full name.
  187. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  188. // wait until we've switched to the selected file
  189. cy.findByText('Loading…').should('not.exist')
  190. cy.findByText(waitFor)
  191. }
  192. export function createNewFile() {
  193. const fileName = `${uuid()}.tex`
  194. cy.log('create new project file')
  195. cy.get('button').contains('New file').click({ force: true })
  196. cy.findByRole('dialog').within(() => {
  197. cy.get('input').clear()
  198. cy.get('input').type(fileName)
  199. cy.findByText('Create').click()
  200. })
  201. // force: The file-tree pane is too narrow to display the full name.
  202. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  203. // wait until we've switched to the newly created empty file
  204. cy.findByText('Loading…').should('not.exist')
  205. cy.get('.cm-line').should('have.length', 1)
  206. return fileName
  207. }
  208. export function expectFileExists(
  209. name: string,
  210. binary: boolean,
  211. content: string
  212. ) {
  213. cy.findByRole('treeitem', { name }).click()
  214. if (binary) {
  215. cy.findByText(content).should('not.have.class', 'cm-line')
  216. } else {
  217. cy.findByText(content).should('have.class', 'cm-line')
  218. }
  219. }
  220. export function prepareFileUploadTest(binary = false) {
  221. const name = `${uuid()}.txt`
  222. const content = `Test File Content ${name}${binary ? ' \x00' : ''}`
  223. cy.get('button').contains('Upload').click({ force: true })
  224. cy.get('input[type=file]')
  225. .first()
  226. .selectFile(
  227. {
  228. contents: Cypress.Buffer.from(content),
  229. fileName: name,
  230. lastModified: Date.now(),
  231. },
  232. { force: true }
  233. )
  234. // wait for the upload to finish
  235. cy.findByRole('treeitem', { name })
  236. return () => expectFileExists(name, binary, content)
  237. }
  238. export function testNewFileUpload() {
  239. it('can upload text file', () => {
  240. const check = prepareFileUploadTest(false)
  241. check()
  242. })
  243. it('can upload binary file', () => {
  244. const check = prepareFileUploadTest(true)
  245. check()
  246. })
  247. }