Skip to content

Commit

Permalink
#140 JwtTokenProvider 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
1o18z committed Sep 23, 2023
1 parent 2797cda commit ed8cd37
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.backlog.auth.domain.oauth;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import org.assertj.core.util.DateUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Base64;
import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class JwtTokenProviderTest {

private static final String SECRET_KEY = Base64.getEncoder().encodeToString(Keys.secretKeyFor(SignatureAlgorithm.HS512).getEncoded());

@InjectMocks
private JwtTokenProvider provider = new JwtTokenProvider(SECRET_KEY);

@Test
void generate() {
String subject = "123L";
Date date = new Date(System.currentTimeMillis() + 3600000);

String result = provider.generate(subject, date);

assertThat(result).isNotNull();
}

@Test
void extractUserId() {
String subject = "123";
Date date = new Date(System.currentTimeMillis() + 3600000);

String token = provider.generate(subject, date);
Long result = provider.extractUserId(token);

assertThat(result).isEqualTo(Long.parseLong(subject));
}

@Test
void isExpiredRefreshToken() {
String subject = "123L";
Date date = new Date(System.currentTimeMillis() + 3600000);

String token = provider.generate(subject, date);

assertThat(provider.isExpiredRefreshToken(token)).isFalse();
}

@Test
void isExpiredRefreshTokenFail() {
String subject = "123L";
Date date = DateUtil.yesterday();

String token = provider.generate(subject, date);

assertThat(provider.isExpiredRefreshToken(token)).isTrue();
}

}

0 comments on commit ed8cd37

Please sign in to comment.