Skip to content

Commit f55a163

Browse files
authored
[conluz-106] Implemented endpoint to get current authenticated user (#113)
1 parent 87137f2 commit f55a163

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.user.get;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
5+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
6+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
7+
import org.lucoenergia.conluz.domain.admin.user.User;
8+
import org.lucoenergia.conluz.domain.admin.user.auth.AuthService;
9+
import org.lucoenergia.conluz.infrastructure.admin.user.UserResponse;
10+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag;
11+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.InternalServerErrorResponse;
12+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.UnauthorizedErrorResponse;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.security.access.prepost.PreAuthorize;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import java.util.Optional;
20+
21+
/**
22+
* Returns the currently authenticated user basic information
23+
*/
24+
@RestController
25+
@RequestMapping("/api/v1/users/current")
26+
public class GetCurrentUserController {
27+
28+
private final AuthService authService;
29+
30+
public GetCurrentUserController(AuthService authService) {
31+
this.authService = authService;
32+
}
33+
34+
@GetMapping
35+
@Operation(
36+
summary = "Get current authenticated user",
37+
description = "Returns basic information about the currently authenticated user.",
38+
tags = ApiTag.USERS,
39+
operationId = "getCurrentUser",
40+
security = @SecurityRequirement(name = "bearerToken")
41+
)
42+
@ApiResponses(value = {
43+
@ApiResponse(
44+
responseCode = "200",
45+
description = "Query executed successfully",
46+
useReturnTypeSchema = true
47+
)
48+
})
49+
@UnauthorizedErrorResponse
50+
@InternalServerErrorResponse
51+
@PreAuthorize("isAuthenticated()")
52+
public ResponseEntity<UserResponse> getCurrentUser() {
53+
Optional<User> currentUser = authService.getCurrentUser();
54+
// Although the endpoint is protected, in case the authentication is missing/invalid, respond 401
55+
return currentUser.map(user -> ResponseEntity.ok(new UserResponse(user)))
56+
.orElseGet(() -> ResponseEntity.status(401).build());
57+
}
58+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.user.get;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.lucoenergia.conluz.domain.admin.user.DefaultUserAdminMother;
5+
import org.lucoenergia.conluz.infrastructure.shared.BaseControllerTest;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
15+
@Transactional
16+
class GetCurrentUserControllerTest extends BaseControllerTest {
17+
18+
private static final String URL = "/api/v1/users/current";
19+
20+
@Test
21+
void testGetCurrentUserSuccess() throws Exception {
22+
String authHeader = loginAsDefaultAdmin();
23+
24+
mockMvc.perform(get(URL)
25+
.header(HttpHeaders.AUTHORIZATION, authHeader))
26+
.andDo(print())
27+
.andExpect(status().isOk())
28+
.andExpect(jsonPath("$.personalId").value(DefaultUserAdminMother.PERSONAL_ID))
29+
.andExpect(jsonPath("$.fullName").value(DefaultUserAdminMother.FULL_NAME))
30+
.andExpect(jsonPath("$.email").value(DefaultUserAdminMother.EMAIL))
31+
.andExpect(jsonPath("$.role").value("ADMIN"))
32+
.andExpect(jsonPath("$.enabled").value(true))
33+
.andExpect(jsonPath("$.password").doesNotExist());
34+
}
35+
36+
@Test
37+
void testGetCurrentUserUnauthorizedWhenMissingToken() throws Exception {
38+
mockMvc.perform(get(URL))
39+
.andDo(print())
40+
.andExpect(status().isUnauthorized())
41+
.andExpect(jsonPath("$.timestamp").isNotEmpty())
42+
.andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value()))
43+
.andExpect(jsonPath("$.message").isNotEmpty())
44+
.andExpect(jsonPath("$.traceId").isNotEmpty());
45+
}
46+
}

0 commit comments

Comments
 (0)