01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.providers.ldap.authenticator;
17:
18: import org.acegisecurity.ldap.MockInitialDirContextFactory;
19:
20: import org.jmock.Mock;
21: import org.jmock.MockObjectTestCase;
22:
23: import javax.naming.directory.Attributes;
24: import javax.naming.directory.BasicAttributes;
25: import javax.naming.directory.DirContext;
26:
27: /**
28: *
29: DOCUMENT ME!
30: *
31: * @author Luke Taylor
32: * @version $Id: PasswordComparisonAuthenticatorMockTests.java 1496 2006-05-23 13:38:33Z benalex $
33: */
34: public class PasswordComparisonAuthenticatorMockTests extends
35: MockObjectTestCase {
36: //~ Methods ========================================================================================================
37:
38: public void testLdapCompareIsUsedWhenPasswordIsNotRetrieved()
39: throws Exception {
40: Mock mockCtx = mock(DirContext.class);
41:
42: PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(
43: new MockInitialDirContextFactory((DirContext) mockCtx
44: .proxy(), "dc=acegisecurity,dc=org"));
45:
46: authenticator
47: .setUserDnPatterns(new String[] { "cn={0},ou=people" });
48:
49: // Get the mock to return an empty attribute set
50: mockCtx.expects(atLeastOnce()).method("getNameInNamespace")
51: .will(returnValue("dc=acegisecurity,dc=org"));
52: mockCtx.expects(once()).method("lookup").with(
53: eq("cn=Bob,ou=people")).will(returnValue(true));
54: mockCtx.expects(once()).method("getAttributes").with(
55: eq("cn=Bob,ou=people"), NULL).will(
56: returnValue(new BasicAttributes()));
57:
58: // Setup a single return value (i.e. success)
59: Attributes searchResults = new BasicAttributes("", null);
60: mockCtx.expects(once()).method("search").with(
61: eq("cn=Bob,ou=people"), eq("(userPassword={0})"),
62: NOT_NULL, NOT_NULL).will(
63: returnValue(searchResults.getAll()));
64: mockCtx.expects(atLeastOnce()).method("close");
65: authenticator.authenticate("Bob", "bobspassword");
66: }
67: }
|