1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.wallace.messages;
20
21 import junitx.framework.StringAssert;
22 import net.sf.acegisecurity.AuthenticationException;
23 import net.sf.acegisecurity.AuthenticationManager;
24 import net.sf.acegisecurity.BadCredentialsException;
25 import net.sf.acegisecurity.DisabledException;
26 import net.sf.acegisecurity.LockedException;
27 import net.sf.wallace.commands.AbstractCommandTestCase;
28
29 import org.easymock.MockControl;
30
31 /***
32 *
33 *
34 * @author rnewson
35 */
36 public final class LoginMessageTest extends AbstractCommandTestCase {
37
38 private MockControl control;
39
40 private AuthenticationManager authenticationManager;
41
42 protected void setUp() {
43 super.setUp();
44 control = MockControl.createStrictControl(AuthenticationManager.class);
45 authenticationManager = (AuthenticationManager) control.getMock();
46 wallaceServerConfig.setAuthenticationManager(authenticationManager);
47 }
48
49 public void testDisabledUser() throws Exception {
50 authenticationManager.authenticate(null);
51 control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
52 control.setThrowable(new DisabledException("user disabled."));
53
54 StringAssert.assertStartsWith("001 NO", testLoginMessage("disableduser", "nopassword"));
55 }
56
57 public void testLockedUser() throws Exception {
58 authenticationManager.authenticate(null);
59 control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
60 control.setThrowable(new LockedException("user locked."));
61
62 StringAssert.assertStartsWith("001 NO", testLoginMessage("lockeduser", "nopassword"));
63 }
64
65 public void testGenericAuthFailure() throws Exception {
66 authenticationManager.authenticate(null);
67 control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
68 control.setThrowable(new AuthenticationException("") {
69 private static final long serialVersionUID = 1L;
70 });
71
72 StringAssert.assertStartsWith("001 NO", testLoginMessage("", ""));
73 }
74
75 public void testGoodUser() throws Exception {
76 control.expectAndReturn(authenticationManager.authenticate(null), null);
77 control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
78
79 StringAssert.assertStartsWith("001 OK", testLoginMessage("gooduser", "password"));
80 }
81
82 public void testBadUser() throws Exception {
83 authenticationManager.authenticate(null);
84 control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
85 control.setThrowable(new BadCredentialsException("unknown user."));
86
87 StringAssert.assertStartsWith("001 NO", testLoginMessage("baduser", "wrongpassword"));
88 }
89
90 private String testLoginMessage(final String username, final String password) throws Exception {
91 control.replay();
92 final LoginMessage message = new LoginMessage();
93 message.setTag("001");
94 message.setUsername(username);
95 message.setPassword(password);
96 message.getCommand().execute(wallaceSession);
97 control.verify();
98 return getResponse().toString();
99 }
100
101 }