From 79b52ba5f52da616ef64250f06a70fd62ef97ed5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 16 Jun 2025 11:10:07 +0200 Subject: [PATCH] Bug 40173: Add test for the api-client Cypress plugin --- t/cypress/integration/t/api-client.ts | 28 ++++++++++++++ t/cypress/plugins/api-client.js | 56 +++++++++++++++++++++++++++ t/cypress/plugins/index.js | 15 +++++++ 3 files changed, 99 insertions(+) create mode 100644 t/cypress/integration/t/api-client.ts create mode 100644 t/cypress/plugins/api-client.js diff --git a/t/cypress/integration/t/api-client.ts b/t/cypress/integration/t/api-client.ts new file mode 100644 index 00000000000..7fb60112272 --- /dev/null +++ b/t/cypress/integration/t/api-client.ts @@ -0,0 +1,28 @@ +const { APIClient } = require("./../../plugins/dist/api-client.cjs.js"); + +describe("Using APIClient", () => { + let client = APIClient.default; + it("should 404 for non-existent biblio", async () => { + try { + await client.koha.get({ + endpoint: "/api/v1/public/biblios/99999", + return_response: true, + }); + } catch (error) { + expect(error.response.status).to.equal(404); + } + }); +}); + +describe("Using the api-client plugin", () => { + it("should 404 for non-existent biblio", async () => { + try { + await cy.task("apiGet", { + endpoint: "/api/v1/public/biblios/99999", + return_response: true, + }); + } catch (error) { + expect(error.response.status).to.equal(404); + } + }); +}); diff --git a/t/cypress/plugins/api-client.js b/t/cypress/plugins/api-client.js new file mode 100644 index 00000000000..db246e419e4 --- /dev/null +++ b/t/cypress/plugins/api-client.js @@ -0,0 +1,56 @@ +const { APIClient } = require("./dist/api-client.cjs.js"); + +const client = APIClient.default.koha; + +const prepareRequest = params => { + const { baseUrl, endpoint, authHeader, headers = {}, ...rest } = params; + const url = baseUrl + endpoint; + const finalHeaders = { + ...headers, + ...(authHeader ? { Authorization: authHeader } : {}), + }; + return { url, headers: finalHeaders, rest }; +}; + +const apiGet = params => { + const { url, headers, rest } = prepareRequest(params); + return client.get({ + endpoint: url, + headers, + ...rest, + }); +}; + +const apiPost = params => { + const { url, headers, rest } = prepareRequest(params); + return client.post({ + endpoint: url, + headers, + ...rest, + }); +}; + +const apiPut = params => { + const { url, headers, rest } = prepareRequest(params); + return client.put({ + endpoint: url, + headers, + ...rest, + }); +}; + +const apiDelete = params => { + const { url, headers, rest } = prepareRequest(params); + return client.delete({ + endpoint: url, + headers, + ...rest, + }); +}; + +module.exports = { + apiGet, + apiPost, + apiPut, + apiDelete, +}; diff --git a/t/cypress/plugins/index.js b/t/cypress/plugins/index.js index 837e6d747cb..4f961d2caeb 100644 --- a/t/cypress/plugins/index.js +++ b/t/cypress/plugins/index.js @@ -4,6 +4,8 @@ const mysql = require("cypress-mysql"); const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); +const { apiGet, apiPost, apiPut, apiDelete } = require("./api-client.js"); + module.exports = (on, config) => { on("dev-server:start", options => startDevServer({ @@ -16,6 +18,19 @@ module.exports = (on, config) => { on("task", { buildSampleObject, buildSampleObjects, + + apiGet(args) { + return apiGet({ ...args, baseUrl, authHeader }); + }, + apiPost(args) { + return apiPost({ ...args, baseUrl, authHeader }); + }, + apiPut(args) { + return apiPut({ ...args, baseUrl, authHeader }); + }, + apiDelete(args) { + return apiDelete({ ...args, baseUrl, authHeader }); + }, }); return config; }; -- 2.34.1