|
46 | 46 | import static org.junit.Assert.assertEquals;
|
47 | 47 | import static org.junit.Assert.assertTrue;
|
48 | 48 | import static org.junit.Assert.fail;
|
49 |
| -import static org.mockito.Mockito.any; |
50 |
| -import static org.mockito.Mockito.eq; |
51 |
| -import static org.mockito.Mockito.mock; |
52 |
| -import static org.mockito.Mockito.when; |
| 49 | +import static org.mockito.Mockito.*; |
53 | 50 | import static org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider.ContextFactory;
|
54 | 51 |
|
55 | 52 | /**
|
@@ -124,6 +121,41 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
|
124 | 121 | assertTrue(result.isAuthenticated());
|
125 | 122 | }
|
126 | 123 |
|
| 124 | + @Test |
| 125 | + public void defaultSearchFilter() throws Exception { |
| 126 | + //given |
| 127 | + final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))"; |
| 128 | + |
| 129 | + DirContext ctx = mock(DirContext.class); |
| 130 | + when(ctx.getNameInNamespace()).thenReturn(""); |
| 131 | + |
| 132 | + DirContextAdapter dca = new DirContextAdapter(); |
| 133 | + SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); |
| 134 | + when(ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class))) |
| 135 | + .thenReturn(new MockNamingEnumeration(sr)); |
| 136 | + |
| 137 | + ActiveDirectoryLdapAuthenticationProvider customProvider |
| 138 | + = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/"); |
| 139 | + customProvider.contextFactory = createContextFactoryReturning(ctx); |
| 140 | + |
| 141 | + //when |
| 142 | + Authentication result = customProvider.authenticate(joe); |
| 143 | + |
| 144 | + //then |
| 145 | + assertTrue(result.isAuthenticated()); |
| 146 | + verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class)); |
| 147 | + } |
| 148 | + |
| 149 | + @Test(expected = IllegalArgumentException.class) |
| 150 | + public void setSearchFilterNull() { |
| 151 | + provider.setSearchFilter(null); |
| 152 | + } |
| 153 | + |
| 154 | + @Test(expected = IllegalArgumentException.class) |
| 155 | + public void setSearchFilterEmpty() { |
| 156 | + provider.setSearchFilter(" "); |
| 157 | + } |
| 158 | + |
127 | 159 | @Test
|
128 | 160 | public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception {
|
129 | 161 | provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/");
|
|
0 commit comments