Skip to content

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

test(cypress): add Forex Rates coverage for platform (#12415)

HS-QUALIBOT2b5e12bf674d
diff --git a/crates/router/src/core/currency.rs b/crates/router/src/core/currency.rs
index 43b63cd6e6..9c5374f4dc 100644
--- a/crates/router/src/core/currency.rs
+++ b/crates/router/src/core/currency.rs
@@ -42,7 +42,14 @@ pub async fn convert_forex(
from_currency,
))
.await
- .change_context(ApiErrorResponse::InternalServerError)?,
+ .map_err(|err| match err.current_context() {
+ ForexCacheError::CurrencyNotAcceptable => {
+ err.change_context(ApiErrorResponse::InvalidRequestData {
+ message: "The provided currency is not acceptable".to_string(),
+ })
+ }
+ _ => err.change_context(ApiErrorResponse::InternalServerError),
+ })?,
))
}
diff --git a/cypress-tests/cypress/e2e/spec/Misc/00003-ForexRates.cy.js b/cypress-tests/cypress/e2e/spec/Misc/00003-ForexRates.cy.js
new file mode 100644
index 0000000000..b062a7e3d9
--- /dev/null
+++ b/cypress-tests/cypress/e2e/spec/Misc/00003-ForexRates.cy.js
@@ -0,0 +1,154 @@
+import * as fixtures from "../../../fixtures/imports";
+import State from "../../../utils/State";
+
+const baseUrl = Cypress.env("CYPRESS_BASEURL") || Cypress.env("BASEURL");
+
+(baseUrl?.includes("localhost") ? describe.skip : describe)(
+ "Forex Rates and Currency Conversion",
+ () => {
+ let globalState;
+
+ before("seed global state and create merchant account", () => {
+ cy.task("getGlobalState").then((state) => {
+ globalState = new State(state);
+ if (!globalState.get("baseUrl")) {
+ globalState.set("baseUrl", baseUrl);
+ }
+ if (!globalState.get("adminApiKey")) {
+ globalState.set(
+ "adminApiKey",
+ Cypress.env("ADMIN_API_KEY") || Cypress.env("CYPRESS_ADMIN_API_KEY")
+ );
+ }
+ return cy
+ .merchantCreateCallTest(fixtures.merchantCreateBody, globalState)
+ .then(() => {
+ return cy.apiKeyCreateTest(fixtures.apiKeyCreateBody, globalState);
+ })
+ .then(() => {
+ return cy.createCustomerCallTest(
+ fixtures.customerCreateBody,
+ globalState
+ );
+ });
+ });
+ });
+
+ after("flush global state", () => {
+ cy.task("setGlobalState", globalState.data);
+ });
+
+ context("Happy Path Tests", () => {
+ it("should retrieve forex rates with base currency", () => {
+ cy.getForexRates(globalState);
+ });
+
+ it("should convert small amount from USD to EUR", () => {
+ const amount = 100;
+ const fromCurrency = "USD";
+ const toCurrency = "EUR";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+
+ it("should convert realistic amount from USD to EUR", () => {
+ const amount = 10000;
+ const fromCurrency = "USD";
+ const toCurrency = "EUR";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+
+ it("should convert realistic amount from EUR to GBP", () => {
+ const amount = 50000;
+ const fromCurrency = "EUR";
+ const toCurrency = "GBP";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+ });
+
+ context("Negative Tests", () => {
+ it("should fail to convert with invalid source currency", () => {
+ const amount = 100;
+ const fromCurrency = "INVALID";
+ const toCurrency = "USD";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+
+ it("should fail to convert with invalid target currency", () => {
+ const amount = 100;
+ const fromCurrency = "USD";
+ const toCurrency = "INVALID";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+
+ it("should fail to convert with negative amount", () => {
+ const amount = -100;
+ const fromCurrency = "USD";
+ const toCurrency = "EUR";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+ });
+
+ context("Auth Failure Tests", () => {
+ it("should fail to retrieve forex rates without api key", () => {
+ cy.getForexRatesWithoutAuth(globalState);
+ });
+
+ it("should fail to convert currency with invalid api key", () => {
+ cy.convertCurrency(100, "USD", "EUR", globalState, {
+ apiKey: "invalid-api-key",
+ assert: (response) => {
+ expect(response.status).to.equal(401);
+ },
+ });
+ });
+ });
+
+ context("Missing Required Params Tests", () => {
+ it("should fail to convert without amount", () => {
+ cy.convertCurrencyMissingParam(
+ "amount",
+ 100,
+ "USD",
+ "EUR",
+ globalState
+ );
+ });
+
+ it("should fail to convert without from_currency", () => {
+ cy.convertCurrencyMissingParam(
+ "from_currency",
+ 100,
+ "USD",
+ "EUR",
+ globalState
+ );
+ });
+
+ it("should fail to convert without to_currency", () => {
+ cy.convertCurrencyMissingParam(
+ "to_currency",
+ 100,
+ "USD",
+ "EUR",
+ globalState
+ );
+ });
+ });
+
+ context("Additional Edge Case Tests", () => {
+ it("should handle float amount", () => {
+ const amount = 100.5;
+ const fromCurrency = "USD";
+ const toCurrency = "EUR";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+
+ it("should handle maximum integer amount", () => {
+ const amount = Number.MAX_SAFE_INTEGER;
+ const fromCurrency = "USD";
+ const toCurrency = "EUR";
+ cy.convertCurrency(amount, fromCurrency, toCurrency, globalState);
+ });
+ });
+ }
+);
diff --git a/cypress-tests/cypress/support/commands.js b/cypress-tests/cypress/support/commands.js
index 5b38228e71..30f9b525ae 100644
--- a/cypress-tests/cypress/support/commands.js
+++ b/cypress-tests/cypress/support/commands.js
@@ -9384,6 +9384,114 @@ Cypress.Commands.add("retrieveNonExistentPaymentLinkTest", (globalState) => {
});
});
+// ============================================
+// Forex Rates Commands
+// ============================================
+
+Cypress.Commands.add("getForexRates", (globalState) => {
+ const apiKey = globalState.get("apiKey") || globalState.get("adminApiKey");
+ const baseUrl = globalState.get("baseUrl");
+
+ cy.request({
+ method: "GET",
+ url: `${baseUrl}/forex/rates`,
+ headers: {
+ "Content-Type": "application/json",
+ "api-key": apiKey,
+ },
+ failOnStatusCode: false,
+ }).then((response) => {
+ logRequestId(response.headers["x-request-id"]);
+
+ cy.wrap(response).then(() => {
+ expect(response.status).to.equal(200);
+ expect(response.body).to.have.property("data");
+ expect(response.body.data).to.have.property("base_currency");
+ expect(response.body.data).to.have.property("conversion");
+ expect(typeof response.body.data.conversion).to.equal("object");
+ cy.task(
+ "cli_log",
+ `Forex rates - base_currency: ${response.body.data.base_currency}, rates count: ${Object.keys(response.body.data.conversion).length}`
+ );
+ });
+ });
+});
+
+function requestForexConvert(
+ baseUrl,
+ apiKey,
+ amount,
+ fromCurrency,
+ toCurrency
+) {
+ return cy.request({
+ method: "GET",
+ url: `${baseUrl}/forex/convert_from_minor?amount=${amount}&from_currency=${fromCurrency}&to_currency=${toCurrency}`,
+ headers: {
+ "Content-Type": "application/json",
+ "api-key": apiKey,
+ },
+ failOnStatusCode: false,
+ });
+}
+
+Cypress.Commands.add(
+ "convertCurrency",
+ (amount, fromCurrency, toCurrency, globalState, options = {}) => {
+ const apiKey =
+ options.apiKey ||
+ globalState.get("apiKey") ||
+ globalState.get("adminApiKey");
+ const baseUrl = globalState.get("baseUrl");
+
+ requestForexConvert(baseUrl, apiKey, amount, fromCurrency, toCurrency).then(
+ (response) => {
+ logRequestId(response.headers["x-request-id"]);
+
+ if (options.assert) {
+ options.assert(response);
+ return;
+ }
+
+ cy.wrap(response).then(() => {
+ if (response.status === 200) {
+ expect(response.body).to.have.property("converted_amount");
+ expect(response.body).to.have.property("currency");
+ expect(response.body.currency).to.equal(toCurrency);
+ cy.task(
+ "cli_log",
+ `Converted ${amount} ${fromCurrency} → ${response.body.converted_amount} ${response.body.currency}`
+ );
+ } else if (response.status === 400) {
+ expect(response.body).to.exist;
+ } else {
+ throw new Error(
+ `Unexpected forex convert status: ${response.status} - ${JSON.stringify(response.body)}`
+ );
+ }
+ });
+ }
+ );
+ }
+);
+
+Cypress.Commands.add("getForexRatesWithoutAuth", (globalState) => {
+ const baseUrl = globalState.get("baseUrl");
+
+ cy.request({
+ method: "GET",
+ url: `${baseUrl}/forex/rates`,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ failOnStatusCode: false,
+ }).then((response) => {
+ cy.wrap(response).then(() => {
+ expect(response.status, "status_code").to.equal(401);
+ });
+ });
+});
+
// ============================================
// Extended Card Info Commands
// ============================================
@@ -9496,3 +9604,30 @@ Cypress.Commands.add("setExtendedCardInfoConfigTest", (globalState) => {
});
});
});
+
+Cypress.Commands.add(
+ "convertCurrencyMissingParam",
+ (missingParam, amount, fromCurrency, toCurrency, globalState) => {
+ const apiKey = globalState.get("apiKey");
+ const baseUrl = globalState.get("baseUrl");
+
+ const params = new URLSearchParams();
+ if (missingParam !== "amount") params.set("amount", amount);
+ if (missingParam !== "from_currency")
+ params.set("from_currency", fromCurrency);
+ if (missingParam !== "to_currency") params.set("to_currency", toCurrency);
+
+ cy.request({
+ method: "GET",
+ url: `${baseUrl}/forex/convert_from_minor?${params.toString()}`,
+ headers: {
+ "Content-Type": "application/json",
+ "api-key": apiKey,
+ },
+ failOnStatusCode: false,
+ }).then((response) => {
+ logRequestId(response.headers["x-request-id"]);
+ expect(response.status).to.be.oneOf([400, 422]);
+ });
+ }
+);