Bug 42772: Cypress.env => cy.env

Warning: The allowCypressEnv configuration option is enabled. This allows any browser code to read values from Cypress.env(). This is insecure and will be removed in a future major version.

1. Replace Cypress.env() calls with cy.env() (for sensitive values) or Cypress.expose() (for public configuration)
2. Set allowCypressEnv: false in your Cypress configuration to disable Cypress.env()

Learn more: https://on.cypress.io/cypress-env-migration

Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Jonathan Druart 2026-06-08 14:51:01 +02:00 committed by Pedro Amorim
parent 7b307694fa
commit f89e68c220
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
4 changed files with 178 additions and 156 deletions

View File

@ -12,11 +12,14 @@ export default defineConfig({
setupNodeEvents(on, config) { setupNodeEvents(on, config) {
return require("./t/cypress/plugins/index.js")(on, config); return require("./t/cypress/plugins/index.js")(on, config);
}, },
allowCypressEnv: false,
baseUrl: process.env.KOHA_INTRANET_URL || "http://localhost:8081", baseUrl: process.env.KOHA_INTRANET_URL || "http://localhost:8081",
specPattern: "t/cypress/integration/**/*.*", specPattern: "t/cypress/integration/**/*.*",
supportFile: "t/cypress/support/e2e.js", supportFile: "t/cypress/support/e2e.js",
env: { env: {
opacBaseUrl: process.env.KOHA_OPAC_URL || "http://localhost:8080", opacBaseUrl: process.env.KOHA_OPAC_URL || "http://localhost:8080",
kohaUsername: process.env.KOHA_USER || "koha",
kohaPassword: process.env.KOHA_PASS || "koha",
apiUsername: process.env.KOHA_USER || "koha", apiUsername: process.env.KOHA_USER || "koha",
apiPassword: process.env.KOHA_PASS || "koha", apiPassword: process.env.KOHA_PASS || "koha",
}, },

View File

@ -55,9 +55,10 @@ describe("Patron self-renewal", () => {
}); });
it("should verify that the patron wants to renew their account", function () { it("should verify that the patron wants to renew their account", function () {
cy.visitOpac("/cgi-bin/koha/opac-user.pl"); cy.visitOpac("/cgi-bin/koha/opac-user.pl");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"GET", "GET",
Cypress.env("opacBaseUrl") + opacBaseUrl +
"/api/v1/public/patrons/*/self_renewal?_per_page=-1", "/api/v1/public/patrons/*/self_renewal?_per_page=-1",
{ {
self_renewal_settings: { self_renewal_settings: {
@ -67,6 +68,7 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("renewalConfig"); ).as("renewalConfig");
});
cy.get("#patronSelfRenewal", { timeout: 10000 }); cy.get("#patronSelfRenewal", { timeout: 10000 });
cy.get("#self_renewal_available a").click(); cy.get("#self_renewal_available a").click();
cy.wait("@renewalConfig"); cy.wait("@renewalConfig");
@ -76,9 +78,10 @@ describe("Patron self-renewal", () => {
}); });
it("should renew a patron's account", function () { it("should renew a patron's account", function () {
cy.visitOpac("/cgi-bin/koha/opac-user.pl"); cy.visitOpac("/cgi-bin/koha/opac-user.pl");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"GET", "GET",
Cypress.env("opacBaseUrl") + opacBaseUrl +
"/api/v1/public/patrons/*/self_renewal?_per_page=-1", "/api/v1/public/patrons/*/self_renewal?_per_page=-1",
{ {
self_renewal_settings: { self_renewal_settings: {
@ -88,13 +91,14 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("renewalConfig"); ).as("renewalConfig");
});
cy.get("#patronSelfRenewal", { timeout: 10000 }); cy.get("#patronSelfRenewal", { timeout: 10000 });
cy.get("#self_renewal_available a").click(); cy.get("#self_renewal_available a").click();
cy.wait("@renewalConfig"); cy.wait("@renewalConfig");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"POST", "POST",
Cypress.env("opacBaseUrl") + opacBaseUrl + "/api/v1/public/patrons/*/self_renewal",
"/api/v1/public/patrons/*/self_renewal",
{ {
statusCode: 201, statusCode: 201,
body: { body: {
@ -103,6 +107,7 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("submitRenewal"); ).as("submitRenewal");
});
cy.get("#patronSelfRenewal .verification_actions") cy.get("#patronSelfRenewal .verification_actions")
.contains("Yes") .contains("Yes")
.click(); .click();
@ -113,26 +118,30 @@ describe("Patron self-renewal", () => {
}); });
it("should display an information message step", function () { it("should display an information message step", function () {
cy.visitOpac("/cgi-bin/koha/opac-user.pl"); cy.visitOpac("/cgi-bin/koha/opac-user.pl");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"GET", "GET",
Cypress.env("opacBaseUrl") + opacBaseUrl +
"/api/v1/public/patrons/*/self_renewal?_per_page=-1", "/api/v1/public/patrons/*/self_renewal?_per_page=-1",
{ {
self_renewal_settings: { self_renewal_settings: {
opac_patron_details: "0", opac_patron_details: "0",
self_renewal_failure_message: self_renewal_failure_message:
"Your self-renewal can't be processed at this time. Please visit your local branch to complete your renewal.", "Your self-renewal can't be processed at this time. Please visit your local branch to complete your renewal.",
self_renewal_information_messages: ["This should be shown"], self_renewal_information_messages: [
"This should be shown",
],
}, },
} }
).as("renewalConfig"); ).as("renewalConfig");
});
cy.get("#patronSelfRenewal", { timeout: 10000 }); cy.get("#patronSelfRenewal", { timeout: 10000 });
cy.get("#self_renewal_available a").click(); cy.get("#self_renewal_available a").click();
cy.wait("@renewalConfig"); cy.wait("@renewalConfig");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"POST", "POST",
Cypress.env("opacBaseUrl") + opacBaseUrl + "/api/v1/public/patrons/*/self_renewal",
"/api/v1/public/patrons/*/self_renewal",
{ {
statusCode: 201, statusCode: 201,
body: { body: {
@ -141,6 +150,7 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("submitRenewal"); ).as("submitRenewal");
});
cy.get("#patronSelfRenewal .verification_question").contains( cy.get("#patronSelfRenewal .verification_question").contains(
"This should be shown" "This should be shown"
); );
@ -158,9 +168,10 @@ describe("Patron self-renewal", () => {
}); });
it("should confirm patron details if required", function () { it("should confirm patron details if required", function () {
cy.visitOpac("/cgi-bin/koha/opac-user.pl"); cy.visitOpac("/cgi-bin/koha/opac-user.pl");
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"GET", "GET",
Cypress.env("opacBaseUrl") + opacBaseUrl +
"/api/v1/public/patrons/*/self_renewal?_per_page=-1", "/api/v1/public/patrons/*/self_renewal?_per_page=-1",
{ {
self_renewal_settings: { self_renewal_settings: {
@ -170,6 +181,7 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("renewalConfig"); ).as("renewalConfig");
});
cy.get("#patronSelfRenewal", { timeout: 10000 }); cy.get("#patronSelfRenewal", { timeout: 10000 });
cy.get("#self_renewal_available a").click(); cy.get("#self_renewal_available a").click();
cy.wait("@renewalConfig"); cy.wait("@renewalConfig");
@ -183,10 +195,10 @@ describe("Patron self-renewal", () => {
cy.get("#update-account div.alert.alert-info").contains( cy.get("#update-account div.alert.alert-info").contains(
"Please verify your details to proceed with your self-renewal" "Please verify your details to proceed with your self-renewal"
); );
cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) => {
cy.intercept( cy.intercept(
"POST", "POST",
Cypress.env("opacBaseUrl") + opacBaseUrl + "/api/v1/public/patrons/*/self_renewal",
"/api/v1/public/patrons/*/self_renewal",
{ {
statusCode: 201, statusCode: 201,
body: { body: {
@ -195,6 +207,7 @@ describe("Patron self-renewal", () => {
}, },
} }
).as("submitRenewal"); ).as("submitRenewal");
});
cy.get("#update-account fieldset.action input[type='submit']") cy.get("#update-account fieldset.action input[type='submit']")
.contains("Submit renewal request") .contains("Submit renewal request")
.click(); .click();

View File

@ -3,12 +3,17 @@ describe("SCO", () => {
// cy.request() — no browser navigation, so no cross-origin issues. // cy.request() — no browser navigation, so no cross-origin issues.
function setSyspref(variable: string, value: string) { function setSyspref(variable: string, value: string) {
// Step 1: GET the login page to get an anonymous session + its CSRF token // Step 1: GET the login page to get an anonymous session + its CSRF token
cy.request("/cgi-bin/koha/mainpage.pl?logout.x=1").then(loginPage => { cy.env(["apiUsername", "apiPassword"]).then(
({ apiUsername, apiPassword }) => {
cy.request("/cgi-bin/koha/mainpage.pl?logout.x=1").then(
loginPage => {
const csrfForLogin = (loginPage.body as string).match( const csrfForLogin = (loginPage.body as string).match(
/<meta name="csrf-token" content="([^"]+)"/ /<meta name="csrf-token" content="([^"]+)"/
)?.[1]; )?.[1];
if (!csrfForLogin) if (!csrfForLogin)
throw new Error("CSRF token not found on login page"); throw new Error(
"CSRF token not found on login page"
);
// Step 2: POST credentials — Koha authenticates and redirects to the // Step 2: POST credentials — Koha authenticates and redirects to the
// logged-in mainpage whose body contains the new session's CSRF token // logged-in mainpage whose body contains the new session's CSRF token
@ -16,11 +21,12 @@ describe("SCO", () => {
method: "POST", method: "POST",
url: "/cgi-bin/koha/mainpage.pl", url: "/cgi-bin/koha/mainpage.pl",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type":
"application/x-www-form-urlencoded",
}, },
body: [ body: [
`login_userid=${encodeURIComponent(Cypress.env("apiUsername"))}`, `login_userid=${encodeURIComponent(apiUsername)}`,
`login_password=${encodeURIComponent(Cypress.env("apiPassword"))}`, `login_password=${encodeURIComponent(apiPassword)}`,
`csrf_token=${encodeURIComponent(csrfForLogin)}`, `csrf_token=${encodeURIComponent(csrfForLogin)}`,
`login_op=cud-login`, `login_op=cud-login`,
].join("&"), ].join("&"),
@ -29,20 +35,26 @@ describe("SCO", () => {
/<meta name="csrf-token" content="([^"]+)"/ /<meta name="csrf-token" content="([^"]+)"/
)?.[1]; )?.[1];
if (!csrfForSvc) if (!csrfForSvc)
throw new Error("CSRF token not found on main page"); throw new Error(
"CSRF token not found on main page"
);
// Step 3: POST to svc endpoint — session cookie sent automatically // Step 3: POST to svc endpoint — session cookie sent automatically
cy.request({ cy.request({
method: "POST", method: "POST",
url: "/cgi-bin/koha/svc/config/systempreferences", url: "/cgi-bin/koha/svc/config/systempreferences",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type":
"application/x-www-form-urlencoded",
"CSRF-TOKEN": csrfForSvc, "CSRF-TOKEN": csrfForSvc,
}, },
body: `pref_${encodeURIComponent(variable)}=${encodeURIComponent(value)}`, body: `pref_${encodeURIComponent(variable)}=${encodeURIComponent(value)}`,
}); });
}); });
}); }
);
}
);
} }
beforeEach(() => { beforeEach(() => {

View File

@ -75,46 +75,40 @@ Cypress.on("uncaught:exception", err => {
} }
}); });
function get_fallback_login_value(param) {
var env_var = param == "username" ? "KOHA_USER" : "KOHA_PASS";
return typeof Cypress.env(env_var) === "undefined"
? "koha"
: Cypress.env(env_var);
}
Cypress.Commands.add("visitOpac", path => { Cypress.Commands.add("visitOpac", path => {
cy.visit(Cypress.env("opacBaseUrl") + path); cy.env(["opacBaseUrl"]).then(({ opacBaseUrl }) =>
cy.visit(opacBaseUrl + path)
);
}); });
Cypress.Commands.add("login", (username, password) => { Cypress.Commands.add("login", (username, password) => {
cy.env(["kohaUsername", "kohaPassword"]).then(
({ kohaUsername, kohaPassword }) => {
var user = var user =
typeof username === "undefined" typeof username === "undefined" ? kohaUsername : username;
? get_fallback_login_value("username")
: username;
var pass = var pass =
typeof password === "undefined" typeof password === "undefined" ? kohaPassword : password;
? get_fallback_login_value("password")
: password;
cy.visit("/cgi-bin/koha/mainpage.pl?logout.x=1"); cy.visit("/cgi-bin/koha/mainpage.pl?logout.x=1");
cy.get("#userid").type(user); cy.get("#userid").type(user);
cy.get("#password").type(pass); cy.get("#password").type(pass);
cy.get("#submit-button").click(); cy.get("#submit-button").click();
}
);
}); });
Cypress.Commands.add("loginOpac", (username, password) => { Cypress.Commands.add("loginOpac", (username, password) => {
cy.env(["kohaUsername", "kohaPassword"]).then(
({ kohaUsername, kohaPassword }) => {
var user = var user =
typeof username === "undefined" typeof username === "undefined" ? kohaUsername : username;
? get_fallback_login_value("username")
: username;
var pass = var pass =
typeof password === "undefined" typeof password === "undefined" ? kohaPassword : password;
? get_fallback_login_value("password")
: password;
cy.visitOpac("/cgi-bin/koha/opac-main.pl?logout.x=1"); cy.visitOpac("/cgi-bin/koha/opac-main.pl?logout.x=1");
cy.get("#userid").type(user); cy.get("#userid").type(user);
cy.get("#password").type(pass); cy.get("#password").type(pass);
cy.get("#auth .action").contains("Log in").click(); cy.get("#auth .action").contains("Log in").click();
}
);
}); });
Cypress.Commands.add("left_menu_active_item_is", label => { Cypress.Commands.add("left_menu_active_item_is", label => {