Skip to content

Commit

Permalink
feat: add user profile settings menu mode configuration (#73)
Browse files Browse the repository at this point in the history
* feat: add user profile settings menu mode configuration

* tests: add tests for user profile menu config
  • Loading branch information
andrejpetras authored Nov 15, 2024
1 parent 3b75342 commit 0f3f8c2
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ h| Version
| onecx-tenant

| https://onecx.github.io/docs/onecx-quarkus/current/onecx-quarkus/onecx-tenant.html[Link]
| https://github.com/onecx/onecx-quarkus/blob/0.33.0/docs/modules/onecx-quarkus/pages/includes/onecx-tenant.adoc[Link]
| 0.33.0
| https://github.com/onecx/onecx-quarkus/blob/0.34.0/docs/modules/onecx-quarkus/pages/includes/onecx-tenant.adoc[Link]
| 0.34.0

| tkit-quarkus-jpa-tenant

Expand Down Expand Up @@ -138,7 +138,7 @@ h| Version
| https://onecx.github.io/docs/onecx-quarkus/current/onecx-quarkus/onecx-core.html[Link]
|
| 0.33.0
| 0.34.0
| quarkus-container-image-docker
Expand All @@ -151,7 +151,7 @@ h| Version
|
|
| 0.33.0
| 0.34.0
|===
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ endif::add-copy-button-to-env-var[]
|string
|

a| [[onecx-user-profile-svc_onecx-user-profile-claims-menu-mode]] [.property-path]##link:#onecx-user-profile-svc_onecx-user-profile-claims-menu-mode[`onecx.user-profile.claims.menu-mode`]##

[.description]
--
User profile menu


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++ONECX_USER_PROFILE_CLAIMS_MENU_MODE+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++ONECX_USER_PROFILE_CLAIMS_MENU_MODE+++`
endif::add-copy-button-to-env-var[]
--
|string
|

a| [[onecx-user-profile-svc_onecx-user-profile-settings-locale]] [.property-path]##link:#onecx-user-profile-svc_onecx-user-profile-settings-locale[`onecx.user-profile.settings.locale`]##

[.description]
Expand Down Expand Up @@ -161,6 +178,23 @@ endif::add-copy-button-to-env-var[]
|string
|`Europe/Berlin`

a| [[onecx-user-profile-svc_onecx-user-profile-settings-menu-mode]] [.property-path]##link:#onecx-user-profile-svc_onecx-user-profile-settings-menu-mode[`onecx.user-profile.settings.menu-mode`]##

[.description]
--
User profile menu


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++ONECX_USER_PROFILE_SETTINGS_MENU_MODE+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++ONECX_USER_PROFILE_SETTINGS_MENU_MODE+++`
endif::add-copy-button-to-env-var[]
--
|string
|`STATIC`

|===


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ interface Claims {
*/
@WithName("timezone")
Optional<String> timeZone();

/**
* User profile menu
*/
@WithName("menu-mode")
Optional<String> menuMode();
}

interface Settings {
Expand All @@ -94,5 +100,13 @@ interface Settings {
@WithName("timezone")
@WithDefault("Europe/Berlin")
String timeZone();

/**
* User profile menu
*/
@WithName("menu-mode")
@WithDefault("STATIC")
String menuMode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.tkit.onecx.user.profile.domain.models.UserPerson;
import org.tkit.onecx.user.profile.domain.models.UserProfile;
import org.tkit.onecx.user.profile.domain.models.UserProfileAccountSettings;
import org.tkit.onecx.user.profile.domain.models.enums.MenuMode;
import org.tkit.quarkus.context.ApplicationContext;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -39,7 +40,8 @@ public UserProfile createProfileFromToken() {
.setTimezone(getClaimOrConfig(token, config.claims().timeZone(), config.settings().timeZone()));
userProfile.getAccountSettings()
.setLocale(getClaimOrConfig(token, config.claims().locale(), config.settings().locale()));

userProfile.getAccountSettings()
.setMenuMode(getClaimOrConfigMenuMode(token, config.claims().menuMode(), config.settings().menuMode()));
return userProfile;
}

Expand All @@ -63,4 +65,16 @@ String getClaimOrConfig(JsonWebToken token, Optional<String> claim, String confi
return config;
}

MenuMode getClaimOrConfigMenuMode(JsonWebToken token, Optional<String> claim, String config) {
String tmp = getClaimOrConfig(token, claim, config);
try {
return MenuMode.valueOf(tmp);
} catch (Exception 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;
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tkit.rs.context.tenant-id.enabled=true
%test.tkit.rs.context.tenant-id.mock.data.org2=tenant-200
%test.tkit.rs.context.tenant-id.mock.data.org3=tenant-300
%test.onecx.user-profile.claims.timezone=timezone
%test.onecx.user-profile.settings.menu-mode=HORIZONTAL
# TEST-IT (integration tests)
quarkus.test.integration-test-profile=test
quarkus.test.enable-callbacks-for-integration-tests=true
Expand Down
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
assertThat(userProfile.getAccountSettings().getMenuMode()).isEqualTo(MenuModeDTO.STATIC);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void getUserProfileTest() {
assertThat(userPofile.getUserId()).isEqualTo("not-existing");
assertThat(userPofile.getOrganization()).isEqualTo("org1");
assertThat(userPofile.getPerson().getEmail()).isEqualTo("[email protected]");
assertThat(userPofile.getAccountSettings().getMenuMode()).isEqualTo(MenuModeDTO.HORIZONTAL);

// load existing user profile
userPofile = given()
Expand All @@ -115,6 +116,7 @@ void getUserProfileTest() {
assertThat(userPofile.getOrganization()).isEqualTo("org2");
assertThat(userPofile.getUserId()).isEqualTo("user3");
assertThat(userPofile.getPerson().getDisplayName()).isEqualTo("User Three");
assertThat(userPofile.getAccountSettings().getMenuMode()).isEqualTo(MenuModeDTO.SLIM);
}

private static Stream<Arguments> claimTimezone() {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/tkit/onecx/user/profile/test/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 0f3f8c2

Please sign in to comment.