-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenServiceTest.java
More file actions
66 lines (52 loc) · 1.82 KB
/
Copy pathTokenServiceTest.java
File metadata and controls
66 lines (52 loc) · 1.82 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 org.example;
import io.vavr.control.Try;
import lombok.extern.java.Log;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import static org.assertj.core.api.BDDAssertions.*;
@Log
@SpringBootTest
class TestTokenService {
@Autowired
TokenService tokenService;
@Test
void a_good_token_can_be_encrypted_and_decrypted() {
// given
final String userId = "1234";
final String role = "USER";
final Instant expiresDate = Instant.now().plus(5, ChronoUnit.MINUTES);
AppToken appToken = AppToken.builder()
.userId(userId)
.role(role)
.expiresAt(expiresDate)
.build();
// when
Try<String> encrypted = tokenService.encrypt(appToken);
then(encrypted.isSuccess()).isTrue();
// when
String token = encrypted.get();
then(token).isNotNull();
log.info(token);
// when
Try<AppToken> decrypted = tokenService.decrypt(token);
then(decrypted.isSuccess()).isTrue();
// when
AppToken decodedAppToken = decrypted.get();
then(decodedAppToken).isNotNull();
then(userId).isEqualTo(decodedAppToken.userId());
then(role).isEqualTo(decodedAppToken.role());
then(expiresDate).isEqualTo(decodedAppToken.expiresAt());
}
@Test
void a_bad_token_cannot_be_decrypted() {
// given
String fakeToken = "v3.local.incorrect-stuff";
// when
Try<AppToken> decrypted = tokenService.decrypt(fakeToken);
then(decrypted).isEmpty();
then(decrypted.getCause().getMessage()).startsWith("Token should start with v4.local.");
}
}