Merge pull request #13076 from ToolJet/test/fix-restAPI-automation

Fixed cypress failure for Rest API
This commit is contained in:
Ganesh Kumar 2025-06-24 23:07:23 +05:30 committed by GitHub
commit 18206c6b53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 86 additions and 47 deletions

View file

@ -337,7 +337,7 @@ describe("Data source Rest API", () => {
`cypress-${data.dataSourceName}-restapi`,
"restapi",
[
{ key: "url", value: Cypress.env("restAPI_BaseURL") },
{ key: "url", value: "https://jsonplaceholder.typicode.com" },
{ key: "auth_type", value: "none" },
{ key: "grant_type", value: "authorization_code" },
{ key: "add_token_to", value: "header" },
@ -370,62 +370,100 @@ describe("Data source Rest API", () => {
cy.apiCreateApp(`${fake.companyName}-restAPI-CURD-App`);
cy.openApp();
createAndRunRestAPIQuery({
queryName: "get_beeceptor_data",
queryName: "get_all_users",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "GET",
urlSuffix: "/api/users",
urlSuffix: "/users",
run: true,
expectedResponseShape: {
"0.id": true,
"0.name": true,
"0.email": true,
},
});
createAndRunRestAPIQuery({
queryName: "post_restapi",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "POST",
headersList: [["Content-Type", "application/json"]],
rawBody: '{"price": 200,"name": "Violin"}',
urlSuffix: "/api/users",
expectedResponseShape: { price: 200, name: "Violin", id: true },
rawBody: `{
"name": "Test User",
"username": "testuser",
"email": "test@example.com",
"address": {
"street": "123 Test St",
"city": "Testville",
"zipcode": "12345"
}
}`,
urlSuffix: "/users",
run: true,
expectedResponseShape: {
id: true,
name: "Test User",
email: "test@example.com",
},
});
cy.readFile("cypress/fixtures/restAPI/storedId.json").then(
(postResponseID) => {
const id1 = postResponseID.id;
const id1 = 1;
createAndRunRestAPIQuery({
queryName: "put_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "PUT",
headersList: [["Content-Type", "application/json"]],
rawBody: '{"price": 500,"name": "Guitar"}',
urlSuffix: `/api/users/${id1}`,
expectedResponseShape: { price: 500, name: "Guitar", id: id1 },
});
createAndRunRestAPIQuery({
queryName: "patch_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "PATCH",
headersList: [["Content-Type", "application/json"]],
rawBody: '{"price": 999 }',
urlSuffix: `/api/users/${id1}`,
run: true,
expectedResponseShape: { price: 999, id: id1 },
});
createAndRunRestAPIQuery({
queryName: "get_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "GET",
urlSuffix: `/api/users/${id1}`,
run: true,
expectedResponseShape: { price: 999, name: "Guitar", id: id1 },
});
createAndRunRestAPIQuery({
queryName: "delete_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "DELETE",
urlSuffix: `/api/users/${id1}`,
run: true,
expectedResponseShape: { success: true },
});
}
);
createAndRunRestAPIQuery({
queryName: "put_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "PUT",
headersList: [["Content-Type", "application/json"]],
rawBody: `{
"id": 1,
"name": "Fully Updated User",
"username": "updateduser",
"email": "updated@example.com",
"address": {
"street": "456 Updated St",
"city": "Updatedville",
"zipcode": "54321"
}
}`,
urlSuffix: `/users/${id1}`,
run: true,
expectedResponseShape: {
id: id1,
name: "Fully Updated User",
email: "updated@example.com",
},
});
createAndRunRestAPIQuery({
queryName: "patch_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "PATCH",
headersList: [["Content-Type", "application/json"]],
rawBody: `{
"email": "partially.updated@example.com"
}`,
urlSuffix: `/users/${id1}`,
run: true,
expectedResponseShape: {
id: id1,
email: "partially.updated@example.com",
},
});
createAndRunRestAPIQuery({
queryName: "get_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "GET",
urlSuffix: `/users/${id1}`,
run: true,
expectedResponseShape: {
id: id1,
email: "Sincere@april.biz",
},
});
createAndRunRestAPIQuery({
queryName: "delete_restapi_id",
dsName: `cypress-${data.dataSourceName}-restapi`,
method: "DELETE",
urlSuffix: `/users/${id1}`,
run: true,
expectedResponseShape: {},
});
cy.apiDeleteApp(`${fake.companyName}-restAPI-CURD-App`);
cy.apiDeleteGDS(`cypress-${data.dataSourceName}-restapi`);
});

View file

@ -122,10 +122,11 @@ export const createAndRunRestAPIQuery = ({
}
if (Array.isArray(responseData)) {
responseData.forEach((item) => {
expect(item).to.have.any.keys("id", "name", "price");
expect(item).to.have.any.keys("id", "name", "username", "email", "address");
});
}
if (responseData?.id) {
cy.log(responseData.id)
cy.writeFile("cypress/fixtures/restAPI/storedId.json", {
id: responseData.id,
});