From a02253c21b7163863ed4d33ad074248787833691 Mon Sep 17 00:00:00 2001 From: asekawa Date: Mon, 12 Feb 2024 23:57:40 +0530 Subject: [PATCH] Generate API commands based on resource access types --- src/api/commands/auth.ts | 54 +++++++++++++++++------------- src/api/commands/user-managment.ts | 12 +++---- types/api/commands.d.ts | 12 ++++--- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/api/commands/auth.ts b/src/api/commands/auth.ts index 7536695..8326f26 100644 --- a/src/api/commands/auth.ts +++ b/src/api/commands/auth.ts @@ -25,32 +25,38 @@ import { RequestContentTypes } from "../models/api-requests"; * if using Bearer Authentication * cy.getAuthorization("https://localhost:9443/t/carbon.super","admin","admin","client_credentails","Bearer" ) */ -Cypress.Commands.add("getAuthentication", (serverHost: string, username: string, password: string, - grantType: string, authType: "Basic" | "Bearer") => { - +Cypress.Commands.add("getBasicAuthentication", (username: string, password: string) => { + const encodedCredentials = btoa(username + ":" + password); - // Retrive token from Basic auth type - if (authType === "Basic") { - return cy.wrap(`Basic ${encodedCredentials}`); - } + return cy.wrap(`Basic ${encodedCredentials}`); + +}); + +Cypress.Commands.add("getBearerAuthentication", (token: string) => { + + + return cy.wrap(`Bearer ${token}`); +}); + +Cypress.Commands.add("getTokenViaClientCredential", (serverHost: string, clientID: string, clientSecret: string) => { + + const encodedCredentials = btoa(clientID + ":" + clientSecret); // Retrieve a bearer token from Bearer auth type `oauth2/token` endpoint. - else { - return cy.request({ - body: { - "grant_type": grantType, - "scope": "SYSTEM" - }, - headers: - { - "Authorization": `Bearer ${encodedCredentials}`, - "Content-Type": RequestContentTypes.URLENCODED - }, - method: "POST", - url: `${serverHost}/oauth2/token` - }); - } - - throw new Error("Invalid Authentication type"); + + return cy.request({ + body: { + + "grant_type": "client_credentials", + "scope": "SYSTEM" + }, + headers: + { + "Authorization": `Basic ${encodedCredentials}`, + "Content-Type": RequestContentTypes.URLENCODED + }, + method: "POST", + url: `${serverHost}/oauth2/token` + }); }); diff --git a/src/api/commands/user-managment.ts b/src/api/commands/user-managment.ts index 62590ef..46bc3c5 100644 --- a/src/api/commands/user-managment.ts +++ b/src/api/commands/user-managment.ts @@ -30,12 +30,8 @@ import { RequestContentTypes, RequestType } from "../models/api-requests"; * @param {jsonbody} reqBody - request body with user profile informations * @param {boolean} failOnStatusCode- Whether to fail on response codes other than 2xx and 3xx * */ -Cypress.Commands.add("createUserViaAPI", (host: string, username: string, password: string, reqBody: Cypress.ObjectLike, - grantType: string, authType: "Basic" | "Bearer", failOnStatusCode = true) => { - - cy.getAuthentication(host, username, password, grantType, authType).then(response => { - - const authHeaderValue: string = response; +Cypress.Commands.add("createUserViaAPI", (host: string ,authzHeader: string, reqBody: Cypress.ObjectLike, + failOnStatusCode = true ) => { return cy.request({ "method": RequestType.POST, @@ -44,9 +40,9 @@ Cypress.Commands.add("createUserViaAPI", (host: string, username: string, passwo "headers": { "Content-Type": RequestContentTypes.SCIMJSON, "accept": RequestContentTypes.SCIMJSON, - "Authorization": authHeaderValue + "Authorization": authzHeader }, "body": reqBody }); - }); + }); diff --git a/types/api/commands.d.ts b/types/api/commands.d.ts index 51422e8..d69bc46 100644 --- a/types/api/commands.d.ts +++ b/types/api/commands.d.ts @@ -25,8 +25,8 @@ declare namespace Cypress { /** * Custom command to create users from scim2.0 POST method */ - createUserViaAPI(host: string, username: string, password: string, reqBody: Cypress.ObjectLike, - grantType: string, authType: "Basic" | "Bearer", failOnStatusCode?: boolean): Cypress.Chainable; + createUserViaAPI(host: string, authzHeader: string, reqBody: Cypress.ObjectLike, + failOnStatusCode?: boolean): Cypress.Chainable; /** * This command use to get the Authentication method with the prefered token and grant type @@ -37,7 +37,11 @@ declare namespace Cypress { * @param {jsonbody} grantType - Prefeered grant type * @param {boolean} authType - Prefeered authentication type * */ - getAuthentication(host: string, username: string, password: string, grantType: string, - authType: "Basic" | "Bearer"): Cypress.Chainable; + getBasicAuthentication(username: string, password: string): Cypress.Chainable; + + getBearerAuthentication(token: string): Cypress.Chainable; + + getTokenViaClientCredential(serverHost: string, clientID: string, clientSecret: string): Cypress.Chainable; + } }