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

Add integration tests for user defined local authenticator mgt. #22016

Open
wants to merge 1 commit 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
Expand Up @@ -18,9 +18,11 @@

package org.wso2.identity.integration.test.rest.api.server.authenticator.management.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -34,14 +36,37 @@
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.AuthenticationType;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.UserDefinedLocalAuthenticatorCreation;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.UserDefinedLocalAuthenticatorUpdate;
import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants;
import org.wso2.identity.integration.test.rest.api.server.authenticator.management.v1.util.UserDefinedLocalAuthenticatorPayload;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;

/**
* Tests for happy paths of the authentication Management REST API.
*/
public class AuthenticatorSuccessTest extends AuthenticatorTestBase{
public class AuthenticatorSuccessTest extends AuthenticatorTestBase {

private UserDefinedLocalAuthenticatorConfig testAuthenticatorConfig;
private UserDefinedLocalAuthenticatorCreation creationPayload;
private UserDefinedLocalAuthenticatorUpdate updatePayload;

private final String AUTHENTICATOR_NAME = "customAuthenticator";
private final String customIdPId = Base64.getUrlEncoder().withoutPadding().encodeToString(
AUTHENTICATOR_NAME.getBytes(StandardCharsets.UTF_8));
private final String AUTHENTICATOR_DISPLAY_NAME = "ABC custom authenticator";
private final String AUTHENTICATOR_ENDPOINT_URI = "https://test.com/authenticate";
private final String UPDATE_VALUE_POSTFIX = "Updated";

@Factory(dataProvider = "restAPIUserConfigProvider")
public AuthenticatorSuccessTest(TestUserMode userMode) throws Exception {
Expand All @@ -54,9 +79,11 @@ public AuthenticatorSuccessTest(TestUserMode userMode) throws Exception {
}

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

super.testInit(API_VERSION, swaggerDefinition, tenant);
testAuthenticatorConfig = createBaseUserDefinedLocalAuthenticator(
AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION);
}

@AfterClass(alwaysRun = true)
Expand All @@ -69,6 +96,10 @@ public void testConclude() {
public void testInit() {

RestAssured.basePath = basePath;
creationPayload = UserDefinedLocalAuthenticatorPayload
.getBasedUserDefinedLocalAuthenticatorCreation(testAuthenticatorConfig);
updatePayload = UserDefinedLocalAuthenticatorPayload
.getBasedUserDefinedLocalAuthenticatorUpdate(testAuthenticatorConfig);
}

@AfterMethod(alwaysRun = true)
Expand Down Expand Up @@ -106,5 +137,76 @@ public void getAuthenticators() throws JSONException {
}
}

@Test(dependsOnMethods = {"getAuthenticators"})
public void testCreateUserDefinedLocalAuthenticator() throws JsonProcessingException {

String body = UserDefinedLocalAuthenticatorPayload.convertToJasonPayload(creationPayload);
Response response = getResponseOfPost(AUTHENTICATOR_CUSTOM_API_BASE_PATH, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_CREATED)
.header(HttpHeaders.LOCATION, notNullValue())
.body("id", equalTo(customIdPId))
.body("name", equalTo(AUTHENTICATOR_NAME))
.body("displayName", equalTo(AUTHENTICATOR_DISPLAY_NAME))
.body("type", equalTo("LOCAL"))
.body("definedBy", equalTo("USER"))
.body("isEnabled", equalTo(true))
.body("self", equalTo(getTenantedRelativePath(
AUTHENTICATOR_CONFIG_API_BASE_PATH + customIdPId, tenant)));
}

@Test(dependsOnMethods = {"testCreateUserDefinedLocalAuthenticator"})
public void testUpdateUserDefinedLocalAuthenticator() throws JsonProcessingException {

updatePayload.displayName(AUTHENTICATOR_DISPLAY_NAME + UPDATE_VALUE_POSTFIX);
updatePayload.isEnabled(false);
updatePayload.getEndpoint().uri(AUTHENTICATOR_ENDPOINT_URI + UPDATE_VALUE_POSTFIX);
String body = UserDefinedLocalAuthenticatorPayload.convertToJasonPayload(updatePayload);
Response response = getResponseOfPutWithNoFilter(AUTHENTICATOR_CUSTOM_API_BASE_PATH + PATH_SEPARATOR +
customIdPId, body);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK)
.body("id", equalTo(customIdPId))
.body("name", equalTo(AUTHENTICATOR_NAME))
.body("displayName", equalTo(AUTHENTICATOR_DISPLAY_NAME + UPDATE_VALUE_POSTFIX))
.body("type", equalTo("LOCAL"))
.body("definedBy", equalTo("USER"))
.body("isEnabled", equalTo(false))
.body("self", equalTo(getTenantedRelativePath(
AUTHENTICATOR_CONFIG_API_BASE_PATH + customIdPId, tenant)));
}

@Test(dependsOnMethods = {"testUpdateUserDefinedLocalAuthenticator"})
public void testDeleteUserDefinedLocalAuthenticator() throws JsonProcessingException {

Response response = getResponseOfDelete(AUTHENTICATOR_CUSTOM_API_BASE_PATH + PATH_SEPARATOR
+ customIdPId);
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_NO_CONTENT);
}

private UserDefinedLocalAuthenticatorConfig createBaseUserDefinedLocalAuthenticator(
AuthenticatorPropertyConstants.AuthenticationType type) {

UserDefinedLocalAuthenticatorConfig config = new UserDefinedLocalAuthenticatorConfig(type);
config.setName(AUTHENTICATOR_NAME);
config.setDisplayName(AUTHENTICATOR_DISPLAY_NAME);
config.setEnabled(true);

UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfig =
new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder();
endpointConfig.uri(AUTHENTICATOR_ENDPOINT_URI);
endpointConfig.authenticationType(String.valueOf(AuthenticationType.TypeEnum.BASIC));
endpointConfig.authenticationProperties(Map.of("username", "admin", "password", "admin"));
config.setEndpointConfig(endpointConfig.build());

return config;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class AuthenticatorTestBase extends RESTAPIServerTestBase {
protected static final String API_PACKAGE_NAME = "org.wso2.carbon.identity.api.server.authenticators.v1";

protected static final String AUTHENTICATOR_API_BASE_PATH = "/authenticators";
protected static final String AUTHENTICATOR_CUSTOM_API_BASE_PATH = "/authenticators/custom";
protected static final String AUTHENTICATOR_CONFIG_API_BASE_PATH = "/api/server/v1/configs/authenticators/";
protected static final String PATH_SEPARATOR = "/";

protected static String swaggerDefinition;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.carbon.identity.api.server.authenticators.v1.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.*;


import io.swagger.annotations.*;
import java.util.Objects;
import javax.validation.Valid;
import javax.xml.bind.annotation.*;

public class AuthenticationType {


@XmlType(name="TypeEnum")
@XmlEnum(String.class)
public enum TypeEnum {

@XmlEnumValue("NONE") NONE(String.valueOf("NONE")), @XmlEnumValue("BEARER") BEARER(String.valueOf("BEARER")), @XmlEnumValue("API_KEY") API_KEY(String.valueOf("API_KEY")), @XmlEnumValue("BASIC") BASIC(String.valueOf("BASIC"));


private String value;

TypeEnum(String v) {
value = v;
}

public String value() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

public static TypeEnum fromValue(String value) {
for (TypeEnum b : TypeEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}

private TypeEnum type;
private Map<String, Object> properties = new HashMap<>();


/**
**/
public AuthenticationType type(TypeEnum type) {

this.type = type;
return this;
}

@ApiModelProperty(example = "BASIC", required = true, value = "")
@JsonProperty("type")
@Valid
@NotNull(message = "Property type cannot be null.")

public TypeEnum getType() {
return type;
}
public void setType(TypeEnum type) {
this.type = type;
}

/**
**/
public AuthenticationType properties(Map<String, Object> properties) {

this.properties = properties;
return this;
}

@ApiModelProperty(example = "{\"username\":\"auth_username\",\"password\":\"auth_password\"}", required = true, value = "")
@JsonProperty("properties")
@Valid
@NotNull(message = "Property properties cannot be null.")

public Map<String, Object> getProperties() {
return properties;
}
public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}


public AuthenticationType putPropertiesItem(String key, Object propertiesItem) {
this.properties.put(key, propertiesItem);
return this;
}



@Override
public boolean equals(java.lang.Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AuthenticationType authenticationType = (AuthenticationType) o;
return Objects.equals(this.type, authenticationType.type) &&
Objects.equals(this.properties, authenticationType.properties);
}

@Override
public int hashCode() {
return Objects.hash(type, properties);
}

@Override
public String toString() {

StringBuilder sb = new StringBuilder();
sb.append("class AuthenticationType {\n");

sb.append(" type: ").append(toIndentedString(type)).append("\n");
sb.append(" properties: ").append(toIndentedString(properties)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {

if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n");
}
}

Loading