From 9cc82e5cdad2312451e7d2f87d6fbacfc9678ad0 Mon Sep 17 00:00:00 2001 From: gantoin Date: Mon, 14 Oct 2024 09:37:04 +0200 Subject: [PATCH] fix(sonar): simplify authentication response type --- .../web/rest/AuthenticateController.java.ejs | 10 +++---- .../web/rest/AccountResourceIT.java.ejs | 26 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/generators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs b/generators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs index 9c8c94892876..4be1c99206c9 100644 --- a/generators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs +++ b/generators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs @@ -117,15 +117,15 @@ public class AuthenticateController { <%_ } _%> /** - * {@code GET /authenticate} : check if the user is authenticated, and return its login. + * {@code GET /authenticate} : check if the user is authenticated, and return a boolean value. * * @param principal the authentication principal. - * @return the login if the user is authenticated. + * @return a boolean indicating if the user is authenticated. */ - @GetMapping(value = "/authenticate", produces = MediaType.TEXT_PLAIN_VALUE) - public String isAuthenticated(Principal principal) { + @GetMapping(value = "/authenticate") + public Boolean isAuthenticated(Principal principal) { LOG.debug("REST request to check if the current user is authenticated"); - return principal == null ? null : principal.getName(); + return principal != null; } public String createToken(Authentication authentication, boolean rememberMe) { diff --git a/generators/spring-boot/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs index 41a763744ec4..d052df0b5453 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs @@ -174,16 +174,17 @@ class AccountResourceIT { <%_ if (reactive) { _%> void testNonAuthenticatedUser() { accountWebTestClient.get().uri("/api/authenticate") - .accept(MediaType.TEXT_PLAIN) .exchange() - .expectStatus().isOk() - .expectBody().isEmpty(); + .expectStatus() + .isOk() + .expectBody(Boolean.class) + .isEqualTo(false); <%_ } else { _%> void testNonAuthenticatedUser() throws Exception { restAccountMockMvc - .perform(get("/api/authenticate").accept(MediaType.TEXT_PLAIN)) + .perform(get("/api/authenticate")) .andExpect(status().isOk()) - .andExpect(content().string("")); + .andExpect(content().string(Boolean.FALSE.toString())); <%_ } _%> } @@ -192,17 +193,20 @@ class AccountResourceIT { <%_ if (reactive) { _%> void testAuthenticatedUser() { accountWebTestClient - .get().uri("/api/authenticate") - .accept(MediaType.TEXT_PLAIN) + .get() + .uri("/api/authenticate") .exchange() - .expectStatus().isOk() - .expectBody(String.class).isEqualTo(TEST_USER_LOGIN); + .expectStatus() + .isOk() + .expectBody(Boolean.class) + .isEqualTo(true); <%_ } else { _%> void testAuthenticatedUser() throws Exception { restAccountMockMvc - .perform(get("/api/authenticate").with(request -> request).accept(MediaType.TEXT_PLAIN)) + .perform(get("/api/authenticate") + .with(request -> request)) .andExpect(status().isOk()) - .andExpect(content().string(TEST_USER_LOGIN)); + .andExpect(content().string(Boolean.TRUE.toString())); <%_ } _%> }