|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
35 | 35 | * Tests for {@link ReactiveJwtAuthenticationConverter}
|
36 | 36 | *
|
37 | 37 | * @author Eric Deandrea
|
| 38 | + * @author Marcus Kainth |
38 | 39 | * @since 5.2
|
39 | 40 | */
|
40 | 41 | public class ReactiveJwtAuthenticationConverterTests {
|
@@ -68,4 +69,47 @@ public void convertWithOverriddenGrantedAuthoritiesConverter() {
|
68 | 69 | assertThat(authorities).containsExactly(new SimpleGrantedAuthority("blah"));
|
69 | 70 | }
|
70 | 71 |
|
| 72 | + @Test |
| 73 | + public void whenSettingNullPrincipalClaimName() { |
| 74 | + // @formatter:off |
| 75 | + assertThatIllegalArgumentException() |
| 76 | + .isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(null)) |
| 77 | + .withMessage("principalClaimName cannot be empty"); |
| 78 | + // @formatter:on |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenSettingEmptyPrincipalClaimName() { |
| 83 | + // @formatter:off |
| 84 | + assertThatIllegalArgumentException() |
| 85 | + .isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName("")) |
| 86 | + .withMessage("principalClaimName cannot be empty"); |
| 87 | + // @formatter:on |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void whenSettingBlankPrincipalClaimName() { |
| 92 | + // @formatter:off |
| 93 | + assertThatIllegalArgumentException() |
| 94 | + .isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(" ")) |
| 95 | + .withMessage("principalClaimName cannot be empty"); |
| 96 | + // @formatter:on |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void convertWhenPrincipalClaimNameSet() { |
| 101 | + this.jwtAuthenticationConverter.setPrincipalClaimName("user_id"); |
| 102 | + Jwt jwt = TestJwts.jwt().claim("user_id", "100").build(); |
| 103 | + AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); |
| 104 | + assertThat(authentication.getName()).isEqualTo("100"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void convertWhenPrincipalClaimNameSetAndClaimValueIsNotString() { |
| 109 | + this.jwtAuthenticationConverter.setPrincipalClaimName("user_id"); |
| 110 | + Jwt jwt = TestJwts.jwt().claim("user_id", 100).build(); |
| 111 | + AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); |
| 112 | + assertThat(authentication.getName()).isEqualTo("100"); |
| 113 | + } |
| 114 | + |
71 | 115 | }
|
0 commit comments