4
4
import static org .junit .jupiter .api .Assertions .assertFalse ;
5
5
import static org .junit .jupiter .api .Assertions .assertNotNull ;
6
6
import static org .junit .jupiter .api .Assertions .assertTrue ;
7
- import static org .mockito .Mockito .when ;
8
7
import static org .springframework .security .oauth2 .client .registration .ClientRegistration .withRegistrationId ;
9
8
10
- import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
11
9
import io .kafbat .ui .config .auth .OAuthProperties ;
12
- import io .kafbat .ui .model . rbac . Role ;
10
+ import io .kafbat .ui .config . auth . RoleBasedAccessControlProperties ;
13
11
import io .kafbat .ui .service .rbac .AccessControlService ;
14
12
import io .kafbat .ui .service .rbac .extractor .CognitoAuthorityExtractor ;
15
13
import io .kafbat .ui .service .rbac .extractor .GithubAuthorityExtractor ;
16
14
import io .kafbat .ui .service .rbac .extractor .GoogleAuthorityExtractor ;
17
15
import io .kafbat .ui .service .rbac .extractor .OauthAuthorityExtractor ;
18
16
import io .kafbat .ui .service .rbac .extractor .ProviderAuthorityExtractor ;
19
17
import io .kafbat .ui .util .AccessControlServiceMock ;
20
- import java .io .IOException ;
21
- import java .io .InputStream ;
22
18
import java .time .Instant ;
23
19
import java .time .temporal .ChronoUnit ;
24
20
import java .util .HashMap ;
28
24
import lombok .SneakyThrows ;
29
25
import org .junit .jupiter .api .BeforeEach ;
30
26
import org .junit .jupiter .api .Test ;
27
+ import org .junit .jupiter .api .extension .ExtendWith ;
28
+ import org .springframework .beans .factory .annotation .Autowired ;
29
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
31
30
import org .springframework .security .core .authority .AuthorityUtils ;
32
31
import org .springframework .security .oauth2 .client .userinfo .OAuth2UserRequest ;
33
32
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
34
33
import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
35
34
import org .springframework .security .oauth2 .core .user .DefaultOAuth2User ;
36
35
import org .springframework .security .oauth2 .core .user .OAuth2User ;
37
-
36
+ import org .springframework .test .context .TestPropertySource ;
37
+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
38
+
39
+ @ ExtendWith (SpringExtension .class )
40
+ @ EnableConfigurationProperties (RoleBasedAccessControlProperties .class )
41
+ @ TestPropertySource (
42
+ locations = "classpath:application-roles-definition.yml" ,
43
+ factory = YamlPropertySourceFactory .class
44
+ )
38
45
public class RegexBasedProviderAuthorityExtractorTest {
39
46
40
-
41
- private final AccessControlService accessControlService = new AccessControlServiceMock (). getMock () ;
42
- ProviderAuthorityExtractor extractor ;
47
+ @ Autowired
48
+ private RoleBasedAccessControlProperties properties ;
49
+ private AccessControlService accessControlService ;
43
50
44
51
@ BeforeEach
45
- void setUp () throws IOException {
46
-
47
- YAMLMapper mapper = new YAMLMapper ();
48
-
49
- InputStream rolesFile = this .getClass ()
50
- .getClassLoader ()
51
- .getResourceAsStream ("roles_definition.yaml" );
52
-
53
- Role [] roles = mapper .readValue (rolesFile , Role [].class );
54
-
55
- when (accessControlService .getRoles ()).thenReturn (List .of (roles ));
56
-
52
+ public void configure () {
53
+ this .accessControlService = new AccessControlServiceMock (properties .getRoles ()).getMock ();
57
54
}
58
55
59
56
@ SneakyThrows
60
57
@ Test
61
58
void extractOauth2Authorities () {
62
59
63
- extractor = new OauthAuthorityExtractor ();
60
+ ProviderAuthorityExtractor extractor = new OauthAuthorityExtractor ();
64
61
65
62
OAuth2User oauth2User = new DefaultOAuth2User (
66
63
AuthorityUtils .createAuthorityList ("SCOPE_message:read" ),
@@ -84,7 +81,7 @@ void extractOauth2Authorities() {
84
81
@ Test ()
85
82
void extractOauth2Authorities_blankEmail () {
86
83
87
- extractor = new OauthAuthorityExtractor ();
84
+ ProviderAuthorityExtractor extractor = new OauthAuthorityExtractor ();
88
85
89
86
OAuth2User oauth2User = new DefaultOAuth2User (
90
87
AuthorityUtils .createAuthorityList ("SCOPE_message:read" ),
@@ -108,7 +105,7 @@ void extractOauth2Authorities_blankEmail() {
108
105
@ Test
109
106
void extractCognitoAuthorities () {
110
107
111
- extractor = new CognitoAuthorityExtractor ();
108
+ ProviderAuthorityExtractor extractor = new CognitoAuthorityExtractor ();
112
109
113
110
OAuth2User oauth2User = new DefaultOAuth2User (
114
111
AuthorityUtils .createAuthorityList ("SCOPE_message:read" ),
@@ -133,7 +130,7 @@ void extractCognitoAuthorities() {
133
130
@ Test
134
131
void extractGithubAuthorities () {
135
132
136
- extractor = new GithubAuthorityExtractor ();
133
+ ProviderAuthorityExtractor extractor = new GithubAuthorityExtractor ();
137
134
138
135
OAuth2User oauth2User = new DefaultOAuth2User (
139
136
AuthorityUtils .createAuthorityList ("SCOPE_message:read" ),
@@ -170,7 +167,7 @@ void extractGithubAuthorities() {
170
167
@ Test
171
168
void extractGoogleAuthorities () {
172
169
173
- extractor = new GoogleAuthorityExtractor ();
170
+ ProviderAuthorityExtractor extractor = new GoogleAuthorityExtractor ();
174
171
175
172
OAuth2User oauth2User = new DefaultOAuth2User (
176
173
AuthorityUtils .createAuthorityList ("SCOPE_message:read" ),
0 commit comments