Skip to content

Commit

Permalink
Add API tests for get, update, delete requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shenali-SJ committed Nov 26, 2024
1 parent d6716db commit 373a4f2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.wso2.identity.integration.test.rest.api.server.idp.v1.util.UserDefinedAuthenticatorPayload;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -53,13 +54,13 @@ public class IdPSuccessTest extends IdPTestBase {
private String idPTemplateId;
private UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload;
private String idpCreatePayload;

private static final String FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER = "<FEDERATED_AUTHENTICATOR_ID>";
private static final String FEDERATED_AUTHENTICATOR_PLACEHOLDER = "\"<FEDERATED_AUTHENTICATOR>\"";
private static final String IDP_NAME_PLACEHOLDER = "<IDP_NAME>";
private static final String FEDERATED_AUTHENTICATOR_ID = "Y3VzdG9tQXV0aGVudGljYXRvcg==";
private static final String FEDERATED_AUTHENTICATOR_ID = "Y3VzdG9tQXV0aGVudGljYXRvcg";
private static final String IDP_NAME = "Custom Auth IDP";
private static final String ENDPOINT_URI = "https://abc.com/authenticate";
private static final String UPDATED_ENDPOINT_URI = "https://xyz.com/authenticate";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String USERNAME_VALUE = "testUser";
Expand All @@ -75,6 +76,15 @@ public IdPSuccessTest(TestUserMode userMode) throws Exception {
this.tenant = context.getContextTenant().getDomain();
}

@DataProvider(name = "restAPIUserConfigProvider")
public static Object[][] restAPIUserConfigProvider() {

return new Object[][]{
{TestUserMode.SUPER_TENANT_ADMIN},
{TestUserMode.TENANT_ADMIN}
};
}

@BeforeClass(alwaysRun = true)
public void init() throws IOException {

Expand Down Expand Up @@ -105,6 +115,27 @@ private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayload()
return userDefinedAuthenticatorPayload;
}

private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayload(String endpointUri) {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = new UserDefinedAuthenticatorPayload();
userDefinedAuthenticatorPayload.setIsEnabled(true);
userDefinedAuthenticatorPayload.setAuthenticatorId(FEDERATED_AUTHENTICATOR_ID);
userDefinedAuthenticatorPayload.setDefinedBy(FederatedAuthenticatorRequest.DefinedByEnum.USER.toString());

Endpoint endpoint = new Endpoint();
endpoint.setUri(endpointUri);
AuthenticationType authenticationType = new AuthenticationType();
authenticationType.setType(AuthenticationType.TypeEnum.BASIC);
Map<String, Object> properties = new HashMap<>();
properties.put(USERNAME, USERNAME_VALUE);
properties.put(PASSWORD, PASSWORD_VALUE);
authenticationType.setProperties(properties);
endpoint.authentication(authenticationType);
userDefinedAuthenticatorPayload.setEndpoint(endpoint);

return userDefinedAuthenticatorPayload;
}

@AfterClass(alwaysRun = true)
public void testConclude() {

Expand All @@ -123,15 +154,6 @@ public void testFinish() {
RestAssured.basePath = StringUtils.EMPTY;
}

@DataProvider(name = "restAPIUserConfigProvider")
public static Object[][] restAPIUserConfigProvider() {

return new Object[][]{
{TestUserMode.SUPER_TENANT_ADMIN},
{TestUserMode.TENANT_ADMIN}
};
}

@Test
public void testListMetaFederatedAuthenticators() throws Exception {

Expand Down Expand Up @@ -319,6 +341,60 @@ public void testAddIdPWithUserDefinedAuthenticator() throws IOException {
assertNotNull(customIdPId);
}

@Test(dependsOnMethods = "testAddIdPWithUserDefinedAuthenticator")
public void testGetUserDefinedAuthenticatorsOfIdP() {

Response response = getResponseOfGet(IDP_API_BASE_PATH + PATH_SEPARATOR + customIdPId +
PATH_SEPARATOR + IDP_FEDERATED_AUTHENTICATORS_PATH);

response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK)
.body("defaultAuthenticatorId", equalTo(FEDERATED_AUTHENTICATOR_ID))
.body("authenticators.find { it.authenticatorId == '" + FEDERATED_AUTHENTICATOR_ID + "' }.name",
equalTo(new String(Base64.getDecoder().decode(FEDERATED_AUTHENTICATOR_ID))))
.body("authenticators.find { it.authenticatorId == '" + FEDERATED_AUTHENTICATOR_ID + "' }.isEnabled",
equalTo(true));
}

@Test(dependsOnMethods = "testAddIdPWithUserDefinedAuthenticator")
public void testUpdateUserDefinedAuthenticatorOfIdP() {

// TODO: check the OpenAPI validation
// The following patch request fails from OpenAPI validations, as the response object does not contains
// "authentication" field in the "endpoint" object.
Response response = getResponseOfPut(IDP_API_BASE_PATH + PATH_SEPARATOR + customIdPId +
PATH_SEPARATOR + IDP_FEDERATED_AUTHENTICATORS_PATH + PATH_SEPARATOR + FEDERATED_AUTHENTICATOR_ID,
createUserDefinedAuthenticatorPayload(UPDATED_ENDPOINT_URI).toString());

response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK)
.body("authenticatorId", equalTo(FEDERATED_AUTHENTICATOR_ID))
.body("name", equalTo(new String(Base64.getDecoder().decode(FEDERATED_AUTHENTICATOR_ID))))
.body("endpoint.uri", equalTo(UPDATED_ENDPOINT_URI));
}

@Test(dependsOnMethods = "testAddIdPWithUserDefinedAuthenticator")
public void testDeleteUserDefinedAuthenticatorOfIdP() throws IOException {

// TODO: check the behaviour of the DELETE functionality
// When a put request is tried with empty authenticators list, postman request is successful
// but this put request fails from openAPI validation saying
// "Provided request body content is not in the expected format."
Response response = getResponseOfPut(IDP_API_BASE_PATH + PATH_SEPARATOR + customIdPId +
PATH_SEPARATOR + IDP_FEDERATED_AUTHENTICATORS_PATH + PATH_SEPARATOR +
FEDERATED_AUTHENTICATOR_ID, readResource("empty-custom-fed-auth.json"));

response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK) // Receiving 400
.body("authenticators", nullValue());
}

@Test(dependsOnMethods = {"testGetMetaOutboundConnector"})
public void testAddIdP() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"authenticators": [],
"defaultAuthenticatorId": ""
}

0 comments on commit 373a4f2

Please sign in to comment.