templates.spec.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { isExcludedBySharding, startWith } from './helpers/config'
  2. import { ensureUserExists, login } from './helpers/login'
  3. import { createProject, NEW_PROJECT_BUTTON_MATCHER } 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', function () {
  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', function () {
  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', function () {
  37. login(WITHOUT_PROJECTS_USER)
  38. cy.visit('/')
  39. cy.findByRole('link', { name: LABEL_BROWSE_TEMPLATES })
  40. .should('have.attr', 'href', '/templates')
  41. .click()
  42. cy.url().should('match', /\/templates$/)
  43. })
  44. it('should have templates feature', function () {
  45. login(TEMPLATES_USER)
  46. const name = `Template ${Date.now()}`
  47. const description = `Template Description ${Date.now()}`
  48. cy.visit('/')
  49. createProject(name, { type: 'Example project' }).as('templateProjectId')
  50. cy.findByRole('navigation', {
  51. name: 'Project actions',
  52. })
  53. .findByRole('button', { name: 'Menu' })
  54. .click()
  55. cy.findByText('Manage Template').click()
  56. cy.findByText('Template Description').as('description').click()
  57. cy.get('@description').parent().get('textarea').type(description)
  58. cy.findByText('Publish').click()
  59. cy.findByText('Publishing…').parent().should('be.disabled')
  60. cy.findByText('Publish').should('not.exist')
  61. cy.findByText('Unpublish', { timeout: 60_000 })
  62. cy.findByText('Republish')
  63. cy.findByText('View it in the template gallery').click()
  64. cy.url()
  65. .should('match', /\/templates\/[a-f0-9]{24}$/)
  66. .as('templateURL')
  67. cy.findAllByText(name).first().should('exist')
  68. cy.findByText(description)
  69. cy.findByText('Open as Template')
  70. cy.findByText('Unpublish')
  71. cy.findByText('Republish')
  72. cy.get('img')
  73. .should('have.attr', 'src')
  74. .and('match', /\/v\/0\//)
  75. cy.findByText('Republish').click()
  76. cy.findByText('Publishing…').parent().should('be.disabled')
  77. cy.findByText('Republish', { timeout: 60_000 })
  78. cy.get('img', { timeout: 60_000 })
  79. .should('have.attr', 'src')
  80. .and('match', /\/v\/1\//)
  81. // custom tag
  82. const tagName = `${Date.now()}`
  83. cy.visit('/')
  84. cy.findByText(name)
  85. .parent()
  86. .parent()
  87. .within(() => cy.get('input[type="checkbox"]').first().check())
  88. cy.get('.project-list-sidebar-scroll').within(() => {
  89. cy.findAllByText('New tag').first().click()
  90. })
  91. cy.focused().type(tagName)
  92. cy.findByText('Create').click()
  93. cy.get('.project-list-sidebar-scroll').within(() => {
  94. cy.findByText(tagName)
  95. .parent()
  96. .within(() => cy.get('.name').should('have.text', `${tagName} (1)`))
  97. })
  98. // Check listing
  99. cy.visit('/templates')
  100. cy.findByText(tagName)
  101. cy.visit('/templates/all')
  102. cy.findByText(name)
  103. cy.visit(`/templates/${tagName}`)
  104. cy.findByText(name)
  105. // Unpublish via template page
  106. cy.get('@templateURL').then(url => cy.visit(`${url}`))
  107. cy.findByText('Unpublish').click()
  108. cy.url().should('match', /\/templates$/)
  109. cy.get('@templateURL').then(url =>
  110. cy.visit(`${url}`, {
  111. failOnStatusCode: false,
  112. })
  113. )
  114. cy.findByText('Not found')
  115. cy.visit('/templates/all')
  116. cy.findByText(name).should('not.exist')
  117. cy.visit(`/templates/${tagName}`)
  118. cy.findByText(name).should('not.exist')
  119. // Publish again
  120. cy.get('@templateProjectId').then(projectId =>
  121. cy.visit(`/project/${projectId}`)
  122. )
  123. cy.findByRole('navigation', {
  124. name: 'Project actions',
  125. })
  126. .findByRole('button', { name: 'Menu' })
  127. .click()
  128. cy.findByText('Manage Template').click()
  129. cy.findByText('Publish').click()
  130. cy.findByText('Unpublish', { timeout: 60_000 })
  131. // Should assign a new template id
  132. cy.findByText('View it in the template gallery').click()
  133. cy.url()
  134. .should('match', /\/templates\/[a-f0-9]{24}$/)
  135. .as('newTemplateURL')
  136. cy.get('@newTemplateURL').then(newURL => {
  137. cy.get('@templateURL').then(prevURL => {
  138. expect(newURL).to.match(/\/templates\/[a-f0-9]{24}$/)
  139. expect(prevURL).to.not.equal(newURL)
  140. })
  141. })
  142. // Open project from template
  143. login(REGULAR_USER)
  144. cy.visit('/templates')
  145. cy.findByText(tagName).click()
  146. cy.findByText(name).click()
  147. cy.findByText('Open as Template').click()
  148. cy.url().should('match', /\/project\/[a-f0-9]{24}$/)
  149. cy.get('.project-name').should('contain.text', 'Your Paper') // might have (1) suffix
  150. cy.findByRole('navigation', {
  151. name: 'Project actions',
  152. })
  153. .findByRole('button', { name: 'Menu' })
  154. .click()
  155. cy.findByText('Word Count') // wait for lazy loading
  156. cy.findByText('Manage Template').should('not.exist')
  157. // Check management as regular user
  158. cy.get('@newTemplateURL').then(url => cy.visit(`${url}`))
  159. cy.findByText('Open as Template')
  160. cy.findByText('Unpublish').should('not.exist')
  161. cy.findByText('Republish').should('not.exist')
  162. // Check management as admin user
  163. login(ADMIN_USER)
  164. cy.get('@newTemplateURL').then(url => cy.visit(`${url}`))
  165. cy.findByText('Open as Template')
  166. cy.findByText('Unpublish')
  167. cy.findByText('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: 'Menu' })
  175. .click()
  176. cy.findByText('Manage Template').click()
  177. cy.findByText('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: 'Menu' })
  188. .click()
  189. cy.findByText('Manage Template').click()
  190. cy.findByText('Unpublish').click()
  191. cy.findByText('Publish')
  192. cy.visit('/templates/all')
  193. cy.findByText(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.findAllByText('All Templates')
  198. .first()
  199. .parent()
  200. .should('have.attr', 'href', '/templates/all')
  201. })
  202. })
  203. function checkDisabled() {
  204. it('should not have templates feature', function () {
  205. login(TEMPLATES_USER)
  206. cy.visit('/')
  207. createProject('maybe templates')
  208. cy.findByRole('navigation', {
  209. name: 'Project actions',
  210. })
  211. .findByRole('button', { name: 'Menu' })
  212. .click()
  213. cy.findByText('Word Count') // wait for lazy loading
  214. cy.findByText('Manage Template').should('not.exist')
  215. cy.visit('/templates', { failOnStatusCode: false })
  216. cy.findByText('Not found')
  217. cy.visit('/templates/all', { failOnStatusCode: false })
  218. cy.findByText('Not found')
  219. // check for template links, after creating the first project
  220. cy.visit('/')
  221. cy.findAllByRole('button', { name: NEW_PROJECT_BUTTON_MATCHER }).click()
  222. cy.findAllByText('All Templates').should('not.exist')
  223. })
  224. it('should not show templates link on welcome page', function () {
  225. login(WITHOUT_PROJECTS_USER)
  226. cy.visit('/')
  227. cy.findByText(NEW_PROJECT_BUTTON_MATCHER) // wait for lazy loading
  228. cy.findByText(LABEL_BROWSE_TEMPLATES).should('not.exist')
  229. })
  230. }
  231. describe('disabled Server Pro', function () {
  232. if (isExcludedBySharding('PRO_DEFAULT_2')) return
  233. startWith({ pro: true })
  234. checkDisabled()
  235. })
  236. describe('unavailable in CE', function () {
  237. if (isExcludedBySharding('CE_CUSTOM_1')) return
  238. startWith({
  239. pro: false,
  240. varsFn,
  241. })
  242. checkDisabled()
  243. })
  244. })