001: package ru.emdev.EmForge.tests.security;
002:
003: import static org.junit.Assert.assertEquals;
004: import static org.junit.Assert.assertNotNull;
005:
006: import java.util.Collection;
007:
008: import org.acegisecurity.GrantedAuthority;
009: import org.acegisecurity.providers.encoding.PasswordEncoder;
010: import org.acegisecurity.userdetails.UserDetails;
011: import org.emforge.tests.BaseUnitTest;
012: import org.junit.Before;
013: import org.junit.Test;
014: import org.springframework.beans.factory.annotation.Autowired;
015:
016: import ru.emdev.EmForge.security.EmForgeUserDetails;
017: import ru.emdev.EmForge.security.dao.Role;
018: import ru.emdev.EmForge.security.dao.User;
019: import ru.emdev.EmForge.security.dao.UserDao;
020: import ru.emdev.EmForge.security.web.SiteRole;
021:
022: /** Used for testing user-dao */
023: public class UserDaoTest extends BaseUnitTest {
024: @Autowired
025: private UserDao m_userDao;
026: @Autowired
027: private PasswordEncoder m_passwordEncoder;
028:
029: @Before
030: public void onSetUpInTransaction() throws Exception {
031: //executeSqlScript("classpath:test-data.sql", false);
032:
033: // populate data
034: createUsers();
035: }
036:
037: @Test
038: public void testLoadUser() {
039: //load user by username
040: UserDetails user = m_userDao.loadUserByUsername("dbuser1");
041: checkDbUser1(user);
042: }
043:
044: @Test
045: public void testLoadAll() {
046: Collection<UserDetails> users = m_userDao.getAllUsers();
047:
048: assertEquals(2, users.size());
049:
050: }
051:
052: @Test
053: public void testLogin() {
054: authenticateUser("dbuser2", "somepass2");
055:
056: // get user details
057: UserDetails userDetails = (UserDetails) getCurrentUser()
058: .getPrincipal();
059: checkDbUser2(userDetails);
060: }
061:
062: private void createUsers() {
063: assertNotNull(m_userDao);
064:
065: // create roles
066: Role roleAdmin = m_userDao
067: .getRoleByName(SiteRole.ADMIN.getId());
068:
069: Role roleWriter = m_userDao.getRoleByName(SiteRole.WRITER
070: .getId());
071:
072: User user1 = new User();
073: user1.setUsername("dbuser1");
074: user1.setPassword(m_passwordEncoder.encodePassword("somepass",
075: ""));
076: user1.setEmail("dbuser@test.com");
077: user1.setFirstName("Test");
078: user1.setLastName("User");
079: user1.setActive(true);
080: user1.addRole(roleAdmin);
081: user1.addRole(roleWriter);
082: m_userDao.saveUser(user1);
083:
084: User user2 = new User();
085: user2.setUsername("dbuser2");
086: user2.setPassword(m_passwordEncoder.encodePassword("somepass2",
087: ""));
088: user2.setEmail("dbuser2@test.com");
089: user2.setFirstName("Test2");
090: user2.setLastName("User");
091: user2.setManager(user1);
092: user2.setActive(true);
093: user2.addRole(roleWriter);
094: m_userDao.saveUser(user2);
095:
096: }
097:
098: private void checkDbUser1(UserDetails user) {
099: assertNotNull(user);
100: assertEquals("dbuser1", user.getUsername());
101:
102: // it should be emforge user
103: EmForgeUserDetails emForgeUser = (EmForgeUserDetails) user;
104: assertEquals("dbuser@test.com", emForgeUser.getEmail());
105: assertEquals("Test User", emForgeUser.getDisplayName());
106:
107: //check roles
108: GrantedAuthority[] roles = user.getAuthorities();
109: assertNotNull(roles);
110: assertEquals(2, roles.length);
111: }
112:
113: private void checkDbUser2(UserDetails user) {
114: assertNotNull(user);
115: assertEquals("dbuser2", user.getUsername());
116:
117: // it should be emforge user
118: EmForgeUserDetails emForgeUser = (EmForgeUserDetails) user;
119: assertEquals("dbuser2@test.com", emForgeUser.getEmail());
120: assertEquals("Test2 User", emForgeUser.getDisplayName());
121:
122: // get manager
123: EmForgeUserDetails manager = emForgeUser.getManager();
124: assertNotNull(manager);
125: assertEquals("dbuser1", manager.getUsername());
126:
127: //check roles
128: GrantedAuthority[] roles = user.getAuthorities();
129: assertNotNull(roles);
130: assertEquals(1, roles.length);
131: }
132: }
|