JWT claims injection not working for no reason #33175
-
package com.github.ninekothecat.unit.quarkus.resources;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.is;
import com.github.ninekothecat.core.entities.HoardItem;
import com.github.ninekothecat.core.entities.HoardItemId;
import com.github.ninekothecat.core.entities.User;
import com.github.ninekothecat.core.entities.UserId;
import com.github.ninekothecat.core.repositories.HoardItemRepository;
import com.github.ninekothecat.core.repositories.UserRepository;
import com.github.ninekothecat.quarkus.resources.HoardItems;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.keycloak.client.KeycloakTestClient;
import io.quarkus.test.security.TestSecurity;
import io.quarkus.test.security.jwt.Claim;
import io.quarkus.test.security.jwt.JwtSecurity;
import javax.inject.Inject;
import javax.inject.Named;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@QuarkusTest
@TestHTTPEndpoint(HoardItems.class)
public class HoardItemsTests {
@Inject
@Named("MongoUserRepository")
private UserRepository repository;
final String opidId = "test-id";
final String userIdRaw = "userId";
final UserId testUserId = UserId.builder()
.id(userIdRaw)
.openid(opidId)
.build();
final String testUserUsername = "test_user";
final User testUser = User.builder()
.id(testUserId)
.username(testUserUsername)
.build();
final HoardItemId testHoardItemId = HoardItemId.builder()
.id("246")
.build();
final HoardItem testHoardItem = HoardItem.builder()
.name("test item")
.id(testHoardItemId)
.build();
@BeforeEach
void setUp() {
repository.createUser(testUser).await().indefinitely();
}
@Inject
HoardItemRepository itemRepository;
@Test
@TestSecurity(user = testUserUsername, roles = { "user"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = opidId),
@Claim(key = "upn" , value = testUserUsername)
})
public void shouldReturnExistingItem() {
//given
itemRepository.createHoardItemForUser(testHoardItem, testUserId);
//when
when().get("item/{id}", testHoardItemId.getId())
.then()
.statusCode(200).body(is(testHoardItem));
}
@Test
@TestSecurity(user = testUserUsername, roles = { "user"})
@JwtSecurity(claims = {
@Claim(key = "sub", value = opidId),
@Claim(key = "upn", value = testUserUsername)
})
public void ShouldNotReturnItemIfThereIsNoItem() {
//given
//when
when().get("item/{id}", testHoardItemId.getId())
.then()
.statusCode(404).body(emptyString());
}
} this class is supposed to test an existing endpoint but the JWT token is not being injected at all |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
/cc @Ladicek (arc), @manovotn (arc), @mkouba (arc), @sberyozkin (jwt) |
Beta Was this translation helpful? Give feedback.
-
@NinekoTheCat I don't see your code injecting the token, can you create a small reproducer ? These annotations are being tested |
Beta Was this translation helpful? Give feedback.
-
https://github.com/NinekoTheCat/JWT-madness @sberyozkin this is a reproduction |
Beta Was this translation helpful? Give feedback.
-
@NinekoTheCat I see, it is just that the test module is only providing a |
Beta Was this translation helpful? Give feedback.
@NinekoTheCat I see, it is just that the test module is only providing a
JsonWebToken
producer but not an individual Claim producer.If you inject the token directly then it will work.
Can you please open an enhancement request?