project.ts 8.1 KB

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