Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/CONLUZ-44] to main #48

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ public EnableUserController(EnableUserService service) {
@PostMapping(path = "/users/{id}/enable")
@Operation(
summary = "Enables a user by ID",
description = "This endpoint serves the purpose of enabling a previously disabled user within the system, with the user's unique identifier specified in the endpoint path. Proper authentication, through an authentication token, is required for secure access. Upon a successful request, the server responds with an HTTP status code of 200, indicating that the user has been successfully enabled. This endpoint provides a crucial mechanism for restoring user access or lifting restrictions, supporting effective user management. In situations where the enabling process encounters errors, the server responds with an appropriate error status code, accompanied by a descriptive error message to assist clients in diagnosing and resolving the issue.",
description = """
This endpoint serves the purpose of enabling a previously disabled user within the system, with the user's unique identifier specified in the endpoint path.

Proper authentication, through an authentication token, is required for secure access.

Upon a successful request, the server responds with an HTTP status code of 200, indicating that the user has been successfully enabled.

This endpoint provides a crucial mechanism for restoring user access or lifting restrictions, supporting effective user management.

In situations where the enabling process encounters errors, the server responds with an appropriate error status code, accompanied by a descriptive error message to assist clients in diagnosing and resolving the issue.""",
tags = ApiTag.USERS,
operationId = "disableUser"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
import org.lucoenergia.conluz.infrastructure.shared.BaseControllerTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Transactional
Expand Down Expand Up @@ -41,4 +46,37 @@ void testEnableUser() throws Exception {
.andExpect(status().isOk());
Assertions.assertTrue(getUserRepository.findByPersonalId(UserPersonalId.of(user.getPersonalId())).get().isEnabled());
}

@Test
void testWithUnknownUser() throws Exception {

String authHeader = loginAsDefaultAdmin();

final String userId = UUID.randomUUID().toString();

mockMvc.perform(post("/api/v1/users/" + userId + "/enable")
.header(HttpHeaders.AUTHORIZATION, authHeader)
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.timestamp").isNotEmpty())
.andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value()))
.andExpect(jsonPath("$.message").isNotEmpty())
.andExpect(jsonPath("$.traceId").isNotEmpty());
}

@Test
void testWithoutToken() throws Exception {

final String userId = UUID.randomUUID().toString();

mockMvc.perform(post("/api/v1/users/" + userId + "/enable")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.timestamp").isNotEmpty())
.andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value()))
.andExpect(jsonPath("$.message").isNotEmpty())
.andExpect(jsonPath("$.traceId").isNotEmpty());
}
}