templates.spec.ts 8.2 KB

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