6060 * @author Luke Taylor
6161 * @author Rob Winch
6262 * @author Gengwu Zhao
63+ * @author Andrey Litvitski
6364 */
6465public class ActiveDirectoryLdapAuthenticationProviderTests {
6566
@@ -97,7 +98,7 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
9798 String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))" ;
9899 DirContextAdapter dca = new DirContextAdapter ();
99100 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
100- given (this .ctx .search (any (Name .class ), eq ( customSearchFilter ), any (Object [] .class ), any (SearchControls .class )))
101+ given (this .ctx .search (any (Name .class ), any (String .class ), any (SearchControls .class )))
101102 .willReturn (new MockNamingEnumeration (sr ));
102103 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
103104 "mydomain.eu" , "ldap://192.168.1.200/" );
@@ -109,34 +110,32 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
109110
110111 @ Test
111112 public void defaultSearchFilter () throws Exception {
112- final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
113113 DirContextAdapter dca = new DirContextAdapter ();
114114 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
115- given (this .ctx .search (any (Name .class ), eq ( defaultSearchFilter ), any (Object [] .class ), any (SearchControls .class )))
115+ given (this .ctx .search (any (Name .class ), any (String .class ), any (SearchControls .class )))
116116 .willReturn (new MockNamingEnumeration (sr ));
117117 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
118118 "mydomain.eu" , "ldap://192.168.1.200/" );
119119 customProvider .contextFactory = createContextFactoryReturning (this .ctx );
120120 Authentication result = customProvider .authenticate (this .joe );
121121 assertThat (result .isAuthenticated ()).isTrue ();
122- verify (this .ctx ).search (any (Name .class ), eq (defaultSearchFilter ), any (Object [].class ),
123- any (SearchControls .class ));
122+ verify (this .ctx ).search (any (Name .class ), any (String .class ), any (SearchControls .class ));
124123 }
125124
126125 // SEC-2897,SEC-2224
127126 @ Test
128127 public void bindPrincipalAndUsernameUsed () throws Exception {
129- final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0} ))" ;
130- ArgumentCaptor <Object [] > captor = ArgumentCaptor .forClass (Object [] .class );
128+ final String captureValue = "(&(objectClass=user)(userPrincipalName=joe@mydomain.eu ))" ;
129+ ArgumentCaptor <String > captor = ArgumentCaptor .forClass (String .class );
131130 DirContextAdapter dca = new DirContextAdapter ();
132131 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
133- given (this .ctx .search (any (Name .class ), eq ( defaultSearchFilter ), captor .capture (), any (SearchControls .class )))
132+ given (this .ctx .search (any (Name .class ), captor .capture (), any (SearchControls .class )))
134133 .willReturn (new MockNamingEnumeration (sr ));
135134 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
136135 "mydomain.eu" , "ldap://192.168.1.200/" );
137136 customProvider .contextFactory = createContextFactoryReturning (this .ctx );
138137 Authentication result = customProvider .authenticate (this .joe );
139- assertThat (captor .getValue ()).containsExactly ( "joe@mydomain.eu" , "joe" );
138+ assertThat (captor .getValue ()).isEqualTo ( captureValue );
140139 assertThat (result .isAuthenticated ()).isTrue ();
141140 }
142141
@@ -156,7 +155,7 @@ public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws
156155 DirContextAdapter dca = new DirContextAdapter ();
157156 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
158157 given (this .ctx .search (eq (LdapNameBuilder .newInstance ("DC=mydomain,DC=eu" ).build ()), any (String .class ),
159- any (Object []. class ), any ( SearchControls .class )))
158+ any (SearchControls .class )))
160159 .willReturn (new MockNamingEnumeration (sr ));
161160 this .provider .contextFactory = createContextFactoryReturning (this .ctx );
162161 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
@@ -165,7 +164,7 @@ public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws
165164
166165 @ Test
167166 public void failedUserSearchCausesBadCredentials () throws Exception {
168- given (this .ctx .search (any (Name .class ), any (String .class ), any (Object []. class ), any ( SearchControls .class )))
167+ given (this .ctx .search (any (Name .class ), any (String .class ), any (SearchControls .class )))
169168 .willThrow (new NameNotFoundException ());
170169 this .provider .contextFactory = createContextFactoryReturning (this .ctx );
171170 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
@@ -174,7 +173,7 @@ public void failedUserSearchCausesBadCredentials() throws Exception {
174173 // SEC-2017
175174 @ Test
176175 public void noUserSearchCausesUsernameNotFound () throws Exception {
177- given (this .ctx .search (any (Name .class ), any (String .class ), any (Object []. class ), any ( SearchControls .class )))
176+ given (this .ctx .search (any (Name .class ), any (String .class ), any (SearchControls .class )))
178177 .willReturn (new MockNamingEnumeration (null ));
179178 this .provider .contextFactory = createContextFactoryReturning (this .ctx );
180179 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
@@ -195,8 +194,7 @@ public void duplicateUserSearchCausesError() throws Exception {
195194 SearchResult searchResult = mock (SearchResult .class );
196195 given (searchResult .getObject ()).willReturn (new DirContextAdapter ("ou=1" ), new DirContextAdapter ("ou=2" ));
197196 given (searchResults .next ()).willReturn (searchResult );
198- given (this .ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
199- .willReturn (searchResults );
197+ given (this .ctx .search (any (Name .class ), any (String .class ), any (SearchControls .class ))).willReturn (searchResults );
200198 this .provider .contextFactory = createContextFactoryReturning (this .ctx );
201199 assertThatExceptionOfType (IncorrectResultSizeDataAccessException .class )
202200 .isThrownBy (() -> this .provider .authenticate (this .joe ));
@@ -353,7 +351,7 @@ private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticatio
353351 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
354352 @ SuppressWarnings ("deprecation" )
355353 Name searchBaseDn = LdapNameBuilder .newInstance (rootDn ).build ();
356- given (this .ctx .search (eq (searchBaseDn ), any (String .class ), any (Object []. class ), any ( SearchControls .class )))
354+ given (this .ctx .search (eq (searchBaseDn ), any (String .class ), any (SearchControls .class )))
357355 .willReturn (new MockNamingEnumeration (sr ))
358356 .willReturn (new MockNamingEnumeration (sr ));
359357 provider .contextFactory = createContextFactoryReturning (this .ctx );
0 commit comments