|
31 | 31 | */
|
32 | 32 | class JwtAudienceValidatorTests {
|
33 | 33 |
|
34 |
| - private final JwtAudienceValidator validator = new JwtAudienceValidator("audience"); |
| 34 | + private final JwtAudienceValidator validatorDefault = new JwtAudienceValidator("audience"); |
| 35 | + |
| 36 | + private final JwtAudienceValidator validatorRequiredTrue = new JwtAudienceValidator("audience", true); |
| 37 | + |
| 38 | + private final JwtAudienceValidator validatorRequiredFalse = new JwtAudienceValidator("audience", false); |
| 39 | + |
| 40 | + @Test |
| 41 | + void givenRequiredDefaultJwtWithMatchingAudienceThenShouldValidate() { |
| 42 | + Jwt jwt = TestJwts.jwt().audience(List.of("audience")).build(); |
| 43 | + OAuth2TokenValidatorResult result = this.validatorDefault.validate(jwt); |
| 44 | + assertThat(result).isEqualTo(OAuth2TokenValidatorResult.success()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void givenRequiredJwtWithMatchingAudienceThenShouldValidate() { |
| 49 | + Jwt jwt = TestJwts.jwt().audience(List.of("audience")).build(); |
| 50 | + OAuth2TokenValidatorResult result = this.validatorRequiredTrue.validate(jwt); |
| 51 | + assertThat(result).isEqualTo(OAuth2TokenValidatorResult.success()); |
| 52 | + } |
35 | 53 |
|
36 | 54 | @Test
|
37 |
| - void givenJwtWithMatchingAudienceThenShouldValidate() { |
| 55 | + void givenNotRequiredJwtWithMatchingAudienceThenShouldValidate() { |
38 | 56 | Jwt jwt = TestJwts.jwt().audience(List.of("audience")).build();
|
39 |
| - OAuth2TokenValidatorResult result = this.validator.validate(jwt); |
| 57 | + OAuth2TokenValidatorResult result = this.validatorRequiredFalse.validate(jwt); |
40 | 58 | assertThat(result).isEqualTo(OAuth2TokenValidatorResult.success());
|
41 | 59 | }
|
42 | 60 |
|
43 | 61 | @Test
|
44 |
| - void givenJwtWithoutMatchingAudienceThenShouldValidate() { |
| 62 | + void givenRequiredDefaultJwtWithoutMatchingAudienceThenShouldValidate() { |
45 | 63 | Jwt jwt = TestJwts.jwt().audience(List.of("other")).build();
|
46 |
| - OAuth2TokenValidatorResult result = this.validator.validate(jwt); |
| 64 | + OAuth2TokenValidatorResult result = this.validatorDefault.validate(jwt); |
47 | 65 | assertThat(result.hasErrors()).isTrue();
|
48 | 66 | }
|
49 | 67 |
|
50 | 68 | @Test
|
51 |
| - void givenJwtWithoutAudienceThenShouldValidate() { |
| 69 | + void givenRequiredJwtWithoutMatchingAudienceThenShouldValidate() { |
| 70 | + Jwt jwt = TestJwts.jwt().audience(List.of("other")).build(); |
| 71 | + OAuth2TokenValidatorResult result = this.validatorRequiredTrue.validate(jwt); |
| 72 | + assertThat(result.hasErrors()).isTrue(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void givenNotRequiredJwtWithoutMatchingAudienceThenShouldValidate() { |
| 77 | + Jwt jwt = TestJwts.jwt().audience(List.of("other")).build(); |
| 78 | + OAuth2TokenValidatorResult result = this.validatorRequiredFalse.validate(jwt); |
| 79 | + assertThat(result.hasErrors()).isTrue(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void givenRequiredDefaultJwtWithoutAudienceThenShouldValidate() { |
52 | 84 | Jwt jwt = TestJwts.jwt().audience(null).build();
|
53 |
| - OAuth2TokenValidatorResult result = this.validator.validate(jwt); |
| 85 | + OAuth2TokenValidatorResult result = this.validatorDefault.validate(jwt); |
54 | 86 | assertThat(result.hasErrors()).isTrue();
|
55 | 87 | }
|
56 | 88 |
|
| 89 | + @Test |
| 90 | + void givenRequiredJwtWithoutAudienceThenShouldValidate() { |
| 91 | + Jwt jwt = TestJwts.jwt().audience(null).build(); |
| 92 | + OAuth2TokenValidatorResult result = this.validatorRequiredTrue.validate(jwt); |
| 93 | + assertThat(result.hasErrors()).isTrue(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void givenNotRequiredJwtWithoutAudienceThenShouldValidate() { |
| 98 | + Jwt jwt = TestJwts.jwt().audience(null).build(); |
| 99 | + OAuth2TokenValidatorResult result = this.validatorRequiredFalse.validate(jwt); |
| 100 | + assertThat(result.hasErrors()).isFalse(); |
| 101 | + } |
| 102 | + |
57 | 103 | }
|
0 commit comments