nginx-proxy-manager/test/cypress/support/commands.mjs
Jamie Curnow 03c70e3902 Fixes for upgraded debian trixie
- Adds request logging in debug mode for some endpoints
- Moves certbot version determination to the startup scripts and removes
  bash script encapsulation when installing plugins
- Revert loose domain validation, which was there for a specific reason
  addressing CVE's
- Fix Cypress suite for cert generation
- Adds Cypress test that iterates over the entire certbot plugins list
  and installs each one, ensuring at the very least that the install
  works
- Fixed some plugins based on this
- (!) Still some work to do on this, hostinger is still broken at least
- Improved cypress tests for custom certs; they will generate on each
  run instead of being baked in. The baked ones were due to expire soon
2026-05-25 07:37:24 +10:00

185 lines
5.4 KiB
JavaScript

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
import 'cypress-wait-until';
Cypress.Commands.add('randomString', (length) => {
let result = '';
const characters = 'ABCDEFGHIJK LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
});
/**
* Check the swagger schema file:
*
* @param {string} url
* @param {string} savePath
*/
Cypress.Commands.add("validateSwaggerFile", (url, savePath) => {
cy.task('log', `validateSwaggerFile: ${url} -- ${savePath}`)
.then(() => {
return cy
.request(url)
.then((response) => cy.writeFile(savePath, response.body, { log: false }))
.then(() => cy.exec(`yarn swagger-lint '${savePath}'`, { failOnNonZeroExit: false }))
.then((result) => cy.task('log', `Swagger Vacuum Results:\n${result.stdout || ''}`)
.then(() => expect(result.exitCode).to.eq(0)));
});
});
/**
* Check the swagger schema for a specific endpoint:
*
* @param {string} method API Method in swagger doc, "get", "put", "post", "delete"
* @param {integer} code Swagger doc endpoint response code, exactly as defined in swagger doc
* @param {string} path Swagger doc endpoint path, exactly as defined in swagger doc
* @param {*} data The API response data to check against the swagger schema
*/
Cypress.Commands.add('validateSwaggerSchema', (method, code, path, data) => {
cy.env(['swaggerBase']).then(({ swaggerBase }) => {
cy.task('validateSwaggerSchema', {
file: swaggerBase,
endpoint: path,
method: method,
statusCode: code,
responseSchema: data,
verbose: true
}).should('equal', null);
});
});
Cypress.Commands.add('createInitialUser', (defaultUser) => {
let user = {
name: 'Cypress McGee',
nickname: 'Cypress',
email: 'cypress@example.com',
auth: {
type: 'password',
secret: 'changeme'
},
};
if (typeof defaultUser === 'object' && defaultUser) {
user = Object.assign({}, user, defaultUser);
}
return cy.task('backendApiPost', {
path: '/api/users',
data: user,
}).then((data) => {
// Check the swagger schema:
cy.validateSwaggerSchema('post', 201, '/users', data);
expect(data).to.have.property('id');
expect(data.id).to.be.greaterThan(0);
cy.wrap(data);
});
});
Cypress.Commands.add('getToken', (defaultUser, defaultAuth) => {
if (typeof defaultAuth === 'object' && defaultAuth) {
if (!defaultUser) {
defaultUser = {};
}
defaultUser.auth = defaultAuth;
}
cy.task('backendApiGet', {
path: '/api/',
}).then((data) => {
// Check the swagger schema:
cy.validateSwaggerSchema('get', 200, '/', data);
if (!data.setup) {
cy.log('Setup = false');
// create a new user
cy.createInitialUser(defaultUser).then(() => {
return cy.getToken(defaultUser);
});
} else {
let auth = {
identity: 'cypress@example.com',
secret: 'changeme',
};
if (typeof defaultUser === 'object' && defaultUser && typeof defaultUser.auth === 'object' && defaultUser.auth) {
auth = Object.assign({}, auth, defaultUser.auth);
}
cy.log('Setup = true');
// login with existing user
cy.task('backendApiPost', {
path: '/api/tokens',
data: auth,
}).then((res) => {
cy.wrap(res.token);
});
}
});
});
Cypress.Commands.add('resetUsers', () => {
cy.task('backendApiDelete', {
path: '/api/users'
}).then((data) => {
expect(data).to.be.equal(true);
cy.wrap(data);
});
});
// TODO: copied from v3, is this usable?
Cypress.Commands.add('waitForCertificateStatus', (token, certID, expected, timeout = 60) => {
cy.log(`Waiting for certificate (${certID}) status (${expected}) timeout (${timeout})`);
cy.waitUntil(() => cy.task('backendApiGet', {
token: token,
path: `/api/certificates/${certID}`
}).then((data) => {
return data.result.status === expected;
}), {
errorMsg: 'Waiting for certificate status failed',
timeout: timeout * 1000,
interval: 5000
});
});
// Creates CA files for testing, if they already exist they will be deleted
// and recreated with the same content. This is to ensure that the files exist
// for testing and are in a known state.
Cypress.Commands.add('createCustomCerts', ({domain, certFile, keyFile}) => {
domain = domain || 'website1.example.com';
certFile = certFile || 'website1.pem';
keyFile = keyFile || 'website1.key.pem';
return cy.task('getFixturesFolder').then((fixturesFolder) => {
const fullCertFile = `${fixturesFolder}/${certFile}`;
const fullKeyFile = `${fixturesFolder}/${keyFile}`;
const cmd = `mkcert -cert-file="${fullCertFile}" -key-file="${fullKeyFile}" "${domain}"`;
cy.log(`Creating custom certs with command: ${cmd}`);
return cy.exec(cmd).then((result) => {
cy.log(`mkcert output:\n${JSON.stringify(result)}`);
expect(result.exitCode).to.eq(0);
return cy.exec('mkcert -install').then((result2) => {
cy.log(`mkcert install output:\n${JSON.stringify(result2)}`);
expect(result2.exitCode).to.eq(0);
}).then(() => {
return cy.wrap({
certFile: fullCertFile,
keyFile: fullKeyFile,
});
});
});
});
});