use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.
the class LdapUserPluginTest method testFillSessionWithDnOff.
@Test
void testFillSessionWithDnOff() throws LdapException {
AbstractLdapProvider mockprovider = mock(LdapFacade.class);
Map<String, Set<String>> attrs = new HashMap<>();
attrs.put("mail", Collections.singleton("foo@example.com"));
final String dn = "cn=FOO_BAR,L=EMEA,DC=EXAMPLE,DC=COM";
AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> result = new AbstractLdapProvider.LdapSearchResult<>(dn, attrs);
assertNotNull(result);
when(mockprovider.lookupLdapContent(isNull(), isNull(), any(String[].class))).thenReturn(result);
Map<String, Object> params = getParamsMap();
params.put(LdapUserPlugin.ATTRIBUTES, "mail");
params.put(LdapUserPlugin.USE_DN, false);
LdapUserPlugin plugin = new LdapUserPlugin();
plugin.load(params, mockprovider);
assertSame(mockprovider, plugin.getLdapProvider());
HttpServletRequest request = new DummyHttpServletRequestLdap();
User user = new User("foo@example.com", "id");
plugin.fillSession(request, user);
assertNotNull(request.getSession().getAttribute(SESSION_ATTR));
assertEquals(dn, ((LdapUser) request.getSession().getAttribute(SESSION_ATTR)).getDn());
}
use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.
the class LdapUserPluginTest method testInstance.
@Test
void testInstance() {
Map<String, Object> params = getParamsMap();
params.put(LdapUserPlugin.ATTRIBUTES, "mail");
params.put(LdapUserPlugin.INSTANCE, "42");
plugin.load(params);
HttpServletRequest request = new DummyHttpServletRequestLdap();
LdapUser ldapUser = new LdapUser();
plugin.updateSession(request, ldapUser);
assertEquals(request.getSession().getAttribute(SESSION_ATTR + "42"), ldapUser);
}
use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.
the class LdapAttrPluginTest method prepareRequest.
private void prepareRequest(String username, String mail, String... ous) {
dummyRequest = new DummyHttpServletRequestLdap();
dummyRequest.setAttribute(UserPlugin.REQUEST_ATTR, new User(username, "123"));
LdapUser ldapUser = new LdapUser();
ldapUser.setAttribute("mail", new TreeSet<>(Collections.singletonList(mail)));
ldapUser.setAttribute("uid", new TreeSet<>(Collections.singletonList("123")));
ldapUser.setAttribute("ou", new TreeSet<>(Arrays.asList(ous)));
dummyRequest.getSession().setAttribute(LdapUserPlugin.SESSION_ATTR, ldapUser);
plugin.setSessionEstablished(dummyRequest, true);
plugin.setSessionUsername(dummyRequest, username);
}
use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.
the class LdapAttrPluginTest method testAttrLookup.
/**
* Test the interaction between {@code LdapUserPlugin} and {@code LdapAttrPlugin}. Namely:
* <ul>
* <li>use of DN from the <code>LdapUser</code> object cached in the session by <code>LdapUserPlugin</code></li>
* <li>configuration of the cached session attribute name</li>
* </ul>
*/
@Test
void testAttrLookup() throws LdapException {
String attr_to_get = "mail";
String instance_num = "42";
String mail_attr_value = "james@bond.com";
// Create mock LDAP provider, simulating the work of LDAP server for LdapAttrPlugin#fillSession().
AbstractLdapProvider mockProvider = mock(LdapFacade.class);
Map<String, Set<String>> attrs = new HashMap<>();
attrs.put(attr_to_get, Collections.singleton(mail_attr_value));
final String dn = "cn=FOO_BAR,L=EMEA,DC=FOO,DC=COM";
AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> result = new AbstractLdapProvider.LdapSearchResult<>(dn, attrs);
assertNotNull(result);
when(mockProvider.lookupLdapContent(anyString(), any(String[].class))).thenReturn(result);
// Load the LdapAttrPlugin using the mock LDAP provider.
LdapAttrPlugin plugin = new LdapAttrPlugin();
Map<String, Object> parameters = new TreeMap<>();
parameters.put(LdapAttrPlugin.FILE_PARAM, whitelistFile.getAbsolutePath());
parameters.put(LdapAttrPlugin.ATTR_PARAM, attr_to_get);
parameters.put(LdapAttrPlugin.INSTANCE_PARAM, instance_num);
plugin.load(parameters, mockProvider);
LdapUser ldapUser = new LdapUser(dn, null);
HttpServletRequest request = new DummyHttpServletRequestLdap();
request.getSession().setAttribute(LdapUserPlugin.SESSION_ATTR + instance_num, ldapUser);
// Here it comes all together.
User user = new User("jbond", "007");
plugin.fillSession(request, user);
// See if LdapAttrPlugin set its own session attribute based on the mocked query.
assertTrue((Boolean) request.getSession().getAttribute(plugin.getSessionAllowedAttrName()));
assertTrue(ldapUser.getAttribute(attr_to_get).contains(mail_attr_value));
}
use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.
the class LdapUserPluginTest method testNegativeCache.
@Test
void testNegativeCache() throws LdapException {
AbstractLdapProvider mockprovider = mock(LdapFacade.class);
when(mockprovider.lookupLdapContent(isNull(), isNull(), any(String[].class))).thenReturn(null);
Map<String, Object> params = getParamsMap();
params.put(LdapUserPlugin.ATTRIBUTES, "mail");
params.put(LdapUserPlugin.USE_DN, false);
LdapUserPlugin origPlugin = new LdapUserPlugin();
LdapUserPlugin plugin = Mockito.spy(origPlugin);
plugin.load(params, mockprovider);
assertSame(mockprovider, plugin.getLdapProvider());
HttpServletRequest dummyRequest = new DummyHttpServletRequestLdap();
User user = new User("foo@example.com", "id");
dummyRequest.setAttribute(UserPlugin.REQUEST_ATTR, new User("foo", "123"));
plugin.fillSession(dummyRequest, user);
assertNotNull(dummyRequest.getSession().getAttribute(SESSION_ATTR));
assertFalse(plugin.isAllowed(dummyRequest, new Project("foo")));
assertFalse(plugin.isAllowed(dummyRequest, new Group("bar")));
// Make sure that the session was filled so that the second call to isAllowed() did not fill it again.
verify(plugin, times(2)).updateSession(eq(dummyRequest), anyString(), anyBoolean());
}
Aggregations