+1
-6
@@ -263,15 +263,10 @@
|
||||
<skip>${jacoco.skip}</skip>
|
||||
<excludes>
|
||||
<exclude>**/config/**</exclude>
|
||||
<exclude>**/dto/**</exclude>
|
||||
<exclude>**/entity/**</exclude>
|
||||
<exclude>**/exception/**</exclude>
|
||||
<exclude>**/filter/**</exclude>
|
||||
<exclude>**/interceptor/**</exclude>
|
||||
<exclude>**/response/**</exclude>
|
||||
<exclude>**/*$*Builder*</exclude>
|
||||
<exclude>**/RestExceptionEndpoint.*</exclude>
|
||||
<exclude>**/BackendStartApiApplication.*</exclude>
|
||||
<exclude>**/JavaApiApiApplication.*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
|
||||
+3
-3
@@ -55,7 +55,7 @@ public class AuthenticationController {
|
||||
})
|
||||
public ResponseEntity<JwtAuthenticationResponse> signUp(
|
||||
@RequestBody @Valid LoginRequest loginRequest) {
|
||||
String token = authService.create(loginRequest);
|
||||
String token = authService.signUpNewUser(loginRequest);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(new JwtAuthenticationResponse(token));
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class AuthenticationController {
|
||||
content = @Content(schema = @Schema(implementation = Void.class)))
|
||||
})
|
||||
public JwtAuthenticationResponse signIn(@RequestBody @Valid LoginRequest loginRequest) {
|
||||
String token = authService.signin(loginRequest);
|
||||
String token = authService.signInUser(loginRequest);
|
||||
return new JwtAuthenticationResponse(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package br.com.tasknoteapp.java_api.controller;
|
||||
|
||||
import br.com.tasknoteapp.java_api.response.ValidationExceptionResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class RestExceptionController {
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
ResponseEntity<ValidationExceptionResponse> handleValidationException(
|
||||
MethodArgumentNotValidException ex) {
|
||||
return ResponseEntity.badRequest().body(new ValidationExceptionResponse(ex.getFieldErrors()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package br.com.tasknoteapp.java_api.response;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
@Schema(description = "An object with fields name and the respective error massages")
|
||||
record FieldIssueResponse(String fieldName, String fieldMessage) {}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package br.com.tasknoteapp.java_api.response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.validation.FieldError;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Schema(description = "An object containing the error message and the invalid fields")
|
||||
public class ValidationExceptionResponse {
|
||||
|
||||
private static final String MESSAGE_TEMPLATE = "%d field(s) with validation problems!";
|
||||
|
||||
@Schema(description = "The error message")
|
||||
private final String errorMessage;
|
||||
|
||||
@Schema(description = "An array of 'FieldIssue' with the invalid fields")
|
||||
private final List<FieldIssueResponse> fields;
|
||||
|
||||
/**
|
||||
* The sole constructor of this class.
|
||||
*
|
||||
* @param errors all the validation problems to be listed as a response
|
||||
*/
|
||||
public ValidationExceptionResponse(List<FieldError> errors) {
|
||||
this.fields =
|
||||
errors.stream()
|
||||
.map(error -> new FieldIssueResponse(error.getField(), error.getDefaultMessage()))
|
||||
.toList();
|
||||
this.errorMessage = String.format(MESSAGE_TEMPLATE, fields.size());
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package br.com.tasknoteapp.java_api.service;
|
||||
import br.com.tasknoteapp.java_api.entity.UserEntity;
|
||||
import br.com.tasknoteapp.java_api.request.LoginRequest;
|
||||
import br.com.tasknoteapp.java_api.response.UserResponse;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
@@ -17,7 +16,7 @@ public interface AuthService {
|
||||
* @param login User details with email and password.
|
||||
* @return Token
|
||||
*/
|
||||
public String create(LoginRequest login);
|
||||
public String signUpNewUser(LoginRequest login);
|
||||
|
||||
/**
|
||||
* Find a user by email in the database.
|
||||
@@ -41,7 +40,7 @@ public interface AuthService {
|
||||
* @param login User details with email and password.
|
||||
* @return Token
|
||||
*/
|
||||
public String signin(LoginRequest login);
|
||||
public String signInUser(LoginRequest login);
|
||||
|
||||
/**
|
||||
* Get all registered users.
|
||||
|
||||
+3
-3
@@ -46,8 +46,8 @@ class AuthServiceImpl implements AuthService {
|
||||
* @return Token
|
||||
*/
|
||||
@Override
|
||||
public String create(LoginRequest login) {
|
||||
log.info("Creating user! {}", login.email());
|
||||
public String signUpNewUser(LoginRequest login) {
|
||||
log.info("Signing up new user! {}", login.email());
|
||||
|
||||
if (findByEmail(login.email()).isPresent()) {
|
||||
throw new UserAlreadyExistsException();
|
||||
@@ -105,7 +105,7 @@ class AuthServiceImpl implements AuthService {
|
||||
* @return Token
|
||||
*/
|
||||
@Override
|
||||
public String signin(LoginRequest login) {
|
||||
public String signInUser(LoginRequest login) {
|
||||
log.info("Signing in user! {}", login.email());
|
||||
|
||||
Optional<UserEntity> user = findByEmail(login.email());
|
||||
|
||||
+32
-3
@@ -2,6 +2,7 @@ package br.com.tasknoteapp.java_api.controller;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@@ -32,7 +33,7 @@ class AuthenticationControllerTest {
|
||||
LoginRequest request = new LoginRequest("user@domain.com", "abcde123456");
|
||||
final String token = "xaxbxcxdx1x2x3A@";
|
||||
|
||||
when(authService.create(request)).thenReturn(token);
|
||||
when(authService.signUpNewUser(request)).thenReturn(token);
|
||||
|
||||
String jsonString =
|
||||
"""
|
||||
@@ -60,7 +61,7 @@ class AuthenticationControllerTest {
|
||||
LoginRequest request = new LoginRequest("user@domain..com", "abcde123456");
|
||||
final String token = "xaxbxcxdx1x2x3@A";
|
||||
|
||||
when(authService.create(request)).thenReturn(token);
|
||||
when(authService.signUpNewUser(request)).thenReturn(token);
|
||||
|
||||
String jsonString =
|
||||
"""
|
||||
@@ -86,7 +87,7 @@ class AuthenticationControllerTest {
|
||||
void signup_userAlreadyExists_shouldFail() throws Exception {
|
||||
LoginRequest request = new LoginRequest("user@domain.com", "abcde123456");
|
||||
|
||||
when(authService.create(request)).thenThrow(new UserAlreadyExistsException());
|
||||
when(authService.signUpNewUser(request)).thenThrow(new UserAlreadyExistsException());
|
||||
|
||||
String jsonString =
|
||||
"""
|
||||
@@ -106,4 +107,32 @@ class AuthenticationControllerTest {
|
||||
.andExpect(status().isBadRequest())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sign in happy path should succeed")
|
||||
void signin_happyPath_shouldSucceed() throws Exception {
|
||||
LoginRequest request = new LoginRequest("user@domain.com", "abcde123456");
|
||||
final String token = "xaxbxcxdx1x2x3A@";
|
||||
|
||||
when(authService.signInUser(request)).thenReturn(token);
|
||||
|
||||
String jsonString =
|
||||
"""
|
||||
{
|
||||
"email": "user@domain.com",
|
||||
"password": "abcde123456"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc
|
||||
.perform(
|
||||
post("/auth/sign-in")
|
||||
.with(csrf().asHeader())
|
||||
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.content(jsonString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.token").value(token))
|
||||
.andReturn();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user