From 3835979ae673c891ea9e6b7a1d1d7a97a5dce908 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Fri, 15 Nov 2024 13:13:00 +0100 Subject: [PATCH] tests: add tests for user profile menu config --- .../domain/service/UserProfileService.java | 5 +- ...ofileV1RestControllerMenuSettingsTest.java | 71 +++++++++++++++++++ .../onecx/user/profile/test/AbstractTest.java | 20 ++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/tkit/onecx/user/profile/rs/external/v1/controllers/UserProfileV1RestControllerMenuSettingsTest.java diff --git a/src/main/java/org/tkit/onecx/user/profile/domain/service/UserProfileService.java b/src/main/java/org/tkit/onecx/user/profile/domain/service/UserProfileService.java index 8bebf00..1d887cd 100644 --- a/src/main/java/org/tkit/onecx/user/profile/domain/service/UserProfileService.java +++ b/src/main/java/org/tkit/onecx/user/profile/domain/service/UserProfileService.java @@ -70,8 +70,9 @@ MenuMode getClaimOrConfigMenuMode(JsonWebToken token, Optional claim, St try { return MenuMode.valueOf(tmp); } catch (Exception ex) { - log.error("Wrong value of the menu mode for the user. Menu mode {}. Returning {} instead.", tmp, MenuMode.STATIC, - ex); + log.error("Wrong value of the menu mode for the user. Menu mode {}. Returning {} instead. Error: {}", tmp, + MenuMode.STATIC, + ex.getMessage()); return MenuMode.STATIC; } } diff --git a/src/test/java/org/tkit/onecx/user/profile/rs/external/v1/controllers/UserProfileV1RestControllerMenuSettingsTest.java b/src/test/java/org/tkit/onecx/user/profile/rs/external/v1/controllers/UserProfileV1RestControllerMenuSettingsTest.java new file mode 100644 index 0000000..3c83322 --- /dev/null +++ b/src/test/java/org/tkit/onecx/user/profile/rs/external/v1/controllers/UserProfileV1RestControllerMenuSettingsTest.java @@ -0,0 +1,71 @@ +package org.tkit.onecx.user.profile.rs.external.v1.controllers; + +import static io.restassured.RestAssured.given; +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; +import static jakarta.ws.rs.core.Response.Status.OK; +import static org.assertj.core.api.Assertions.assertThat; +import static org.tkit.quarkus.security.test.SecurityTestUtils.getKeycloakClientToken; + +import jakarta.inject.Inject; + +import org.eclipse.microprofile.config.Config; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.tkit.onecx.user.profile.domain.config.UserProfileConfig; +import org.tkit.onecx.user.profile.test.AbstractTest; +import org.tkit.quarkus.security.test.GenerateKeycloakClient; +import org.tkit.quarkus.test.WithDBData; + +import gen.org.tkit.onecx.user.profile.rs.external.v1.model.*; +import io.quarkus.test.InjectMock; +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.config.SmallRyeConfig; + +@QuarkusTest +@TestHTTPEndpoint(UserProfileV1RestController.class) +@WithDBData(value = "data/testdata.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true) +@GenerateKeycloakClient(clientName = "testClient", scopes = { "ocx-up:read" }) +class UserProfileV1RestControllerMenuSettingsTest extends AbstractTest { + + @InjectMock + UserProfileConfig userProfileConfig; + + @Inject + Config config; + + @BeforeEach + void beforeEach() { + var tmp = config.unwrap(SmallRyeConfig.class).getConfigMapping(UserProfileConfig.class); + + var m = Mockito.mock(UserProfileConfig.Settings.class); + Mockito.when(m.locale()).thenReturn(tmp.settings().locale()); + Mockito.when(m.timeZone()).thenReturn(tmp.settings().timeZone()); + Mockito.when(m.menuMode()).thenReturn("WRONG_ENUM_KEY"); + + Mockito.when(userProfileConfig.claims()).thenReturn(tmp.claims()); + Mockito.when(userProfileConfig.settings()).thenReturn(m); + + } + + @Test + void getUserProfileTest() { + // not existing user profile, should be created + var userProfile = given() + .auth().oauth2(getKeycloakClientToken("testClient")) + .when() + .contentType(APPLICATION_JSON) + .header(APM_HEADER_PARAM, createToken("not-existing", null)) + .get() + .then() + .statusCode(OK.getStatusCode()) + .extract().as(UserProfileDTO.class); + + assertThat(userProfile.getUserId()).isEqualTo("not-existing"); + assertThat(userProfile.getOrganization()).isEqualTo("org1"); + assertThat(userProfile.getPerson().getEmail()).isEqualTo("not-existing@cap.de"); + assertThat(userProfile.getAccountSettings().getMenuMode()).isEqualTo(MenuModeDTO.STATIC); + } + +} diff --git a/src/test/java/org/tkit/onecx/user/profile/test/AbstractTest.java b/src/test/java/org/tkit/onecx/user/profile/test/AbstractTest.java index c3899ff..1e510f9 100644 --- a/src/test/java/org/tkit/onecx/user/profile/test/AbstractTest.java +++ b/src/test/java/org/tkit/onecx/user/profile/test/AbstractTest.java @@ -6,16 +6,23 @@ import java.security.PrivateKey; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; +import jakarta.inject.Inject; import jakarta.json.Json; import jakarta.json.JsonObjectBuilder; +import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.jwt.Claims; +import org.tkit.onecx.user.profile.domain.config.UserProfileConfig; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import io.quarkus.test.Mock; import io.restassured.config.RestAssuredConfig; +import io.smallrye.config.SmallRyeConfig; import io.smallrye.jwt.build.Jwt; import io.smallrye.jwt.util.KeyUtils; @@ -76,4 +83,17 @@ protected static JsonObjectBuilder createClaims(String userName, String orgId, S return claims; } + + public static class ConfigProducer { + + @Inject + Config config; + + @Produces + @ApplicationScoped + @Mock + UserProfileConfig config() { + return config.unwrap(SmallRyeConfig.class).getConfigMapping(UserProfileConfig.class); + } + } }