templates.spec.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { isExcludedBySharding, startWith } from './helpers/config'
  2. import { ensureUserExists, login } from './helpers/login'
  3. import { createProject } from './helpers/project'
  4. const WITHOUT_PROJECTS_USER = 'user-without-projects@example.com'
  5. const ADMIN_USER = 'admin@example.com'
  6. const REGULAR_USER = 'user@example.com'
  7. const TEMPLATES_USER = 'templates@example.com'
  8. // Re-use value for "exists" and "does not exist" tests
  9. const LABEL_BROWSE_TEMPLATES = 'Browse templates'
  10. describe('Templates', () => {
  11. ensureUserExists({ email: TEMPLATES_USER })
  12. ensureUserExists({ email: WITHOUT_PROJECTS_USER })
  13. let OVERLEAF_TEMPLATES_USER_ID: string
  14. before(function () {
  15. login(TEMPLATES_USER)
  16. cy.visit('/')
  17. cy.get('meta[name="ol-user_id"]').then(el => {
  18. OVERLEAF_TEMPLATES_USER_ID = el.attr('content')!
  19. })
  20. })
  21. function varsFn() {
  22. return {
  23. OVERLEAF_TEMPLATES_USER_ID,
  24. OVERLEAF_NEW_PROJECT_TEMPLATE_LINKS:
  25. '[{"name":"All Templates","url":"/templates/all"}]',
  26. }
  27. }
  28. describe('enabled in Server Pro', () => {
  29. if (isExcludedBySharding('PRO_CUSTOM_2')) return
  30. startWith({
  31. pro: true,
  32. varsFn,
  33. })
  34. ensureUserExists({ email: REGULAR_USER })
  35. ensureUserExists({ email: ADMIN_USER, isAdmin: true })
  36. it('should show templates link on welcome page', () => {
  37. login(WITHOUT_PROJECTS_USER)
  38. cy.visit('/')
  39. cy.findByText(LABEL_BROWSE_TEMPLATES).click()
  40. cy.url().should('match', /\/templates$/)
  41. })
  42. // eslint-disable-next-line mocha/no-skipped-tests
  43. it.skip('should have templates feature', () => {
  44. login(TEMPLATES_USER)
  45. const name = `Template ${Date.now()}`
  46. const description = `Template Description ${Date.now()}`
  47. cy.visit('/')
  48. createProject(name).as('templateProjectId')
  49. cy.get('header').findByText('Menu').click()
  50. cy.findByText('Manage Template').click()
  51. cy.findByText('Template Description')
  52. .click()
  53. .parent()
  54. .get('textarea')
  55. .type(description)
  56. cy.findByText('Publish').click()
  57. cy.findByText('Publishing…').should('be.disabled')
  58. cy.findByText('Publish').should('not.exist')
  59. cy.findByText('Unpublish', { timeout: 10_000 })
  60. cy.findByText('Republish')
  61. cy.findByText('View it in the template gallery').click()
  62. cy.url()
  63. .should('match', /\/templates\/[a-f0-9]{24}$/)
  64. .as('templateURL')
  65. cy.findAllByText(name).first().should('exist')
  66. cy.findByText(description)
  67. cy.findByText('Open as Template')
  68. cy.findByText('Unpublish')
  69. cy.findByText('Republish')
  70. cy.get('img')
  71. .should('have.attr', 'src')
  72. .and('match', /\/v\/0\//)
  73. cy.findByText('Republish').click()
  74. cy.findByText('Publishing…').parent().should('be.disabled')
  75. cy.findByText('Republish', { timeout: 10_000 })
  76. cy.get('img', { timeout: 10_000 })
  77. .should('have.attr', 'src')
  78. .and('match', /\/v\/1\//)
  79. // custom tag
  80. const tagName = `${Date.now()}`
  81. cy.visit('/')
  82. cy.findByText(name)
  83. .parent()
  84. .parent()
  85. .within(() => cy.get('input[type="checkbox"]').first().check())
  86. cy.get('.project-list-sidebar-react').within(() => {
  87. cy.findAllByText('New Tag').first().click()
  88. })
  89. cy.focused().type(tagName)
  90. cy.findByText('Create').click()
  91. cy.get('.project-list-sidebar-react').within(() => {
  92. cy.findByText(tagName)
  93. .parent()
  94. .within(() => cy.get('.name').should('have.text', `${tagName} (1)`))
  95. })
  96. // Check listing
  97. cy.visit('/templates')
  98. cy.findByText(tagName)
  99. cy.visit('/templates/all')
  100. cy.findByText(name)
  101. cy.visit(`/templates/${tagName}`)
  102. cy.findByText(name)
  103. // Unpublish via template page
  104. cy.get('@templateURL').then(url => cy.visit(`${url}`))
  105. cy.findByText('Unpublish').click()
  106. cy.url().should('match', /\/templates$/)
  107. cy.get('@templateURL').then(url =>
  108. cy.visit(`${url}`, {
  109. failOnStatusCode: false,
  110. })
  111. )
  112. cy.findByText('Not found')
  113. cy.visit('/templates/all')
  114. cy.findByText(name).should('not.exist')
  115. cy.visit(`/templates/${tagName}`)
  116. cy.findByText(name).should('not.exist')
  117. // Publish again
  118. cy.get('@templateProjectId').then(projectId =>
  119. cy.visit(`/project/${projectId}`)
  120. )
  121. cy.get('header').findByText('Menu').click()
  122. cy.findByText('Manage Template').click()
  123. cy.findByText('Publish').click()
  124. cy.findByText('Unpublish', { timeout: 10_000 })
  125. // Should assign a new template id
  126. cy.findByText('View it in the template gallery').click()
  127. cy.url()
  128. .should('match', /\/templates\/[a-f0-9]{24}$/)
  129. .as('newTemplateURL')
  130. cy.get('@newTemplateURL').then(newURL => {
  131. cy.get('@templateURL').then(prevURL => {
  132. expect(newURL).to.match(/\/templates\/[a-f0-9]{24}$/)
  133. expect(prevURL).to.not.equal(newURL)
  134. })
  135. })
  136. // Open project from template
  137. login(REGULAR_USER)
  138. cy.visit('/templates')
  139. cy.findByText(tagName).click()
  140. cy.findByText(name).click()
  141. cy.findByText('Open as Template').click()
  142. cy.url().should('match', /\/project\/[a-f0-9]{24}$/)
  143. cy.get('.project-name').findByText(name)
  144. cy.get('header').findByText('Menu').click()
  145. cy.findByText('Word Count') // wait for lazy loading
  146. cy.findByText('Manage Template').should('not.exist')
  147. // Check management as regular user
  148. cy.get('@newTemplateURL').then(url => cy.visit(`${url}`))
  149. cy.findByText('Open as Template')
  150. cy.findByText('Unpublish').should('not.exist')
  151. cy.findByText('Republish').should('not.exist')
  152. // Check management as admin user
  153. login(ADMIN_USER)
  154. cy.get('@newTemplateURL').then(url => cy.visit(`${url}`))
  155. cy.findByText('Open as Template')
  156. cy.findByText('Unpublish')
  157. cy.findByText('Republish')
  158. cy.get('@templateProjectId').then(projectId =>
  159. cy.visit(`/project/${projectId}`)
  160. )
  161. cy.get('header').findByText('Menu').click()
  162. cy.findByText('Manage Template').click()
  163. cy.findByText('Unpublish')
  164. // Back to templates user
  165. login(TEMPLATES_USER)
  166. // Unpublish via editor
  167. cy.get('@templateProjectId').then(projectId =>
  168. cy.visit(`/project/${projectId}`)
  169. )
  170. cy.get('header').findByText('Menu').click()
  171. cy.findByText('Manage Template').click()
  172. cy.findByText('Unpublish').click()
  173. cy.findByText('Publish')
  174. cy.visit('/templates/all')
  175. cy.findByText(name).should('not.exist')
  176. // check for template links, after creating the first project
  177. cy.visit('/')
  178. cy.findAllByRole('button')
  179. .contains(/new project/i)
  180. .click()
  181. cy.findAllByText('All Templates')
  182. .first()
  183. .should('have.attr', 'href', '/templates/all')
  184. })
  185. })
  186. function checkDisabled() {
  187. it('should not have templates feature', () => {
  188. login(TEMPLATES_USER)
  189. cy.visit('/')
  190. createProject('maybe templates')
  191. cy.get('header').findByText('Menu').click()
  192. cy.findByText('Word Count') // wait for lazy loading
  193. cy.findByText('Manage Template').should('not.exist')
  194. cy.visit('/templates', { failOnStatusCode: false })
  195. cy.findByText('Not found')
  196. cy.visit('/templates/all', { failOnStatusCode: false })
  197. cy.findByText('Not found')
  198. // check for template links, after creating the first project
  199. cy.visit('/')
  200. cy.findAllByRole('button')
  201. .contains(/new project/i)
  202. .click()
  203. cy.findAllByText('All Templates').should('not.exist')
  204. })
  205. it('should not show templates link on welcome page', () => {
  206. login(WITHOUT_PROJECTS_USER)
  207. cy.visit('/')
  208. cy.findByText(/new project/i) // wait for lazy loading
  209. cy.findByText(LABEL_BROWSE_TEMPLATES).should('not.exist')
  210. })
  211. }
  212. describe('disabled Server Pro', () => {
  213. if (isExcludedBySharding('PRO_DEFAULT_2')) return
  214. startWith({ pro: true })
  215. checkDisabled()
  216. })
  217. describe('unavailable in CE', () => {
  218. if (isExcludedBySharding('CE_CUSTOM_1')) return
  219. startWith({
  220. pro: false,
  221. varsFn,
  222. })
  223. checkDisabled()
  224. })
  225. })