-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Factor out some common object mocking in tests #15396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -59,6 +59,7 @@ | |
/** | ||
* @author Luke Taylor | ||
* @author Rob Winch | ||
* @author Gengwu Zhao | ||
*/ | ||
public class ActiveDirectoryLdapAuthenticationProviderTests { | ||
|
||
|
@@ -70,9 +71,13 @@ public class ActiveDirectoryLdapAuthenticationProviderTests { | |
|
||
UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken.unauthenticated("joe", "password"); | ||
|
||
DirContext ctx; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
public void setUp() throws NamingException { | ||
this.provider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/"); | ||
this.ctx = mock(DirContext.class); | ||
given(this.ctx.getNameInNamespace()).willReturn(""); | ||
} | ||
|
||
@Test | ||
|
@@ -90,15 +95,13 @@ public void successfulAuthenticationProducesExpectedAuthorities() throws Excepti | |
@Test | ||
public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Exception { | ||
String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))"; | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
DirContextAdapter dca = new DirContextAdapter(); | ||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); | ||
given(ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class))) | ||
.willReturn(new MockNamingEnumeration(sr)); | ||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( | ||
"mydomain.eu", "ldap://192.168.1.200/"); | ||
customProvider.contextFactory = createContextFactoryReturning(ctx); | ||
customProvider.contextFactory = createContextFactoryReturning(this.ctx); | ||
customProvider.setSearchFilter(customSearchFilter); | ||
Authentication result = customProvider.authenticate(this.joe); | ||
assertThat(result.isAuthenticated()).isTrue(); | ||
|
@@ -107,34 +110,31 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti | |
@Test | ||
public void defaultSearchFilter() throws Exception { | ||
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))"; | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
DirContextAdapter dca = new DirContextAdapter(); | ||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); | ||
given(ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class))) | ||
.willReturn(new MockNamingEnumeration(sr)); | ||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( | ||
"mydomain.eu", "ldap://192.168.1.200/"); | ||
customProvider.contextFactory = createContextFactoryReturning(ctx); | ||
customProvider.contextFactory = createContextFactoryReturning(this.ctx); | ||
Authentication result = customProvider.authenticate(this.joe); | ||
assertThat(result.isAuthenticated()).isTrue(); | ||
verify(ctx).search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class)); | ||
verify(this.ctx).search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), | ||
any(SearchControls.class)); | ||
} | ||
|
||
// SEC-2897,SEC-2224 | ||
@Test | ||
public void bindPrincipalAndUsernameUsed() throws Exception { | ||
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))"; | ||
ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class); | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
DirContextAdapter dca = new DirContextAdapter(); | ||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); | ||
given(ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class))) | ||
.willReturn(new MockNamingEnumeration(sr)); | ||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( | ||
"mydomain.eu", "ldap://192.168.1.200/"); | ||
customProvider.contextFactory = createContextFactoryReturning(ctx); | ||
customProvider.contextFactory = createContextFactoryReturning(this.ctx); | ||
Authentication result = customProvider.authenticate(this.joe); | ||
assertThat(captor.getValue()).containsExactly("[email protected]", "joe"); | ||
assertThat(result.isAuthenticated()).isTrue(); | ||
|
@@ -153,36 +153,30 @@ public void setSearchFilterEmpty() { | |
@Test | ||
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception { | ||
this.provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/"); | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
DirContextAdapter dca = new DirContextAdapter(); | ||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); | ||
given(ctx.search(eq(LdapNameBuilder.newInstance("DC=mydomain,DC=eu").build()), any(String.class), | ||
given(this.ctx.search(eq(LdapNameBuilder.newInstance("DC=mydomain,DC=eu").build()), any(String.class), | ||
any(Object[].class), any(SearchControls.class))) | ||
.willReturn(new MockNamingEnumeration(sr)); | ||
this.provider.contextFactory = createContextFactoryReturning(ctx); | ||
this.provider.contextFactory = createContextFactoryReturning(this.ctx); | ||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe)); | ||
this.provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("[email protected]", "password")); | ||
} | ||
|
||
@Test | ||
public void failedUserSearchCausesBadCredentials() throws Exception { | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
.willThrow(new NameNotFoundException()); | ||
this.provider.contextFactory = createContextFactoryReturning(ctx); | ||
this.provider.contextFactory = createContextFactoryReturning(this.ctx); | ||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe)); | ||
} | ||
|
||
// SEC-2017 | ||
@Test | ||
public void noUserSearchCausesUsernameNotFound() throws Exception { | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
.willReturn(new EmptyEnumeration<>()); | ||
this.provider.contextFactory = createContextFactoryReturning(ctx); | ||
this.provider.contextFactory = createContextFactoryReturning(this.ctx); | ||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe)); | ||
} | ||
|
||
|
@@ -196,16 +190,14 @@ public void sec2500PreventAnonymousBind() { | |
@Test | ||
@SuppressWarnings("unchecked") | ||
public void duplicateUserSearchCausesError() throws Exception { | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
NamingEnumeration<SearchResult> searchResults = mock(NamingEnumeration.class); | ||
given(searchResults.hasMore()).willReturn(true, true, false); | ||
SearchResult searchResult = mock(SearchResult.class); | ||
given(searchResult.getObject()).willReturn(new DirContextAdapter("ou=1"), new DirContextAdapter("ou=2")); | ||
given(searchResults.next()).willReturn(searchResult); | ||
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
.willReturn(searchResults); | ||
this.provider.contextFactory = createContextFactoryReturning(ctx); | ||
this.provider.contextFactory = createContextFactoryReturning(this.ctx); | ||
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class) | ||
.isThrownBy(() -> this.provider.authenticate(this.joe)); | ||
} | ||
|
@@ -357,16 +349,14 @@ DirContext createContext(Hashtable<?, ?> env) { | |
|
||
private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticationProvider provider) | ||
throws NamingException { | ||
DirContext ctx = mock(DirContext.class); | ||
given(ctx.getNameInNamespace()).willReturn(""); | ||
DirContextAdapter dca = new DirContextAdapter(); | ||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); | ||
@SuppressWarnings("deprecation") | ||
Name searchBaseDn = LdapNameBuilder.newInstance(rootDn).build(); | ||
given(ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
given(this.ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class))) | ||
.willReturn(new MockNamingEnumeration(sr)) | ||
.willReturn(new MockNamingEnumeration(sr)); | ||
provider.contextFactory = createContextFactoryReturning(ctx); | ||
provider.contextFactory = createContextFactoryReturning(this.ctx); | ||
Authentication result = provider.authenticate(this.joe); | ||
assertThat(result.getAuthorities()).isEmpty(); | ||
dca.addAttributeValue("memberOf", "CN=Admin,CN=Users,DC=mydomain,DC=eu"); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.