templates.spec.ts 9.7 KB

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