From 66d71f0697a10aaabb74c90228870b6563e8d88a Mon Sep 17 00:00:00 2001 From: ch4mpy Date: Fri, 17 Feb 2023 12:18:02 -1000 Subject: [PATCH] Improve anonymous tests readability --- ...iveResourceServerApplicationIntegrationTest.java | 10 ++++++---- .../SpringAddonsGreetingControllerUnitTest.java | 10 ++++++---- ...pringSecurityTestGreetingControllerUnitTest.java | 13 +++++++++---- ...letResourceServerApplicationIntegrationTest.java | 4 +++- .../SpringAddonsGreetingControllerUnitTest.java | 5 ++++- ...pringSecurityTestGreetingControllerUnitTest.java | 7 ++++++- 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/ReactiveResourceServerApplicationIntegrationTest.java b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/ReactiveResourceServerApplicationIntegrationTest.java index e3c45883c2..d464aa37ad 100644 --- a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/ReactiveResourceServerApplicationIntegrationTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/ReactiveResourceServerApplicationIntegrationTest.java @@ -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(); diff --git a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java index e28136dd25..38bbb4d0bd 100644 --- a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java @@ -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(); } diff --git a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java index 291a405e29..40ba700c21 100644 --- a/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/reactive-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java @@ -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() diff --git a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/ServletResourceServerApplicationIntegrationTest.java b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/ServletResourceServerApplicationIntegrationTest.java index 0c6504bff0..eba908da75 100644 --- a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/ServletResourceServerApplicationIntegrationTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/ServletResourceServerApplicationIntegrationTest.java @@ -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")) diff --git a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java index 76902bb9b7..f18ad85832 100644 --- a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringAddonsGreetingControllerUnitTest.java @@ -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); diff --git a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java index aa53dd7531..83a95cf508 100644 --- a/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java +++ b/spring-security-modules/spring-security-oauth2-testing/servlet-resource-server/src/test/java/com/baeldung/SpringSecurityTestGreetingControllerUnitTest.java @@ -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