Skip to content

Commit

Permalink
fix: register & refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Toto-hitori committed Feb 28, 2024
1 parent 8005bf6 commit 4c2c993
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
34 changes: 32 additions & 2 deletions api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -33,6 +35,7 @@
public class AuthServiceTest {
@InjectMocks
AuthService authService;
@Mock
UserService userService;
@Mock
UserRepository userRepository;
Expand All @@ -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
Expand All @@ -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);

}
}

0 comments on commit 4c2c993

Please sign in to comment.