Skip to content

Commit f7f40d7

Browse files
author
Vladimir Kotal
committed
first working test
1 parent 464d259 commit f7f40d7

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

plugins/src/main/java/opengrok/auth/plugin/LdapAttrPlugin.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public class LdapAttrPlugin extends AbstractLdapPlugin {
5252

5353
private static final Logger LOGGER = Logger.getLogger(LdapAttrPlugin.class.getName());
5454

55-
protected static final String ATTR_PARAM = "attribute"; // LDAP attribute name to check
56-
protected static final String FILE_PARAM = "file";
57-
private static final String INSTANCE = "instance";
55+
static final String ATTR_PARAM = "attribute"; // LDAP attribute name to check
56+
static final String FILE_PARAM = "file";
57+
static final String INSTANCE_PARAM = "instance";
5858

59-
private static final String SESSION_ALLOWED_PREFIX = "opengrok-attr-plugin-allowed";
59+
static final String SESSION_ALLOWED_PREFIX = "opengrok-ldap-attr-plugin-allowed";
6060
private String sessionAllowed = SESSION_ALLOWED_PREFIX;
6161

6262
/**
@@ -99,7 +99,7 @@ void init(Map<String, Object> parameters) {
9999
throw new NullPointerException("Missing param [" + FILE_PARAM + "] in the setup");
100100
}
101101

102-
String instance = (String) parameters.get(INSTANCE);
102+
String instance = (String) parameters.get(INSTANCE_PARAM);
103103
if (instance != null) {
104104
ldapUserInstance = Integer.parseInt(instance);
105105
}
@@ -120,10 +120,14 @@ protected boolean sessionExists(HttpServletRequest req) {
120120
&& req.getSession().getAttribute(sessionAllowed) != null;
121121
}
122122

123-
private String getSessionAttr() {
123+
private String getSessionAttrName() {
124124
return (LdapUserPlugin.SESSION_ATTR + (ldapUserInstance != null ? ldapUserInstance.toString() : ""));
125125
}
126126

127+
String getSessionAllowedAttrName() {
128+
return sessionAllowed;
129+
}
130+
127131
@SuppressWarnings("unchecked")
128132
@Override
129133
public void fillSession(HttpServletRequest req, User user) {
@@ -134,7 +138,7 @@ public void fillSession(HttpServletRequest req, User user) {
134138

135139
updateSession(req, false);
136140

137-
if ((ldapUser = (LdapUser) req.getSession().getAttribute(getSessionAttr())) == null) {
141+
if ((ldapUser = (LdapUser) req.getSession().getAttribute(getSessionAttrName())) == null) {
138142
LOGGER.log(Level.WARNING, "cannot get {0} attribute", LdapUserPlugin.SESSION_ATTR);
139143
return;
140144
}

plugins/src/main/java/opengrok/auth/plugin/LdapUserPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protected void updateSession(HttpServletRequest req, LdapUser user) {
208208
req.getSession().setAttribute(getSessionAttr(), user);
209209
}
210210

211-
private String getSessionAttr() {
211+
String getSessionAttr() {
212212
return (SESSION_ATTR + (instance != null ? instance.toString() : ""));
213213
}
214214

plugins/src/test/java/opengrok/auth/plugin/LdapAttrPluginTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@
3030
import java.nio.file.Files;
3131
import java.util.Arrays;
3232
import java.util.Collections;
33+
import java.util.HashMap;
3334
import java.util.HashSet;
3435
import java.util.Map;
36+
import java.util.Set;
3537
import java.util.TreeMap;
3638
import java.util.TreeSet;
3739
import javax.servlet.http.HttpServletRequest;
3840
import opengrok.auth.entity.LdapUser;
3941
import opengrok.auth.plugin.entity.User;
42+
import opengrok.auth.plugin.ldap.AbstractLdapProvider;
4043
import opengrok.auth.plugin.ldap.FakeLdapFacade;
44+
import opengrok.auth.plugin.ldap.LdapException;
45+
import opengrok.auth.plugin.ldap.LdapFacade;
4146
import opengrok.auth.plugin.util.DummyHttpServletRequestLdap;
4247
import org.junit.AfterClass;
4348
import org.junit.Assert;
@@ -47,6 +52,11 @@
4752
import org.opengrok.indexer.configuration.Group;
4853
import org.opengrok.indexer.configuration.Project;
4954

55+
import static org.junit.jupiter.api.Assertions.*;
56+
import static org.mockito.ArgumentMatchers.*;
57+
import static org.mockito.Mockito.mock;
58+
import static org.mockito.Mockito.when;
59+
5060
public class LdapAttrPluginTest {
5161

5262
private HttpServletRequest dummyRequest;
@@ -144,4 +154,51 @@ public void testIsAllowed() {
144154
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));
145155
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 2")));
146156
}
157+
158+
/**
159+
* Test the interaction between {@code LdapUserPlugin} and {@code LdapAttrPlugin}, namely:
160+
* <ul>
161+
* <li>use of DN from the <code>LdapUser</code> object cached in the session by <code>LdapUserPlugin</code></li>
162+
* <li>configuration of the cached session attribute name</li>
163+
* </ul>
164+
*/
165+
@Test
166+
public void testAttrLookup() throws LdapException {
167+
String attr_to_get = "mail";
168+
String instance_num = "42";
169+
String mail_attr_value = "[email protected]";
170+
171+
// Create mock LDAP provider, simulating the work of LdapUserPlugin.
172+
AbstractLdapProvider mockprovider = mock(LdapFacade.class);
173+
Map<String, Set<String>> attrs = new HashMap<>();
174+
attrs.put(attr_to_get, Collections.singleton(mail_attr_value));
175+
final String dn = "cn=FOO_BAR,L=EMEA,DC=FOO,DC=COM";
176+
AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> result =
177+
new AbstractLdapProvider.LdapSearchResult<>(dn, attrs);
178+
assertNotNull(result);
179+
// TODO use Mockito Argument captor ?
180+
when(mockprovider.lookupLdapContent(anyString(), any(String[].class))).
181+
thenReturn(result);
182+
183+
// Load the LdapAttrPlugin using the mock LDAP provider.
184+
LdapAttrPlugin plugin = new LdapAttrPlugin();
185+
Map<String, Object> parameters = new TreeMap<>();
186+
parameters.put(LdapAttrPlugin.FILE_PARAM, whitelistFile.getAbsolutePath());
187+
parameters.put(LdapAttrPlugin.ATTR_PARAM, attr_to_get);
188+
parameters.put(LdapAttrPlugin.INSTANCE_PARAM, instance_num);
189+
plugin.load(parameters, mockprovider);
190+
191+
// TODO prepareRequest() ?
192+
LdapUser ldapUser = new LdapUser(dn, null);
193+
HttpServletRequest request = new DummyHttpServletRequestLdap();
194+
request.getSession().setAttribute(LdapUserPlugin.SESSION_ATTR + instance_num, ldapUser);
195+
196+
// Here it comes all together.
197+
User user = new User("[email protected]", "id");
198+
plugin.fillSession(request, user);
199+
200+
// See if LdapAttrPlugin set its own session attribute based on the mocked query.
201+
assertTrue((Boolean)request.getSession().getAttribute(plugin.getSessionAllowedAttrName()));
202+
assertTrue(ldapUser.getAttribute(attr_to_get).contains(mail_attr_value));
203+
}
147204
}

0 commit comments

Comments
 (0)