-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtUtilTest.java
More file actions
66 lines (52 loc) · 1.89 KB
/
JwtUtilTest.java
File metadata and controls
66 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.mycodingtest.common.util;
import io.jsonwebtoken.Claims;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Tag("unit")
class JwtUtilTest {
private JwtUtil jwtUtil;
@BeforeEach
void setUp() {
jwtUtil = new JwtUtil();
String plainSecretKey = Base64.getEncoder().encodeToString("my-super-secret-key-for-jwt-testing".getBytes(StandardCharsets.UTF_8));
ReflectionTestUtils.setField(jwtUtil, "plainSecretKey", plainSecretKey);
ReflectionTestUtils.setField(jwtUtil, "expiration", 3600000L);
jwtUtil.init();
}
@Test
@DisplayName("Jwt 생성 한다")
void testGenerateToken() {
// given
Long userId = 1L;
String picture = "profile.jpg";
String name = "testUser";
// when
String token = jwtUtil.generateToken(userId, picture, name);
// then
assertNotNull(token);
}
@Test
@DisplayName("생성된 유효한 Jwt에서 클레임을 추출한다")
void testExtractAllClaims() {
// given
Long userId = 2L;
String picture = "picture";
String name = "user";
String token = jwtUtil.generateToken(userId, picture, name);
// when
Claims claims = jwtUtil.extractAllClaims(token);
// then
assertNotNull(claims);
assertEquals("api", claims.getSubject());
assertEquals(userId, claims.get("userId", Long.class));
assertEquals(name, claims.get("name", String.class));
assertEquals(picture, claims.get("picture", String.class));
}
}