Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
test: add Client Session Validation Cypress tests (#12465)
HS-QUALIBOT58098a225006
diff --git a/cypress-tests/cypress/e2e/configs/Payment/Commons.js b/cypress-tests/cypress/e2e/configs/Payment/Commons.jsindex f74548850a..7e3dd90ad8 100644--- a/cypress-tests/cypress/e2e/configs/Payment/Commons.js+++ b/cypress-tests/cypress/e2e/configs/Payment/Commons.js@@ -3386,6 +3386,27 @@ export const connectorDetails = {},},}),+ ClientSessionInvalidConfirm: getCustomExchange({+ Request: {+ payment_method: "card",+ payment_method_data: {+ card: successfulNo3DSCardDetails,+ },+ currency: "USD",+ customer_acceptance: null,+ setup_future_usage: "on_session",+ },+ Response: {+ status: 401,+ body: {+ error: {+ type: "invalid_request",+ message: "API key not provided or invalid API key used",+ code: "IR_01",+ },+ },+ },+ }),},upi_pm: {PaymentIntent: getCustomExchange({diff --git a/cypress-tests/cypress/e2e/configs/Payment/Stripe.js b/cypress-tests/cypress/e2e/configs/Payment/Stripe.jsindex 4047fd1a2d..cc1c1a400c 100644--- a/cypress-tests/cypress/e2e/configs/Payment/Stripe.js+++ b/cypress-tests/cypress/e2e/configs/Payment/Stripe.js@@ -1005,6 +1005,8 @@ export const connectorDetails = {},},},+ ClientSessionInvalidConfirm:+ commonConnectorDetails.card_pm.ClientSessionInvalidConfirm,},bank_transfer_pm: {Ach: {diff --git a/cypress-tests/cypress/e2e/configs/Payment/Utils.js b/cypress-tests/cypress/e2e/configs/Payment/Utils.jsindex 9caa11d5c9..cd93247078 100644--- a/cypress-tests/cypress/e2e/configs/Payment/Utils.js+++ b/cypress-tests/cypress/e2e/configs/Payment/Utils.js@@ -655,6 +655,7 @@ export const CONNECTOR_LISTS = {"worldpayvantiv",],POLL_CONFIG: ["stripe"],+ CLIENT_SESSION_VALIDATION: ["stripe"],// Add more inclusion lists},};diff --git a/cypress-tests/cypress/e2e/spec/Payment/52-ClientSessionValidation.cy.js b/cypress-tests/cypress/e2e/spec/Payment/52-ClientSessionValidation.cy.jsnew file mode 100644index 0000000000..82aab3b4fe--- /dev/null+++ b/cypress-tests/cypress/e2e/spec/Payment/52-ClientSessionValidation.cy.js@@ -0,0 +1,230 @@+import * as fixtures from "../../../fixtures/imports";+import State from "../../../utils/State";+import getConnectorDetails, {+ CONNECTOR_LISTS,+} from "../../configs/Payment/Utils";+import * as utils from "../../configs/Payment/Utils";++let globalState;++describe("Client Session Validation", () => {+ before("seed global state", () => {+ cy.task("getGlobalState").then((state) => {+ globalState = new State(state);+ });+ });++ after("flush global state", () => {+ cy.task("setGlobalState", globalState.data);+ });++ before("connector gate", function () {+ const connectorId = globalState.get("connectorId");+ if (+ !CONNECTOR_LISTS.INCLUDE.CLIENT_SESSION_VALIDATION.includes(connectorId)+ ) {+ this.skip();+ }+ });++ context("Valid SDK Authorization - Confirm with SDK Authorization", () => {+ let shouldContinue = true;++ beforeEach(function () {+ if (!shouldContinue) {+ this.skip();+ }+ });++ it("Create Payment Intent", () => {+ const data = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["PaymentIntent"];++ cy.createPaymentIntentTest(+ fixtures.createPaymentBody,+ data,+ "no_three_ds",+ "automatic",+ globalState+ );++ if (shouldContinue) shouldContinue = utils.should_continue_further(data);+ });++ it("Confirm with valid SDK Authorization", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["No3DSAutoCapture"];++ cy.confirmWithSdkAuthTest(+ fixtures.confirmBody,+ confirmData,+ true,+ globalState+ );++ if (shouldContinue)+ shouldContinue = utils.should_continue_further(confirmData);+ });++ it("Retrieve Payment", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["No3DSAutoCapture"];++ cy.retrievePaymentCallTest({ globalState, data: confirmData });+ });+ });++ context(+ "Invalid SDK Authorization - Confirm with tampered sdk_authorization",+ () => {+ let shouldContinue = true;++ beforeEach(function () {+ if (!shouldContinue) {+ this.skip();+ }+ });++ it("Create Payment Intent", () => {+ const data = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["PaymentIntent"];++ cy.createPaymentIntentTest(+ fixtures.createPaymentBody,+ data,+ "no_three_ds",+ "automatic",+ globalState+ );++ if (shouldContinue)+ shouldContinue = utils.should_continue_further(data);+ });++ it("Confirm with invalid sdk_authorization - expect 401", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["ClientSessionInvalidConfirm"];++ cy.confirmWithSdkAuthTest(+ fixtures.confirmBody,+ confirmData,+ true,+ globalState,+ "invalid_session"+ );+ });+ }+ );++ context(+ "Missing SDK Authorization - Confirm without sdk_authorization (legacy fallback)",+ () => {+ let shouldContinue = true;++ beforeEach(function () {+ if (!shouldContinue) {+ this.skip();+ }+ });++ it("Create Payment Intent", () => {+ const data = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["PaymentIntent"];++ cy.createPaymentIntentTest(+ fixtures.createPaymentBody,+ data,+ "no_three_ds",+ "automatic",+ globalState+ );++ if (shouldContinue)+ shouldContinue = utils.should_continue_further(data);+ });++ it("Confirm without sdk_authorization - legacy fallback", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["No3DSAutoCapture"];++ cy.confirmWithSdkAuthTest(+ fixtures.confirmBody,+ confirmData,+ true,+ globalState,+ "missing_session"+ );++ if (shouldContinue)+ shouldContinue = utils.should_continue_further(confirmData);+ });++ it("Retrieve Payment", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["No3DSAutoCapture"];++ cy.retrievePaymentCallTest({ globalState, data: confirmData });+ });+ }+ );++ context(+ "Expired SDK Authorization - Confirm after session TTL expires",+ () => {+ let shouldContinue = true;++ beforeEach(function () {+ if (!shouldContinue) {+ this.skip();+ }+ });++ it("Create Payment Intent with 60s session expiry", () => {+ const data = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["PaymentIntent"];++ const createBody = {+ ...fixtures.createPaymentBody,+ session_expiry: 60,+ };++ cy.createPaymentIntentTest(+ createBody,+ data,+ "no_three_ds",+ "automatic",+ globalState+ );++ if (shouldContinue)+ shouldContinue = utils.should_continue_further(data);+ });++ it("Wait for Redis session TTL to expire", () => {+ cy.wait(61000); // Wait 61 seconds for Redis TTL to expire+ });++ it("Confirm with expired sdk_authorization - expect 401", () => {+ const confirmData = getConnectorDetails(globalState.get("connectorId"))[+ "card_pm"+ ]["ClientSessionInvalidConfirm"];++ cy.confirmWithSdkAuthTest(+ fixtures.confirmBody,+ confirmData,+ true,+ globalState+ );+ });+ }+ );+});diff --git a/cypress-tests/cypress/support/commands.js b/cypress-tests/cypress/support/commands.jsindex 32373c6afb..c486f92a61 100644--- a/cypress-tests/cypress/support/commands.js+++ b/cypress-tests/cypress/support/commands.js@@ -2404,7 +2404,12 @@ Cypress.Commands.add(const clientSecret = response.body.client_secret;globalState.set("clientSecret", clientSecret);globalState.set("paymentID", response.body.payment_id);- // Store the actual setup_future_usage value from the response+ if (response.body.sdk_authorization) {+ globalState.set(+ "sdkAuthorization",+ response.body.sdk_authorization+ );+ }globalState.set("actualSetupFutureUsage",response.body.setup_future_usage@@ -9583,6 +9588,160 @@ Cypress.Commands.add(});+Cypress.Commands.add(+ "confirmWithSdkAuthTest",+ (confirmBody, data, confirm, globalState, overrideSdkAuth) => {+ const {+ Configs: configs = {},+ Request: reqData,+ Response: resData,+ } = data || {};++ const configInfo = execConfig(validateConfig(configs));+ const merchantConnectorId = globalState.get(+ `${configInfo.merchantConnectorPrefix}Id`+ );+ const paymentIntentID = globalState.get("paymentID");+ const profileId = globalState.get(`${configInfo.profilePrefix}Id`);+ const url = `${globalState.get("baseUrl")}/payments/${paymentIntentID}/confirm`;++ if (confirmBody.split_payments) {+ delete confirmBody.split_payments;+ }++ const sdkAuth = globalState.get("sdkAuthorization") || "";+ const authParts = {};+ if (sdkAuth) {+ try {+ const decodedSdkAuth = atob(sdkAuth);+ decodedSdkAuth.split(",").forEach((part) => {+ const [key, ...valueParts] = part.split("=");+ authParts[key.trim()] = valueParts.join("=").trim();+ });+ } catch (e) {+ Cypress.log({+ name: "confirmWithSdkAuthTest",+ message: `Failed to decode sdkAuthorization: ${e.message}`,+ });+ }+ } else {+ Cypress.log({+ name: "confirmWithSdkAuthTest",+ message:+ "sdkAuthorization is empty - falling back to publishable_key auth",+ });+ }++ const publishableKey =+ authParts["publishable_key"] || globalState.get("publishableKey");+ const clientSecret =+ authParts["client_secret"] || globalState.get("clientSecret");+ const profileIdForHeader = authParts["profile_id"] || profileId;+ const customerIdFromAuth = authParts["customer_id"];+ const paymentIdFromAuth = authParts["payment_id"];++ let authorizationHeader;+ if (overrideSdkAuth === "missing_session") {+ // Legacy fallback: do NOT send Authorization header.+ // confirmWithSdkAuthTest will naturally fall back to api-key header+ // and client_secret in body when authorizationHeader is undefined.+ authorizationHeader = undefined;+ } else if (overrideSdkAuth === "invalid_session") {+ let header = `profile_id=${profileIdForHeader},publishable_key=${publishableKey},client_secret=${clientSecret},client_session_id=cs_invalid_tampered_session_id`;+ if (customerIdFromAuth) header += `,customer_id=${customerIdFromAuth}`;+ if (paymentIdFromAuth) header += `,payment_id=${paymentIdFromAuth}`;+ authorizationHeader = btoa(header);+ } else if (+ overrideSdkAuth &&+ overrideSdkAuth !== "missing_session" &&+ overrideSdkAuth !== "invalid_session"+ ) {+ authorizationHeader = overrideSdkAuth;+ } else {+ authorizationHeader = sdkAuth;+ }++ if (!authorizationHeader) {+ confirmBody.client_secret = clientSecret;+ }+ confirmBody.confirm = confirm;+ confirmBody.profile_id = profileId;++ for (const key in reqData) {+ if (key !== "split_payments") {+ confirmBody[key] = reqData[key];+ }+ }++ if (!reqData?.setup_future_usage && confirmBody.setup_future_usage) {+ delete confirmBody.setup_future_usage;+ }++ const headers = {+ "Content-Type": "application/json",+ };+ if (authorizationHeader) {+ headers["Authorization"] = authorizationHeader;+ } else {+ headers["api-key"] = publishableKey;+ }++ cy.request({+ method: "POST",+ url: url,+ headers: headers,+ failOnStatusCode: false,+ body: confirmBody,+ }).then((response) => {+ logRequestId(response.headers["x-request-id"]);+ storeRequestId(response.headers["x-request-id"], globalState);++ cy.wrap(response).then(() => {+ expect(response.headers["content-type"]).to.include("application/json");++ if (response.status === 200) {+ globalState.set("paymentID", paymentIntentID);+ updateConnectorState(globalState, response.body.connector);+ globalState.set(+ "connectorTransactionID",+ response.body.connector_transaction_id+ );+ globalState.set("paymentIntentStatus", response.body.status);++ const expectedConnector = getOriginalConnectorName(+ globalState.get("connectorId")+ );+ expect(response.body.connector, "connector").to.equal(+ expectedConnector+ );+ expect(paymentIntentID, "payment_id").to.equal(+ response.body.payment_id+ );+ expect(response.body.payment_method_data, "payment_method_data").to+ .not.be.empty;+ expect(merchantConnectorId, "connector_id").to.equal(+ response.body.merchant_connector_id+ );++ for (const key in resData.body) {+ expect(resData.body[key], [key]).to.deep.equal(response.body[key]);+ }++ validateErrorMessage(response, resData);+ } else {+ const errorDetail = `status=${response.status} body=${JSON.stringify(response.body)}`;+ if (resData.status === 200) {+ throw new Error(+ `Expecting valid response but got an error response: ${errorDetail}`+ );+ }+ defaultErrorHandler(response, resData);+ }+ });+ });+ }+);+Cypress.Commands.add("retrieveNonExistentPaymentLinkTest", (globalState) => {cy.request({method: "GET",