|
30 | 30 | import java.nio.file.Files;
|
31 | 31 | import java.util.Arrays;
|
32 | 32 | import java.util.Collections;
|
| 33 | +import java.util.HashMap; |
33 | 34 | import java.util.HashSet;
|
34 | 35 | import java.util.Map;
|
| 36 | +import java.util.Set; |
35 | 37 | import java.util.TreeMap;
|
36 | 38 | import java.util.TreeSet;
|
37 | 39 | import javax.servlet.http.HttpServletRequest;
|
38 | 40 | import opengrok.auth.entity.LdapUser;
|
39 | 41 | import opengrok.auth.plugin.entity.User;
|
| 42 | +import opengrok.auth.plugin.ldap.AbstractLdapProvider; |
40 | 43 | import opengrok.auth.plugin.ldap.FakeLdapFacade;
|
| 44 | +import opengrok.auth.plugin.ldap.LdapException; |
| 45 | +import opengrok.auth.plugin.ldap.LdapFacade; |
41 | 46 | import opengrok.auth.plugin.util.DummyHttpServletRequestLdap;
|
42 | 47 | import org.junit.AfterClass;
|
43 | 48 | import org.junit.Assert;
|
|
47 | 52 | import org.opengrok.indexer.configuration.Group;
|
48 | 53 | import org.opengrok.indexer.configuration.Project;
|
49 | 54 |
|
| 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 | + |
50 | 60 | public class LdapAttrPluginTest {
|
51 | 61 |
|
52 | 62 | private HttpServletRequest dummyRequest;
|
@@ -144,4 +154,51 @@ public void testIsAllowed() {
|
144 | 154 | Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));
|
145 | 155 | Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 2")));
|
146 | 156 | }
|
| 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 | + } |
147 | 204 | }
|
0 commit comments