templates.spec.ts 8.8 KB

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