|
| 1 | +package devkor.ontime_back.service; |
| 2 | + |
| 3 | +import devkor.ontime_back.entity.Role; |
| 4 | +import devkor.ontime_back.entity.User; |
| 5 | +import devkor.ontime_back.global.jwt.JwtTokenProvider; |
| 6 | +import devkor.ontime_back.repository.UserRepository; |
| 7 | +import devkor.ontime_back.response.InvalidAccessTokenException; |
| 8 | +import devkor.ontime_back.response.InvalidRefreshTokenException; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 13 | +import org.springframework.transaction.annotation.Transactional; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 16 | + |
| 17 | +@SpringBootTest |
| 18 | +@Transactional |
| 19 | +class AuthTokenServiceIntegrationTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private AuthTokenService authTokenService; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private UserRepository userRepository; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private JwtTokenProvider jwtTokenProvider; |
| 29 | + |
| 30 | + @Test |
| 31 | + void loggingInAgainMakesPreviousRefreshCredentialUnusable() { |
| 32 | + User user = userRepository.saveAndFlush(user()); |
| 33 | + MockHttpServletResponse firstLoginResponse = new MockHttpServletResponse(); |
| 34 | + MockHttpServletResponse secondLoginResponse = new MockHttpServletResponse(); |
| 35 | + |
| 36 | + AuthTokenService.AuthTokens firstLoginTokens = authTokenService.issueLoginTokens(user, firstLoginResponse); |
| 37 | + AuthTokenService.AuthTokens secondLoginTokens = authTokenService.issueLoginTokens(user, secondLoginResponse); |
| 38 | + |
| 39 | + assertThatCode(() -> authTokenService.rotateRefreshToken(firstLoginTokens.refreshToken(), new MockHttpServletResponse())) |
| 40 | + .isInstanceOf(InvalidRefreshTokenException.class); |
| 41 | + assertThatCode(() -> authTokenService.rotateRefreshToken(secondLoginTokens.refreshToken(), new MockHttpServletResponse())) |
| 42 | + .doesNotThrowAnyException(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void loggingInAgainMakesPreviousAccessCredentialUnusable() { |
| 47 | + User user = userRepository.saveAndFlush(user()); |
| 48 | + AuthTokenService.AuthTokens firstLoginTokens = |
| 49 | + authTokenService.issueLoginTokens(user, new MockHttpServletResponse()); |
| 50 | + |
| 51 | + authTokenService.issueLoginTokens(user, new MockHttpServletResponse()); |
| 52 | + |
| 53 | + assertThatCode(() -> jwtTokenProvider.isAccessTokenValid(firstLoginTokens.accessToken())) |
| 54 | + .isInstanceOf(InvalidAccessTokenException.class); |
| 55 | + } |
| 56 | + |
| 57 | + private User user() { |
| 58 | + return User.builder() |
| 59 | + .email("single-session@example.com") |
| 60 | + .password("password") |
| 61 | + .name("single-session-user") |
| 62 | + .role(Role.USER) |
| 63 | + .build(); |
| 64 | + } |
| 65 | +} |
0 commit comments