Skip to content

Commit

Permalink
Merge pull request #44 from wso2/aw/2
Browse files Browse the repository at this point in the history
Generate API commands based on resource access types
  • Loading branch information
asekawa authored Feb 12, 2024
2 parents e02a95d + a02253c commit f60e1af
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
54 changes: 30 additions & 24 deletions src/api/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
});
});
12 changes: 4 additions & 8 deletions src/api/commands/user-managment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
});
});

});
12 changes: 8 additions & 4 deletions types/api/commands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
createUserViaAPI(host: string, authzHeader: string, reqBody: Cypress.ObjectLike,
failOnStatusCode?: boolean): Cypress.Chainable<any>;

/**
* This command use to get the Authentication method with the prefered token and grant type
Expand All @@ -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<any>;
getBasicAuthentication(username: string, password: string): Cypress.Chainable<any>;

getBearerAuthentication(token: string): Cypress.Chainable<any>;

getTokenViaClientCredential(serverHost: string, clientID: string, clientSecret: string): Cypress.Chainable<any>;

}
}

0 comments on commit f60e1af

Please sign in to comment.