-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJwtServiceTest.java
88 lines (70 loc) · 3.16 KB
/
JwtServiceTest.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tri.le.purchasedata.service;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import tri.le.purchasedata.dto.TokenInfo;
import tri.le.purchasedata.error.NSTException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ActiveProfiles("test")
@SpringBootTest
public class JwtServiceTest {
@Autowired
JwtService jwtService;
// JWT token in this test generated by https://jwt.io/
// Secret = SHA256("this-is-secret-string-for-test")
@Test
@DisplayName("Test getTokenInfo: Expired")
void testGetTokenInfo_tokenExpired() throws NSTException {
//{
// "sub": "trile",
// "userId": 1,
// "exp": 1609495590 => Friday, 1 January 2021 00:00:00 GMT
//}
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0cmlsZSIsInVzZXJJZCI6MSwiZXhwIjoxNjA5NDk1NTkwfQ.eusZLhqBDmey3HGeuPalYrw2FNg0U9gTWqZ4KMi0e1HBW_ptXTequQ0vhpeukwchEOkcmcs2OWF8n5tBeGEV_w";
Exception exception = assertThrows(NSTException.class, () -> jwtService.getTokenInfo(token), "ExceptionType");
assertEquals("Token expired", exception.getMessage(), "ExceptionMessage");
}
@Test
@DisplayName("Test getTokenInfo: Signature invalid")
void testGetTokenInfo_tokenSignatureInvalid() throws NSTException {
//{
// "sub": "trile",
// "userId": 1,
// "exp": 7952256000 => Monday, 31 December 2221 00:00:00 GMT
//}
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0cmlsZSIsInVzZXJJZCI6MSwiZXhwIjo3OTUyMjU2MDAwfQ.123";
Exception exception = assertThrows(NSTException.class, () -> jwtService.getTokenInfo(token), "ExceptionType");
assertEquals("Signature invalid", exception.getMessage(), "ExceptionMessage");
}
@Test
@DisplayName("Test getTokenInfo: Token valid")
void testGetTokenInfo_tokenValid() throws NSTException {
//{
// "sub": "trile",
// "userId": 1,
// "exp": 7952256000 => Monday, 31 December 2221 00:00:00 GMT
//}
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0cmlsZSIsInVzZXJJZCI6MSwiZXhwIjo3OTUyMjU2MDAwfQ.NNs7qteaKhfVLsqEcPzxN8Sc-4Poquojrj6eNDPculL_zwIeQchXr8jJ--15EPG51WA4RoRNfnXeVgB0hgbP2w";
TokenInfo tokenInfo = jwtService.getTokenInfo(token);
assertEquals("trile", tokenInfo.getUserName(), "Token.sub");
assertEquals(1L, tokenInfo.getUserId(), "Token.userId");
}
@Test
@DisplayName("Test createToken: Correct token")
void testCreateToken_tokenValid() throws NSTException {
//{
// "sub": "trile",
// "userId": 1,
// "exp": 7952256 => Monday, 31 December 2221 00:00:00 GMT
//}
String expectToken = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0cmlsZSIsInVzZXJJZCI6MSwiZXhwIjo3OTUyMjU2fQ.njM_FQi8vWN6UE99ODJ3KpdJjO_XUKNjpYUmICJeyxOWhEAlSeVX2dQcUFe4YADpQzQdMlunO6A3Eh67Ry1AQA";
String sub = "trile";
Long userId = 1L;
long currentTimeMillis = 7952256000L - 100 * 1000;
String actualToken = jwtService.createToken(sub, userId, currentTimeMillis);
assertEquals(expectToken, actualToken, "Token");
}
}