|
47 | 47 | public class TestKeycloakRoleProvider { |
48 | 48 |
|
49 | 49 | @Mock |
50 | | - private SubjectInformationProvider<Object> subjectInformationProvider; |
| 50 | + private SubjectInformationProvider<Object> subjectInformationProvider; |
51 | 51 |
|
52 | 52 | @Mock |
53 | | - private Jwt jwt; |
| 53 | + private Jwt jwt; |
54 | 54 |
|
55 | 55 | @InjectMocks |
56 | | - private KeycloakRoleProvider keycloakRoleProvider; |
| 56 | + private KeycloakRoleProvider keycloakRoleProvider; |
57 | 57 |
|
58 | 58 | @Before |
59 | | - public void setUp() { |
60 | | - MockitoAnnotations.openMocks(this); |
| 59 | + public void setUp() { |
| 60 | + MockitoAnnotations.openMocks(this); |
61 | 61 |
|
62 | | - @SuppressWarnings("unchecked") |
63 | | - SubjectInformation<Object> subjectInfo = mock(SubjectInformation.class); |
64 | | - when(subjectInfo.get()).thenReturn(jwt); |
65 | | - when(subjectInformationProvider.get()).thenReturn(subjectInfo); |
66 | | - } |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + SubjectInformation<Object> subjectInfo = mock(SubjectInformation.class); |
| 64 | + when(subjectInfo.get()).thenReturn(jwt); |
| 65 | + when(subjectInformationProvider.get()).thenReturn(subjectInfo); |
| 66 | + } |
67 | 67 |
|
68 | 68 | @Test |
69 | | - public void getRoles_whenBothRealmAndResourceRolesPresent() { |
70 | | - Map<String, Collection<String>> realmAccess = new HashMap<>(); |
71 | | - realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
| 69 | + public void getRoles_whenBothRealmAndResourceRolesPresent() { |
| 70 | + Map<String, Collection<String>> realmAccess = new HashMap<>(); |
| 71 | + realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
72 | 72 |
|
73 | | - Map<String, Collection<String>> resourceAccess = new HashMap<>(); |
74 | | - resourceAccess.put("roles", Arrays.asList("ROLE_SUPERUSER", "ROLE_ADMIN")); |
| 73 | + Map<String, Map<String, Collection<String>>> resourceAccess = new HashMap<>(); |
| 74 | + resourceAccess.put("client1", new HashMap<>(Collections.singletonMap("roles", Arrays.asList("ROLE_SUPERUSER", "ROLE_ADMIN")))); |
| 75 | + resourceAccess.put("client2", new HashMap<>(Collections.singletonMap("roles", Arrays.asList("ROLE_SUPPORT")))); |
75 | 76 |
|
76 | | - when(jwt.hasClaim("realm_access")).thenReturn(true); |
77 | | - when(jwt.hasClaim("resource_access")).thenReturn(true); |
78 | | - when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
79 | | - when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
| 77 | + when(jwt.hasClaim("realm_access")).thenReturn(true); |
| 78 | + when(jwt.hasClaim("resource_access")).thenReturn(true); |
| 79 | + when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
| 80 | + when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
80 | 81 |
|
81 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
| 82 | + List<String> roles = keycloakRoleProvider.getRoles(); |
82 | 83 |
|
83 | | - assertEquals(3, roles.size()); |
84 | | - assertTrue(roles.contains("ROLE_USER")); |
85 | | - assertTrue(roles.contains("ROLE_ADMIN")); |
86 | | - assertTrue(roles.contains("ROLE_SUPERUSER")); |
87 | | - } |
| 84 | + assertEquals(4, roles.size()); |
| 85 | + assertTrue(roles.contains("ROLE_USER")); |
| 86 | + assertTrue(roles.contains("ROLE_ADMIN")); |
| 87 | + assertTrue(roles.contains("ROLE_SUPERUSER")); |
| 88 | + assertTrue(roles.contains("ROLE_SUPPORT")); |
| 89 | + } |
88 | 90 |
|
89 | 91 | @Test |
90 | | - public void getRoles_whenOnlyRealmRolesPresent() { |
91 | | - Map<String, Collection<String>> realmAccess = new HashMap<>(); |
92 | | - realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
| 92 | + public void getRoles_whenOnlyRealmRolesPresent() { |
| 93 | + Map<String, Collection<String>> realmAccess = new HashMap<>(); |
| 94 | + realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
93 | 95 |
|
94 | | - when(jwt.hasClaim("realm_access")).thenReturn(true); |
95 | | - when(jwt.hasClaim("resource_access")).thenReturn(true); |
96 | | - when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
97 | | - when(jwt.getClaim("resource_access")).thenReturn(Collections.emptyMap()); |
| 96 | + when(jwt.hasClaim("realm_access")).thenReturn(true); |
| 97 | + when(jwt.hasClaim("resource_access")).thenReturn(true); |
| 98 | + when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
| 99 | + when(jwt.getClaim("resource_access")).thenReturn(Collections.emptyMap()); |
98 | 100 |
|
99 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
| 101 | + List<String> roles = keycloakRoleProvider.getRoles(); |
100 | 102 |
|
101 | | - assertEquals(2, roles.size()); |
102 | | - assertTrue(roles.contains("ROLE_USER")); |
103 | | - assertTrue(roles.contains("ROLE_ADMIN")); |
104 | | - } |
| 103 | + assertEquals(2, roles.size()); |
| 104 | + assertTrue(roles.contains("ROLE_USER")); |
| 105 | + assertTrue(roles.contains("ROLE_ADMIN")); |
| 106 | + } |
105 | 107 |
|
106 | 108 | @Test |
107 | | - public void getRoles_whenOnlyResourceRolesPresent() { |
108 | | - Map<String, Collection<String>> resourceAccess = new HashMap<>(); |
109 | | - resourceAccess.put("roles", Arrays.asList("ROLE_SUPERUSER", "ROLE_SUPPORT")); |
| 109 | + public void getRoles_whenOnlyResourceRolesPresent() { |
| 110 | + Map<String, Map<String, Collection<String>>> resourceAccess = new HashMap<>(); |
| 111 | + resourceAccess.put("client1", new HashMap<>(Collections.singletonMap("roles", Arrays.asList("ROLE_SUPERUSER", "ROLE_SUPPORT")))); |
110 | 112 |
|
111 | | - when(jwt.hasClaim("realm_access")).thenReturn(true); |
112 | | - when(jwt.hasClaim("resource_access")).thenReturn(true); |
113 | | - when(jwt.getClaim("realm_access")).thenReturn(Collections.emptyMap()); |
114 | | - when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
| 113 | + when(jwt.hasClaim("realm_access")).thenReturn(true); |
| 114 | + when(jwt.hasClaim("resource_access")).thenReturn(true); |
| 115 | + when(jwt.getClaim("realm_access")).thenReturn(Collections.emptyMap()); |
| 116 | + when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
115 | 117 |
|
116 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
| 118 | + List<String> roles = keycloakRoleProvider.getRoles(); |
117 | 119 |
|
118 | | - assertEquals(2, roles.size()); |
119 | | - assertTrue(roles.contains("ROLE_SUPERUSER")); |
120 | | - assertTrue(roles.contains("ROLE_SUPPORT")); |
121 | | - } |
| 120 | + assertEquals(2, roles.size()); |
| 121 | + assertTrue(roles.contains("ROLE_SUPERUSER")); |
| 122 | + assertTrue(roles.contains("ROLE_SUPPORT")); |
| 123 | + } |
122 | 124 |
|
123 | 125 | @Test |
124 | | - public void getRoles_whenNoRolesPresent() { |
125 | | - when(jwt.hasClaim("realm_access")).thenReturn(true); |
126 | | - when(jwt.hasClaim("resource_access")).thenReturn(true); |
127 | | - when(jwt.getClaim("realm_access")).thenReturn(Collections.emptyMap()); |
128 | | - when(jwt.getClaim("resource_access")).thenReturn(Collections.emptyMap()); |
129 | | - |
130 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
131 | | - |
132 | | - assertTrue(roles.isEmpty()); |
133 | | - } |
| 126 | + public void getRoles_whenNoRolesPresent() { |
| 127 | + when(jwt.hasClaim("realm_access")).thenReturn(true); |
| 128 | + when(jwt.hasClaim("resource_access")).thenReturn(true); |
| 129 | + when(jwt.getClaim("realm_access")).thenReturn(Collections.emptyMap()); |
| 130 | + when(jwt.getClaim("resource_access")).thenReturn(Collections.emptyMap()); |
134 | 131 |
|
135 | | - @Test(expected = NullSubjectException.class) |
136 | | - public void getRoles_whenJwtIsNull() { |
| 132 | + List<String> roles = keycloakRoleProvider.getRoles(); |
137 | 133 |
|
138 | | - @SuppressWarnings("unchecked") |
139 | | - SubjectInformation<Object> subjectInfo = mock(SubjectInformation.class); |
140 | | - when(subjectInfo.get()).thenReturn(null); |
141 | | - when(subjectInformationProvider.get()).thenReturn(subjectInfo); |
| 134 | + assertTrue(roles.isEmpty()); |
| 135 | + } |
142 | 136 |
|
143 | | - keycloakRoleProvider.getRoles(); |
144 | | - } |
| 137 | + @Test(expected = NullSubjectException.class) |
| 138 | + public void getRoles_whenJwtIsNull() { |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + SubjectInformation<Object> subjectInfo = mock(SubjectInformation.class); |
| 141 | + when(subjectInfo.get()).thenReturn(null); |
| 142 | + when(subjectInformationProvider.get()).thenReturn(subjectInfo); |
145 | 143 |
|
146 | | - @Test |
147 | | - public void getRoles_whenRealmAccessNotPresentButResourceAccessPresent() { |
148 | | - Map<String, Collection<String>> resourceAccess = new HashMap<>(); |
149 | | - resourceAccess.put("roles", Arrays.asList("ROLE_SUPPORT", "ROLE_USER")); |
| 144 | + keycloakRoleProvider.getRoles(); |
| 145 | + } |
150 | 146 |
|
151 | | - when(jwt.hasClaim("realm_access")).thenReturn(false); |
| 147 | + @Test |
| 148 | + public void getRoles_whenRealmAccessNotPresentButResourceAccessPresent() { |
| 149 | + Map<String, Map<String, Collection<String>>> resourceAccess = new HashMap<>(); |
| 150 | + resourceAccess.put("client1", new HashMap<>(Collections.singletonMap("roles", Arrays.asList("ROLE_SUPPORT", "ROLE_USER")))); |
152 | 151 |
|
153 | | - when(jwt.hasClaim("resource_access")).thenReturn(true); |
154 | | - when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
| 152 | + when(jwt.hasClaim("realm_access")).thenReturn(false); |
| 153 | + when(jwt.hasClaim("resource_access")).thenReturn(true); |
| 154 | + when(jwt.getClaim("resource_access")).thenReturn(resourceAccess); |
155 | 155 |
|
156 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
| 156 | + List<String> roles = keycloakRoleProvider.getRoles(); |
157 | 157 |
|
158 | | - assertEquals(2, roles.size()); |
159 | | - assertTrue(roles.contains("ROLE_SUPPORT")); |
160 | | - assertTrue(roles.contains("ROLE_USER")); |
161 | | - } |
| 158 | + assertEquals(2, roles.size()); |
| 159 | + assertTrue(roles.contains("ROLE_SUPPORT")); |
| 160 | + assertTrue(roles.contains("ROLE_USER")); |
| 161 | + } |
162 | 162 |
|
163 | 163 | @Test |
164 | | - public void getRoles_whenResourceAccessNotPresentButRealmAccessPresent() { |
165 | | - Map<String, Collection<String>> realmAccess = new HashMap<>(); |
166 | | - realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
167 | | - when(jwt.hasClaim("resource_access")).thenReturn(false); |
| 164 | + public void getRoles_whenResourceAccessNotPresentButRealmAccessPresent() { |
| 165 | + Map<String, Collection<String>> realmAccess = new HashMap<>(); |
| 166 | + realmAccess.put("roles", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); |
168 | 167 |
|
169 | | - when(jwt.hasClaim("realm_access")).thenReturn(true); |
170 | | - when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
| 168 | + when(jwt.hasClaim("resource_access")).thenReturn(false); |
| 169 | + when(jwt.hasClaim("realm_access")).thenReturn(true); |
| 170 | + when(jwt.getClaim("realm_access")).thenReturn(realmAccess); |
171 | 171 |
|
172 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
| 172 | + List<String> roles = keycloakRoleProvider.getRoles(); |
173 | 173 |
|
174 | | - assertEquals(2, roles.size()); |
175 | | - assertTrue(roles.contains("ROLE_USER")); |
176 | | - assertTrue(roles.contains("ROLE_ADMIN")); |
177 | | - } |
| 174 | + assertEquals(2, roles.size()); |
| 175 | + assertTrue(roles.contains("ROLE_USER")); |
| 176 | + assertTrue(roles.contains("ROLE_ADMIN")); |
| 177 | + } |
178 | 178 |
|
179 | 179 | @Test |
180 | | - public void getRoles_whenClaimNotPresent() { |
181 | | - |
182 | | - when(jwt.hasClaim("realm_access")).thenReturn(false); |
183 | | - when(jwt.hasClaim("resource_access")).thenReturn(false); |
184 | | - |
185 | | - List<String> roles = keycloakRoleProvider.getRoles(); |
186 | | - |
187 | | - assertTrue(roles.isEmpty()); |
188 | | - } |
| 180 | + public void getRoles_whenClaimNotPresent() { |
| 181 | + when(jwt.hasClaim("realm_access")).thenReturn(false); |
| 182 | + when(jwt.hasClaim("resource_access")).thenReturn(false); |
| 183 | + |
| 184 | + List<String> roles = keycloakRoleProvider.getRoles(); |
| 185 | + |
| 186 | + assertTrue(roles.isEmpty()); |
| 187 | + } |
189 | 188 | } |
0 commit comments