Improve anonymous tests readability

This commit is contained in:
ch4mpy 2023-02-17 12:18:02 -10:00
parent b4d18fd227
commit 66d71f0697
6 changed files with 34 additions and 15 deletions

View File

@ -24,7 +24,7 @@ class ReactiveResourceServerApplicationIntegrationTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetGreet_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetGreet_thenUnauthorized() throws Exception {
// @formatter:off
api.get().uri("/greet").exchange()
.expectStatus().isUnauthorized();
@ -32,7 +32,9 @@ class ReactiveResourceServerApplicationIntegrationTest {
}
@Test
@WithMockJwtAuth(authorities = { "admin", "ROLE_AUTHORIZED_PERSONNEL" }, claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
@WithMockJwtAuth(
authorities = {"admin", "ROLE_AUTHORIZED_PERSONNEL"},
claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void givenUserIsAuthenticated_whenGetGreet_thenOk() throws Exception {
// @formatter:off
api.get().uri("/greet").exchange()
@ -48,7 +50,7 @@ class ReactiveResourceServerApplicationIntegrationTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetSecuredRoute_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredRoute_thenUnauthorized() throws Exception {
// @formatter:off
api.get().uri("/secured-route").exchange()
.expectStatus().isUnauthorized();
@ -81,7 +83,7 @@ class ReactiveResourceServerApplicationIntegrationTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetSecuredMethod_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredMethod_thenUnauthorized() throws Exception {
// @formatter:off
api.get().uri("/secured-method").exchange()
.expectStatus().isUnauthorized();

View File

@ -34,12 +34,14 @@ class SpringAddonsGreetingControllerUnitTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetGreet_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetGreet_thenUnauthorized() throws Exception {
api.get().uri("/greet").exchange().expectStatus().isUnauthorized();
}
@Test
@WithMockJwtAuth(claims = @OpenIdClaims(preferredUsername = "Tonton Pirate"))
@WithMockJwtAuth(
authorities = {"admin", "ROLE_AUTHORIZED_PERSONNEL"},
claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void givenUserIsAuthenticated_whenGetGreet_thenOk() throws Exception {
final var greeting = "Whatever the service returns";
when(messageService.greet()).thenReturn(Mono.just(greeting));
@ -60,7 +62,7 @@ class SpringAddonsGreetingControllerUnitTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetSecuredRoute_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredRoute_thenUnauthorized() throws Exception {
api.get().uri("/secured-route").exchange().expectStatus().isUnauthorized();
}
@ -93,7 +95,7 @@ class SpringAddonsGreetingControllerUnitTest {
@Test
@WithAnonymousUser
void givenUserIsNotAuthenticated_whenGetSecuredMethod_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredMethod_thenUnauthorized() throws Exception {
api.get().uri("/secured-method").exchange().expectStatus().isUnauthorized();
}

View File

@ -6,6 +6,8 @@ import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockAuthentication;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockJwt;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
@ -13,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.oidc.StandardClaimNames;
import org.springframework.test.web.reactive.server.WebTestClient;
import com.baeldung.ReactiveResourceServerApplication.GreetingController;
@ -39,7 +42,7 @@ class SpringSecurityTestGreetingControllerUnitTest {
/*-----------------------------------------------------------------------------*/
@Test
void givenUserIsNotAuthenticated_whenGetGreet_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetGreet_thenUnauthorized() throws Exception {
// @formatter:off
api.mutateWith(mockAuthentication(ANONYMOUS_AUTHENTICATION))
.get().uri("/greet").exchange()
@ -53,7 +56,9 @@ class SpringSecurityTestGreetingControllerUnitTest {
when(messageService.greet()).thenReturn(Mono.just(greeting));
// @formatter:off
api.mutateWith(mockJwt())
api.mutateWith(mockJwt()
.authorities(List.of(new SimpleGrantedAuthority("admin"), new SimpleGrantedAuthority("ROLE_AUTHORIZED_PERSONNEL")))
.jwt(jwt -> jwt.claim(StandardClaimNames.PREFERRED_USERNAME, "ch4mpy")))
.get().uri("/greet").exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo(greeting);
@ -68,7 +73,7 @@ class SpringSecurityTestGreetingControllerUnitTest {
/*---------------------------------------------------------------------------------------------------------------------*/
@Test
void givenUserIsNotAuthenticated_whenGetSecuredRoute_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredRoute_thenUnauthorized() throws Exception {
// @formatter:off
api.mutateWith(mockAuthentication(ANONYMOUS_AUTHENTICATION))
.get().uri("/secured-route").exchange()
@ -104,7 +109,7 @@ class SpringSecurityTestGreetingControllerUnitTest {
/*---------------------------------------------------------------------------------------------------------*/
@Test
void givenUserIsNotAuthenticated_whenGetSecuredMethod_thenUnauthorized() throws Exception {
void givenUserIsAnonymous_whenGetSecuredMethod_thenUnauthorized() throws Exception {
// @formatter:off
api.mutateWith(mockAuthentication(ANONYMOUS_AUTHENTICATION))
.get().uri("/secured-method").exchange()

View File

@ -36,7 +36,9 @@ class ServletResourceServerApplicationIntegrationTest {
}
@Test
@WithMockJwtAuth(authorities = { "admin", "ROLE_AUTHORIZED_PERSONNEL" }, claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
@WithMockJwtAuth(
authorities = {"admin", "ROLE_AUTHORIZED_PERSONNEL"},
claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void givenUserIsAuthenticated_whenGetGreet_thenOk() throws Exception {
// @formatter:off
api.perform(get("/greet"))

View File

@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.MockMvc;
import com.baeldung.ServletResourceServerApplication.GreetingController;
import com.baeldung.ServletResourceServerApplication.MessageService;
import com.c4_soft.springaddons.security.oauth2.test.annotations.OpenIdClaims;
import com.c4_soft.springaddons.security.oauth2.test.annotations.WithMockJwtAuth;
@WebMvcTest(controllers = GreetingController.class, properties = { "server.ssl.enabled=false" })
@ -42,7 +43,9 @@ class SpringAddonsGreetingControllerUnitTest {
}
@Test
@WithMockJwtAuth
@WithMockJwtAuth(
authorities = {"admin", "ROLE_AUTHORIZED_PERSONNEL"},
claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void givenUserIsAuthenticated_whenGetGreet_thenOk() throws Exception {
final var greeting = "Whatever the service returns";
when(messageService.greet()).thenReturn(greeting);

View File

@ -9,11 +9,14 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.oidc.StandardClaimNames;
import org.springframework.test.web.servlet.MockMvc;
import com.baeldung.ServletResourceServerApplication.GreetingController;
@ -47,7 +50,9 @@ class SpringSecurityTestGreetingControllerUnitTest {
when(messageService.greet()).thenReturn(greeting);
// @formatter:off
api.perform(get("/greet").with(jwt()))
api.perform(get("/greet").with(jwt()
.authorities(List.of(new SimpleGrantedAuthority("admin"), new SimpleGrantedAuthority("ROLE_AUTHORIZED_PERSONNEL")))
.jwt(jwt -> jwt.claim(StandardClaimNames.PREFERRED_USERNAME, "ch4mpy"))))
.andExpect(status().isOk())
.andExpect(content().string(greeting));
// @formatter:on