Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pre issue access token password grant test. #21091

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.identity.integration.test.mocks.MockServer;
import org.wso2.identity.integration.test.actions.mockserver.ActionsMockServer;
import org.wso2.identity.integration.test.rest.api.server.action.management.v1.model.ActionModel;
import org.wso2.identity.integration.test.rest.api.server.action.management.v1.model.AuthenticationType;
import org.wso2.identity.integration.test.rest.api.server.action.management.v1.model.Endpoint;
Expand Down Expand Up @@ -141,6 +141,7 @@ public class PreIssueAccessTokenClientCredentialsGrantTestCase extends ActionsBa
private String userId;
private String roleId;
private JWTClaimsSet jwtClaims;
private ActionsMockServer actionsMockServer;

/**
* Initializes Test environment and sets up necessary configurations.
Expand Down Expand Up @@ -187,7 +188,9 @@ protected boolean isRedirectable(String method) {

addUserWithRole(applicationId, customScopes);

MockServer.createMockServer(MOCK_SERVER_ENDPOINT);
actionsMockServer = new ActionsMockServer();

actionsMockServer.createMockServer(MOCK_SERVER_ENDPOINT);
actionId = createPreIssueAccessTokenAction();
}

Expand All @@ -199,7 +202,7 @@ public void atEnd() throws Exception {
deleteApp(applicationId);
deleteDomainAPI(domainAPIId);
scim2RestClient.deleteUser(userId);
MockServer.shutDownMockServer();
actionsMockServer.shutDownMockServer();
restClient.closeHttpClient();
scim2RestClient.closeHttpClient();
actionsRestClient.closeHttpClient();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package org.wso2.identity.integration.test.mocks;
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.identity.integration.test.actions.mockserver;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import org.apache.commons.lang.StringUtils;

import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

Expand All @@ -14,16 +40,49 @@
* This class starts a mock server on a specified port and sets up predefined
* responses for POST requests to simulate various operations relation to action execution.
*/
public class MockServer {
public class ActionsMockServer {

private WireMockServer wireMockServer;

public void startServer() {

private static WireMockServer wireMockServer;
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(8587));
wireMockServer.start();
}

public void stopServer() {

if (wireMockServer != null && wireMockServer.isRunning()) {
wireMockServer.stop();
}
}

public void setupStub(String url, String authMethod, String responseBody) {

wireMockServer.stubFor(post(urlEqualTo(url))
.withHeader("Authorization", matching(authMethod))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(responseBody)));
}

public String getReceivedRequestPayload(String url) {

List<LoggedRequest> requestList = wireMockServer.findAll(postRequestedFor(urlEqualTo(url)));
if (requestList == null || requestList.isEmpty()) {
return StringUtils.EMPTY;
}

return requestList.get(0).getBodyAsString();
}

/**
* Create a mock server with wiremock.
*
* @throws Exception If an error occurred while creating the server
*/
public static void createMockServer(String mockEndpoint) throws Exception {
public void createMockServer(String mockEndpoint) throws Exception {

wireMockServer = new WireMockServer(wireMockConfig().port(8587));

Expand Down Expand Up @@ -142,7 +201,7 @@ public static void createMockServer(String mockEndpoint) throws Exception {
/**
* Shut down the wiremock server instance.
*/
public static void shutDownMockServer() {
public void shutDownMockServer() {

wireMockServer.stop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.identity.integration.test.actions.model;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* This class represents the model of the access token sent in the request payload
* to the API endpoint of the pre issue access token action.
*/
@JsonDeserialize(builder = AccessToken.Builder.class)
public class AccessToken {

private final String tokenType;
List<String> scopes;
List<Claim> claims;

private AccessToken(Builder builder) {

this.tokenType = builder.tokenType;
this.scopes = builder.scopes;
this.claims = builder.claims;
}

public String getTokenType() {

return tokenType;
}

public List<String> getScopes() {

return scopes;
}

public List<Claim> getClaims() {

return claims;
}

public Claim getClaim(String name) {

if (claims != null) {
for (Claim claim : claims) {
if (claim.getName().equals(name)) {
return claim;
}
}
}

return null;
}

@Override
public boolean equals(Object o) {

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccessToken that = (AccessToken) o;
return Objects.equals(tokenType, that.tokenType) && (scopes == null ? that.scopes == null :
scopes.size() == that.scopes.size() && scopes.containsAll(that.scopes)) &&
(claims == null ? that.claims == null :
claims.size() == that.claims.size() && claims.containsAll(that.claims));
}

@Override
public int hashCode() {

return Objects.hash(tokenType, scopes, claims);
}

/**
* Enum for standard claim names.
*/
public enum ClaimNames {

SUB("sub"),
ISS("iss"),
AUD("aud"),
CLIENT_ID("client_id"),
AUTHORIZED_USER_TYPE("aut"),
EXPIRES_IN("expires_in"),

TOKEN_BINDING_REF("binding_ref"),
TOKEN_BINDING_TYPE("binding_type"),
SUBJECT_TYPE("subject_type");

private final String name;

ClaimNames(String name) {

this.name = name;
}

public static boolean contains(String name) {

return Arrays.stream(ClaimNames.values())
.anyMatch(claimName -> claimName.name.equals(name));
}

public String getName() {

return name;
}
}

/**
* Model class for claims.
*/
public static class Claim {

private String name;
private Object value;

public Claim() {

}

public Claim(String name, Object value) {

this.name = name;
this.value = value;
}

public String getName() {

return name;
}

public void setName(String name) {

this.name = name;
}

public Object getValue() {

return value;
}

public void setValue(Object value) {

this.value = value;
}

@Override
public boolean equals(Object o) {

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Claim claim = (Claim) o;
return Objects.equals(name, claim.name) && Objects.equals(value, claim.value);
}

@Override
public int hashCode() {

return Objects.hash(name, value);
}
}

/**
* Builder for AccessToken.
*/
@JsonPOJOBuilder(withPrefix = "")
public static class Builder {

private String tokenType;
private List<String> scopes = new ArrayList<>();
private List<Claim> claims = new ArrayList<>();

public Builder tokenType(String tokenType) {

this.tokenType = tokenType;
return this;
}

public Builder scopes(List<String> scopes) {

this.scopes = scopes;
return this;
}

public Builder claims(List<Claim> claims) {

this.claims = claims;
return this;
}

public Builder addClaim(String name, Object value) {

this.claims.add(new Claim(name, value));
return this;
}

public Builder addScope(String scope) {

this.scopes.add(scope);
return this;
}

public String getTokenType() {

return tokenType;
}

public List<String> getScopes() {

return scopes;
}

public List<Claim> getClaims() {

return claims;
}

public Claim getClaim(String name) {

if (claims != null) {
for (Claim claim : claims) {
if (claim.getName().equals(name)) {
return claim;
}
}
}

return null;
}

public AccessToken build() {

return new AccessToken(this);
}
}
}
Loading