01: package ru.emdev.EmForge.tests.security;
02:
03: import static org.junit.Assert.assertEquals;
04: import static org.junit.Assert.assertFalse;
05: import static org.junit.Assert.assertNotNull;
06: import static org.junit.Assert.assertTrue;
07: import static org.junit.Assert.fail;
08:
09: import org.acegisecurity.AuthenticationException;
10: import org.acegisecurity.GrantedAuthority;
11: import org.acegisecurity.userdetails.UserDetails;
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14: import org.emforge.tests.BaseUnitTest;
15: import org.junit.Test;
16:
17: import ru.emdev.EmForge.security.EmForgeUserDetails;
18: import ru.emdev.EmForge.security.memory.EmForgeUserAttribute;
19: import ru.emdev.EmForge.security.memory.EmForgeUserAttributeEditor;
20:
21: public class MemoryTest extends BaseUnitTest {
22: protected final Log logger = LogFactory.getLog(getClass());
23:
24: @Test
25: public void testLogin() {
26: authenticateUser("dummy1", "dummy1pass");
27:
28: // get user details
29: UserDetails userDetails = (UserDetails) getCurrentUser()
30: .getPrincipal();
31: checkDummy1(userDetails);
32: }
33:
34: @Test
35: public void testLoginDisabled() {
36: try {
37: authenticateUser("dummy2", "dummy2pass");
38: } catch (AuthenticationException ex) {
39: // user is disabled - so, we should out here
40: return;
41: }
42:
43: fail("AuthenticationException is expected");
44: }
45:
46: /** Testing utility class used for reading user attributes */
47: @Test
48: public void testUserAttributeEditor() {
49: EmForgeUserAttributeEditor configAttribEd = new EmForgeUserAttributeEditor();
50:
51: // Convert value to a password, enabled setting, and list of granted authorities
52: configAttribEd
53: .setAsText("dummy1pass,dummy1@emforge.org,dummy2,ROLE_USER, disabled,ROLE_MANAGER");
54:
55: EmForgeUserAttribute attr = (EmForgeUserAttribute) configAttribEd
56: .getValue();
57:
58: assertEquals("dummy1pass", attr.getPassword());
59: assertEquals("dummy1@emforge.org", attr.getEmail());
60: assertEquals("dummy2", attr.getManager());
61: assertFalse(attr.isEnabled());
62:
63: GrantedAuthority[] roles = attr.getAuthorities();
64: assertEquals(2, roles.length);
65: assertEquals("ROLE_USER", roles[0].getAuthority());
66: assertEquals("ROLE_MANAGER", roles[1].getAuthority());
67:
68: }
69:
70: /** Check properties for akakunin user */
71: protected void checkDummy1(UserDetails i_user) {
72: assertNotNull(i_user);
73:
74: assertEquals("dummy1", i_user.getUsername());
75:
76: // it should be emforge specific user details
77: assertTrue("It should be EmForge Specific User Details",
78: i_user instanceof EmForgeUserDetails);
79:
80: // get emforge specific user details
81: EmForgeUserDetails emForgeUser = (EmForgeUserDetails) i_user;
82:
83: // check EmForge specific attributes
84: assertEquals("dummy1", emForgeUser.getDisplayName());
85: assertEquals("dummy1@emforge.org", emForgeUser.getEmail());
86:
87: assertFalse(emForgeUser.isAnonymous());
88:
89: // try to get Manager
90: EmForgeUserDetails manager = emForgeUser.getManager();
91:
92: assertNotNull(manager);
93:
94: // it should be akakunin itself
95: assertEquals("dummy2", manager.getUsername());
96: }
97:
98: }
|