17
17
import org .eclipse .edc .jwt .validation .jti .JtiValidationEntry ;
18
18
import org .eclipse .edc .jwt .validation .jti .JtiValidationStore ;
19
19
import org .eclipse .edc .spi .iam .ClaimToken ;
20
+ import org .eclipse .edc .spi .result .StoreResult ;
21
+ import org .junit .jupiter .api .BeforeEach ;
20
22
import org .junit .jupiter .api .Test ;
21
23
22
24
import java .time .Instant ;
23
25
import java .util .Map ;
24
26
25
27
import static org .eclipse .edc .junit .assertions .AbstractResultAssert .assertThat ;
28
+ import static org .mockito .ArgumentMatchers .any ;
26
29
import static org .mockito .ArgumentMatchers .eq ;
27
30
import static org .mockito .Mockito .mock ;
31
+ import static org .mockito .Mockito .never ;
32
+ import static org .mockito .Mockito .verify ;
28
33
import static org .mockito .Mockito .when ;
29
34
30
35
class JtiValidationRuleTest {
31
36
32
37
private final JtiValidationStore store = mock ();
33
38
private final JtiValidationRule rule = new JtiValidationRule (store , mock ());
34
39
40
+ @ BeforeEach
41
+ void setUp () {
42
+ when (store .storeEntry (any ())).thenReturn (StoreResult .success ());
43
+ }
44
+
35
45
@ Test
36
46
void checkRule_noExpiration_success () {
37
47
when (store .findById (eq ("test-id" ))).thenReturn (new JtiValidationEntry ("test-id" ));
38
- assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isSucceeded ();
48
+ assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isFailed ()
49
+ .detail ().isEqualTo ("The JWT id 'test-id' was already used." );
50
+ verify (store ).storeEntry (any ());
39
51
}
40
52
41
53
@ Test
42
54
void checkRule_withExpiration_success () {
43
55
when (store .findById (eq ("test-id" ))).thenReturn (new JtiValidationEntry ("test-id" , Instant .now ().plusSeconds (3600 ).toEpochMilli ()));
44
- assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isSucceeded ();
56
+ assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isFailed ()
57
+ .detail ().isEqualTo ("The JWT id 'test-id' was already used." );
58
+ verify (store ).storeEntry (any ());
45
59
}
46
60
47
61
@ Test
48
62
void checkRule_withExpiration_alreadyExpired () {
49
63
when (store .findById (eq ("test-id" ))).thenReturn (new JtiValidationEntry ("test-id" , Instant .now ().minusSeconds (3600 ).toEpochMilli ()));
50
64
assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isSucceeded ();
65
+ verify (store ).storeEntry (any ());
51
66
}
52
67
53
68
@ Test
54
69
void checkRule_entryNotFound_success () {
55
70
when (store .findById (eq ("test-id" ))).thenReturn (null );
71
+ assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isSucceeded ();
72
+ verify (store ).storeEntry (any ());
73
+ }
74
+
75
+ @ Test
76
+ void checkRule_entryNotFound_storeFails_failure () {
77
+ when (store .findById (eq ("test-id" ))).thenReturn (null );
78
+ when (store .storeEntry (any ())).thenReturn (StoreResult .duplicateKeys ("foobar" ));
56
79
assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().claim ("jti" , "test-id" ).build (), Map .of ())).isFailed ()
57
- .detail ().isEqualTo ("The JWT id 'test-id' was not found" );
80
+ .detail ().isEqualTo ("foobar" );
81
+ }
82
+
83
+ @ Test
84
+ void checkRule_whenClaimTokenNoKid () {
85
+ assertThat (rule .checkRule (ClaimToken .Builder .newInstance ().build (), Map .of ())).isSucceeded ();
86
+ verify (store , never ()).storeEntry (any ());
58
87
}
59
88
}
0 commit comments