From 546ca8dbbf995be0dc07e27b85b4c0d66d7e7421 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 18 Jun 2025 12:18:38 +0200 Subject: [PATCH] Bug 40174: Introduce Cypress auth plugin So we can easily retrieve the value for the Authorization header. --- cypress.config.ts | 4 ++++ t/cypress/plugins/auth.js | 12 ++++++++++++ t/cypress/plugins/index.js | 15 ++++++++++++++- t/cypress/plugins/insertData.js | 8 +++----- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 t/cypress/plugins/auth.js diff --git a/cypress.config.ts b/cypress.config.ts index e67eb344f30..fd17efc7857 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -16,4 +16,8 @@ export default defineConfig({ specPattern: "t/cypress/integration/**/*.*", supportFile: "t/cypress/support/e2e.js", }, + env: { + apiUsername: "koha", + apiPassword: "koha", + }, }); diff --git a/t/cypress/plugins/auth.js b/t/cypress/plugins/auth.js new file mode 100644 index 00000000000..6104c0c626d --- /dev/null +++ b/t/cypress/plugins/auth.js @@ -0,0 +1,12 @@ +const { Buffer } = require("buffer"); + +const getBasicAuthHeader = (username, password) => { + const credentials = Buffer.from(`${username}:${password}`).toString( + "base64" + ); + return `Basic ${credentials}`; +}; + +module.exports = { + getBasicAuthHeader, +}; diff --git a/t/cypress/plugins/index.js b/t/cypress/plugins/index.js index 1045e9b6919..ff52883306a 100644 --- a/t/cypress/plugins/index.js +++ b/t/cypress/plugins/index.js @@ -8,12 +8,19 @@ const { deleteSampleObjects, } = require("./insertData.js"); +const { getBasicAuthHeader } = require("./auth.js"); + const { query } = require("./db.js"); const { apiGet, apiPost, apiPut, apiDelete } = require("./api-client.js"); module.exports = (on, config) => { const baseUrl = config.baseUrl; + const authHeader = getBasicAuthHeader( + config.env.apiUsername, + config.env.apiPassword + ); + on("dev-server:start", options => startDevServer({ options, @@ -21,10 +28,16 @@ module.exports = (on, config) => { ); on("task", { + getBasicAuthHeader() { + return getBasicAuthHeader( + config.env.apiUsername, + config.env.apiPassword + ); + }, buildSampleObject, buildSampleObjects, insertSampleBiblio({ item_count }) { - return insertSampleBiblio(item_count, baseUrl); + return insertSampleBiblio(item_count, baseUrl, authHeader); }, insertObject({ type, object }) { return insertObject(type, object, baseUrl, authHeader); diff --git a/t/cypress/plugins/insertData.js b/t/cypress/plugins/insertData.js index a9d63de45f4..e4fb6b484e7 100644 --- a/t/cypress/plugins/insertData.js +++ b/t/cypress/plugins/insertData.js @@ -2,9 +2,8 @@ const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); const { query } = require("./db.js"); const { APIClient } = require("./dist/api-client.cjs.js"); -const { Buffer } = require("buffer"); -const insertSampleBiblio = async (item_count, baseUrl) => { +const insertSampleBiblio = async (item_count, baseUrl, authHeader) => { let client = APIClient.default; let generated_objects = {}; const objects = [{ object: "library" }, { object: "item_type" }]; @@ -53,13 +52,12 @@ const insertSampleBiblio = async (item_count, baseUrl) => { }, ], }; - const credentials = Buffer.from("koha:koha").toString("base64"); let result = await client.koha.post({ endpoint: `${baseUrl}/api/v1/biblios`, headers: { "Content-Type": "application/marc-in-json", "x-confirm-not-duplicate": 1, - Authorization: "Basic " + credentials, + Authorization: authHeader, }, body: biblio, }); @@ -132,7 +130,7 @@ const insertSampleBiblio = async (item_count, baseUrl) => { body: item, headers: { "Content-Type": "application/json", - Authorization: "Basic " + credentials, + Authorization: authHeader, }, }) .then(i => createdItems.push(i)); -- 2.34.1