From a0a3c2b58da87018897ba57fd005f47599bcb170 Mon Sep 17 00:00:00 2001 From: PiX <69745008+pixincreate@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:42:10 +0530 Subject: [PATCH] refactor(configs): re-use config function --- .../spec/Misc/00000-MemoryCacheConfigs.cy.js | 12 +- .../e2e/spec/Routing/00003-Retries.cy.js | 108 +++++++++--- cypress-tests/cypress/support/commands.js | 160 ++++-------------- 3 files changed, 123 insertions(+), 157 deletions(-) diff --git a/cypress-tests/cypress/e2e/spec/Misc/00000-MemoryCacheConfigs.cy.js b/cypress-tests/cypress/e2e/spec/Misc/00000-MemoryCacheConfigs.cy.js index 9a93dd10e9f4..3ac1161c7056 100644 --- a/cypress-tests/cypress/e2e/spec/Misc/00000-MemoryCacheConfigs.cy.js +++ b/cypress-tests/cypress/e2e/spec/Misc/00000-MemoryCacheConfigs.cy.js @@ -2,7 +2,7 @@ import State from "../../../utils/State"; let globalState; -describe("In Memory Cache Test", () => { +describe("In Memory Cache configs", () => { before("seed global state", () => { cy.task("getGlobalState").then((state) => { globalState = new State(state); @@ -19,17 +19,17 @@ describe("In Memory Cache Test", () => { const newValue = "new test value"; it("Create Configs", () => { - cy.createConfigs(globalState, key, value); - cy.fetchConfigs(globalState, key, value); + cy.setConfigs(globalState, key, value, "CREATE"); + cy.setConfigs(globalState, key, value, "FETCH"); }); it("Update Configs", () => { - cy.updateConfigs(globalState, key, newValue); - cy.fetchConfigs(globalState, key, newValue); + cy.setConfigs(globalState, key, newValue, "UPDATE"); + // cy.setConfigs(globalState, key, value, "FETCH"); }); it("delete configs", () => { - cy.deleteConfigs(globalState, key, newValue); + cy.setConfigs(globalState, key, newValue, "DELETE"); }); }); }); diff --git a/cypress-tests/cypress/e2e/spec/Routing/00003-Retries.cy.js b/cypress-tests/cypress/e2e/spec/Routing/00003-Retries.cy.js index 5ac47c6a24c1..ffbc829dfefe 100644 --- a/cypress-tests/cypress/e2e/spec/Routing/00003-Retries.cy.js +++ b/cypress-tests/cypress/e2e/spec/Routing/00003-Retries.cy.js @@ -42,7 +42,13 @@ describe("Auto Retries & Step Up 3DS", () => { context("Auto Retries", () => { context("[Config: enable] Auto retries", () => { it("Enable auto retries", () => { - cy.updateConfig("autoRetry", globalState, "true"); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `should_call_gsm_${merchantId}`, + "true", + "UPDATE" + ); }); context("Max auto retries", () => { @@ -93,9 +99,15 @@ describe("Auto Retries & Step Up 3DS", () => { }); context("Max auto retries = 2", () => { - const max_auto_retries = 2; + const maxAutoRetries = 2; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -133,16 +145,22 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); }); context("Max auto retries = 1", () => { - const max_auto_retries = 1; + const maxAutoRetries = 1; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -180,15 +198,21 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); }); context("Max auto retries = 0", () => { - const max_auto_retries = 0; + const maxAutoRetries = 0; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -226,7 +250,7 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); @@ -280,9 +304,15 @@ describe("Auto Retries & Step Up 3DS", () => { }); context("Max auto retries = 2", () => { - const max_auto_retries = 2; + const maxAutoRetries = 2; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -320,16 +350,22 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); }); context("Max auto retries = 1", () => { - const max_auto_retries = 1; + const maxAutoRetries = 1; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -367,16 +403,22 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); }); context("Max auto retries = 0", () => { - const max_auto_retries = 0; + const maxAutoRetries = 0; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); context("Make payment", () => { @@ -414,7 +456,7 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); @@ -429,14 +471,26 @@ describe("Auto Retries & Step Up 3DS", () => { }); it("[Config: enable] Step up for Stripe", () => { - cy.updateConfig("stepUp", globalState, '["stripe"]'); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `step_up_enabled_${merchantId}`, + '["stripe"]', + "UPDATE" + ); }); }); context("Make Payment", () => { - const max_auto_retries = 1; + const maxAutoRetries = 1; it("Update max auto retries", () => { - cy.updateConfig("maxRetries", globalState, `${max_auto_retries}`); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `max_auto_retries_enabled_${merchantId}`, + maxAutoRetries, + "UPDATE" + ); }); it("Payment create call", () => { @@ -468,7 +522,7 @@ describe("Auto Retries & Step Up 3DS", () => { globalState, null, true, - max_auto_retries + 1 + maxAutoRetries + 1 ); }); }); @@ -477,7 +531,13 @@ describe("Auto Retries & Step Up 3DS", () => { context("[Config: disable] Auto retries", () => { it("[Config: disable] Auto retries", () => { - cy.updateConfig("autoRetry", globalState, "false"); + const merchantId = globalState.get("merchantId"); + cy.setConfigs( + globalState, + `should_call_gsm_${merchantId}`, + "false", + "UPDATE" + ); }); it("[Config: disable] Step up GSM", () => { diff --git a/cypress-tests/cypress/support/commands.js b/cypress-tests/cypress/support/commands.js index 444210757242..48d2ff466762 100644 --- a/cypress-tests/cypress/support/commands.js +++ b/cypress-tests/cypress/support/commands.js @@ -32,8 +32,8 @@ import { } from "../e2e/configs/Payment/Utils"; import { execConfig, validateConfig } from "../utils/featureFlags"; import * as RequestBodyUtils from "../utils/RequestBodyUtils"; -import { handleRedirection } from "./redirectionHandler"; import { isoTimeTomorrow, validateEnv } from "../utils/RequestBodyUtils.js"; +import { handleRedirection } from "./redirectionHandler"; function logRequestId(xRequestId) { if (xRequestId) { @@ -3247,56 +3247,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 || {}; @@ -3363,94 +3313,50 @@ 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"); - - cy.request({ - method: "POST", - url: `${base_url}/configs/`, - 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("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("setConfigs", (globalState, key, value, requestType) => { + if (!key || !requestType) { + throw new Error("Key and requestType are required parameters"); + } -Cypress.Commands.add("updateConfigs", (globalState, key, value) => { - const base_url = globalState.get("baseUrl"); - const api_key = globalState.get("adminApiKey"); + const REQUEST_CONFIG = { + CREATE: { method: "POST", useKey: false }, + UPDATE: { method: "POST", useKey: true }, + FETCH: { method: "GET", useKey: true }, + DELETE: { method: "DELETE", useKey: true }, + }; - 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"]); + const config = REQUEST_CONFIG[requestType]; + if (!config) { + throw new Error(`Invalid requestType: ${requestType}`); + } - 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); - }); -}); + const apiKey = globalState.get("adminApiKey"); + const baseUrl = globalState.get("baseUrl"); + const url = `${baseUrl}/configs/${config.useKey ? key : ""}`; -Cypress.Commands.add("deleteConfigs", (globalState, key, value) => { - const base_url = globalState.get("baseUrl"); - const api_key = globalState.get("adminApiKey"); + const body = ["CREATE", "UPDATE"].includes(requestType) + ? { key, value } + : undefined; cy.request({ - method: "DELETE", - url: `${base_url}/configs/${key}`, + method: config.method, + url, headers: { "Content-Type": "application/json", - "api-key": api_key, + "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); + 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}` + ); + } }); });