Skip to content

Commit

Permalink
add integration tests for claim mgt
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-ariyaratne committed Nov 7, 2024
1 parent 8360241 commit 17b365a
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class OAuth2ServiceJWTGrantTestCase extends OAuth2ServiceAbstractIntegrat
private static final String COUNTRY_OIDC_CLAIM = "country";
private static final String COUNTRY_NEW_OIDC_CLAIM = "customclaim";
private static final String COUNTRY_LOCAL_CLAIM_URI = "http://wso2.org/claims/country";
private static final String STATE_LOCAL_CLAIM_URI = "http://wso2.org/claims/stateorprovince";
private static final String EMAIL_OIDC_CLAIM = "email";
private static final String EMAIL_CLAIM_VALUE = "[email protected]";
private static final String EMAIL_LOCAL_CLAIM_URI = "http://wso2.org/claims/emailaddress";
Expand Down Expand Up @@ -590,7 +591,9 @@ private void addAdminUser() throws Exception {
*/
private void changeCountryOIDCDialect() throws Exception {

claimManagementRestClient.deleteExternalClaim(ENCODED_OIDC_CLAIM_DIALECT, COUNTRY_CLAIM_ID);
ExternalClaimReq updateCountryReq = new ExternalClaimReq().claimURI(COUNTRY_OIDC_CLAIM)
.mappedLocalClaimURI(STATE_LOCAL_CLAIM_URI);
claimManagementRestClient.updateExternalClaim(ENCODED_OIDC_CLAIM_DIALECT, COUNTRY_CLAIM_ID, updateCountryReq);

ExternalClaimReq externalClaimReq = new ExternalClaimReq();
externalClaimReq.setClaimURI(COUNTRY_NEW_OIDC_CLAIM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.axis2.AxisFault;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
Expand All @@ -43,8 +42,8 @@
*/
public class ClaimManagementNegativeTest extends ClaimManagementTestBase {

private static String testDialectId = "aHR0cDovL3VwZGF0ZWRkdW1teS5vcmcvY6xhaW0";
private static String testClaimId = "aHR0cDovL2ludmFsaWRkdW1teS5vcmcvY2xhaW0vZW1haWxhZGRyZXNz";
private static final String testDialectId = "aHR0cDovL3VwZGF0ZWRkdW1teS5vcmcvY6xhaW0";
private static final String testClaimId = "aHR0cDovL2ludmFsaWRkdW1teS5vcmcvY2xhaW0vZW1haWxhZGRyZXNz";

@Factory(dataProvider = "restAPIUserConfigProvider")
public ClaimManagementNegativeTest(TestUserMode userMode) throws Exception {
Expand Down Expand Up @@ -96,6 +95,135 @@ public void testGetDialectWithInvalidDialectId() {
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "CMT-50016", testDialectId);
}

@Test
public void testRemoveSystemDefaultDialect() {

String dialectId = "local";
removeDialect(dialectId);
getResponseOfGet(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId)
.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK)
.body("id", equalTo(dialectId))
.body("dialectURI", equalTo("http://wso2.org/claims"));

dialectId = "dXJuOmlldGY6cGFyYW1zOnNjaW06c2NoZW1hczpjb3JlOjIuMA";
getResponseOfDelete(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId)
.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_FORBIDDEN)
.body("code", equalTo("CMT-60008"));
}

@Test
public void testAddExistingDialect() throws IOException {

String dialectId = createDialect();

String body = readResource("claim-management-add-dialect.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_CONFLICT)
.body("code", equalTo("CMT-60002"));

removeDialect(dialectId);
}

@Test
public void testUpdateDefaultDialectURI() throws IOException {

String dialectId = "aHR0cDovL3dzbzIub3JnL29pZGMvY2xhaW0";
String body = readResource("claim-management-update-dialect.json");
Response response = getResponseOfPut(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_FORBIDDEN)
.body("code", equalTo("CMT-60007"));
}

@Test
public void testGetLocalClaimsWithInvalidClaimId() {

Response response =
getResponseOfGet(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/" + testClaimId);
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "CMT-50019", testClaimId);
}

@Test
public void testUpdateExistingLocalClaimUri() throws IOException {

String localClaimUri = "http://wso2.org/claims/dummyemailaddress";
String claimId = createLocalClaim();

String body = readResource("claim-management-update-local-claim-conflict.json");
Response response = getResponseOfPut(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/" + claimId,
body);
validateErrorResponse(response, HttpStatus.SC_CONFLICT, "CMT-50021", localClaimUri);

removeLocalClaim(claimId);
}

@Test
public void testAddLocalClaimWithInvalidUsertore() throws IOException {

String userstore = "DUMMY";
String body = readResource("claim-management-add-local-claim-invalid-userstore.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI, body);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50026", userstore);
}

@Test
public void testRemoveLocalClaimWithExternalClaimAssociation() throws IOException {

String localClaimId = createLocalClaim();
String dialectId = createDialect();
String claimId = createExternalClaimMappedToCustomLocalClaim(dialectId);

Response response = getResponseOfDelete(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/"
+ localClaimId);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50031");

removeExternalClaim(dialectId, claimId);
removeDialect(dialectId);
removeLocalClaim(localClaimId);
}

@Test
public void testRemoveDefaultLocalClaim() throws IOException {

String dialectId = "dXJuOmlldGY6cGFyYW1zOnNjaW06c2NoZW1hczpjb3JlOjIuMA";
String claimId = "dXJuOmlldGY6cGFyYW1zOnNjaW06c2NoZW1hczpjb3JlOjIuMDptZXRhLnJlc291cmNlVHlwZQ==";

String body = readResource("claim-management-update-default-external-claim.json");
Response response = getResponseOfPut(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI +
"/" + claimId, body);
response.then()
.assertThat()
.statusCode(HttpStatus.SC_OK);

String defaultLocalClaimId = "aHR0cDovL3dzbzIub3JnL2NsYWltcy9yZXNvdXJjZVR5cGU=";

response = getResponseOfDelete(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI +
"/" + defaultLocalClaimId);
validateErrorResponse(response, HttpStatus.SC_FORBIDDEN, "CMT-60006");
}

@Test
public void testAddLocalClaimWithExistingURI() throws IOException {

String body = readResource("claim-management-add-local-claim-with-existing-uri.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_CONFLICT);
}

@Test
public void testGetClaimsWithInvalidDialectId() {

Expand All @@ -122,7 +250,7 @@ public void testGetExternalClaimsWhenEmpty() throws IOException {
public void testGetExternalClaimsWithInvalidClaimId() throws IOException {

String dialectId = createDialect();
String claimId = createExternalClaim(dialectId);
String claimId = createExternalClaimMappedToDefaultLocalClaim(dialectId);

Response response = getResponseOfGet(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI +
"/" + testClaimId);
Expand All @@ -132,20 +260,12 @@ public void testGetExternalClaimsWithInvalidClaimId() throws IOException {
removeDialect(dialectId);
}

@Test
public void testGetLocalClaimsWithInvalidClaimId() {

Response response =
getResponseOfGet(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/" + testClaimId);
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "CMT-50019", testClaimId);
}

@Test
public void testUpdateExistingExternalClaimUri() throws IOException {

String externalClaimUri = "http://updateddummy.org/claim/emailaddress";
String dialectId = createDialect();
String claimId = createExternalClaim(dialectId);
String claimId = createExternalClaimMappedToDefaultLocalClaim(dialectId);

String body = readResource("claim-management-update-external-claim-conflict.json");
Response response = getResponseOfPut(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI +
Expand All @@ -157,17 +277,63 @@ public void testUpdateExistingExternalClaimUri() throws IOException {
}

@Test
public void testUpdateExistingLocalClaimUri() throws IOException {
public void testAddExternalClaimWithInvalidDialect() throws IOException {

String localClaimUri = "http://wso2.org/claims/dummyemailaddress";
String claimId = createLocalClaim();
String body = readResource("claim-management-add-external-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + testDialectId + CLAIMS_ENDPOINT_URI
, body);
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "CMT-50027", testDialectId);
}

String body = readResource("claim-management-update-local-claim-conflict.json");
Response response = getResponseOfPut(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/" + claimId,
@Test
public void testAddExternalClaimWithInvalidMappedClaim() throws IOException {

String dialectId = createDialect();

String body = readResource("claim-management-add-external-claim-invalid-mapped-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI,
body);
validateErrorResponse(response, HttpStatus.SC_CONFLICT, "CMT-50021", localClaimUri);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50036");

removeLocalClaim(claimId);
removeDialect(dialectId);
}

@Test
public void testAddExternalClaimWithExistingClaimURI() throws IOException {

String dialectId = createDialect();
String claimId = createExternalClaimMappedToDefaultLocalClaim(dialectId);

String body = readResource("claim-management-add-external-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId +
CLAIMS_ENDPOINT_URI, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_CONFLICT)
.body("code", equalTo("CMT-50038"));

removeExternalClaim(dialectId, claimId);
removeDialect(dialectId);
}

@Test
public void testAddExternalClaimWithAlreadyMappedLocalClaim() throws IOException {

String dialectId = createDialect();
String claimId = createExternalClaimMappedToDefaultLocalClaim(dialectId);

String body = readResource("claim-management-add-external-claim-already-mapped.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId +
CLAIMS_ENDPOINT_URI, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("code", equalTo("CMT-60004"));

removeExternalClaim(dialectId, claimId);
removeDialect(dialectId);
}

@Test
Expand Down Expand Up @@ -208,52 +374,6 @@ public void testAttributeFilteringNotImplemented() {
validateErrorResponse(response, HttpStatus.SC_NOT_IMPLEMENTED, "CMT-50025");
}

@Test
public void testAddLocalClaimWithInvalidUsertore() throws IOException {

String userstore = "DUMMY";
String body = readResource("claim-management-add-local-claim-invalid-userstore.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI, body);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50026", userstore);
}

@Test
public void testAddExternalClaimWithInvalidDialect() throws IOException {

String body = readResource("claim-management-add-external-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + testDialectId + CLAIMS_ENDPOINT_URI
, body);
validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "CMT-50027", testDialectId);
}

@Test
public void testRemoveLocalClaimWithExternalClaimAssociation() throws IOException {

String mappedLocalClaimId = "aHR0cDovL3dzbzIub3JnL2NsYWltcy9lbWFpbGFkZHJlc3M";
String dialectId = createDialect();
String claimId = createExternalClaim(dialectId);

Response response =
getResponseOfDelete(CLAIM_DIALECTS_ENDPOINT_URI + LOCAL_CLAIMS_ENDPOINT_URI + "/" + mappedLocalClaimId);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50031");

removeExternalClaim(dialectId, claimId);
removeDialect(dialectId);
}

@Test
public void testAddExternalClaimWithInvalidMappedClaim() throws IOException {

String dialectId = createDialect();

String body = readResource("claim-management-add-external-claim-invalid-mapped-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI,
body);
validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "CMT-50036");

removeDialect(dialectId);
}

private String createDialect() throws IOException {

String body = readResource("claim-management-add-dialect.json");
Expand All @@ -277,7 +397,7 @@ private void removeDialect(String dialectId) {
.statusCode(HttpStatus.SC_NO_CONTENT);
}

private String createExternalClaim(String dialectId) throws IOException {
private String createExternalClaimMappedToDefaultLocalClaim(String dialectId) throws IOException {

String body = readResource("claim-management-add-external-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId +
Expand All @@ -292,6 +412,21 @@ private String createExternalClaim(String dialectId) throws IOException {
return location.substring(location.lastIndexOf("/") + 1);
}

private String createExternalClaimMappedToCustomLocalClaim(String dialectId) throws IOException {

String body = readResource("claim-management-add-external-claim-mapped-to-custom-local-claim.json");
Response response = getResponseOfPost(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId +
CLAIMS_ENDPOINT_URI, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_CREATED)
.header(HttpHeaders.LOCATION, notNullValue());

String location = response.getHeader(HttpHeaders.LOCATION);
return location.substring(location.lastIndexOf("/") + 1);
}

private void removeExternalClaim(String dialectId, String externalClaimId) {

getResponseOfDelete(CLAIM_DIALECTS_ENDPOINT_URI + "/" + dialectId + CLAIMS_ENDPOINT_URI + "/" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ public void deleteExternalClaim(String dialectId, String claimId) throws IOExcep
}
}

public void updateExternalClaim(String dialectId, String claimId, ExternalClaimReq claimRequest) throws IOException {

String endPointUrl = serverBasePath + CLAIM_DIALECTS_ENDPOINT_URI + PATH_SEPARATOR + dialectId +
CLAIMS_ENDPOINT_URI + PATH_SEPARATOR + claimId;
String jsonRequest = toJSONString(claimRequest);
try (CloseableHttpResponse response = getResponseOfHttpPut(endPointUrl, jsonRequest, getHeaders())) {
Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpServletResponse.SC_OK,
"External claim update failed");
}
}

private Header[] getHeaders() {

Header[] headerList = new Header[2];
Expand Down
Loading

0 comments on commit 17b365a

Please sign in to comment.