forked from gothinkster/spring-boot-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultJwtServiceTest.java
More file actions
42 lines (35 loc) · 1.36 KB
/
Copy pathDefaultJwtServiceTest.java
File metadata and controls
42 lines (35 loc) · 1.36 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
package io.spring.infrastructure.service;
import io.spring.core.service.JwtService;
import io.spring.core.user.User;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DefaultJwtServiceTest {
private JwtService jwtService;
@BeforeEach
public void setUp() {
jwtService =
new DefaultJwtService("123123123123123123123123123123123123123123123123123123123123", 3600);
}
@Test
public void should_generate_and_parse_token() {
User user = new User("email@email.com", "username", "123", "", "");
String token = jwtService.toToken(user);
Assertions.assertNotNull(token);
Optional<String> optional = jwtService.getSubFromToken(token);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(optional.get(), user.getId());
}
@Test
public void should_get_null_with_wrong_jwt() {
Optional<String> optional = jwtService.getSubFromToken("123");
Assertions.assertFalse(optional.isPresent());
}
@Test
public void should_get_null_with_expired_jwt() {
String token =
"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaXNlbnNpeSIsImV4cCI6MTUwMjE2MTIwNH0.SJB-U60WzxLYNomqLo4G3v3LzFxJKuVrIud8D8Lz3-mgpo9pN1i7C8ikU_jQPJGm8HsC1CquGMI-rSuM7j6LDA";
Assertions.assertFalse(jwtService.getSubFromToken(token).isPresent());
}
}