Bug 40173: Add test for the api-client Cypress plugin

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@openfifth.co.uk>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 892b0f4d53)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Jonathan Druart 2025-06-16 11:10:07 +02:00 committed by Paul Derscheid
parent 0df46127d7
commit 523fa34d69
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
3 changed files with 99 additions and 0 deletions

View File

@ -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);
}
});
});

View File

@ -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,
};

View File

@ -4,6 +4,8 @@ const { buildSampleObject, buildSampleObjects } = require("./mockData.js");
const { query } = require("./db.js");
const { apiGet, apiPost, apiPut, apiDelete } = require("./api-client.js");
module.exports = (on, config) => {
on("dev-server:start", options =>
startDevServer({
@ -15,6 +17,19 @@ module.exports = (on, config) => {
buildSampleObject,
buildSampleObjects,
query,
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;
};