Skip to content

Commit

Permalink
tests: add tests for user profile menu config
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Nov 15, 2024
1 parent 650a091 commit 3835979
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ MenuMode getClaimOrConfigMenuMode(JsonWebToken token, Optional<String> 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;
}
}
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);
}

}
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 3835979

Please sign in to comment.