project.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. cy.findAllByText(type).first().click()
  40. cy.findByRole('dialog').within(() => {
  41. cy.get('input').type(name)
  42. cy.findByText('Create').click()
  43. })
  44. if (open) {
  45. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  46. waitForMainDocToLoad()
  47. return cy
  48. .url()
  49. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  50. .then(url => url.split('/').pop())
  51. } else {
  52. const alias = `@${interceptId}` // IDEs do not like computed values in cy.wait().
  53. cy.wait(alias)
  54. return cy.then(() => projectId)
  55. }
  56. }
  57. export function openProjectByName(projectName: string) {
  58. cy.visit('/project')
  59. cy.findByText(projectName).click()
  60. waitForMainDocToLoad()
  61. }
  62. export function openProjectViaLinkSharingAsAnon(url: string) {
  63. cy.visit(url)
  64. waitForMainDocToLoad()
  65. }
  66. export function openProjectViaLinkSharingAsUser(
  67. url: string,
  68. projectName: string,
  69. email: string
  70. ) {
  71. cy.visit(url)
  72. cy.findByText(projectName) // wait for lazy loading
  73. cy.findByText(email)
  74. cy.findByText('OK, join project').click()
  75. waitForMainDocToLoad()
  76. }
  77. export function openProjectViaInviteNotification(projectName: string) {
  78. cy.visit('/project')
  79. cy.findByText(projectName)
  80. .parent()
  81. .parent()
  82. .within(() => {
  83. cy.findByText('Join Project').click()
  84. })
  85. cy.findByText('Open Project').click()
  86. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  87. waitForMainDocToLoad()
  88. }
  89. function shareProjectByEmail(
  90. projectName: string,
  91. email: string,
  92. level: 'Can view' | 'Can edit'
  93. ) {
  94. openProjectByName(projectName)
  95. cy.findByText('Share').click()
  96. cy.findByRole('dialog').within(() => {
  97. cy.findByLabelText('Add people', { selector: 'input' }).type(`${email},`)
  98. cy.findByLabelText('Add people', { selector: 'input' })
  99. .parents('form')
  100. .within(() => cy.findByText('Can edit').parent().select(level))
  101. cy.findByText('Invite').click({ force: true })
  102. cy.findByText('Invite not yet accepted.')
  103. })
  104. }
  105. export function shareProjectByEmailAndAcceptInviteViaDash(
  106. projectName: string,
  107. email: string,
  108. level: 'Can view' | 'Can edit'
  109. ) {
  110. shareProjectByEmail(projectName, email, level)
  111. login(email)
  112. openProjectViaInviteNotification(projectName)
  113. }
  114. export function shareProjectByEmailAndAcceptInviteViaEmail(
  115. projectName: string,
  116. email: string,
  117. level: 'Can view' | 'Can edit'
  118. ) {
  119. shareProjectByEmail(projectName, email, level)
  120. login(email)
  121. openEmail(projectName, frame => {
  122. frame.contains('View project').then(a => {
  123. cy.log(
  124. 'bypass target=_blank and navigate current browser tab/cypress-iframe to project invite'
  125. )
  126. cy.visit(a.attr('href')!)
  127. })
  128. })
  129. cy.url().should('match', /\/project\/[a-f0-9]+\/invite\/token\/[a-f0-9]+/)
  130. cy.findByText(/user would like you to join/)
  131. cy.contains(new RegExp(`You are accepting this invite as ${email}`))
  132. cy.findByText('Join Project').click()
  133. waitForMainDocToLoad()
  134. }
  135. export function enableLinkSharing() {
  136. let linkSharingReadOnly: string
  137. let linkSharingReadAndWrite: string
  138. waitForMainDocToLoad()
  139. cy.findByText('Share').click()
  140. cy.findByText('Turn on link sharing').click()
  141. cy.findByText('Anyone with this link can view this project')
  142. .next()
  143. .should('contain.text', 'http://sharelatex/')
  144. .then(el => {
  145. linkSharingReadOnly = el.text()
  146. })
  147. cy.findByText('Anyone with this link can edit this project')
  148. .next()
  149. .should('contain.text', 'http://sharelatex/')
  150. .then(el => {
  151. linkSharingReadAndWrite = el.text()
  152. })
  153. return cy.then(() => {
  154. return { linkSharingReadOnly, linkSharingReadAndWrite }
  155. })
  156. }
  157. export function waitForMainDocToLoad() {
  158. cy.log('Wait for main doc to load; it will steal the focus after loading')
  159. cy.get('.cm-content').should('contain.text', 'Introduction')
  160. }