refactor(cypress): move memory cache tests out of payment for misc (#6992)

Co-authored-by: Sumit Kumar <sumit.kumar@Sumit-Kumar-D9GXRYR75W.local>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Sumit Kumar <sumit.kumar@juspay.in>
Co-authored-by: Kartikeya Hegde <karthikey.hegde@juspay.in>
This commit is contained in:
Pa1NarK
2025-01-24 21:55:07 +05:30
committed by GitHub
parent 7fd3551afd
commit 4382fc650a
87 changed files with 746 additions and 543 deletions

View File

@ -29,9 +29,10 @@ import {
defaultErrorHandler,
extractIntegerAtEnd,
getValueByKey,
} from "../e2e/PaymentUtils/Utils";
} from "../e2e/configs/Payment/Utils";
import { execConfig, validateConfig } from "../utils/featureFlags";
import * as RequestBodyUtils from "../utils/RequestBodyUtils";
import { isoTimeTomorrow, validateEnv } from "../utils/RequestBodyUtils.js";
import { handleRedirection } from "./redirectionHandler";
function logRequestId(xRequestId) {
@ -44,11 +45,34 @@ function logRequestId(xRequestId) {
function validateErrorMessage(response, resData) {
if (resData.body.status !== "failed") {
expect(response.body.error_message).to.be.null;
expect(response.body.error_code).to.be.null;
expect(response.body.error_message, "error_message").to.be.null;
expect(response.body.error_code, "error_code").to.be.null;
}
}
Cypress.Commands.add("healthCheck", (globalState) => {
const baseUrl = globalState.get("baseUrl");
const url = `${baseUrl}/health`;
cy.request({
method: "GET",
url: url,
headers: {
Accept: "application/json",
},
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
if (response.status === 200) {
expect(response.body).to.equal("health is good");
} else {
throw new Error(
`Health Check failed with status: \`${response.status}\` and body: \`${response.body}\``
);
}
});
});
Cypress.Commands.add(
"merchantCreateCallTest",
(merchantCreateBody, globalState) => {
@ -297,34 +321,70 @@ Cypress.Commands.add(
});
}
);
// API Key API calls
Cypress.Commands.add("apiKeyCreateTest", (apiKeyCreateBody, globalState) => {
// Define the necessary variables and constant
const apiKey = globalState.get("adminApiKey");
const baseUrl = globalState.get("baseUrl");
// We do not want to keep API Key forever,
// so we set the expiry to tomorrow as new merchant accounts are created with every run
const expiry = isoTimeTomorrow();
const keyIdType = "key_id";
const keyId = validateEnv(baseUrl, keyIdType);
const merchantId = globalState.get("merchantId");
const url = `${baseUrl}/api_keys/${merchantId}`;
// Update request body
apiKeyCreateBody.expiration = expiry;
cy.request({
method: "POST",
url: `${globalState.get("baseUrl")}/api_keys/${globalState.get("merchantId")}`,
url: url,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"api-key": globalState.get("adminApiKey"),
"api-key": apiKey,
},
body: apiKeyCreateBody,
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
// Handle the response as needed
globalState.set("apiKey", response.body.api_key);
globalState.set("apiKeyId", response.body.key_id);
if (response.status === 200) {
expect(response.body.merchant_id).to.equal(merchantId);
expect(response.body.description).to.equal(apiKeyCreateBody.description);
// API Key assertions are intentionally excluded to avoid being exposed in the logs
expect(response.body).to.have.property(keyIdType).and.to.include(keyId)
.and.to.not.be.empty;
globalState.set("apiKeyId", response.body.key_id);
globalState.set("apiKey", response.body.api_key);
cy.task("setGlobalState", globalState.data);
} else {
// to be updated
throw new Error(
`API Key create call failed with status ${response.status} and message: "${response.body.error.message}"`
);
}
});
});
Cypress.Commands.add("apiKeyUpdateCall", (apiKeyUpdateBody, globalState) => {
const merchant_id = globalState.get("merchantId");
const api_key_id = globalState.get("apiKeyId");
const merchantId = globalState.get("merchantId");
const apiKeyId = globalState.get("apiKeyId");
// We do not want to keep API Key forever,
// so we set the expiry to tomorrow as new merchant accounts are created with every run
const expiry = isoTimeTomorrow();
// Update request body
apiKeyUpdateBody.expiration = expiry;
cy.request({
method: "POST",
url: `${globalState.get("baseUrl")}/api_keys/${merchant_id}/${api_key_id}`,
url: `${globalState.get("baseUrl")}/api_keys/${merchantId}/${apiKeyId}`,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
@ -335,10 +395,18 @@ Cypress.Commands.add("apiKeyUpdateCall", (apiKeyUpdateBody, globalState) => {
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
// Handle the response as needed
expect(response.body.name).to.equal("Updated API Key");
expect(response.body.key_id).to.equal(api_key_id);
expect(response.body.merchant_id).to.equal(merchant_id);
if (response.status === 200) {
expect(response.body.name).to.equal("Updated API Key");
expect(response.body.merchant_id).to.equal(merchantId);
// API Key assertions are intentionally excluded to avoid being exposed in the logs
expect(response.body.key_id).to.equal(apiKeyId);
} else {
// to be updated
throw new Error(
`API Key create call failed with status ${response.status} and message: "${response.body.error.message}"`
);
}
});
});
@ -3030,6 +3098,9 @@ Cypress.Commands.add("userLogin", (globalState) => {
.be.empty;
const totpToken = response.body.token;
if (!totpToken) {
throw new Error("No token received from login");
}
globalState.set("totpToken", totpToken);
}
} else {
@ -3063,6 +3134,9 @@ Cypress.Commands.add("terminate2Fa", (globalState) => {
.to.not.be.empty;
const userInfoToken = response.body.token;
if (!userInfoToken) {
throw new Error("No user info token received");
}
globalState.set("userInfoToken", userInfoToken);
}
} else {
@ -3075,14 +3149,18 @@ Cypress.Commands.add("terminate2Fa", (globalState) => {
Cypress.Commands.add("userInfo", (globalState) => {
// Define the necessary variables and constant
const baseUrl = globalState.get("baseUrl");
const apiKey = globalState.get("userInfoToken");
const userInfoToken = globalState.get("userInfoToken");
const url = `${baseUrl}/user`;
if (!userInfoToken) {
throw new Error("No user info token available");
}
cy.request({
method: "GET",
url: url,
headers: {
Authorization: `Bearer ${apiKey}`,
Authorization: `Bearer ${userInfoToken}`,
"Content-Type": "application/json",
},
failOnStatusCode: false,
@ -3101,7 +3179,7 @@ Cypress.Commands.add("userInfo", (globalState) => {
globalState.set("organizationId", response.body.org_id);
globalState.set("profileId", response.body.profile_id);
globalState.set("userInfoToken", apiKey);
globalState.set("userInfoToken", userInfoToken);
} else {
throw new Error(
`User login call failed to fetch user info with status: "${response.status}" and message: "${response.body.error.message}"`
@ -3254,56 +3332,6 @@ Cypress.Commands.add(
}
);
Cypress.Commands.add("updateConfig", (configType, globalState, value) => {
const base_url = globalState.get("baseUrl");
const merchant_id = globalState.get("merchantId");
const api_key = globalState.get("adminApiKey");
let key;
let url;
let body;
switch (configType) {
case "autoRetry":
key = `should_call_gsm_${merchant_id}`;
url = `${base_url}/configs/${key}`;
body = { key: key, value: value };
break;
case "maxRetries":
key = `max_auto_retries_enabled_${merchant_id}`;
url = `${base_url}/configs/${key}`;
body = { key: key, value: value };
break;
case "stepUp":
key = `step_up_enabled_${merchant_id}`;
url = `${base_url}/configs/${key}`;
body = { key: key, value: value };
break;
default:
throw new Error(
`Invalid config type passed into the configs: "${api_key}: ${value}"`
);
}
cy.request({
method: "POST",
url: url,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
body: body,
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
if (response.status === 200) {
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
}
});
});
Cypress.Commands.add("incrementalAuth", (globalState, data) => {
const { Request: reqData, Response: resData } = data || {};
@ -3370,94 +3398,52 @@ Cypress.Commands.add("incrementalAuth", (globalState, data) => {
});
});
Cypress.Commands.add("createConfigs", (globalState, key, value) => {
const base_url = globalState.get("baseUrl");
const api_key = globalState.get("adminApiKey");
Cypress.Commands.add("setConfigs", (globalState, key, value, requestType) => {
if (!key || !requestType) {
throw new Error("Key and requestType are required parameters");
}
const REQUEST_CONFIG = {
CREATE: { method: "POST", useKey: false },
UPDATE: { method: "POST", useKey: true },
FETCH: { method: "GET", useKey: true },
DELETE: { method: "DELETE", useKey: true },
};
const config = REQUEST_CONFIG[requestType];
if (!config) {
throw new Error(`Invalid requestType: ${requestType}`);
}
const apiKey = globalState.get("adminApiKey");
const baseUrl = globalState.get("baseUrl");
const url = `${baseUrl}/configs/${config.useKey ? key : ""}`;
const getRequestBody = {
CREATE: () => ({ key, value }),
UPDATE: () => ({ value }),
};
const body = getRequestBody[requestType]?.() || undefined;
cy.request({
method: "POST",
url: `${base_url}/configs/`,
method: config.method,
url,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
body: {
key: key,
value: value,
"api-key": apiKey,
},
...(body && { body }),
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
expect(response.status).to.equal(200);
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
});
});
Cypress.Commands.add("fetchConfigs", (globalState, key, value) => {
const base_url = globalState.get("baseUrl");
const api_key = globalState.get("adminApiKey");
cy.request({
method: "GET",
url: `${base_url}/configs/${key}`,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
expect(response.status).to.equal(200);
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
});
});
Cypress.Commands.add("updateConfigs", (globalState, key, value) => {
const base_url = globalState.get("baseUrl");
const api_key = globalState.get("adminApiKey");
cy.request({
method: "POST",
url: `${base_url}/configs/${key}`,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
body: {
key: key,
value: value,
},
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
expect(response.status).to.equal(200);
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
});
});
Cypress.Commands.add("deleteConfigs", (globalState, key, value) => {
const base_url = globalState.get("baseUrl");
const api_key = globalState.get("adminApiKey");
cy.request({
method: "DELETE",
url: `${base_url}/configs/${key}`,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
expect(response.status).to.equal(200);
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
if (response.status === 200) {
expect(response.body).to.have.property("key").to.equal(key);
expect(response.body).to.have.property("value").to.equal(value);
} else {
throw new Error(
`Failed to set configs with status ${response.status} and message ${response.body.error.message}`
);
}
});
});