Skip to content

Commit

Permalink
test: extend token verified tests (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Feb 15, 2024
1 parent c587ada commit 9e57339
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ tkit.dataimport.configurations.permission.stop-at-error=true

# TEST
quarkus.test.integration-test-profile=test
%test.onecx.permission.token.verified=false
%test.onecx.permission.token.verified=true
%test.onecx.permission.token.claim.path=groups
%test.tkit.rs.context.tenant-id.enabled=true
%test.tkit.rs.context.tenant-id.mock.enabled=true
%test.tkit.rs.context.tenant-id.mock.default-tenant=default
%test.tkit.rs.context.tenant-id.mock.claim-org-id=orgId
%test.tkit.rs.context.tenant-id.mock.data.org1=100
%test.tkit.rs.context.tenant-id.mock.data.org2=200
%test.tkit.rs.context.tenant-id.mock.data.i100=i100
%test.quarkus.keycloak.devservices.roles.bob=n3
%test.quarkus.keycloak.devservices.roles.alice=n3
%test.quarkus.keycloak.devservices.roles.bob=n3-100
%test.smallrye.jwt.verify.key.location=${keycloak.url}/realms/quarkus/protocol/openid-connect/certs
%test.tkit.rs.context.token.header-param=apm-principal-token

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class PermissionRestControllerTenantTest extends AbstractTest {
@Test
void getApplicationPermissionsTest() {

var accessToken = createToken("org1", List.of("n3-100"));
var accessToken = createAccessTokenBearer(USER_BOB);
var idToken = createToken("org1", List.of("n3-100"));

var dto = given()
.contentType(APPLICATION_JSON)
.header(APM_HEADER_PARAM, accessToken)
.header(APM_HEADER_PARAM, idToken)
.body(new PermissionRequestDTOV1().token(accessToken))
.post("app1")
.then()
Expand All @@ -45,11 +46,13 @@ void getApplicationPermissionsTest() {
@Test
void getApplicationsPermissionsTest() {

var accessToken = createToken("org1", List.of("n3-100"));
var accessToken = createAccessTokenBearer(USER_BOB);

var idToken = createToken("org1", List.of("n3-100"));

var dto = given()
.contentType(APPLICATION_JSON)
.header(APM_HEADER_PARAM, accessToken)
.header(APM_HEADER_PARAM, idToken)
.body(new PermissionRequestDTOV1().token(accessToken))
.post()
.then()
Expand All @@ -62,7 +65,7 @@ void getApplicationsPermissionsTest() {

dto = given()
.contentType(APPLICATION_JSON)
.header(APM_HEADER_PARAM, accessToken)
.header(APM_HEADER_PARAM, idToken)
.body(new PermissionRequestDTOV1().token(accessToken))
.post()
.then()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.jboss.resteasy.reactive.RestResponse.Status.*;

import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
Expand All @@ -30,7 +29,7 @@ class PermissionRestControllerTest extends AbstractTest {
void getApplicationPermissionsTest() {

// bearer prefix
var accessToken = createTokenBearer(List.of("n3"));
var accessToken = createAccessTokenBearer(USER_ALICE);

var dto = given()
.contentType(APPLICATION_JSON)
Expand All @@ -46,7 +45,7 @@ void getApplicationPermissionsTest() {
assertThat(dto.getPermissions().get("o1")).isNotNull().hasSize(1).containsExactly("a3");

// without bearer prefix
accessToken = createToken(null, List.of("n3"));
accessToken = createAccessToken(USER_ALICE);

dto = given()
.contentType(APPLICATION_JSON)
Expand Down Expand Up @@ -100,7 +99,7 @@ void getApplicationPermissionsWrongTongTest() {
@Test
void getApplicationsPermissionsTest() {

var accessToken = createTokenBearer(List.of("n3"));
var accessToken = createAccessTokenBearer(USER_ALICE);

var dto = given()
.contentType(APPLICATION_JSON)
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/org/tkit/onecx/permission/test/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import io.quarkus.test.Mock;
import io.quarkus.test.keycloak.client.KeycloakTestClient;
import io.restassured.config.RestAssuredConfig;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.jwt.build.Jwt;
Expand Down Expand Up @@ -50,8 +51,18 @@ protected static String createToken(String organizationId) {
return createToken(organizationId, null);
}

protected static String createTokenBearer(List<String> roles) {
return "Bearer " + createToken(null, roles);
protected static final String USER_ALICE = "alice";

protected static final String USER_BOB = "bob";

KeycloakTestClient keycloakClient = new KeycloakTestClient();

protected String createAccessTokenBearer(String user) {
return "Bearer " + createAccessToken(user);
}

protected String createAccessToken(String user) {
return keycloakClient.getAccessToken(user);
}

protected static String createToken(String organizationId, List<String> roles) {
Expand Down

0 comments on commit 9e57339

Please sign in to comment.