Skip to content

Commit

Permalink
Update test cases by removing OpenApiValidationFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Shenali-SJ committed Dec 1, 2024
1 parent f989e04 commit 5d7bd4f
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ protected Response getResponseOfPost(String endpointUri, String body) {
.post(endpointUri);
}

protected Response getResponseOfPostNoFilter(String endpointUri, String body) {

return given().auth().preemptive().basic(authenticatingUserName, authenticatingCredential)
.contentType(ContentType.JSON)
.header(HttpHeaders.ACCEPT, ContentType.JSON)
.body(body)
.log().ifValidationFails()
.when()
.log().ifValidationFails()
.post(endpointUri);
}

/**
* Invoke given endpointUri for POST with given body and Basic authentication, authentication credential being the
* authenticatingUserName and authenticatingCredential
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.identity.integration.test.actions.model.User;
import org.wso2.identity.integration.test.rest.api.server.idp.v1.model.AuthenticationType;
import org.wso2.identity.integration.test.rest.api.server.idp.v1.model.Endpoint;
import org.wso2.identity.integration.test.rest.api.server.idp.v1.model.FederatedAuthenticatorRequest;
import org.wso2.identity.integration.test.rest.api.server.idp.v1.util.UserDefinedAuthenticatorPayload;

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

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;

/**
* Test class for Identity Provider Management REST APIs failure paths.
Expand All @@ -60,8 +57,9 @@ public class IdPFailureTest extends IdPTestBase {
private static final String FEDERATED_AUTHENTICATOR_PLACEHOLDER_2 = "\"<FEDERATED_AUTHENTICATOR_2>\"";
private static final String IDP_NAME_PLACEHOLDER = "<IDP_NAME>";
private static final String AUTHENTICATOR_ID_1 = "Y3VzdG9tQXV0aGVudGljYXRvcjE=";
private static final String AUTHENTICATOR_ID_2 = "Y3VzdG9tQXV0aGVudGljYXRvcg==";
private static final String ENDPOINT_URI = "https://abc.com/authenticate";
private String idPId;
private String customIdPId;
private String idpCreatePayload;
private UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload;
private UserDefinedAuthenticatorPayload duplicatedUserDefinedAuthenticatorPayload;
Expand Down Expand Up @@ -108,7 +106,6 @@ public void testInit() {
public void testFinish() {

RestAssured.basePath = StringUtils.EMPTY;
customIdPId = null;
}

@Test
Expand Down Expand Up @@ -203,118 +200,93 @@ public void testPatchIdPNonExistentProperties() throws IOException {
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "IDP-65005", "JWKS URI");
}

// TODO: check 400
// @Test
// public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointUriIsEmpty() throws IOException {
//
// UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = createUserDefinedAuthenticatorPayload(
// "U2VjdXJlU3RyaW5nUGFyc2Vy",
// "",
// "testUser",
// "testPassword");
//
// String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
// userDefinedAuthenticatorPayload.getAuthenticatorId());
// body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
// userDefinedAuthenticatorPayload.convertToJasonPayload());
// Response response = getResponseOfPost(IDP_API_BASE_PATH, body);
// response.then()
// .log().ifValidationFails()
// .assertThat()
// .statusCode(FIELD_NOT_FOUND)
// .header(HttpHeaders.LOCATION, notNullValue());
//
// String location = response.getHeader(HttpHeaders.LOCATION);
// assertNotNull(location);
// String customIdPId = location.substring(location.lastIndexOf("/") + 1);
// assertNotNull(customIdPId);
// }
//

// TODO: check 400
// @Test
// public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointUriIsInvalid() throws IOException {
//
// UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = createUserDefinedAuthenticatorPayload(
// "U2VjdXJlU3RyaW5nUGFyc2Vy",
// "hjdhskadhasd",
// "testUser",
// "testPassword");
//
// String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
// userDefinedAuthenticatorPayload.getAuthenticatorId());
// body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
// userDefinedAuthenticatorPayload.convertToJasonPayload());
// Response response = getResponseOfPost(IDP_API_BASE_PATH, body);
// response.then()
// .log().ifValidationFails()
// .assertThat()
// .statusCode(FIELD_NOT_FOUND)
// .header(HttpHeaders.LOCATION, notNullValue());
//
// String location = response.getHeader(HttpHeaders.LOCATION);
// assertNotNull(location);
// String customIdPId = location.substring(location.lastIndexOf("/") + 1);
// assertNotNull(customIdPId);
// }

// TODO: check 400
// @Test
// public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointConfigIsEmpty() throws IOException {
//
// UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload =
// createUserDefinedAuthenticatorPayloadWithEmptyEndpointConfig(
// "Y3VzdG9tQXV0aGVudGljYXRvcg==");
//
// String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
// userDefinedAuthenticatorPayload.getAuthenticatorId());
// body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
// userDefinedAuthenticatorPayload.convertToJasonPayload());
// Response response = getResponseOfPost(IDP_API_BASE_PATH, body);
// response.then()
// .log().ifValidationFails()
// .assertThat()
// .statusCode(FIELD_NOT_FOUND)
// .header(HttpHeaders.LOCATION, notNullValue());
//
// String location = response.getHeader(HttpHeaders.LOCATION);
// assertNotNull(location);
// String customIdPId = location.substring(location.lastIndexOf("/") + 1);
// assertNotNull(customIdPId);
// }

// TODO: check 400
// @Test
// public void testAddIdPWithUserDefinedAuthenticatorWhenAuthenticatorAuthDetailsIsEmpty() throws IOException {
//
// UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload =
// createUserDefinedAuthenticatorPayloadWithEmptyEndpointConfig("UmFuZG9tU3RyaW5nR2VuZXJhdG9y");
//
// String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
// userDefinedAuthenticatorPayload.getAuthenticatorId());
// body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
// userDefinedAuthenticatorPayload.convertToJasonPayload());
// Response response = getResponseOfPost(IDP_API_BASE_PATH, body);
// response.then()
// .log().ifValidationFails()
// .assertThat()
// .statusCode(HttpStatus.SC_CREATED)
// .header(HttpHeaders.LOCATION, notNullValue());
//
// String location = response.getHeader(HttpHeaders.LOCATION);
// assertNotNull(location);
// String customIdPId = location.substring(location.lastIndexOf("/") + 1);
// assertNotNull(customIdPId);
// }
@Test
public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointUriIsEmpty() throws IOException {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = createUserDefinedAuthenticatorPayload(
AUTHENTICATOR_ID_1,
"",
"testUser",
"testPassword");
String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
userDefinedAuthenticatorPayload.getAuthenticatorId());
body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
userDefinedAuthenticatorPayload.convertToJasonPayload());

Response response = getResponseOfPostNoFilter(IDP_API_BASE_PATH, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("message", equalTo("Invalid Request"))
.body("description", equalTo("must match \"^https?://.+\""));
}

@Test
public void testAddIdPWithUserDefinedAuthenticatorWhenAuthenticatorNameIsDuplicated() throws IOException {
public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointUriIsInvalid() throws IOException {

userDefinedAuthenticatorPayload =
createUserDefinedAuthenticatorPayload(AUTHENTICATOR_ID_1,
"https://abc.com/authenticate",
"testUser",
"testPassword");
UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = createUserDefinedAuthenticatorPayload(
AUTHENTICATOR_ID_1,
"ftp://test.com",
"testUser",
"testPassword");
String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
userDefinedAuthenticatorPayload.getAuthenticatorId());
body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
userDefinedAuthenticatorPayload.convertToJasonPayload());

Response response = getResponseOfPostNoFilter(IDP_API_BASE_PATH, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("message", equalTo("Invalid Request"))
.body("description", equalTo("must match \"^https?://.+\""));
}

@Test
public void testAddIdPWithUserDefinedAuthenticatorWhenEndpointConfigIsEmpty() throws IOException {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload =
createUserDefinedAuthenticatorPayloadWithEmptyEndpointConfig(AUTHENTICATOR_ID_1);
String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
userDefinedAuthenticatorPayload.getAuthenticatorId());
body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
userDefinedAuthenticatorPayload.convertToJasonPayload());

Response response = getResponseOfPostNoFilter(IDP_API_BASE_PATH, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("message", equalTo("Endpoint configuration must be provided for the user defined " +
"federated authenticators " + new String(Base64.getDecoder().decode(AUTHENTICATOR_ID_1)) + "."));
}

@Test
public void testAddIdPWithUserDefinedAuthenticatorWhenAuthenticatorAuthDetailsIsEmpty() throws IOException {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload =
createUserDefinedAuthenticatorPayloadWithEmptyAuthenticationProperties(AUTHENTICATOR_ID_1, ENDPOINT_URI);
String body = idpCreatePayload.replace(FEDERATED_AUTHENTICATOR_ID_PLACEHOLDER,
userDefinedAuthenticatorPayload.getAuthenticatorId());
body = body.replace(FEDERATED_AUTHENTICATOR_PLACEHOLDER,
userDefinedAuthenticatorPayload.convertToJasonPayload());

Response response = getResponseOfPostNoFilter(IDP_API_BASE_PATH, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("message", equalTo("Invalid Request"))
.body("description", equalTo("Property authentication cannot be null."));
}

@Test
public void testAddIdPWithUserDefinedAuthenticatorWithExistingAuthenticatorName() throws IOException {

userDefinedAuthenticatorPayload = createUserDefinedAuthenticatorPayload(AUTHENTICATOR_ID_1, ENDPOINT_URI,
"testUser", "testPassword");
Response response = createUserDefAuthenticator("CustomAuthIDP1", userDefinedAuthenticatorPayload);
response.then()
.log().ifValidationFails()
Expand All @@ -323,7 +295,8 @@ public void testAddIdPWithUserDefinedAuthenticatorWhenAuthenticatorNameIsDuplica
.header(HttpHeaders.LOCATION, notNullValue());
String location = response.getHeader(HttpHeaders.LOCATION);
assertNotNull(location);
customIdPId = location.substring(location.lastIndexOf("/") + 1);
String customIdPId = location.substring(location.lastIndexOf("/") + 1);
assertNotNull(customIdPId);

// duplicate the authenticator creation
duplicatedUserDefinedAuthenticatorPayload =
Expand All @@ -347,13 +320,13 @@ public void testAddIdPWithUserDefinedAuthenticatorWithMultipleAuthenticators() t
String idpCreateErrorPayload = readResource("add-idp-with-custom-fed-multi-auth.json");

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload1 = createUserDefinedAuthenticatorPayload(
"Y3VzdG9tQXV0aGVudGljYXRvclg=",
"https://abc.com/authenticate",
AUTHENTICATOR_ID_1,
ENDPOINT_URI,
"testUser",
"testPassword");
UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload2 = createUserDefinedAuthenticatorPayload(
"Y3VzdG9tQXV0aGVudGljYXRvclg=",
"https://abc.com/authenticate",
AUTHENTICATOR_ID_2,
ENDPOINT_URI,
"testUser",
"testPassword");

Expand All @@ -367,6 +340,14 @@ public void testAddIdPWithUserDefinedAuthenticatorWithMultipleAuthenticators() t
.body("message", equalTo("Multiple authenticators found."));
}

/**
* Create a user-defined authenticator and sends a POST request to the IDP API.
*
* @param idpName Name of the identity provider.
* @param userDefinedAuthenticatorPayload Payload containing authenticator details.
* @return Response received from the API call.
* @throws JsonProcessingException If there's an error while processing the JSON.
*/
private Response createUserDefAuthenticator(String idpName,
UserDefinedAuthenticatorPayload
userDefinedAuthenticatorPayload)
Expand All @@ -380,6 +361,16 @@ private Response createUserDefAuthenticator(String idpName,
return getResponseOfPost(IDP_API_BASE_PATH, body);
}

/**
* Create multiple user-defined authenticators for an IdP and sends a POST request to the IDP API.
*
* @param idpName Name of the identity provider.
* @param idpCreatePayload Base payload template for the identity provider.
* @param userDefinedAuthenticatorPayload1 First authenticator payload.
* @param userDefinedAuthenticatorPayload2 Second authenticator payload.
* @return Response received from the API call.
* @throws JsonProcessingException If there's an error while processing the JSON.
*/
private Response createMultiUserDefAuthenticators(String idpName, String idpCreatePayload,
UserDefinedAuthenticatorPayload
userDefinedAuthenticatorPayload1,
Expand All @@ -396,6 +387,15 @@ private Response createMultiUserDefAuthenticators(String idpName, String idpCrea
return getResponseOfPost(IDP_API_BASE_PATH, body);
}

/**
* Create a user-defined authenticator payload with provided details.
*
* @param id Authenticator ID.
* @param endpoint_uri Endpoint URI for the authenticator.
* @param username Username for basic authentication.
* @param password Password for basic authentication.
* @return A user-defined authenticator payload.
*/
private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayload(String id, String endpoint_uri,
String username, String password) {

Expand All @@ -418,6 +418,12 @@ private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayload(St
return userDefinedAuthenticatorPayload;
}

/**
* Create a user-defined authenticator payload with an empty endpoint configuration.
*
* @param id Authenticator ID.
* @return A user-defined authenticator payload with no endpoint configuration.
*/
private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayloadWithEmptyEndpointConfig(String id) {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = new UserDefinedAuthenticatorPayload();
Expand All @@ -428,7 +434,14 @@ private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayloadWit
return userDefinedAuthenticatorPayload;
}

private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayloadWithEmptyAuthenticationDetails(
/**
* Create a user-defined authenticator payload with an endpoint URI with empty authentication properties.
*
* @param id Authenticator ID.
* @param endpoint_uri The endpoint URI for the authenticator.
* @return A user-defined authenticator payload with endpoint URI but without authentication details.
*/
private UserDefinedAuthenticatorPayload createUserDefinedAuthenticatorPayloadWithEmptyAuthenticationProperties(
String id, String endpoint_uri) {

UserDefinedAuthenticatorPayload userDefinedAuthenticatorPayload = new UserDefinedAuthenticatorPayload();
Expand Down

0 comments on commit 5d7bd4f

Please sign in to comment.