001: package org.emforge.projectmanager.tests;
002:
003: import static org.junit.Assert.assertEquals;
004: import static org.junit.Assert.assertNotNull;
005: import static org.junit.Assert.assertTrue;
006:
007: import java.util.Collection;
008:
009: import org.acegisecurity.providers.encoding.PasswordEncoder;
010: import org.emforge.projectmanager.ProjectService;
011: import org.emforge.projectmanager.base.ProjectDO;
012: import org.emforge.tests.BaseUnitTest;
013: import org.junit.Before;
014: import org.junit.Test;
015: import org.springframework.beans.factory.annotation.Autowired;
016:
017: import ru.emdev.EmForge.security.EmForgeUserDetails;
018: import ru.emdev.EmForge.security.UserFactory;
019: import ru.emdev.EmForge.security.dao.Role;
020: import ru.emdev.EmForge.security.dao.User;
021: import ru.emdev.EmForge.security.dao.UserDao;
022:
023: /** Used for testing relations between users, roles and projects
024: */
025: public class ProjectUserRoleTest extends BaseUnitTest {
026: @Autowired
027: private UserDao m_userDao;
028: @Autowired
029: private PasswordEncoder m_passwordEncoder;
030:
031: @Autowired
032: private ProjectService m_projectService;
033: @Autowired
034: private UserFactory m_userFactory;
035:
036: @Before
037: public void onSetUpInTransaction() throws Exception {
038: //executeSqlScript("classpath:test-data.sql", false);
039:
040: // populate data
041: createData();
042: }
043:
044: private void createData() {
045: // authenticate user
046: authenticateUser("dummy1", "dummy1pass");
047:
048: // create roles
049: Role roleAdmin = m_userDao
050: .getRoleByName(ProjectService.ROLE_MANAGER);
051: assertNotNull(roleAdmin);
052:
053: Role roleDeveloper = m_userDao
054: .getRoleByName(ProjectService.ROLE_DEVELOPER);
055: assertNotNull(roleDeveloper);
056:
057: Role roleUser = m_userDao
058: .getRoleByName(ProjectService.ROLE_USER);
059: assertNotNull(roleUser);
060:
061: User user1 = new User();
062: user1.setUsername("admin");
063: user1.setPassword(m_passwordEncoder.encodePassword("somepass",
064: ""));
065: user1.setEmail("dbuser@test.com");
066: user1.setFirstName("Test");
067: user1.setLastName("User");
068: user1.setActive(true);
069: m_userDao.saveUser(user1);
070:
071: User user2 = new User();
072: user2.setUsername("developer1");
073: user2.setPassword(m_passwordEncoder.encodePassword("somepass2",
074: ""));
075: user2.setEmail("dbuser2@test.com");
076: user2.setFirstName("Test2");
077: user2.setLastName("User");
078: user2.setManager(user1);
079: user2.setActive(true);
080: m_userDao.saveUser(user2);
081:
082: User user3 = new User();
083: user3.setUsername("developer2");
084: user3.setPassword(m_passwordEncoder.encodePassword("somepass2",
085: ""));
086: user3.setEmail("dbuser2@test.com");
087: user3.setFirstName("Test2");
088: user3.setLastName("User");
089: user3.setManager(user1);
090: user3.setActive(true);
091: m_userDao.saveUser(user3);
092:
093: //create projects
094: ProjectDO project1 = new ProjectDO();
095: project1.setName("Project1");
096: m_projectService.saveProject(project1);
097:
098: ProjectDO project2 = new ProjectDO();
099: project2.setName("Project2");
100: m_projectService.saveProject(project2);
101: }
102:
103: @Test
104: public void testProjectRoles() {
105: // load data
106: EmForgeUserDetails admin = m_userFactory.getUser("admin");
107: assertNotNull(admin);
108: EmForgeUserDetails developer1 = m_userFactory
109: .getUser("developer1");
110: assertNotNull(developer1);
111: EmForgeUserDetails developer2 = m_userFactory
112: .getUser("developer2");
113: assertNotNull(developer2);
114:
115: Role roleAdmin = m_userDao
116: .getRoleByName(ProjectService.ROLE_MANAGER);
117: assertNotNull(roleAdmin);
118: Role roleDeveloper = m_userDao
119: .getRoleByName(ProjectService.ROLE_DEVELOPER);
120: assertNotNull(roleDeveloper);
121: Role roleUser = m_userDao
122: .getRoleByName(ProjectService.ROLE_USER);
123: assertNotNull(roleUser);
124: Role roleTester = m_userDao
125: .getRoleByName(ProjectService.ROLE_TESTER);
126: assertNotNull(roleTester);
127:
128: ProjectDO project1 = m_projectService.getProject("Project1");
129: assertNotNull(project1);
130: ProjectDO project2 = m_projectService.getProject("Project2");
131: assertNotNull(project2);
132:
133: assertEquals(4, m_projectService.getAvialableRoles(project1)
134: .size());
135: assertEquals(4, m_projectService.getAvialableRoles(project2)
136: .size());
137:
138: // add users into projects
139: m_projectService.addUser(project1, admin, roleAdmin);
140: m_projectService.addUser(project1, developer1, roleDeveloper);
141: m_projectService.addUser(project1, developer2, roleDeveloper);
142:
143: m_projectService.addUser(project2, developer1, roleDeveloper);
144:
145: // now, check - project has specified roles
146: assertTrue(m_projectService.hasRole(project1, admin, roleAdmin));
147: assertTrue(m_projectService.hasRole(project1, developer1,
148: roleDeveloper));
149: assertTrue(m_projectService.hasRole(project1, developer2,
150: roleDeveloper));
151:
152: assertTrue(m_projectService.hasRole(project2, developer1,
153: roleDeveloper));
154:
155: // get roles list
156: // project has 2 admins: first - is dummy1 - who created project, second is adminUser added manually
157: assertEquals(2, m_projectService.getUsers(project1, roleAdmin)
158: .size());
159:
160: assertEquals(2, m_projectService.getUsers(project1,
161: roleDeveloper).size());
162:
163: Collection<EmForgeUserDetails> developers2 = m_projectService
164: .getUsers(project2, roleDeveloper);
165: assertEquals(1, developers2.size());
166:
167: assertEquals(0, m_projectService.getUsers(project1, roleUser)
168: .size());
169: assertEquals(0, m_projectService.getUsers(project1, roleTester)
170: .size());
171: assertEquals(1, m_projectService.getUsers(project2, roleAdmin)
172: .size());
173: assertEquals(0, m_projectService.getUsers(project2, roleUser)
174: .size());
175: assertEquals(0, m_projectService.getUsers(project2, roleTester)
176: .size());
177:
178: assertEquals("developer1", developers2.iterator().next()
179: .getUsername());
180:
181: // remove role
182: m_projectService.deleteUserRole(project1, admin, roleAdmin);
183: m_projectService.deleteUserRole(project1, developer1,
184: roleDeveloper);
185:
186: assertEquals(1, m_projectService.getUsers(project1, roleAdmin)
187: .size());
188:
189: Collection<EmForgeUserDetails> developers1 = m_projectService
190: .getUsers(project1, roleDeveloper);
191: assertEquals(1, developers1.size());
192: assertEquals("developer2", developers1.iterator().next()
193: .getUsername());
194: }
195:
196: }
|