From 4c2c9931dae39ea3fea94ce26a30f34bc9e321f6 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 28 Feb 2024 19:14:24 +0100 Subject: [PATCH] fix: register & refresh --- .../auth/dtos/RefreshTokenResponseDto.java | 6 ++-- .../en2b/quizapi/auth/AuthServiceTest.java | 34 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/lab/en2b/quizapi/auth/dtos/RefreshTokenResponseDto.java b/api/src/main/java/lab/en2b/quizapi/auth/dtos/RefreshTokenResponseDto.java index e87000b7..1035ad5d 100644 --- a/api/src/main/java/lab/en2b/quizapi/auth/dtos/RefreshTokenResponseDto.java +++ b/api/src/main/java/lab/en2b/quizapi/auth/dtos/RefreshTokenResponseDto.java @@ -1,15 +1,13 @@ package lab.en2b.quizapi.auth.dtos; import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; @Getter @Setter @AllArgsConstructor @NoArgsConstructor +@EqualsAndHashCode public class RefreshTokenResponseDto { private String token; diff --git a/api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java b/api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java index 0b9f6f58..37af25a6 100644 --- a/api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java @@ -1,8 +1,8 @@ package lab.en2b.quizapi.auth; +import ch.qos.logback.core.util.TimeUtil; import lab.en2b.quizapi.auth.config.UserDetailsImpl; -import lab.en2b.quizapi.auth.dtos.JwtResponseDto; -import lab.en2b.quizapi.auth.dtos.LoginDto; +import lab.en2b.quizapi.auth.dtos.*; import lab.en2b.quizapi.auth.jwt.JwtUtils; import lab.en2b.quizapi.commons.user.User; import lab.en2b.quizapi.commons.user.UserRepository; @@ -20,6 +20,8 @@ import org.springframework.security.core.Authentication; import org.springframework.test.context.junit.jupiter.SpringExtension; +import javax.swing.text.html.Option; +import java.time.Instant; import java.util.List; import java.util.Optional; import java.util.Set; @@ -33,6 +35,7 @@ public class AuthServiceTest { @InjectMocks AuthService authService; + @Mock UserService userService; @Mock UserRepository userRepository; @@ -53,6 +56,8 @@ void setUp() { .username("test") .roles(Set.of(new Role("user"))) .password("password") + .refreshToken("token") + .refreshExpiration(Instant.ofEpochSecond(TimeUtil.computeStartOfNextSecond(System.currentTimeMillis()+ 1000))) .build(); } @Test @@ -78,4 +83,29 @@ void testLogin(){ ,actual); } + @Test + void testRegister(){ + + when(userRepository.existsByEmail(any())).thenReturn(false); + when(userRepository.existsByUsername(any())).thenReturn(false); + when(userRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); + when(roleRepository.findByName(any())).thenReturn(Optional.of(new Role("user"))); + + ResponseEntity actual = authService.register(new RegisterDto("test","username","password")); + + assertEquals(ResponseEntity.of(Optional.of("User registered successfully!")),actual); + + } + + @Test + void testRefreshToken(){ + + when(userRepository.findByRefreshToken(any())).thenReturn(Optional.of(defaultUser)); + when(jwtUtils.generateTokenFromEmail(any())).thenReturn("jwtToken"); + + ResponseEntity actual = authService.refreshToken(new RefreshTokenDto("token")); + + assertEquals(ResponseEntity.of(Optional.of(new RefreshTokenResponseDto("jwtToken","token"))),actual); + + } }