diff --git a/.gitignore b/.gitignore index 1dd5f46..f969291 100644 --- a/.gitignore +++ b/.gitignore @@ -107,6 +107,12 @@ local.properties # .idea/modules # *.iml # *.ipr +# *.idea/compiler.xml#. +# *.idea/dataSources.xml +# *.idea/hatcher-api.iml +# *.idea/misc.xml +# *.idea/sqldialects.xml + # CMake cmake-build-*/ diff --git a/src/main/java/org/ayty/hatcher/api/HatcherApiApplication.java b/src/main/java/org/ayty/hatcher/api/HatcherApiApplication.java index 3c108a4..7a54cc0 100644 --- a/src/main/java/org/ayty/hatcher/api/HatcherApiApplication.java +++ b/src/main/java/org/ayty/hatcher/api/HatcherApiApplication.java @@ -1,11 +1,7 @@ package org.ayty.hatcher.api; -import org.ayty.hatcher.api.v1.security.JwtAuthFilter; import org.springframework.boot.SpringApplication; - import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; @SpringBootApplication public class HatcherApiApplication { @@ -15,18 +11,6 @@ public static void main(String[] args) { SpringApplication.run(HatcherApiApplication.class, args); } - /* - @Bean - public FilterRegistrationBean filtroJwt(){ - FilterRegistrationBean filtroRB = new FilterRegistrationBean(); - filtroRB.setFilter(new JwtAuthFilter()); - filtroRB.addUrlPatterns("hatcher/profile/registerUser","hatcher/profile/user/registerCourse","hatcher/profile/user/course/remove","hatche/profile/user/course/edit"); - return filtroRB; - - - - } - */ } diff --git a/src/main/java/org/ayty/hatcher/api/util/ConfigurationInternationalization.java b/src/main/java/org/ayty/hatcher/api/util/ConfigurationInternationalization.java index 98e330b..30484dd 100644 --- a/src/main/java/org/ayty/hatcher/api/util/ConfigurationInternationalization.java +++ b/src/main/java/org/ayty/hatcher/api/util/ConfigurationInternationalization.java @@ -2,6 +2,7 @@ import java.util.Locale; + import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,20 +12,21 @@ @Configuration public class ConfigurationInternationalization { - @Bean - public MessageSource messageSource(){ - ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); - messageSource.setBasename("classpath:messages"); - messageSource.setDefaultEncoding("ISO-8859-1"); - messageSource.setDefaultLocale(Locale.getDefault()); - return messageSource; - } + @Bean + public MessageSource messageSource(){ + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setBasename("classpath:messages.properties"); + messageSource.setDefaultEncoding("ISO-8859-1"); + messageSource.setDefaultLocale(Locale.getDefault()); + return messageSource; + } + + @Bean + public LocalValidatorFactoryBean validatorFactoryBean(){ + LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); + bean.setValidationMessageSource(messageSource()); + return bean; + } - @Bean - public LocalValidatorFactoryBean validatorFactoryBean(){ - LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); - bean.setValidationMessageSource(messageSource()); - return bean; - } } diff --git a/src/main/java/org/ayty/hatcher/api/v1/security/JwtAuthFilter.java b/src/main/java/org/ayty/hatcher/api/v1/security/JwtAuthFilter.java index cc8dd30..f834403 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/security/JwtAuthFilter.java +++ b/src/main/java/org/ayty/hatcher/api/v1/security/JwtAuthFilter.java @@ -8,30 +8,27 @@ import javax.servlet.http.HttpServletResponse; import org.ayty.hatcher.api.v1.user.service.UserServiceImpl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; import org.springframework.web.filter.OncePerRequestFilter; +import lombok.RequiredArgsConstructor; +@RequiredArgsConstructor public class JwtAuthFilter extends OncePerRequestFilter { - @Autowired - private JwtService jwtService; - @Autowired - private UserServiceImpl userService; + private final JwtService jwtService; + + + private final UserServiceImpl userService; + - public JwtAuthFilter( JwtService jwtService, UserServiceImpl userService ) { - this.jwtService = jwtService; - this.userService= userService; - } @Override protected void doFilterInternal( @@ -58,30 +55,5 @@ protected void doFilterInternal( } - /* - -@Value("${security.jwt.token.secret-key}") - private String secretKey; - - @Value("${security.jwt.token.expire-length}") - private String expiration; - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - HttpServletRequest req = (HttpServletRequest) request; - - String header = req.getHeader("Authorization"); - - if (header == null || !header.startsWith("Beare ")) { - throw new ServletException("missing or invalid token"); - } - String token = header.substring(7); - try { - Jwts.parser().setSigningKey(this.secretKey).parseClaimsJws(token).getBody(); - } catch (SignatureException e) { - throw new ServletException("Token Inválido"); - } - chain.doFilter(request, response); - } -*/ + } \ No newline at end of file diff --git a/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java b/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java index 4dd9bcf..42f8c69 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java +++ b/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java @@ -1,12 +1,15 @@ package org.ayty.hatcher.api.v1.security; import java.time.Instant; - import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; +import java.util.HashMap; +import java.util.Optional; import org.ayty.hatcher.api.v1.user.dto.LoginDTO; +import org.ayty.hatcher.api.v1.user.entity.User; +import org.ayty.hatcher.api.v1.user.jpa.UserRepository; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -14,7 +17,9 @@ import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import lombok.RequiredArgsConstructor; +@RequiredArgsConstructor @Service public class JwtService { @@ -23,48 +28,53 @@ public class JwtService { @Value("${security.jwt.token.expire-length}") private String expiration; - - - public String generateToken( LoginDTO user ){ - - long expString = Long.valueOf(expiration); - LocalDateTime expireLength = LocalDateTime.now().plusMinutes(expString); - Instant instant = expireLength.atZone(ZoneId.systemDefault()).toInstant(); - Date date = Date.from(instant); - - - //claims admin - - String token = Jwts.builder() - .setSubject(user.getLogin()) - .signWith(SignatureAlgorithm.HS512,secretKey) - .setExpiration(date) - .compact(); - return token; - } - - private Claims getClaims( String token ) throws ExpiredJwtException { - return Jwts - .parser() - .setSigningKey(secretKey) - .parseClaimsJws(token) - .getBody(); - } - - public boolean validToken( String token ){ - try{ - Claims claims = getClaims(token); - Date expirationDate= claims.getExpiration(); - LocalDateTime date = - expirationDate.toInstant() - .atZone(ZoneId.systemDefault()).toLocalDateTime(); - return !LocalDateTime.now().isAfter(date); - }catch (Exception e){ - return false; - } - } - public String getUserLogin(String token) throws ExpiredJwtException{ - return (String) getClaims(token).getSubject(); - } + + private final UserRepository userBD; + + public String generateToken(LoginDTO user) { + + long expString = Long.valueOf(expiration); + LocalDateTime expireLength = LocalDateTime.now().plusMinutes(expString); + Instant instant = expireLength.atZone(ZoneId.systemDefault()).toInstant(); + Date date = Date.from(instant); + + HashMap claim = new HashMap(); + Optional userAdmin = userBD.findByLogin(user.getLogin()); + + if (userAdmin.get().isAdmin() == true) { + claim.put("Roles", "ADMIN"); + claim.put("Login", user.getLogin()); + claim.put("Email", userAdmin.get().getEmail()); + claim.put("Id", userAdmin.get().getId()); + } else { + claim.put("Roles", "USER"); + claim.put("Login", user.getLogin()); + claim.put("Email", userAdmin.get().getEmail()); + claim.put("Id", userAdmin.get().getId()); + } + + String token = Jwts.builder().setSubject(user.getLogin()).setClaims(claim) + .signWith(SignatureAlgorithm.HS512, secretKey).setExpiration(date).compact(); + return token; + } + + private Claims getClaims(String token) throws ExpiredJwtException { + return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody(); + } + + public boolean validToken(String token) { + try { + Claims claims = getClaims(token); + Date expirationDate = claims.getExpiration(); + LocalDateTime date = expirationDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + return !LocalDateTime.now().isAfter(date); + } catch (Exception e) { + return false; + } + } + + public String getUserLogin(String token) throws ExpiredJwtException { + return (String) getClaims(token).getSubject(); + } } diff --git a/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java b/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java index e967aaa..de4c2ed 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java +++ b/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java @@ -25,7 +25,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { private UserServiceImpl userService; @Autowired - private JwtService jwtService; + JwtService jwtService; @Bean public PasswordEncoder passwordEncoder() { @@ -49,6 +49,14 @@ protected void configure( HttpSecurity http ) throws Exception { http .cors().and().csrf().disable() .authorizeRequests() + .antMatchers(HttpMethod.POST,"/hatcher/Authenticate") + .permitAll() + .antMatchers(HttpMethod.GET,"/hatcher/listUsers") + .permitAll() + .antMatchers(HttpMethod.POST,"/hatcher/register") + .permitAll() + .antMatchers(HttpMethod.DELETE,"/hatcher/remove/**") + .permitAll() .antMatchers("/hatcher/profile/registerUser") .hasAnyRole("ADMIN") .antMatchers("/hatcher/profile") @@ -59,8 +67,7 @@ protected void configure( HttpSecurity http ) throws Exception { .hasAnyRole("ADMIN") .antMatchers("/hatcher/profile/user/course/edit") .hasAnyRole("ADMIN") - .antMatchers(HttpMethod.POST, "/api/usuarios/**") - .permitAll() + .anyRequest().authenticated() .and() .sessionManagement() diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/controller/LoginController.java b/src/main/java/org/ayty/hatcher/api/v1/user/controller/LoginController.java deleted file mode 100644 index 773e4c9..0000000 --- a/src/main/java/org/ayty/hatcher/api/v1/user/controller/LoginController.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.ayty.hatcher.api.v1.user.controller; - -import java.util.Optional; - - - -import org.ayty.hatcher.api.v1.security.JwtService; -import org.ayty.hatcher.api.v1.user.dto.LoginDTO; -import org.ayty.hatcher.api.v1.user.entity.LoginResponse; -import org.ayty.hatcher.api.v1.user.entity.User; -import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword; -import org.ayty.hatcher.api.v1.user.exception.LoginNotFound; -import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException; -import org.ayty.hatcher.api.v1.user.jpa.UserRepository; -//import org.ayty.hatcher.api.v1.user.service.RegisterUserImpl; -//import org.ayty.hatcher.api.v1.user.service.UserServiceImpl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -@RestController() -@RequestMapping("/hatcher") -public class LoginController { - - - - - - /* - @Autowired - UserServiceImpl userService; - - @Autowired - RegisterUserImpl registerUser; - - @Autowired - JwtService jwtService; - - @Autowired - UserRepository userBD; - - @Value("${security.jwt.token.secret-key}") - private String expiration; - - @Value("${security.jwt.token.expire-length}") - private String secretKey; - - @RequestMapping(value = "/authenticate", consumes=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) - public LoginResponse LoginAccess(@RequestBody LoginDTO login) { - Optional authenticatedUser = userBD.findByLogin(login.getLogin()); - - if(login.getLogin() == null || login.getPassword()==null) { - throw new LoginNotFound(); - } - if(authenticatedUser == null) { - throw new UsernameNotFoundException(); - } - if(!authenticatedUser.get().getPassword().equals(login.getPassword())) { - throw new IncorrectUserOrPassword(); - } - String token = jwtService.generateToken(login); - - return new LoginResponse(token); - - } -*/ - -} diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java b/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java index ed48810..7575f19 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java @@ -6,118 +6,106 @@ import org.ayty.hatcher.api.v1.security.JwtService; import org.ayty.hatcher.api.v1.user.dto.LoginDTO; +import org.ayty.hatcher.api.v1.user.dto.OutRegisterDTO; import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; import org.ayty.hatcher.api.v1.user.dto.TokenDTO; import org.ayty.hatcher.api.v1.user.dto.UserListDTO; import org.ayty.hatcher.api.v1.user.entity.User; +import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword; import org.ayty.hatcher.api.v1.user.exception.InvalidData; -import org.ayty.hatcher.api.v1.user.exception.InvalidPasswordException; +import org.ayty.hatcher.api.v1.user.exception.LoginNotFound; +import org.ayty.hatcher.api.v1.user.exception.UserAlreadyExists; +import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException; import org.ayty.hatcher.api.v1.user.service.ListUsersImpl; import org.ayty.hatcher.api.v1.user.service.LoginImpl; import org.ayty.hatcher.api.v1.user.service.RegisterUserImpl; import org.ayty.hatcher.api.v1.user.service.RemoveUserImpl; -import org.ayty.hatcher.api.v1.user.service.UserServiceImpl; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor @RestController @RequestMapping("/hatcher") public class UserController { + private final LoginImpl loginImpl; - @Autowired - private LoginImpl loginImpl; - - @Autowired - private RegisterUserImpl registerImpl; + private final RegisterUserImpl registerImpl; - private PasswordEncoder passwordEncoder; + private final PasswordEncoder passwordEncoder; - @Autowired - private JwtService jwtService; + private final JwtService jwtService; - @Autowired - private ListUsersImpl listUserService; + private final ListUsersImpl listUserService; - @Autowired - private RemoveUserImpl removeUserService; + private final RemoveUserImpl removeUserService; @PostMapping("/register") - @ResponseStatus(HttpStatus.CREATED) - public User registerUser(@RequestBody @Valid RegisterUserDTO user) { + public ResponseEntity registerUser(@Valid @RequestBody RegisterUserDTO user) { String EncryptedPassword = passwordEncoder.encode(user.getPassword()); user.setPassword(EncryptedPassword); - - return registerImpl.save(user); + try { + + return new ResponseEntity(registerImpl.save(user),HttpStatus.CREATED); + } + catch(UserAlreadyExists e) { + throw new UserAlreadyExists(); + } + catch(InvalidData e) { + throw new InvalidData(); + } + } @PostMapping("/Authenticate") - public TokenDTO authenticate(@RequestBody LoginDTO credenciais){ + @ResponseStatus(HttpStatus.ACCEPTED) + public TokenDTO authenticate( @Valid @RequestBody LoginDTO credenciais){ try{ - User user = User.builder() - .login(credenciais.getLogin()) - .password(credenciais.getPassword()) + User user = User.builder().login(credenciais.getLogin()).password(credenciais.getPassword()) .build(); UserDetails authenticateUser = loginImpl.authenticate(user); String token = jwtService.generateToken(credenciais); return new TokenDTO(user.getLogin(), token); - } catch (UsernameNotFoundException | InvalidPasswordException e ){ - throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getMessage()); + } catch (IncorrectUserOrPassword e){ + throw new IncorrectUserOrPassword(); + } + catch(UsernameNotFoundException e) { + throw new UsernameNotFoundException(); } + catch(LoginNotFound e) { + throw new LoginNotFound(); + } + } - @RequestMapping(value = "/listUsers", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE) + @RequestMapping(value = "/listUsers", method = RequestMethod.GET) public ResponseEntity> ListUsers() { return new ResponseEntity>(listUserService.listOfRegisteredUsers(), HttpStatus.OK); } - @RequestMapping(value = "/remove", method = RequestMethod.DELETE, consumes = MediaType.APPLICATION_JSON_VALUE) - public void removeUsers(String login, String senha) { + @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE) + public void removeUsers(@PathVariable Long id) { try { - removeUserService.removeUser(login, senha); + removeUserService.removeUser(id); } catch (UsernameNotFoundException e) { throw new InvalidData(); } } - -/* - @Autowired - private RegisterUserImpl registerUserService; - - @Autowired - private ListUsersImpl listUserService; - - - - @RequestMapping(value = "/registerUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity registerUser(@RequestBody RegisterUserDTO userDto, String token) { - User user = registerUserService.registre(token, userDto); - try { - return new ResponseEntity(user, HttpStatus.OK); - }catch(UserAlreadyExists e){ - throw new UserAlreadyExists(); - } - } - - - -*/ } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/dto/OutRegisterDTO.java b/src/main/java/org/ayty/hatcher/api/v1/user/dto/OutRegisterDTO.java new file mode 100644 index 0000000..0f7eae7 --- /dev/null +++ b/src/main/java/org/ayty/hatcher/api/v1/user/dto/OutRegisterDTO.java @@ -0,0 +1,31 @@ +package org.ayty.hatcher.api.v1.user.dto; + +import org.ayty.hatcher.api.v1.user.entity.User; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OutRegisterDTO { + + private String login; + + private String email; + + private String fullName; + + private boolean admin; + + public OutRegisterDTO(User user) { + this.login = user.getLogin(); + this.email = user.getEmail(); + this.fullName = user.getFullName(); + this.admin = user.isAdmin(); + } + +} diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java b/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java index a333136..bfb9e7c 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java @@ -1,13 +1,7 @@ package org.ayty.hatcher.api.v1.user.dto; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; - -import org.ayty.hatcher.api.v1.user.entity.Perfil; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -28,14 +22,11 @@ public class RegisterUserDTO { @Email(message = "{field.email.invalid}") private String email; - @NotBlank(message = "{field.fullname.mandatory}") - @Size(min = 3,max = 50) private String fullName; private String image; - @Enumerated(EnumType.STRING) - private Perfil perfil; + private String perfil; private boolean admin; diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/dto/UserListDTO.java b/src/main/java/org/ayty/hatcher/api/v1/user/dto/UserListDTO.java index cabd1ce..db0cf82 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/dto/UserListDTO.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/dto/UserListDTO.java @@ -1,5 +1,7 @@ package org.ayty.hatcher.api.v1.user.dto; +import org.ayty.hatcher.api.v1.user.entity.User; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -10,11 +12,19 @@ @NoArgsConstructor @AllArgsConstructor public class UserListDTO { + + private String login; - private String nome; + private String fullname; private String email; + public UserListDTO(User user) { + this.login = user.getLogin(); + this.fullname = user.getFullName(); + this.email = user.getEmail(); + } + diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/entity/Perfil.java b/src/main/java/org/ayty/hatcher/api/v1/user/entity/Perfil.java deleted file mode 100644 index 9471624..0000000 --- a/src/main/java/org/ayty/hatcher/api/v1/user/entity/Perfil.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.ayty.hatcher.api.v1.user.entity; - -public enum Perfil { - - ALUNO, - PROFESSOR; - - -} diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java b/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java index d875de7..5eb5604 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java @@ -4,8 +4,6 @@ import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -13,7 +11,7 @@ import javax.persistence.Table; import javax.persistence.UniqueConstraint; import javax.validation.constraints.Email; -import javax.validation.constraints.Size; +import javax.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Builder; @@ -32,29 +30,31 @@ public class User implements Serializable{ private static final long serialVersionUID = 1L; - @GeneratedValue(strategy = GenerationType.AUTO , generator = "User_generator") - @SequenceGenerator(name = "User_generator", sequenceName = "User_sequence", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE , generator = "user_generator") + @SequenceGenerator(name = "user_generator", sequenceName = "user_sequence", allocationSize = 1) @Id private Long id; @Column(nullable = false,unique = true) + @NotBlank(message = "{field.login.mandatory}") private String login; + @Column(nullable = false) + @NotBlank(message = "{field.password.mandatory}") private String password; - @Email + @Email(message = "{field.email.invalid}") private String email; - @Size(min = 3, max = 50) private String fullName; private String image; + private String perfil; private boolean admin; - @Enumerated(EnumType.STRING) - private Perfil perfil; + diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ApiErrors.java b/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ApiErrors.java new file mode 100644 index 0000000..3d5f8cf --- /dev/null +++ b/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ApiErrors.java @@ -0,0 +1,31 @@ +package org.ayty.hatcher.api.v1.user.exception.handling; + +import java.util.Arrays; +import java.util.List; + +public class ApiErrors { + + private List erros; + + public ApiErrors(String msg) { + this.erros = Arrays.asList(msg); + } + public ApiErrors(List msg) { + this.erros = msg; + } + + + public ApiErrors() { + super(); + + } + + + public List getErros() { + return erros; + } + + + + +} \ No newline at end of file diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ExceptionHandle.java b/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ExceptionHandle.java index 23a047c..725aa1e 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ExceptionHandle.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/exception/handling/ExceptionHandle.java @@ -4,60 +4,49 @@ import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword; import org.ayty.hatcher.api.v1.user.exception.InvalidData; -import org.ayty.hatcher.api.v1.user.exception.InvalidPasswordException; -import org.ayty.hatcher.api.v1.user.exception.InvalidToken; import org.ayty.hatcher.api.v1.user.exception.LoginNotFound; import org.ayty.hatcher.api.v1.user.exception.UserAlreadyExists; -import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; -@RestControllerAdvice +@ControllerAdvice public class ExceptionHandle { @ExceptionHandler(IncorrectUserOrPassword.class) public ResponseEntity IncorrectUserOrPasswordHandle(IncorrectUserOrPassword e,HttpServletRequest request){ HttpStatus status = HttpStatus.FORBIDDEN; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "login or password is incorrect :(", request.getRequestURI()); - return ResponseEntity.status(status).body(err); - } - @ExceptionHandler(InvalidPasswordException.class) - public ResponseEntity InvalidPasswordHandle(InvalidPasswordException e,HttpServletRequest request){ - HttpStatus status = HttpStatus.FORBIDDEN; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "password is incorrect :(", request.getRequestURI()); - return ResponseEntity.status(status).body(err); - } - @ExceptionHandler(InvalidToken.class) - public ResponseEntity InvalidTokenHandle(InvalidToken e,HttpServletRequest request){ - HttpStatus status = HttpStatus.FORBIDDEN; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "invalid token :(", request.getRequestURI()); + StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "Incorrec tUser Or Password", request.getRequestURI()); return ResponseEntity.status(status).body(err); } + @ExceptionHandler(LoginNotFound.class) public ResponseEntity LoginNotFoundHandle(LoginNotFound e,HttpServletRequest request){ HttpStatus status = HttpStatus.NOT_FOUND; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "login not found :(", request.getRequestURI()); + StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "Login Not Found", request.getRequestURI()); return ResponseEntity.status(status).body(err); } @ExceptionHandler(UserAlreadyExists.class) public ResponseEntity UserAlreadyExistHandle(UserAlreadyExists e,HttpServletRequest request){ HttpStatus status = HttpStatus.CONFLICT; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Conflit", "User Already Exists :(", request.getRequestURI()); - return ResponseEntity.status(status).body(err); - } - @ExceptionHandler(UsernameNotFoundException.class) - public ResponseEntity UsernameNotFoundHandle(UsernameNotFoundException e,HttpServletRequest request){ - HttpStatus status = HttpStatus.NOT_FOUND; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Not Found", "Username Not Found :(", request.getRequestURI()); + StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Conflict", "User Already Exists", request.getRequestURI()); return ResponseEntity.status(status).body(err); } + @ExceptionHandler(InvalidData.class) public ResponseEntity InvalidDataHandle(InvalidData e,HttpServletRequest request){ HttpStatus status = HttpStatus.BAD_REQUEST; - StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "BAD REQUEST", "Invalid data :(", request.getRequestURI()); + StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Valid Data", "Data is invalid", request.getRequestURI()); return ResponseEntity.status(status).body(err); } - + /* + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ApiErrors handleMethodNotValidException(MethodArgumentNotValidException ex) { + Listerrors = ex.getBindingResult().getAllErrors().stream().map(erro ->erro.getDefaultMessage()).collect(Collectors.toList()); + + return new ApiErrors(errors); + } + */ } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/jpa/UserRepository.java b/src/main/java/org/ayty/hatcher/api/v1/user/jpa/UserRepository.java index d148508..46ca521 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/jpa/UserRepository.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/jpa/UserRepository.java @@ -10,6 +10,7 @@ public interface UserRepository extends JpaRepository{ Optional findByLogin(String login); boolean existsByLogin(String login); + boolean existsByPassword(String password); void deleteByLogin(String login); diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersImpl.java index 4eb15dc..d2a96ef 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersImpl.java @@ -6,23 +6,25 @@ import org.ayty.hatcher.api.v1.user.dto.UserListDTO; import org.ayty.hatcher.api.v1.user.entity.User; import org.ayty.hatcher.api.v1.user.jpa.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; + @Service +@RequiredArgsConstructor public class ListUsersImpl implements ListUsers{ - @Autowired - private UserRepository userBD; + + private final UserRepository userBD; @Override public List listOfRegisteredUsers() { List listUser = userBD.findAll(); - Listdto = listUser.stream().map(x -> new UserListDTO(x.getLogin(), - x.getFullName(), x.getEmail())).collect(Collectors.toList()); + System.out.println(userBD.findAll().size()); + List dto = listUser.stream().map(x -> new UserListDTO(x)).collect(Collectors.toList()); return dto; - } + } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/LoadUserByUsarname.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/LoadUserByUsarname.java index fd912ee..2fa96d9 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/LoadUserByUsarname.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/LoadUserByUsarname.java @@ -1,28 +1,31 @@ package org.ayty.hatcher.api.v1.user.service; -import org.ayty.hatcher.api.v1.security.JwtService; + + import org.ayty.hatcher.api.v1.user.entity.User; +import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException; import org.ayty.hatcher.api.v1.user.jpa.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +@RequiredArgsConstructor @Service public class LoadUserByUsarname implements UserDetailsService{ - @Autowired - JwtService jwtService; + - @Autowired - UserRepository userBD; + + private final UserRepository userBD; @Override public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException { + User user = userBD.findByLogin(login) - .orElseThrow(() -> - new UsernameNotFoundException("User not found in the database")); + .orElseThrow(() -> + new UsernameNotFoundException()); + String[] roles = user.isAdmin() ? new String[] {"ADMIN","USER"} : new String[] {"USER"}; @@ -31,7 +34,6 @@ public UserDetails loadUserByUsername(String login) throws UsernameNotFoundExcep .username(user.getLogin()) .password(user.getPassword()) .roles(roles) - .build(); } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/Login.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/Login.java index 4517b4f..d5c4697 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/Login.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/Login.java @@ -1,7 +1,5 @@ package org.ayty.hatcher.api.v1.user.service; -import org.ayty.hatcher.api.v1.user.dto.LoginDTO; -import org.ayty.hatcher.api.v1.user.entity.LoginResponse; import org.ayty.hatcher.api.v1.user.entity.User; import org.springframework.security.core.userdetails.UserDetails; diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/LoginImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/LoginImpl.java index 03ad421..19d1ad7 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/LoginImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/LoginImpl.java @@ -2,30 +2,39 @@ import org.ayty.hatcher.api.v1.user.entity.User; -import org.ayty.hatcher.api.v1.user.exception.InvalidPasswordException; -import org.springframework.beans.factory.annotation.Autowired; +import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword; +import org.ayty.hatcher.api.v1.user.exception.LoginNotFound; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor @Service public class LoginImpl implements Login { - @Autowired - private PasswordEncoder encoder; - @Autowired - private LoadUserByUsarname load; + private final PasswordEncoder encoder; + + + private final LoadUserByUsarname load; + + public UserDetails authenticate( User user ){ UserDetails userDetails = load.loadUserByUsername(user.getLogin()); - boolean PasswordsMatch= encoder.matches( user.getPassword(), userDetails.getPassword() ); + + if(userDetails.getUsername()== null ){ + throw new LoginNotFound(); + } + + boolean PasswordsMatch = encoder.matches(user.getPassword(),userDetails.getPassword() ); if(PasswordsMatch){ return userDetails; } - - throw new InvalidPasswordException(); + throw new IncorrectUserOrPassword(); } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUser.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUser.java index c9c0e50..a0dca9f 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUser.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUser.java @@ -1,15 +1,13 @@ package org.ayty.hatcher.api.v1.user.service; -import org.ayty.hatcher.api.v1.user.entity.User; - -import org.ayty.hatcher.api.v1.user.dto.*; +import org.ayty.hatcher.api.v1.user.dto.OutRegisterDTO; +import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; @FunctionalInterface public interface RegisterUser { - //User registre (String token,RegisterUserDTO user); - - User save(RegisterUserDTO user); + + OutRegisterDTO save(RegisterUserDTO user); diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImpl.java index ee3a861..1aca500 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImpl.java @@ -2,88 +2,65 @@ import javax.transaction.Transactional; - +import org.ayty.hatcher.api.v1.user.dto.OutRegisterDTO; import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; import org.ayty.hatcher.api.v1.user.entity.User; +import org.ayty.hatcher.api.v1.user.exception.InvalidData; +import org.ayty.hatcher.api.v1.user.exception.UserAlreadyExists; import org.ayty.hatcher.api.v1.user.jpa.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; + + +@RequiredArgsConstructor @Service public class RegisterUserImpl implements RegisterUser { - @Autowired - private UserRepository userBD; + private final UserRepository userBD; + + @Transactional - public User save(RegisterUserDTO user) { + public OutRegisterDTO save(RegisterUserDTO user) { boolean admin = false; if(user.isAdmin() ==true) { admin = true; } - return userBD.save(User.builder() + + + if(userBD.existsByLogin(user.getLogin())==true){ + throw new UserAlreadyExists(); + + } + if(user.getLogin().matches("[a-zA-Z.]*")) { + + userBD.save(User.builder() + .id(null) .login(user.getLogin()) .password(user.getPassword()) .email(user.getEmail()) .fullName(user.getFullName()) .image(user.getImage()) - .admin(admin) .perfil(user.getPerfil()) + .admin(admin) .build()); - } - } - - - - - - - - /* - - @Override - @Transactional - public User registre(String token, RegisterUserDTO user) { - if(jwtService.validToken(token)==false) { - throw new InvalidToken(); + return new OutRegisterDTO(User.builder() + .login(user.getLogin()) + .email(user.getEmail()) + .fullName(user.getFullName()) + .admin(user.isAdmin()) + .build()); } - if(userBD.existsByLogin(user.getLogin())==true) { - throw new UserAlreadyExists(); + else { + throw new InvalidData(); } + } +} + - String userToken = jwtService.getUserLogin(token); - Optional userOpt = userBD.findByLogin(userToken); - User userImpl = new User(); - if(userOpt.isPresent()&&userOpt.get().isAdmin()) { - - userImpl.setLogin(user.getLogin()); - userImpl.setPassword(user.getPassword()); - userImpl.setEmail(user.getEmail()); - userImpl.setFullName(user.getFullName()); - userImpl.setImage(user.getImage()); - userImpl.setPerfil(user.getPerfil()); - - if(!user.isAdmin()) { - userImpl.setAdmin(false); - } - else { - userImpl.setAdmin(true); - - } - - - userBD.save(userImpl); - } - return null; - - - - } -} -*/ diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUser.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUser.java index f473942..75385cd 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUser.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUser.java @@ -3,6 +3,6 @@ @FunctionalInterface public interface RemoveUser { - void removeUser(String login, String password); + void removeUser(Long id); } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserImpl.java index 6080057..67831a4 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserImpl.java @@ -1,30 +1,28 @@ package org.ayty.hatcher.api.v1.user.service; import java.util.Optional; - import org.ayty.hatcher.api.v1.user.entity.User; -import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException; +import org.ayty.hatcher.api.v1.user.exception.InvalidData; import org.ayty.hatcher.api.v1.user.jpa.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor @Service -public class RemoveUserImpl implements RemoveUser{ - - @Autowired - private UserRepository userBD; +public class RemoveUserImpl implements RemoveUser { + + private final UserRepository userBD; @Override - public void removeUser(String login,String password) { - Optional userOpt = userBD.findByLogin(login); - if(!userOpt.isPresent()) { - throw new UsernameNotFoundException(); - } - if(userOpt.get().getLogin().equals(login)&&userOpt.get().getPassword().equals(password)) { - userBD.deleteByLogin(login); + public void removeUser(Long id) { + Optional user = userBD.findById(id); + if (user.isPresent()) { + userBD.delete(user.get()); + } else { + throw new InvalidData(); } - } } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/UserServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/UserServiceImpl.java index 213e426..8fc1cef 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/UserServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/UserServiceImpl.java @@ -7,6 +7,8 @@ import javax.transaction.Transactional; + + import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; import org.ayty.hatcher.api.v1.user.entity.User; import org.ayty.hatcher.api.v1.user.exception.InvalidPasswordException; @@ -26,7 +28,7 @@ public class UserServiceImpl implements UserDetailsService { private PasswordEncoder encoder; @Autowired - private UserRepository userBD; + UserRepository userBD; @Transactional @@ -42,7 +44,6 @@ public User save(RegisterUserDTO user) { .fullName(user.getFullName()) .image(user.getImage()) .admin(admin) - .perfil(user.getPerfil()) .build()); } public UserDetails authenticate( User user ){ diff --git a/src/main/resources/db/migration/data/V20210204094501__insert_table_user_admin.sql b/src/main/resources/db/migration/data/V20210204094501__insert_table_user_admin.sql deleted file mode 100644 index a0ec7b1..0000000 --- a/src/main/resources/db/migration/data/V20210204094501__insert_table_user_admin.sql +++ /dev/null @@ -1 +0,0 @@ -insert into tb_user values(1,'admin','admin','admin@gmail.com','admin','admin',true,'admin'); \ No newline at end of file diff --git a/src/main/resources/db/migration/data/V20210208120105__insert_table_user_admin.sql b/src/main/resources/db/migration/data/V20210208120105__insert_table_user_admin.sql new file mode 100644 index 0000000..8a06da9 --- /dev/null +++ b/src/main/resources/db/migration/data/V20210208120105__insert_table_user_admin.sql @@ -0,0 +1,2 @@ +insert into tb_user +values(DEFAULT,'admin','$2y$04$Hexn6JOpJn8ohHTdX0zJdODijX1ks6JmjUqlgJYJiAuV9KatL3aqS','admin@gmail.com','admin','admin',true,'admin'); \ No newline at end of file diff --git a/src/main/resources/db/migration/schema/V20210203175529__create_table_user.sql b/src/main/resources/db/migration/schema/V20210203175529__create_table_user.sql index 629b569..a7bac51 100644 --- a/src/main/resources/db/migration/schema/V20210203175529__create_table_user.sql +++ b/src/main/resources/db/migration/schema/V20210203175529__create_table_user.sql @@ -1,13 +1,15 @@ +CREATE SEQUENCE user_sequence INCREMENT BY 1; + Create table tb_user( - id bigint, - login varchar (50) not null, - password varchar(50) not null, - email varchar (50) not null, - full_name varchar(50) not null, - image varchar (50), + id bigint DEFAULT nextval('user_sequence'), + login varchar (255) not null, + password varchar(255) not null, + email varchar (255) not null, + full_name varchar(255) not null, + image varchar (255), admin BOOLEAN, perfil varchar(255) ); - CREATE SEQUENCE User_sequence INCREMENT BY 1; \ No newline at end of file + \ No newline at end of file diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 31ac5ae..5845c65 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -2,4 +2,4 @@ field.login.mandatory = "login field is mandatory " field.password.mandatory= "password field is mandatory " field.fullname.mandatory= "fullname field is mandatory " -field.email.invalid = "Campo email invalido" \ No newline at end of file +field.email.invalid = "field email invalid" \ No newline at end of file diff --git a/src/test/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImplTest.java b/src/test/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImplTest.java new file mode 100644 index 0000000..f16a554 --- /dev/null +++ b/src/test/java/org/ayty/hatcher/api/v1/user/service/RegisterUserImplTest.java @@ -0,0 +1,49 @@ +package org.ayty.hatcher.api.v1.user.service; + +import static org.mockito.ArgumentMatchers.any; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; +import org.ayty.hatcher.api.v1.user.entity.User; +import org.ayty.hatcher.api.v1.user.jpa.UserRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class RegisterUserImplTest { + + + UserRepository userBD = Mockito.mock(UserRepository.class); + + RegisterUserImpl register = new RegisterUserImpl(userBD); + + + + @Test + final void test() { + + + + //RegisterUserDTO user = new RegisterUserDTO("ruancruz", "ruansenha", "ruan@gmail.com", "ruan cruz soares", "imagee", Perfil.ALUNO, false); + + // User users = new User(1L,"ruancruz", "ruansenha", "ruan@gmail.com", "ruan cruz soares", "imagee", false, Perfil.ALUNO); + + + // when(userBD.save(any(User.class))).thenReturn(users); + + //User test = register.save(user); + + + //Assertions.assertEquals("ruancruz",test.getLogin()); + + //verify(userBD,times(1)).save(any(User.class)); + + + + + } + +}