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

Bypass staging rate limiting when running cypress tests #2046

Merged
merged 10 commits into from
Jan 13, 2025
36 changes: 31 additions & 5 deletions tests_cypress/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import LoginPage from "../Notify/Admin/Pages/LoginPage";
let links_checked = [];
let svgs_checked = [];

// if the tests failed, reset the arrays so the links are re-checked and a link failure is treated as a real failure
afterEach(function() {
if (this.currentTest.state === 'failed') {
links_checked = [];
svgs_checked = [];
}
});

Cypress.Commands.add('a11yScan', (url, options = { a11y: true, htmlValidate: true, deadLinks: true, mimeTypes: true, axeConfig: false }) => {
const current_hostname = config.Hostnames.Admin;
// bypass rate limiting
cy.intercept(`${current_hostname}/*`, (req) => {
req.headers['waf-secret'] = Cypress.env(config.CONFIG_NAME).WAF_SECRET
});


if (url) {
cy.visit(url);
}
Expand Down Expand Up @@ -102,3 +106,25 @@ Cypress.Commands.add('login', (username, password, agreeToTerms = true) => {
LoginPage.Login(username, password, agreeToTerms);
});
});

// this adds the waf-secret to cy.visit()'s that target the admin hostname
Cypress.Commands.overwrite('visit', (originalFn, url, options = {}) => {
// Get full URL by combining baseUrl with path
const fullUrl = url.startsWith('http')
? url
: `${Cypress.config('baseUrl')}${url}`;

// Only add headers if URL matches admin hostname
if (fullUrl.includes(config.Hostnames.Admin)) {
const mergedOptions = {
...options,
headers: {
...options.headers,
'waf-secret': Cypress.env(config.CONFIG_NAME).WAF_SECRET
}
};
return originalFn(url, mergedOptions);
}

return originalFn(url, options);
});
13 changes: 12 additions & 1 deletion tests_cypress/cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@
import './commands'
import 'cypress-axe'
import 'cypress-html-validate/commands'
import config from "../../config";

// Alternatively you can use CommonJS syntax:
// require('./commands')

console.log("config", Cypress.config('baseUrl'));
console.log("config", Cypress.config('baseUrl'));

before(() => {
// Bypass rate-limit in staging for any requests not using cy.visit (css, js, etc)
cy.intercept('**/*', (req) => {
if (req.url.includes(config.Hostnames.Admin)) {
req.headers['waf-secret'] = Cypress.env(config.CONFIG_NAME).WAF_SECRET;
}
}).as('allRequests');

});
Loading