Skip to content

Commit

Permalink
fix(sonar): simplify authentication response type
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoin committed Oct 14, 2024
1 parent 059b93d commit 9cc82e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
<%_ } _%>
}
Expand All @@ -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()));
<%_ } _%>
}
Expand Down

0 comments on commit 9cc82e5

Please # to comment.