Skip to content

Commit

Permalink
Update pre issue access token password grant test.
Browse files Browse the repository at this point in the history
  • Loading branch information
malithie committed Sep 12, 2024
1 parent c9d41e0 commit 5dc975e
Show file tree
Hide file tree
Showing 19 changed files with 1,733 additions and 62 deletions.
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
Loading

0 comments on commit 5dc975e

Please sign in to comment.