Skip to content

Commit

Permalink
fix(cypress): fix user login in routing cypress framework (#5950)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixincreate authored Sep 19, 2024
1 parent dee91b3 commit 90949d9
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 83 deletions.
14 changes: 12 additions & 2 deletions cypress-tests/cypress/e2e/PaymentUtils/Commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ function normalise(input) {
};

if (typeof input !== "string") {
const spec_name = Cypress.spec.name.split("-")[1].split(".")[0];
return `${spec_name}`;
const specName = Cypress.spec.name;

if (specName.includes("-")) {
const parts = specName.split("-");

if (parts.length > 1 && parts[1].includes(".")) {
return parts[1].split(".")[0];
}
}

// Fallback
return `${specName}`;
}

const lowerCaseInput = input.toLowerCase();
Expand Down
48 changes: 24 additions & 24 deletions cypress-tests/cypress/e2e/PaymentUtils/Novalnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ export const connectorDetails = {
payment_method: "card",
billing: {
address: {
line1: "1467",
line2: "CA",
line3: "CA",
city: "Musterhausen",
state: "California",
zip: "12345",
country: "DE",
first_name: "Max",
last_name: "Mustermann"
line1: "1467",
line2: "CA",
line3: "CA",
city: "Musterhausen",
state: "California",
zip: "12345",
country: "DE",
first_name: "Max",
last_name: "Mustermann",
},
email: "[email protected]",
phone: {
number: "9123456789",
country_code: "+91"
}
number: "9123456789",
country_code: "+91",
},
},
payment_method_data: {
card: successfulThreeDSTestCardDetails,
Expand All @@ -68,21 +68,21 @@ export const connectorDetails = {
payment_method: "card",
billing: {
address: {
line1: "1467",
line2: "CA",
line3: "CA",
city: "Musterhausen",
state: "California",
zip: "12345",
country: "DE",
first_name: "Max",
last_name: "Mustermann"
line1: "1467",
line2: "CA",
line3: "CA",
city: "Musterhausen",
state: "California",
zip: "12345",
country: "DE",
first_name: "Max",
last_name: "Mustermann",
},
email: "[email protected]",
phone: {
number: "9123456789",
country_code: "+91"
}
number: "9123456789",
country_code: "+91",
},
},
payment_method_data: {
card: successfulThreeDSTestCardDetails,
Expand Down
15 changes: 15 additions & 0 deletions cypress-tests/cypress/e2e/PayoutUtils/Commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ function normalise(input) {
// Add more known exceptions here
};

if (typeof input !== "string") {
const specName = Cypress.spec.name;

if (specName.includes("-")) {
const parts = specName.split("-");

if (parts.length > 1 && parts[1].includes(".")) {
return parts[1].split(".")[0];
}
}

// Fallback
return `${specName}`;
}

if (exceptions[input.toLowerCase()]) {
return exceptions[input.toLowerCase()];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ let globalState;

describe("Priority Based Routing Test", () => {
let should_continue = true;
context("Create Jwt Token", () => {

context("Login", () => {
before("seed global state", () => {
cy.task("getGlobalState").then((state) => {
globalState = new State(state);
Expand All @@ -16,14 +17,11 @@ describe("Priority Based Routing Test", () => {
after("flush global state", () => {
cy.task("setGlobalState", globalState.data);
});
it("create-jwt-token", () => {
let data = utils.getConnectorDetails("common")["jwt"];
let req_data = data["Request"];
let res_data = data["Response"];

cy.createJWTToken(req_data, res_data, globalState);
if (should_continue)
should_continue = utils.should_continue_further(res_data);
it("User login", () => {
cy.userLogin(globalState);
cy.terminate2Fa(globalState);
cy.userInfo(globalState);
});

it("merchant retrieve call", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import * as utils from "../RoutingUtils/Utils";
let globalState;

describe("Volume Based Routing Test", () => {
let should_continue = true;

context("Create Jwt Token", () => {
context("Login", () => {
before("seed global state", () => {
cy.task("getGlobalState").then((state) => {
globalState = new State(state);
Expand All @@ -17,14 +15,11 @@ describe("Volume Based Routing Test", () => {
after("flush global state", () => {
cy.task("setGlobalState", globalState.data);
});
it("create-jwt-token", () => {
let data = utils.getConnectorDetails("common")["jwt"];
let req_data = data["Request"];
let res_data = data["Response"];

cy.createJWTToken(req_data, res_data, globalState);
if (should_continue)
should_continue = utils.should_continue_further(res_data);
it("User login", () => {
cy.userLogin(globalState);
cy.terminate2Fa(globalState);
cy.userInfo(globalState);
});

it("merchant retrieve call", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import * as utils from "../RoutingUtils/Utils";
let globalState;

describe("Rule Based Routing Test", () => {
let should_continue = true;

context("Create Jwt Token", () => {
context("Login", () => {
before("seed global state", () => {
cy.task("getGlobalState").then((state) => {
globalState = new State(state);
Expand All @@ -17,12 +15,11 @@ describe("Rule Based Routing Test", () => {
after("flush global state", () => {
cy.task("setGlobalState", globalState.data);
});
it("create-jwt-token", () => {
let data = utils.getConnectorDetails("common")["jwt"];
let req_data = data["Request"];
let res_data = data["Response"];

cy.createJWTToken(req_data, res_data, globalState);
it("User login", () => {
cy.userLogin(globalState);
cy.terminate2Fa(globalState);
cy.userInfo(globalState);
});

it("merchant retrieve call", () => {
Expand Down
10 changes: 4 additions & 6 deletions cypress-tests/cypress/e2e/RoutingTest/00003-Retries.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ describe("Auto Retries & Step Up 3DS", () => {
cy.task("setGlobalState", globalState.data);
});

it("Create JWT token", () => {
let data = utils.getConnectorDetails("common")["jwt"];
let req_data = data["Request"];
let res_data = data["Response"];

cy.createJWTToken(req_data, res_data, globalState);
it("User login", () => {
cy.userLogin(globalState);
cy.terminate2Fa(globalState);
cy.userInfo(globalState);
});

it("List MCA", () => {
Expand Down
106 changes: 81 additions & 25 deletions cypress-tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,6 @@ Cypress.Commands.add(
(confirmBody, req_data, res_data, confirm, globalState) => {
const paymentIntentId = globalState.get("paymentID");
const connectorId = globalState.get("connectorId");
console.log("connectorId", connectorId);
for (const key in req_data) {
confirmBody[key] = req_data[key];
}
Expand All @@ -989,7 +988,6 @@ Cypress.Commands.add(
expect(response.headers["content-type"]).to.include("application/json");
globalState.set("paymentID", paymentIntentId);
globalState.set("connectorId", response.body.connector);
console.log("connectorId", response.body.connector);
globalState.set("paymentMethodType", confirmBody.payment_method_type);

switch (response.body.authentication_type) {
Expand Down Expand Up @@ -2063,46 +2061,104 @@ Cypress.Commands.add("retrievePayoutCallTest", (globalState) => {
});
});

Cypress.Commands.add("createJWTToken", (req_data, res_data, globalState) => {
const jwt_body = {
// User API calls
// Below 3 commands should be called in sequence to login a user
Cypress.Commands.add("userLogin", (globalState) => {
// Define the necessary variables and constant
const base_url = globalState.get("baseUrl");
const query_params = `token_only=true`;
const signin_body = {
email: `${globalState.get("email")}`,
password: `${globalState.get("password")}`,
};
const url = `${base_url}/user/v2/signin?${query_params}`;

cy.request({
method: "POST",
url: `${globalState.get("baseUrl")}/user/v2/signin`,
url: url,
headers: {
"Content-Type": "application/json",
},
body: signin_body,
failOnStatusCode: false,
body: jwt_body,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);
expect(response.headers["content-type"]).to.include("application/json");

if (response.status === 200) {
expect(response.body).to.have.property("token");
//set jwt_token
globalState.set("jwtToken", response.body.token);
if (response.body.token_type === "totp") {
expect(response.body).to.have.property("token").and.to.not.be.empty;

//setting merchantId for manual create merchant
globalState.set("merchantId", response.body.merchant_id);
globalState.set("totpToken", response.body.token);
cy.task("setGlobalState", globalState.data);
}
} else {
throw new Error(
`User login call failed to get totp token with status ${response.status} and message ${response.body.message}`
);
}
});
});
Cypress.Commands.add("terminate2Fa", (globalState) => {
// Define the necessary variables and constant
const base_url = globalState.get("baseUrl");
const query_params = `skip_two_factor_auth=true`;
const api_key = globalState.get("totpToken");
const url = `${base_url}/user/2fa/terminate?${query_params}`;

// set session cookie
const sessionCookie = response.headers["set-cookie"][0];
const sessionValue = sessionCookie.split(";")[0];
globalState.set("cookie", sessionValue);
cy.request({
method: "GET",
url: url,
headers: {
Authorization: `Bearer ${api_key}`,
"Content-Type": "application/json",
},
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);

// set api key
globalState.set("apiKey", globalState.get("routingApiKey"));
globalState.set("merchantId", response.body.merchant_id);
if (response.status === 200) {
if (response.body.token_type === "user_info") {
expect(response.body).to.have.property("token").and.to.not.be.empty;

for (const key in res_data.body) {
expect(res_data.body[key]).to.equal(response.body[key]);
globalState.set("userInfoToken", response.body.token);
cy.task("setGlobalState", globalState.data);
}
} else {
defaultErrorHandler(response, res_data);
throw new Error(
`2FA terminate call failed with status ${response.status} and message ${response.body.message}`
);
}
});
});
Cypress.Commands.add("userInfo", (globalState) => {
// Define the necessary variables and constant
const base_url = globalState.get("baseUrl");
const api_key = globalState.get("userInfoToken");
const url = `${base_url}/user`;

cy.request({
method: "GET",
url: url,
headers: {
Authorization: `Bearer ${api_key}`,
"Content-Type": "application/json",
},
failOnStatusCode: false,
}).then((response) => {
logRequestId(response.headers["x-request-id"]);

if (response.status === 200) {
expect(response.body).to.have.property("merchant_id").and.to.not.be.empty;
expect(response.body).to.have.property("org_id").and.to.not.be.empty;
expect(response.body).to.have.property("profile_id").and.to.not.be.empty;

globalState.set("merchantId", response.body.merchant_id);
globalState.set("organizationId", response.body.org_id);
globalState.set("profileId", response.body.profile_id);
} else {
throw new Error(
`User login call failed to fetch user info with status ${response.status} and message ${response.body.message}`
);
}
});
});
Expand Down Expand Up @@ -2145,9 +2201,9 @@ Cypress.Commands.add(
method: "POST",
url: `${globalState.get("baseUrl")}/routing`,
headers: {
Authorization: `Bearer ${globalState.get("userInfoToken")}`,
"Content-Type": "application/json",
Cookie: `${globalState.get("cookie")}`,
"api-key": `Bearer ${globalState.get("jwtToken")}`,
},
failOnStatusCode: false,
body: routingBody,
Expand Down Expand Up @@ -2176,9 +2232,9 @@ Cypress.Commands.add(
method: "POST",
url: `${globalState.get("baseUrl")}/routing/${routing_config_id}/activate`,
headers: {
Authorization: `Bearer ${globalState.get("userInfoToken")}`,
"Content-Type": "application/json",
Cookie: `${globalState.get("cookie")}`,
"api-key": globalState.get("apiKey"),
},
failOnStatusCode: false,
}).then((response) => {
Expand All @@ -2205,9 +2261,9 @@ Cypress.Commands.add(
method: "GET",
url: `${globalState.get("baseUrl")}/routing/${routing_config_id}`,
headers: {
Authorization: `Bearer ${globalState.get("userInfoToken")}`,
"Content-Type": "application/json",
Cookie: `${globalState.get("cookie")}`,
"api-key": globalState.get("apiKey"),
},
failOnStatusCode: false,
}).then((response) => {
Expand Down

0 comments on commit 90949d9

Please sign in to comment.