Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor plugin tests #1940

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
// local dependencies
const { waitForApplication, uninstallPlugin } = require('../../utils')
const { uninstallPlugin } = require('../../utils')
const { managePluginsPagePath, loadTemplatesPage, loadPluginsPage } = require('../plugin-utils')

const managePluginsPagePath = '/manage-prototype/plugins'
const manageTemplatesPagePath = '/manage-prototype/templates'
const panelCompleteQuery = '[aria-live="polite"] #panel-complete'

async function loadPluginsPage () {
cy.task('log', 'Visit the manage prototype plugins page')
await waitForApplication(managePluginsPagePath)
}

async function loadTemplatesPage () {
cy.task('log', 'Visit the manage prototype templates page')
await waitForApplication(manageTemplatesPagePath)
}

async function installPluginTests ({ plugin, templates, version }) {
describe(plugin, () => {
before(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

// core dependencies
const path = require('path')

// npm dependencies
const { capitalize } = require('lodash')

// local dependencies
const { waitForApplication, getTemplateLink, deleteFile } = require('../../utils')
const { deleteFile } = require('../../utils')
const {
failAction,
performPluginAction,
managePluginsPagePath,
getTemplateLink,
loadPluginsPage
} = require('../plugin-utils')
const { showHideAllLinkQuery, assertVisible, assertHidden } = require('../../step-by-step-utils')

const appViews = path.join(Cypress.env('projectFolder'), 'app', 'views')
const managePluginsPagePath = '/manage-prototype/plugins'
const plugin = '@govuk-prototype-kit/step-by-step'
const version1 = '1.0.0'
const version2 = '2.1.0'
Expand All @@ -19,41 +21,6 @@ const pluginPageTemplate = '/templates/step-by-step-navigation.html'
const pluginPageTitle = 'Step by step navigation'
const pluginPagePath = '/step-by-step-navigation'

const panelProcessingQuery = '[aria-live="polite"] #panel-processing'
const panelCompleteQuery = '[aria-live="polite"] #panel-complete'
const panelErrorQuery = '[aria-live="polite"] #panel-error'

const performPluginAction = (action) => {
cy.task('log', `The ${plugin} plugin should be displayed`)
cy.get('h2')
.should('contains.text', `${capitalize(action)} ${pluginName}`)

cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelProcessingQuery)
.should('be.visible')
.should('contain.text', `${capitalize(action === 'upgrade' ? 'Upgrad' : action)}ing ...`)

cy.get(panelProcessingQuery, { timeout: 20000 })
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('be.visible')
.should('contain.text', `${capitalize(action)} complete`)

cy.get('#instructions-complete a')
.should('contain.text', 'Back to plugins')
.wait(3000)
.click()

cy.get('h1').should('have.text', 'Plugins')
}

const provePluginFunctionalityWorks = () => {
cy.wait(2000)

Expand All @@ -62,12 +29,12 @@ const provePluginFunctionalityWorks = () => {
cy.visit(pluginPagePath)

// click toggle button and check that all steps details are visible
cy.get(showHideAllLinkQuery).should('contains.text', 'Show all').click()
cy.get(showHideAllLinkQuery).contains('Show all').click()
assertVisible(1)
assertVisible(2)

// click toggle button and check that all steps details are hidden
cy.get(showHideAllLinkQuery).should('contains.text', 'Hide all').click()
cy.get(showHideAllLinkQuery).contains('Hide all').click()
assertHidden(1)
assertHidden(2)
}
Expand Down Expand Up @@ -98,11 +65,6 @@ describe('Management plugins: ', () => {
cy.wait(4000)
})

const loadPluginsPage = async () => {
cy.task('log', 'Visit the manage prototype plugins page')
await waitForApplication(managePluginsPagePath)
}

it('CSRF Protection on POST action', () => {
const installUrl = `${managePluginsPagePath}/install`
cy.task('log', `Posting to ${installUrl} without csrf protection`)
Expand All @@ -122,21 +84,21 @@ describe('Management plugins: ', () => {

cy.get('#plugin-action-button').click()

performPluginAction('install')
performPluginAction('install', plugin, pluginName)
})

it(`Upgrade the ${plugin}@${version1} plugin to ${plugin}@${version2}`, () => {
loadPluginsPage()
cy.task('log', `Upgrade the ${plugin} plugin`)
cy.get(`[data-plugin-package-name="${plugin}"] button`).contains('Upgrade').click()

performPluginAction('upgrade')
performPluginAction('upgrade', plugin, pluginName)
})

it(`Create a page using a template from the ${plugin} plugin`, () => {
loadPluginsPage()
cy.get('a[href*="/templates"]')
.should('contains.text', 'Templates').click()
.contains('Templates').click()

cy.get('h2').contains(pluginName)

Expand All @@ -146,7 +108,7 @@ describe('Management plugins: ', () => {

// create step-by-step-navigation page
cy.get('.govuk-heading-l')
.should('contains.text', `Create new ${pluginPageTitle} page`)
.contains(`Create new ${pluginPageTitle} page`)
cy.get('#chosen-url')
.type(pluginPagePath)
cy.get('.govuk-button').contains('Create page').click()
Expand All @@ -159,7 +121,7 @@ describe('Management plugins: ', () => {
cy.task('log', `Uninstall the ${plugin} plugin`)
cy.get(`[data-plugin-package-name="${plugin}"] button`).contains('Uninstall').click()

performPluginAction('uninstall')
performPluginAction('uninstall', plugin, pluginName)

provePluginFunctionalityFails()
})
Expand All @@ -169,7 +131,7 @@ describe('Management plugins: ', () => {
cy.task('log', `Install the ${plugin} plugin`)
cy.get(`[data-plugin-package-name="${plugin}"] button`).contains('Install').click()

performPluginAction('install')
performPluginAction('install', plugin, pluginName)

provePluginFunctionalityWorks()
})
Expand All @@ -181,47 +143,23 @@ describe('Management plugins: ', () => {

cy.get('#plugin-action-button').click()

performPluginAction('install')
performPluginAction('install', plugin, pluginName)
})

describe('fail', () => {
const failAction = () => {
cy.get('#plugin-action-button').click()

cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelProcessingQuery)
.should('be.visible')
.should('contain.text', 'Installing ...')

cy.get(panelProcessingQuery, { timeout: 40000 })
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('be.visible')

cy.get(`${panelErrorQuery} .govuk-panel__title`)
.should('contain.text', 'There was a problem installing')
cy.get(`${panelErrorQuery} a`)
.should('contain.text', 'Please contact support')
}

it('Fail when installing a non existent plugin', () => {
const pkg = 'invalid-prototype-kit-plugin'
const pluginName = 'Invalid Prototype Kit Plugin'
cy.visit(`${managePluginsPagePath}/install?package=${encodeURIComponent(pkg)}`)
cy.get('h2').contains(`Install ${pluginName}`)
failAction()
failAction('install')
})

it('Fail when installing a plugin with a non existent version', () => {
cy.visit(`${managePluginsPagePath}/install?package=${encodeURIComponent(plugin)}&version=0.0.1`)
cy.get('h2')
.should('contains.text', `Install ${pluginName}`)
failAction()
.contains(`Install ${pluginName}`)
failAction('install')
})
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// local dependencies
const { installPlugin, waitForApplication, getTemplateLink } = require('../../utils')
const { installPlugin, waitForApplication } = require('../../utils')
const { showHideAllLinkQuery, assertVisible, assertHidden } = require('../../step-by-step-utils')
const { manageTemplatesPagePath, getTemplateLink } = require('../plugin-utils')

const manageTemplatesPagePath = '/manage-prototype/templates'
const plugin = '@govuk-prototype-kit/step-by-step'
const version1 = '@1.0.0'
const version2 = '@2.1.0'
Expand Down
91 changes: 91 additions & 0 deletions cypress/e2e/plugins/plugin-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// npm dependencies
const { capitalize } = require('lodash')
const { urlencode } = require('nunjucks/src/filters')
const { waitForApplication } = require('../utils')

const manageTemplatesPagePath = '/manage-prototype/templates'
const managePluginsPagePath = '/manage-prototype/plugins'

const panelProcessingQuery = '[aria-live="polite"] #panel-processing'
const panelCompleteQuery = '[aria-live="polite"] #panel-complete'
const panelErrorQuery = '[aria-live="polite"] #panel-error'

function getTemplateLink (type, packageName, path) {
const queryString = `?package=${urlencode(packageName)}&template=${urlencode(path)}`
return `${manageTemplatesPagePath}/${type}${queryString}`
}

async function loadPluginsPage () {
cy.task('log', 'Visit the manage prototype plugins page')
await waitForApplication(managePluginsPagePath)
}

async function loadTemplatesPage () {
cy.task('log', 'Visit the manage prototype templates page')
await waitForApplication(manageTemplatesPagePath)
}

function performPluginAction (action, plugin, pluginName) {
cy.task('log', `The ${plugin} plugin should be displayed`)
cy.get('h2')
.contains(`${capitalize(action)} ${pluginName}`)

cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelProcessingQuery)
.should('be.visible')
.contains(`${capitalize(action === 'upgrade' ? 'Upgrad' : action)}ing ...`)

cy.get(panelProcessingQuery, { timeout: 20000 })
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('be.visible')
.contains(`${capitalize(action)} complete`)

cy.get('#instructions-complete a')
.contains('Back to plugins')
.wait(3000)
.click()

cy.get('h1').should('have.text', 'Plugins')
}

function failAction (action) {
cy.get('#plugin-action-button').click()

cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('not.be.visible')
cy.get(panelProcessingQuery)
.should('be.visible')
.contains(`${capitalize(action === 'upgrade' ? 'Upgrad' : action)}ing ...`)

cy.get(panelProcessingQuery, { timeout: 40000 })
.should('not.be.visible')
cy.get(panelCompleteQuery)
.should('not.be.visible')
cy.get(panelErrorQuery)
.should('be.visible')

cy.get(`${panelErrorQuery} .govuk-panel__title`)
.contains(`There was a problem ${action === 'upgrade' ? 'Upgrad' : action}ing`)
cy.get(`${panelErrorQuery} a`)
.contains('Please contact support')
}

module.exports = {
managePluginsPagePath,
manageTemplatesPagePath,
loadPluginsPage,
loadTemplatesPage,
getTemplateLink,
performPluginAction,
failAction
}
9 changes: 0 additions & 9 deletions cypress/e2e/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

// npm dependencies
const { urlencode } = require('nunjucks/src/filters')

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

const authenticate = () => {
Expand Down Expand Up @@ -40,11 +37,6 @@ const replaceInFile = (filename, originalText, source, newText) => {
cy.task('replaceTextInFile', { filename, originalText, source, newText })
}

const getTemplateLink = (type, packageName, path) => {
const queryString = `?package=${urlencode(packageName)}&template=${urlencode(path)}`
return `/manage-prototype/templates/${type}${queryString}`
}

function uninstallPlugin (plugin) {
cy.task('log', `Uninstalling ${plugin}`)
cy.exec(`cd ${Cypress.env('projectFolder')} && npm uninstall ${plugin}`)
Expand All @@ -65,7 +57,6 @@ module.exports = {
authenticate,
sleep,
waitForApplication,
getTemplateLink,
copyFile,
deleteFile,
createFile,
Expand Down