-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/test/java/dev/backlog/auth/domain/oauth/JwtTokenProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |