Skip to content

Commit 357e3c3

Browse files
committed
Fix Tests
1 parent 56f71a4 commit 357e3c3

File tree

5 files changed

+482
-2
lines changed

5 files changed

+482
-2
lines changed

backend/src/main/java/org/example/app/general/common/security/PermissionService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.example.app.general.common.security;
22

3+
import io.quarkus.runtime.configuration.ConfigUtils;
34
import io.smallrye.jwt.auth.principal.ParseException;
45
import jakarta.enterprise.context.ApplicationScoped;
56
import jakarta.inject.Inject;
67
import jakarta.ws.rs.container.ContainerRequestContext;
78
import jakarta.ws.rs.core.Response;
9+
import jakarta.ws.rs.core.SecurityContext;
810

911
import java.util.ArrayList;
1012
import java.util.List;
@@ -27,8 +29,14 @@ public class PermissionService {
2729

2830
private List<String> getPermissions() throws ParseException {
2931
List<String> permissions = new ArrayList<>();
30-
String sessionId = requestContext.getCookies().get("SESSION_ID").getValue();
31-
List<String> roles = jwtService.getRoles(sessionService.getSession(sessionId).get().getJwt());
32+
String jwt;
33+
if (ConfigUtils.getProfiles().contains("test")) {
34+
jwt = requestContext.getHeaders().get("Authorization").getFirst();
35+
}else {
36+
String sessionId = requestContext.getCookies().get("SESSION_ID").getValue();
37+
jwt = sessionService.getSession(sessionId).get().getJwt();
38+
}
39+
List<String> roles = jwtService.getRoles(jwt);
3240
roles.forEach(role->{
3341
Roles userRole = Roles.valueOf(role.toUpperCase());
3442
switch (userRole) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.example.app.task.resource;
2+
3+
import io.restassured.RestAssured;
4+
import io.restassured.http.ContentType;
5+
import io.restassured.response.Response;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class KeycloakTokenProvider {
11+
12+
private static final String KEYCLOAK_URL = "http://localhost:8180/realms/quarkus/protocol/openid-connect/token";
13+
private static final String CLIENT_ID = "backend-service";
14+
private static final String USERNAME_ADMIN = "alice";
15+
private static final String PASSWORD_ADMIN = "alice";
16+
private static final String USERNAME_USER = "bob";
17+
private static final String PASSWORD_USER = "bob";
18+
private static final String SECRET = "secret";
19+
20+
public static String getAccessTokenWithAdmin() {
21+
return getAccessToken(USERNAME_ADMIN, PASSWORD_ADMIN);
22+
}
23+
24+
public static String getAccessTokenWithUser() {
25+
return getAccessToken(USERNAME_USER, PASSWORD_USER);
26+
}
27+
28+
public static String getAccessToken(String username, String password) {
29+
Map<String, String> params = new HashMap<>();
30+
params.put("grant_type", "password");
31+
params.put("client_id", CLIENT_ID);
32+
params.put("username", username);
33+
params.put("password", password);
34+
params.put("client_secret", SECRET);
35+
36+
37+
Response response = RestAssured.given()
38+
.contentType(ContentType.URLENC)
39+
.formParams(params)
40+
.post(KEYCLOAK_URL);
41+
42+
if (response.getStatusCode() != 200) {
43+
throw new RuntimeException("Failed to get token: " + response.getBody().asString());
44+
}
45+
46+
return response.jsonPath().getString("access_token");
47+
}
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.app.task.service;
2+
3+
import io.quarkus.test.junit.QuarkusTestProfile;
4+
5+
public class IntegrationTestProfile implements QuarkusTestProfile {
6+
7+
@Override
8+
public String getConfigProfile() {
9+
return "test";
10+
}
11+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
package org.example.app.task.service;
3+
4+
import static io.restassured.RestAssured.given;
5+
import static org.hamcrest.Matchers.emptyString;
6+
import static org.hamcrest.Matchers.not;
7+
8+
import io.quarkus.test.junit.TestProfile;
9+
import org.example.app.task.resource.KeycloakTokenProvider;
10+
import org.hamcrest.Matchers;
11+
import org.junit.jupiter.api.*;
12+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
13+
import org.junit.jupiter.api.TestInstance.Lifecycle;
14+
15+
import io.quarkus.test.junit.QuarkusIntegrationTest;
16+
import io.restassured.http.ContentType;
17+
import io.restassured.response.Response;
18+
19+
/**
20+
* E2E black-box test of the To-Do service only via its public REST resource.
21+
*/
22+
@QuarkusIntegrationTest
23+
@TestMethodOrder(OrderAnnotation.class)
24+
@TestProfile(IntegrationTestProfile.class)
25+
@TestInstance(Lifecycle.PER_CLASS)
26+
class TaskServiceIT {
27+
28+
private Integer taskListId;
29+
30+
private Integer taskItemId;
31+
32+
private String token;
33+
34+
@BeforeAll
35+
void getJwt(){
36+
token = KeycloakTokenProvider.getAccessTokenWithAdmin();
37+
}
38+
39+
@Test
40+
@Order(1)
41+
void shouldAllowCreatingANewTaskList() {
42+
43+
Response response = given().when().header("Authorization", token).body("{ \"title\": \"Shopping List\" }").contentType(ContentType.JSON)
44+
.post("/task/list");
45+
response.then().statusCode(201).header("Location", not(emptyString()));
46+
47+
this.taskListId = Integer.parseInt(response.header("Location").replaceAll(".*?/task/list/", ""));
48+
}
49+
50+
@Test
51+
@Order(2)
52+
void shouldAllowAddingATaskToATaskList() {
53+
54+
Response response = given().when().header("Authorization", token).body("{ \"title\": \"Buy Milk\", \"taskListId\": " + this.taskListId + " }")
55+
.contentType(ContentType.JSON).post("/task/item");
56+
57+
response.then().statusCode(201).header("Location", not(emptyString()));
58+
59+
this.taskItemId = Integer.parseInt(response.header("Location").replaceAll(".*?/task/item/", ""));
60+
}
61+
62+
@Test
63+
@Order(3)
64+
void shouldAllowRetrievingATaskListWithTaskItems() {
65+
66+
given().when().header("Authorization", token).get("/task/list-with-items/{taskListId}", this.taskListId).then().statusCode(200)
67+
.body("list.title", Matchers.equalTo("Shopping List")).and().body("list.id", Matchers.equalTo(this.taskListId))
68+
.and().body("items[0].title", Matchers.equalTo("Buy Milk"));
69+
}
70+
71+
@Test
72+
@Order(4)
73+
void shouldAllowDeletingATaskListCompletely() {
74+
75+
given().when().header("Authorization", token).delete("/task/list/{taskListId}", this.taskListId).then().statusCode(204);
76+
given().when().header("Authorization", token).get("/task/list/{taskListId}", this.taskListId).then().statusCode(404);
77+
given().when().header("Authorization", token).get("/task/item/{itemId}", this.taskItemId).then().statusCode(404);
78+
79+
}
80+
}

0 commit comments

Comments
 (0)