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

Integration Test for Input validation rest api #16983

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,86 @@
/*
* 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.rest.api.server.validation.rules.v1;

import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.apache.http.HttpStatus;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;

public class ValidationRulesNegativeTest extends ValidationRulesTestBase {

@Factory(dataProvider = "restAPIUserConfigProvider")
public ValidationRulesNegativeTest(TestUserMode userMode) throws Exception {

super.init(userMode);
this.context = isServer;
this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName();
this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword();
this.tenant = context.getContextTenant().getDomain();
}

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

super.testInit(API_VERSION, swaggerDefinition, tenant);
}

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

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

@Test(description = "Negative test case to test bad request where the alpha numeric validation doesn't " +
"have proper numeric validation properties.")
public void testNegativeUsernameValidationUpdate() throws Exception {

String NegativeInputRequestBody =
readResource("put-enable-alphanumeric-type-username-validation-negative.json");
Response responseOfPut = getResponseOfPut(VALIDATION_RULES_PATH, NegativeInputRequestBody);
ValidatableResponse validatableResponse = responseOfPut.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_BAD_REQUEST);
String responseBody = validatableResponse.extract().body().asString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(responseBody);
if (json == null) {
throw new Exception("Error occurred while getting the response.");
}
Assert.assertEquals(json.get("code"),"IVM-60027","Error code of Bad Request for " +
"password validation update is not match.");
Assert.assertEquals(json.get("message"),"Unable to update input validation configurations.",
"Error message of Bad Request for password validation update is not match");
Assert.assertEquals(json.get("description"),"LengthValidator must be configured along " +
"with the AlphanumericValidator for username.",
"Error description of Bad Request for password validation update is not match");
Assert.assertTrue(json.containsKey("traceId"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.rest.api.server.validation.rules.v1;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.identity.integration.test.rest.api.server.validation.rules.v1.model.MappingModel;
import org.wso2.identity.integration.test.rest.api.server.validation.rules.v1.model.RuleModel;
import org.wso2.identity.integration.test.rest.api.server.validation.rules.v1.model.ValidationConfigModel;

import java.util.Arrays;
import java.util.List;

public class ValidationRulesSuccessTest extends ValidationRulesTestBase {

@Factory(dataProvider = "restAPIUserConfigProvider")
public ValidationRulesSuccessTest(TestUserMode userMode) throws Exception {

super.init(userMode);
this.context = isServer;
this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName();
this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword();
this.tenant = context.getContextTenant().getDomain();
}

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

super.testInit(API_VERSION, swaggerDefinition, tenant);
}

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

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

@Test (description = "test default response from get /validation-rules end point")
public void testDefaultResponse() throws Exception {

ObjectMapper jsonWriter = new ObjectMapper(new JsonFactory());
String expectedResponse = readResource("default-response-positive.json");
List<ValidationConfigModel> expectedValidationConfigModels =
Arrays.asList(jsonWriter.readValue(expectedResponse, ValidationConfigModel[].class));

Response response = getResponseOfGet(VALIDATION_RULES_PATH);
response.then().log().ifValidationFails().assertThat()
.statusCode(HttpStatus.SC_OK);
List<ValidationConfigModel> retrievedValidationConfigModels =
Arrays.asList(jsonWriter.readValue(response.asString(), ValidationConfigModel[].class));
Assert.assertEquals(retrievedValidationConfigModels, expectedValidationConfigModels,
"Response of the get /validation-rules doesn't match.");
}

@Test (description = "test for put /validation-rules/password end point",
dependsOnMethods = "testDefaultResponse")
public void testUpdatePasswordValidation() throws Exception {

ObjectMapper jsonWriter = new ObjectMapper(new JsonFactory());
String passwordValidationUpdateRequestBody
= readResource("put-password-validation-update-positive.json");

Response responseOfPut = getResponseOfPut(VALIDATION_RULES_PATH + PASSWORD,
passwordValidationUpdateRequestBody);
responseOfPut.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK);
ValidationConfigModel retrievedValidationConfigModel =
jsonWriter.readValue(responseOfPut.asString(), ValidationConfigModel.class);
Assert.assertEquals(retrievedValidationConfigModel.getField(), "password",
"Response of the put /validation-rules/password doesn't contain password filed.");
RuleModel ruleModel = new RuleModel();
ruleModel.setValidator("LengthValidator");
ruleModel.addPropertiesItem(new MappingModel().key("min.length").value("10"));
ruleModel.addPropertiesItem(new MappingModel().key("max.length").value("30"));
Assert.assertTrue(retrievedValidationConfigModel.getRules().contains(ruleModel),
"Response of the put /validation-rules/password doesn't contain 10 as min length.");

ruleModel = new RuleModel();
ruleModel.setValidator("UpperCaseValidator");
ruleModel.addPropertiesItem(new MappingModel().key("min.length").value("0"));
Assert.assertTrue(retrievedValidationConfigModel.getRules().contains(ruleModel),
"Response of the put /validation-rules/password doesn't contain 0 as UpperCaseValidator.");

ruleModel = new RuleModel();
ruleModel.setValidator("LowerCaseValidator");
ruleModel.addPropertiesItem(new MappingModel().key("min.length").value("0"));
Assert.assertTrue(retrievedValidationConfigModel.getRules().contains(ruleModel),
"Response of the put /validation-rules/password doesn't contain 0 as LowerCaseValidator.");

ruleModel = new RuleModel();
ruleModel.setValidator("SpecialCharacterValidator");
ruleModel.addPropertiesItem(new MappingModel().key("min.length").value("0"));
Assert.assertTrue(retrievedValidationConfigModel.getRules().contains(ruleModel),
"Response of the put /validation-rules/password doesn't contain 0 as SpecialCharacterValidator.");
}

@Test (description = "Test to set configuration to default.",dependsOnMethods = "testUpdatePasswordValidation")
public void testSetBackToDefault() throws Exception{

String defaultSetting = readResource("default-response-positive.json");
Response responseOfPut = getResponseOfPut(VALIDATION_RULES_PATH, defaultSetting);
responseOfPut.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.rest.api.server.validation.rules.v1;

import io.restassured.RestAssured;
import org.apache.commons.lang.StringUtils;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.wso2.identity.integration.test.rest.api.server.common.RESTAPIServerTestBase;

import java.io.IOException;

public class ValidationRulesTestBase extends RESTAPIServerTestBase {

private static final String API_DEFINITION_NAME = "inputValidation.yaml";
static final String API_VERSION = "v1";
private static final String API_PACKAGE_NAME = "org.wso2.carbon.identity.api.server.input.validation.v1";

static final String VALIDATION_RULES_PATH = "/validation-rules";
static final String PASSWORD = "/password";

protected static String swaggerDefinition;

static {
try {
swaggerDefinition = getAPISwaggerDefinition(API_PACKAGE_NAME, API_DEFINITION_NAME);
} catch (IOException e) {
Assert.fail(String.format("Unable to read the swagger definition %s from %s", API_DEFINITION_NAME,
API_PACKAGE_NAME), e);
}
}

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

super.conclude();
}

@BeforeMethod(alwaysRun = true)
public void testInit() {

RestAssured.basePath = basePath;
}

@AfterMethod(alwaysRun = true)
public void testFinish() {

RestAssured.basePath = StringUtils.EMPTY;
}
}
Loading