NCBACK-9 #7
@@ -9,7 +9,9 @@ import org.springframework.validation.BindingResult;
|
|||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.soune.no_copy.dto.BaseRequest;
|
import ru.soune.no_copy.dto.BaseRequest;
|
||||||
|
import ru.soune.no_copy.dto.BaseResponse;
|
||||||
import ru.soune.no_copy.dto.MessageCode;
|
import ru.soune.no_copy.dto.MessageCode;
|
||||||
|
import ru.soune.no_copy.dto.RegAnswer;
|
||||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||||
import ru.soune.no_copy.exception.ValidationException;
|
import ru.soune.no_copy.exception.ValidationException;
|
||||||
import ru.soune.no_copy.handler.LoginRequestHandler;
|
import ru.soune.no_copy.handler.LoginRequestHandler;
|
||||||
@@ -48,15 +50,15 @@ public class ApiController {
|
|||||||
RequestHandler handler = handlers.get(msgId);
|
RequestHandler handler = handlers.get(msgId);
|
||||||
|
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
return ResponseEntity.ok().body(fillResponseInfo(msgId,
|
new BaseResponse(msgId,
|
||||||
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
||||||
MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
MessageCode.MSG_ID_NOT_FOUND.getDescription(),
|
||||||
|
new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> response = handler.handle(request);
|
BaseResponse response = handler.handle(request);
|
||||||
|
|
||||||
return ResponseEntity.ok().body(response);
|
return ResponseEntity.ok().body(response);
|
||||||
|
|
||||||
} catch (ValidationException e) {
|
} catch (ValidationException e) {
|
||||||
return createValidationErrorResponse(e.getBindingResult(), e.getMsgId());
|
return createValidationErrorResponse(e.getBindingResult(), e.getMsgId());
|
||||||
} catch (NotValidFieldException e) {
|
} catch (NotValidFieldException e) {
|
||||||
@@ -67,20 +69,17 @@ public class ApiController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<Map<String, Object>> createValidationErrorResponse(
|
private ResponseEntity<BaseResponse> createValidationErrorResponse(BindingResult bindingResult, Integer msgId) {
|
||||||
BindingResult bindingResult, Integer msgId) {
|
|
||||||
|
|
||||||
Map<String, Object> response = fillResponseInfo(msgId, MessageCode.INVALID_FIELD.getCode(),
|
|
||||||
MessageCode.INVALID_FIELD.getDescription());
|
|
||||||
|
|
||||||
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
||||||
.stream()
|
.stream()
|
||||||
.map(this::createErrorDetail)
|
.map(this::createErrorDetail)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
response.put("field_errors", fieldErrors);
|
RegAnswer regAnswer = new RegAnswer();
|
||||||
|
regAnswer.setFieldErrors(fieldErrors);
|
||||||
|
|
||||||
return ResponseEntity.badRequest().body(response);
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.INVALID_FIELD.getCode(),
|
||||||
|
MessageCode.INVALID_FIELD.getDescription(), regAnswer));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, String> createErrorDetail(FieldError fieldError) {
|
private Map<String, String> createErrorDetail(FieldError fieldError) {
|
||||||
@@ -96,13 +95,4 @@ public class ApiController {
|
|||||||
|
|
||||||
return errorDetail;
|
return errorDetail;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> fillResponseInfo(Integer msgId, Integer messageCode, String messageDesc) {
|
|
||||||
Map<String, Object> response = new HashMap<>();
|
|
||||||
response.put("msg_id", msgId);
|
|
||||||
response.put("message_code", messageCode);
|
|
||||||
response.put("message_desc", messageDesc);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package ru.soune.no_copy.dto;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
public record AuthResponse (
|
|
||||||
boolean success,
|
|
||||||
String message,
|
|
||||||
String token,
|
|
||||||
LocalDateTime expiresAt
|
|
||||||
) {}
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.soune.no_copy.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class LogOutAnswer {
|
||||||
|
private String Token;
|
||||||
|
|
||||||
|
private List<Map<String, String>> fieldErrors;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.soune.no_copy.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class LoginAnswer {
|
||||||
|
private String Token;
|
||||||
|
|
||||||
|
private List<Map<String, String>> fieldErrors;
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package ru.soune.no_copy.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import ru.soune.no_copy.entity.User;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class LoginResponse {
|
|
||||||
private boolean success;
|
|
||||||
private String email;
|
|
||||||
private String token;
|
|
||||||
private String expiresAt;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.soune.no_copy.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class RegAnswer {
|
||||||
|
private String Token;
|
||||||
|
|
||||||
|
private List<Map<String, String>> fieldErrors;
|
||||||
|
}
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package ru.soune.no_copy.dto;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
import jakarta.validation.constraints.Email;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.Past;
|
|
||||||
import jakarta.validation.constraints.Size;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
import ru.soune.no_copy.entity.GenderType;
|
|
||||||
import ru.soune.no_copy.entity.SubscriptionType;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
public record RegisterRequest(
|
|
||||||
@NotBlank(message = "error.name.length")
|
|
||||||
String fullName,
|
|
||||||
|
|
||||||
String companyName,
|
|
||||||
|
|
||||||
@Size(min = 11, max = 14)
|
|
||||||
String phone,
|
|
||||||
|
|
||||||
@NotBlank(message = "error.not.blank")
|
|
||||||
@Email(message = "error.not.email")
|
|
||||||
@Size(max = 128)
|
|
||||||
String email,
|
|
||||||
|
|
||||||
@DateTimeFormat(pattern = "dd-MM-yyyy")
|
|
||||||
@JsonFormat(pattern = "dd-MM-yyyy")
|
|
||||||
@Past
|
|
||||||
LocalDate birthday,
|
|
||||||
|
|
||||||
@NotBlank(message = "error.not.blank")
|
|
||||||
@Size(min = 8)
|
|
||||||
String password,
|
|
||||||
|
|
||||||
SubscriptionType subscriptionType,
|
|
||||||
|
|
||||||
GenderType genderType
|
|
||||||
) {}
|
|
||||||
@@ -1,23 +1,18 @@
|
|||||||
package ru.soune.no_copy.exception;
|
package ru.soune.no_copy.exception;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import ru.soune.no_copy.dto.BaseResponse;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class NotValidFieldException extends RuntimeException {
|
public class NotValidFieldException extends RuntimeException {
|
||||||
|
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|
||||||
private final Integer messageId;
|
private final BaseResponse baseResponse;
|
||||||
|
|
||||||
private final Integer messageCode;
|
public NotValidFieldException(String message, BaseResponse baseResponse) {
|
||||||
|
|
||||||
private final String messageDescription;
|
|
||||||
|
|
||||||
public NotValidFieldException(String message, Integer messageId, Integer messageCode, String messageDescription) {
|
|
||||||
super(message);
|
super(message);
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.messageId = messageId;
|
this.baseResponse = baseResponse;
|
||||||
this.messageCode = messageCode;
|
|
||||||
this.messageDescription = messageDescription;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,7 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.body(Map.of(
|
.body(ex.getBaseResponse());
|
||||||
"msg_id", ex.getMessageId(),
|
|
||||||
"message_code", ex.getMessageCode(),
|
|
||||||
"message_desc", ex.getMessageDescription()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(UserNotFoundException.class)
|
@ExceptionHandler(UserNotFoundException.class)
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package ru.soune.no_copy.handler;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class JwtTokenProvider {
|
|
||||||
}
|
|
||||||
@@ -3,13 +3,13 @@ package ru.soune.no_copy.handler;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.soune.no_copy.dto.BaseRequest;
|
import ru.soune.no_copy.dto.*;
|
||||||
import ru.soune.no_copy.dto.LoginRequest;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.dto.MessageCode;
|
|
||||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
import ru.soune.no_copy.service.AuthService;
|
import ru.soune.no_copy.service.AuthService;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -22,26 +22,24 @@ public class LoginRequestHandler implements RequestHandler {
|
|||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> handle(BaseRequest request) {
|
public BaseResponse handle(BaseRequest request) {
|
||||||
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
||||||
|
|
||||||
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
||||||
|
LoginAnswer loginAnswer = new LoginAnswer();
|
||||||
|
loginAnswer.setFieldErrors(Arrays.asList(Map.of("email", loginRequest.getEmail())));
|
||||||
|
|
||||||
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
||||||
request.getMsgId(), MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
new BaseResponse(request.getMsgId(), MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
||||||
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription());
|
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription(), loginAnswer));
|
||||||
}
|
}
|
||||||
|
|
||||||
authService.login(loginRequest);
|
AuthToken authToken = authService.login(loginRequest);
|
||||||
|
|
||||||
return fillResponseInfo(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
LoginAnswer loginAnswer = new LoginAnswer();
|
||||||
MessageCode.SUCCESS.getDescription());
|
loginAnswer.setToken(authToken.getToken());
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> fillResponseInfo(Integer msgId, Integer messageCode, String messageDesc) {
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
Map<String, Object> response = new HashMap<>();
|
MessageCode.SUCCESS.getDescription(), loginAnswer);
|
||||||
response.put("msg_id", msgId);
|
|
||||||
response.put("message_code", messageCode);
|
|
||||||
response.put("message_desc", messageDesc);
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,35 +6,40 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.validation.BeanPropertyBindingResult;
|
import org.springframework.validation.BeanPropertyBindingResult;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import ru.soune.no_copy.dto.BaseRequest;
|
import ru.soune.no_copy.dto.*;
|
||||||
import ru.soune.no_copy.dto.MessageCode;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.dto.RegRequest;
|
|
||||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||||
import ru.soune.no_copy.exception.ValidationException;
|
import ru.soune.no_copy.exception.ValidationException;
|
||||||
|
import ru.soune.no_copy.handler.validator.RegRequestValidator;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
import ru.soune.no_copy.service.AuthService;
|
import ru.soune.no_copy.service.AuthService;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RegRequestHandler implements RequestHandler {
|
public class RegRequestHandler implements RequestHandler {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
private final RegRequestValidator regRequestValidator;
|
private final RegRequestValidator regRequestValidator;
|
||||||
|
|
||||||
private final AuthService authService;
|
private final AuthService authService;
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> handle(BaseRequest request) throws ValidationException {
|
public BaseResponse handle(BaseRequest request) throws ValidationException {
|
||||||
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||||||
|
|
||||||
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
||||||
|
RegAnswer regAnswer = new RegAnswer();
|
||||||
|
regAnswer.setFieldErrors(Arrays.asList(Map.of("email", regRequest.getEmail())));
|
||||||
|
|
||||||
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
|
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
|
||||||
request.getMsgId(), MessageCode.REG_EMAIL_EXISTS.getCode(),
|
new BaseResponse(request.getMsgId(), MessageCode.REG_EMAIL_EXISTS.getCode(),
|
||||||
MessageCode.REG_EMAIL_EXISTS.getDescription());
|
MessageCode.REG_EMAIL_EXISTS.getDescription(), regAnswer));
|
||||||
}
|
}
|
||||||
|
|
||||||
BindingResult bindingResult = new BeanPropertyBindingResult(regRequest, "regRequest");
|
BindingResult bindingResult = new BeanPropertyBindingResult(regRequest, "regRequest");
|
||||||
@@ -44,16 +49,12 @@ public class RegRequestHandler implements RequestHandler {
|
|||||||
throw new ValidationException(bindingResult, request.getMsgId());
|
throw new ValidationException(bindingResult, request.getMsgId());
|
||||||
}
|
}
|
||||||
|
|
||||||
authService.register(regRequest);
|
AuthToken authToken = authService.register(regRequest);
|
||||||
return fillResponseInfo(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
|
||||||
MessageCode.SUCCESS.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> fillResponseInfo(Integer msgId, Integer messageCode, String messageDesc) {
|
RegAnswer regAnswer = new RegAnswer();
|
||||||
Map<String, Object> response = new HashMap<>();
|
regAnswer.setToken(authToken.getToken());
|
||||||
response.put("msg_id", msgId);
|
|
||||||
response.put("message_code", messageCode);
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
response.put("message_desc", messageDesc);
|
MessageCode.SUCCESS.getDescription(), regAnswer);
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
package ru.soune.no_copy.handler;
|
package ru.soune.no_copy.handler;
|
||||||
|
|
||||||
import ru.soune.no_copy.dto.BaseRequest;
|
import ru.soune.no_copy.dto.BaseRequest;
|
||||||
|
import ru.soune.no_copy.dto.BaseResponse;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public interface RequestHandler {
|
public interface RequestHandler {
|
||||||
Map<String, Object> handle(BaseRequest request) throws Exception;
|
BaseResponse handle(BaseRequest request) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.soune.no_copy.handler;
|
package ru.soune.no_copy.handler.validator;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
@@ -5,9 +5,7 @@ import org.springframework.context.MessageSource;
|
|||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.soune.no_copy.dto.LoginRequest;
|
import ru.soune.no_copy.dto.*;
|
||||||
import ru.soune.no_copy.dto.MessageCode;
|
|
||||||
import ru.soune.no_copy.dto.RegRequest;
|
|
||||||
import ru.soune.no_copy.entity.AuthToken;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.entity.User;
|
import ru.soune.no_copy.entity.User;
|
||||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||||
@@ -18,7 +16,9 @@ import ru.soune.no_copy.repository.UserRepository;
|
|||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -65,9 +65,12 @@ public class AuthService {
|
|||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
|
|
||||||
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
||||||
throw new NotValidFieldException("Invalid password", 20003,
|
LoginAnswer loginAnswer = new LoginAnswer();
|
||||||
|
loginAnswer.setFieldErrors(Arrays.asList(Map.of("password", request.getPassword())));
|
||||||
|
|
||||||
|
throw new NotValidFieldException("Invalid password", new BaseResponse(20003,
|
||||||
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getCode(),
|
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getCode(),
|
||||||
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getDescription());
|
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getDescription(), loginAnswer));
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setLastLoginAt(LocalDateTime.now());
|
user.setLastLoginAt(LocalDateTime.now());
|
||||||
|
|||||||
@@ -9,21 +9,17 @@ import org.mockito.MockitoAnnotations;
|
|||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import ru.soune.no_copy.dto.LoginRequest;
|
import ru.soune.no_copy.dto.LoginRequest;
|
||||||
import ru.soune.no_copy.dto.RegisterRequest;
|
|
||||||
import ru.soune.no_copy.entity.AuthToken;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.entity.User;
|
import ru.soune.no_copy.entity.User;
|
||||||
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
||||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
|
||||||
import ru.soune.no_copy.exception.UserNotFoundException;
|
import ru.soune.no_copy.exception.UserNotFoundException;
|
||||||
import ru.soune.no_copy.repository.AuthTokenRepository;
|
import ru.soune.no_copy.repository.AuthTokenRepository;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -49,43 +45,6 @@ public class AuthServiceTest {
|
|||||||
MockitoAnnotations.openMocks(this);
|
MockitoAnnotations.openMocks(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void registerSuccess() {
|
|
||||||
RegisterRequest request = new RegisterRequest("John","A.","124124",
|
|
||||||
"john@example.com", null,"password123", null, null);
|
|
||||||
|
|
||||||
when(userRepository.existsByEmail(request.email())).thenReturn(false);
|
|
||||||
when(passwordEncoder.encode(request.password())).thenReturn("hashed");
|
|
||||||
when(userRepository.save(any(User.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
|
||||||
when(authTokenRepository.save(any(AuthToken.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
|
||||||
|
|
||||||
AuthToken token = authService.register(request);
|
|
||||||
User user = token.getUser();
|
|
||||||
String email = user.getEmail();
|
|
||||||
|
|
||||||
assertNotNull(token);
|
|
||||||
assertNotNull(token.getToken());
|
|
||||||
assertNotNull(token.getUser());
|
|
||||||
assertEquals("john@example.com", email);
|
|
||||||
assertNotNull(token.getExpiresAt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void registerExistingEmailThrows() {
|
|
||||||
RegisterRequest request = new RegisterRequest("John","A.","124124",
|
|
||||||
"john@example.com", null,"password123", null, null);
|
|
||||||
|
|
||||||
when(userRepository.existsByEmail(request.email()))
|
|
||||||
.thenReturn(true);
|
|
||||||
when(messageSource.getMessage(anyString(), any(), any(Locale.class)))
|
|
||||||
.thenReturn("User exists");
|
|
||||||
|
|
||||||
NotValidFieldException ex = assertThrows(NotValidFieldException.class,
|
|
||||||
() -> authService.register(request));
|
|
||||||
|
|
||||||
assertEquals("User exists", ex.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void login_success() {
|
void login_success() {
|
||||||
LoginRequest request = new LoginRequest("test@mail.com", "password");
|
LoginRequest request = new LoginRequest("test@mail.com", "password");
|
||||||
|
|||||||
Reference in New Issue
Block a user