Skip to content

Commit

Permalink
test(cy) extract and use api for seeding pages
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Nov 28, 2023
1 parent 28e47aa commit fbc37fc
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 111 deletions.
6 changes: 3 additions & 3 deletions cypress/e2e/collective-readonly.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe('Read-only collective', function() {
before(function() {
cy.loginAs('alice')
cy.deleteAndSeedCollective('PermissionCollective')
cy.visit('apps/collectives/PermissionCollective')
cy.seedPage('SecondPage', '', 'Readme.md')
cy.seedCollectivePermissions('PermissionCollective', 'edit', 4)
.seedPage('SecondPage')
cy.circleFind('PermissionCollective')
.circleAddMember('bob')
cy.visit('apps/collectives/PermissionCollective')
cy.seedCollectivePermissions('PermissionCollective', 'edit', 4)
})

describe('in read-only collective', function() {
Expand Down
3 changes: 1 addition & 2 deletions cypress/e2e/dashboard-widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ describe('Collectives dashboard widget', function() {
cy.loginAs('bob')
cy.enableDashboardWidget('collectives-recent-pages')
cy.deleteAndSeedCollective('Dashboard Collective1')
cy.visit('apps/collectives/Dashboard Collective1')
cy.seedPage('Page 1', '', 'Readme.md')
.seedPage('Page 1', '', 'Readme.md')
})
it('Lists pages in the dashboard widget', function() {
cy.visit('/apps/dashboard/')
Expand Down
5 changes: 2 additions & 3 deletions cypress/e2e/page-details.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ describe('Page details', function() {
before(function() {
cy.loginAs('bob')
cy.deleteAndSeedCollective('Our Garden')
cy.visit('/apps/collectives/Our Garden')
cy.seedPage('Day 1', '', 'Readme.md')
.seedPage('Day 1', '', 'Readme.md')
.seedPage('TableOfContents', '', 'Readme.md')
cy.seedPageContent('Our Garden/Day 2.md', 'A test string with Day 2 in the middle and a [link to Day 1](/index.php/apps/collectives/Our%20Garden/Day%201).')
cy.seedPage('TableOfContents', '', 'Readme.md')
cy.seedPageContent('Our Garden/TableOfContents.md', '## Second-Level Heading')
})

Expand Down
7 changes: 3 additions & 4 deletions cypress/e2e/page-landingpage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ describe('Page landing page', function() {
before(function() {
cy.loginAs('bob')
cy.deleteAndSeedCollective(collective)
cy.visit(`/apps/collectives/${collective}`)
.seedPage('Page 1', '', 'Readme.md')
.seedPage('Page 2', '', 'Readme.md')
.seedPage('Page 3', '', 'Readme.md')
cy.circleFind(collective).circleAddMember('alice')
cy.circleFind(collective).circleAddMember('jane')
cy.circleFind(collective).circleAddMember('john')
cy.seedPage('Page 1', '', 'Readme.md')
cy.seedPage('Page 2', '', 'Readme.md')
cy.seedPage('Page 3', '', 'Readme.md')
})

beforeEach(function() {
Expand Down
32 changes: 16 additions & 16 deletions cypress/e2e/page-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ describe('Page list', function() {
before(function() {
cy.loginAs('bob')
cy.deleteAndSeedCollective('Our Garden')
cy.deleteAndSeedCollective('MoveCopyTargetCollective')
cy.visit('apps/collectives/Our Garden')
cy.seedPage('Target', '', 'Readme.md')
cy.seedPage('Target Subpage', '', 'Target.md')
.as('garden')
.seedPage('Target', '', 'Readme.md')
.seedPage('Target Subpage', '', 'Target.md')
// Wait 1 second to make sure that page order by time is right
cy.wait(1000) // eslint-disable-line cypress/no-unnecessary-waiting
cy.seedPage('Day 1', '', 'Readme.md')
cy.seedPage('Subpage Title', '', 'Day 1.md')
cy.seedPage('Day 2', '', 'Readme.md')
cy.seedPage('Page Title', '', 'Readme.md')
cy.seedPage('Move me internal', '', 'Readme.md')
cy.seedPage('Copy me internal', '', 'Readme.md')
cy.seedPage('Move me external', '', 'Readme.md')
cy.seedPage('Copy me external', '', 'Readme.md')
cy.seedPage('#% special chars', '', 'Readme.md')
cy.openCollective('MoveCopyTargetCollective')
cy.seedPage('Target external', '', 'Readme.md')
cy.seedPage('Target Subpage external', '', 'Target external.md')
cy.then(() => this.garden)
.seedPage('Day 1', '', 'Readme.md')
.seedPage('Subpage Title', '', 'Day 1.md')
.seedPage('Day 2', '', 'Readme.md')
.seedPage('Page Title', '', 'Readme.md')
.seedPage('Move me internal', '', 'Readme.md')
.seedPage('Copy me internal', '', 'Readme.md')
.seedPage('Move me external', '', 'Readme.md')
.seedPage('Copy me external', '', 'Readme.md')
.seedPage('#% special chars', '', 'Readme.md')
cy.deleteAndSeedCollective('MoveCopyTargetCollective')
.seedPage('Target external', '', 'Readme.md')
.seedPage('Target Subpage external', '', 'Target external.md')
})

beforeEach(function() {
Expand Down
20 changes: 9 additions & 11 deletions cypress/e2e/pages-links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ describe('Page Link Handling', function() {
before(function() {
cy.loginAs('bob')
cy.deleteAndSeedCollective('Another Collective')
.seedPage('First Page', '', 'Readme.md').then(({ pageId }) => {
anotherCollectiveFirstPageId = pageId
})
cy.deleteAndSeedCollective('Link Testing')
cy.visit('/apps/collectives/Another Collective')
cy.seedPage('First Page', '', 'Readme.md').then((id) => {
anotherCollectiveFirstPageId = id
})
cy.openCollective('Link Testing')
cy.seedPage('Parent', '', 'Readme.md')
cy.seedPage('Child', '', 'Parent.md')
cy.seedPage('Link Target', '', 'Readme.md').then((id) => {
linkTargetPageId = id
})
.seedPage('Parent', '', 'Readme.md')
.seedPage('Child', '', 'Parent.md')
.seedPage('Link Target', '', 'Readme.md').then(({ pageId }) => {
linkTargetPageId = pageId
})
.seedPage('Link Source', '', 'Readme.md')
cy.seedPageContent('Link%20Testing/Link%20Target.md', 'Some content')
cy.seedPage('Link Source', '', 'Readme.md')
cy.uploadFile('test.md', 'text/markdown').then((id) => {
textId = id
}).then(() => {
Expand Down
14 changes: 7 additions & 7 deletions cypress/e2e/pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
describe('Page', function() {
before(function() {
cy.loginAs('bob')
cy.deleteAndSeedCollective('Our Garden')
cy.visit('/apps/collectives/Our Garden')
cy.seedPage('Day 1', '', 'Readme.md')
cy.deleteAndSeedCollective('Our Garden').as('garden')
.seedPage('Day 1', '', 'Readme.md')
// Wait 1 second to make sure that page order by time is right
cy.wait(1000) // eslint-disable-line cypress/no-unnecessary-waiting
cy.seedPage('Day 2', '', 'Readme.md')
cy.seedPage('Page Title', '', 'Readme.md')
cy.seedPage('#% special chars', '', 'Readme.md')
cy.then(() => this.garden)
.seedPage('Day 2', '', 'Readme.md')
.seedPage('Page Title', '', 'Readme.md')
.seedPage('#% special chars', '', 'Readme.md')
.seedPage('Template', '', 'Readme.md')
cy.seedPageContent('Our Garden/Day 2.md', 'A test string with Day 2 in the middle and a [link to Day 1](/index.php/apps/collectives/Our%20Garden/Day%201).')
cy.seedPage('Template', '', 'Readme.md')
cy.seedPageContent('Our Garden/Template.md', 'This is going to be our template.')
})

Expand Down
121 changes: 63 additions & 58 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import {
UPDATE_COLLECTIVE_EDIT_PERMISSIONS,
UPDATE_COLLECTIVE_SHARE_PERMISSIONS,
UPDATE_COLLECTIVE_PAGE_MODE,
GET_PAGES,
NEW_PAGE,
GET_CIRCLES,
} from '../../src/store/actions.js'

import * as api from '../../src/api.js'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

const url = Cypress.config('baseUrl').replace(/\/index.php\/?$/g, '')
Cypress.env('baseUrl', url)
Expand Down Expand Up @@ -272,49 +271,37 @@ Cypress.Commands.add('seedCollectivePageMode', (name, mode) => {
.dispatch(UPDATE_COLLECTIVE_PAGE_MODE, { mode })
})

/**
* Add a page to a collective
*/
Cypress.Commands.add('seedPage', (name, parentFilePath, parentFileName) => {
Cypress.log()
cy.dispatch(GET_PAGES)
cy.store('state.pages.pages')
.findBy({ filePath: parentFilePath, fileName: parentFileName })
.its('id')
.as('parentId')
.then(id => ({ parentId: id }))
.dispatch(NEW_PAGE, { title: name, pagePath: name })
// Return pageId of created page
cy.get('@parentId').then(parentId => {
return cy.store('state.pages.pages')
.findBy({ parentId, title: name })
.its('id')
})
function collectiveContext(collective) {
return {
isPublic: false,
collectiveId: collective.id,
shareTokenParam: null,
}
}

Cypress.Commands.add('getPages', collective => {
return api.getPages(collectiveContext(collective))
.then(response => response.data.data)
})

/**
* Upload a file
* Add a page to a collective
*/
Cypress.Commands.add('uploadFile', (path, mimeType, remotePath = '') => {
Cypress.log()
// Get fixture
return cy.fixture(path, 'base64').then(file => {
// convert the base64 string to a blob
const blob = Cypress.Blob.base64StringToBlob(file, mimeType)
try {
const file = new File([blob], path, { type: mimeType })
return cy.uploadContent(remotePath + path, file, mimeType)
.then(response => {
const ocFileId = response.headers['oc-fileid']
const fileId = parseInt(ocFileId.substring(0, ocFileId.indexOf('oc')))
return fileId
})
} catch (error) {
cy.log(error)
throw new Error(`Unable to process file ${path}`)
}
Cypress.Commands.add('seedPage',
{ prevSubject: true },
(subject, name, parentFilePath = '', parentFileName = 'Readme.md') => {
Cypress.log()
cy.getPages(subject)
.findBy({ filePath: parentFilePath, fileName: parentFileName })
.then(({ id: parentId }) => {
return api.createPage(
collectiveContext(subject),
{ parentId, title: name, pagePath: name },
)
})
.its('data.data.id')
.then(pageId => ({ ...subject, pageId }))
})
})

/**
* Upload content of a page
Expand All @@ -327,32 +314,46 @@ Cypress.Commands.add('seedPageContent', (pagePath, content) => {
cy.uploadContent(`Collectives/${pagePath}`, content)
})

Cypress.Commands.add('uploadFile', (path, mimeType, remotePath = '') => {
Cypress.log()
// Get fixture
return cy.fixture(path, 'base64').then(data => {
// convert the base64 string to a blob
const blob = Cypress.Blob.base64StringToBlob(data, mimeType)
const file = new File([blob], path, { type: mimeType })
return cy.uploadContent(remotePath + path, file, mimeType)
.then(response => {
const ocFileId = response.headers['oc-fileid']
const fileId = parseInt(ocFileId.substring(0, ocFileId.indexOf('oc')))
return fileId
})
})
})


/**
* Generic upload of content - used by seedPageContent and uploadPage
*/
Cypress.Commands.add('uploadContent', (path, content, mimetype = 'text/markdown') => {
// @nextcloud/axios automatic handling for request tokens does not work for webdav
cy.window()
.its('app.OC.requestToken')
.then(requesttoken => {
const url = `${Cypress.env('baseUrl')}/remote.php/webdav/${path}`
return axios.put(url, content, {
headers: {
requesttoken,
'Content-Type': mimetype,
},
})
cy.request('/csrftoken').then(({ body }) => {
const requesttoken = body.token
const url = `${Cypress.env('baseUrl')}/remote.php/webdav/${path}`
return axios.put(url, content, {
headers: {
requesttoken,
'Content-Type': mimetype,
},
})
})
})

/**
* Create a circle (optionally with given config)
*/
Cypress.Commands.add('seedCircle', (name, config = null) => {
Cypress.log()
cy.dispatch(GET_CIRCLES)
cy.store('state.circles.circles')
.findBy({ sanitizedName: name })
cy.circleFind(name)
.then(async circle => {
const url = `${Cypress.env('baseUrl')}/ocs/v2.php/apps/circles/circles`
let circleId
Expand Down Expand Up @@ -380,16 +381,20 @@ Cypress.Commands.add('seedCircle', (name, config = null) => {
})
})

/**
* Add someone to a circle
*/
Cypress.Commands.add('getCircles', () => {
return axios.get(generateOcsUrl('apps/circles/circles'))
.then(response => response.data.ocs.data)
})

Cypress.Commands.add('circleFind', (name) => {
Cypress.log()
cy.dispatch(GET_CIRCLES)
return cy.store('state.circles.circles')
cy.getCircles()
.findBy({ sanitizedName: name })
})

/**
* Add someone to a circle
*/
Cypress.Commands.add('circleAddMember',
{ prevSubject: true },
async ({ id }, userId, type = 1) => {
Expand Down
15 changes: 15 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ export function deleteCollective(id, circle) {
}
return axios.delete(url('/trash/' + id + doCircle))
}

export function getPages(context) {
return axios.get(pagesUrl(context))
}

export function createPage(context, page) {
const url = pagesUrl(context) + '/' + page.parentId
return axios.post(url, page)
}

export function pagesUrl({ isPublic = false, shareTokenParam, collectiveId }) {
return isPublic
? url(`/p/${shareTokenParam}/_pages`)
: url(`/${collectiveId}/_pages`)
}
Loading

0 comments on commit fbc37fc

Please sign in to comment.