smolnar82 commented on code in PR #1258:
URL: https://github.com/apache/knox/pull/1258#discussion_r3401691596
##########
gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorTest.java:
##########
@@ -68,14 +90,95 @@ public void testModifyEntryNoMemberOfNoRoles() throws
Exception {
assertNull(modifiedEntry.get("memberOf"));
}
- private LDAPRolesLookupInterceptor createInterceptor() {
+ @Test
+ public void testRolesLookupNoBypass() throws Exception {
Review Comment:
With a little bit of refactor, we might save some duplicated lines here too
(you may think this is "my thing" :) )
```
private TestContext createTestContext(boolean bypass, LDAPRolesLookupService
rolesService) throws Exception {
DirectoryService directoryService = new SimpleDirectoryService();
directoryService.setShutdownHookEnabled(false);
directoryService.setSchemaManager(SchemaManagerFactory.createSchemaManager());
LDAPRolesLookupInterceptor interceptor =
new LDAPRolesLookupInterceptor(rolesService,
ROLES_LOOKUP_BYPASS_CONTROL_OID);
interceptor.init(directoryService);
directoryService.addLast(interceptor);
ConfigurableEntriesTestInterceptor nextInterceptor =
new ConfigurableEntriesTestInterceptor("NEXT");
nextInterceptor.init(directoryService);
directoryService.addLast(nextInterceptor);
SearchOperationContext ctx =
new SearchOperationContext(directoryService.getSession());
ctx.setInterceptors(List.of(interceptor.getName(), "NEXT"));
RolesLookupBypassControl control =
new
RolesLookupBypassControlImpl(ROLES_LOOKUP_BYPASS_CONTROL_OID);
control.setBypassRolesLookup(bypass);
ctx.addRequestControl(control);
return new TestContext(interceptor, nextInterceptor, ctx);
}
private record TestContext(
LDAPRolesLookupInterceptor interceptor,
ConfigurableEntriesTestInterceptor nextInterceptor,
SearchOperationContext ctx) {
}
```
Then tests are simpler:
```
@Test
public void testRolesLookupNoBypass() throws Exception {
LDAPRolesLookupService mockRolesService =
EasyMock.createMock(LDAPRolesLookupService.class);
Collection<String> roles = List.of("roleA", "roleG");
expect(mockRolesService.lookupRoles(anyString(), anyObject()))
.andReturn(roles)
.atLeastOnce();
replay(mockRolesService);
TestContext tc = createTestContext(false, mockRolesService);
Entry userEntry = createUserEntry("alice",
"cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org");
tc.nextInterceptor().setEntries(List.of(userEntry));
EntryFilteringCursor entries = tc.interceptor().search(tc.ctx());
assertTrue(entries.next());
assertMemberOf(entries.get(),
"cn=roleA,ou=groups,dc=hadoop,dc=apache,dc=org",
"cn=roleG,ou=groups,dc=hadoop,dc=apache,dc=org");
assertFalse(entries.next());
}
@Test
public void testRolesLookupWithBypass() throws Exception {
TestContext tc = createTestContext(true, createMockRolesService());
Entry userEntry = createUserEntry("alice",
"cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org");
tc.nextInterceptor().setEntries(List.of(userEntry));
EntryFilteringCursor entries = tc.interceptor().search(tc.ctx());
assertTrue(entries.next());
assertMemberOf(entries.get(),
"cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org");
assertFalse(entries.next());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]