templates.spec.ts 7.9 KB

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