Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.seagate.rd.auth;
- import static org.junit.Assert.*;
- import java.beans.PropertyVetoException;
- import java.util.List;
- import javax.naming.AuthenticationException;
- import javax.sql.DataSource;
- import org.junit.AfterClass;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- import com.seagate.rd.auth.AuthService;
- import com.seagate.rd.auth.Permission;
- import com.seagate.rd.auth.Role;
- import com.seagate.rd.auth.UserAttribute;
- public class AuthServiceTest {
- private AuthService service;
- private static ComboPooledDataSource ds;
- @BeforeClass
- public static void init() throws PropertyVetoException {
- ds = new ComboPooledDataSource();
- ds.setJdbcUrl( "jdbc:oracle:thin:@nrmlnhd1.nrm.minn.seagate.com:1521:nrmlnhd1" );
- ds.setUser("hismes");
- ds.setPassword("Seagate123");
- }
- @AfterClass
- public static void shutDown() {
- ds.close();
- }
- @Before
- public void setUp() {
- service = new AuthService() {
- @Override
- public DataSource getDataSource(String jndiName) {
- return ds;
- }
- };
- }
- @Test(expected=AuthenticationException.class)
- public void testLdapAuthentication() throws Exception
- {
- boolean ok = service.ldapAuthenticate("502380", "password"); // don't put your real password
- assertTrue(ok);
- }
- @Test
- public void testGetPropertyFromDB()
- {
- String url = service.getPropertyFromDB("LDAP_PROVIDER_URL");
- assertEquals("ldaps://ldap.seagate.com:636", url);
- }
- @Test
- public void testAuthenticateWithDBDefaultPassword()
- {
- boolean ok = service.authenticateWithDB("17093", "seagate");
- assertTrue(ok);
- }
- @Test
- public void testAuthenticateWithDB()
- {
- boolean ok = service.authenticateWithDB("502380", "nopassword");
- assertFalse(ok);
- }
- @Test
- public void testGetUserName()
- {
- String name = service.getUserName("17093");
- assertEquals("Jarunee Saksung", name);
- }
- @Test
- public void testGetRoleAndPermissionGUEST()
- {
- List<Role> list = service.getRoleAndPermission("guest");
- assertFalse(list.isEmpty());
- assertEquals(10, list.get(0).getPermissionList().size());
- for (Role role : list) {
- for (Permission p : role.getPermissionList()) {
- if(p.getPermissionName().equals("VIEW_BO")) {
- String attr = p.getPermissionScopeAttributeNameList().get(0);
- assertEquals("FACTORY", attr);
- }
- }
- }
- }
- @Test
- public void testGetRoleAndPermission()
- {
- List<Role> list = service.getRoleAndPermission("502380");
- assertFalse(list.isEmpty());
- assertEquals(3, list.size());
- }
- @Test
- public void testGetSuperUserAttributes()
- {
- List<UserAttribute> list = service.getSuperUserAttributes();
- assertFalse(list.isEmpty());
- assertEquals(1, list.size());
- assertEquals("ALL", list.get(0).getUserAttributeName());
- }
- @Test
- public void testGetNormalUserAttributes()
- {
- List<UserAttribute> list = service.getUserAttributes("7468");
- assertFalse(list.isEmpty());
- assertEquals(3, list.size());
- assertEquals("FACTORY", list.get(0).getUserAttributeName());
- }
- @Test
- public void testSHA()
- {
- String hash = service.sha("password");
- assertEquals("Gn%�K`SOh����yPS", hash);
- }
- @Test
- public void testSetUserRolesAndAttributesSuperUser()
- {
- User user = new User();
- user.setGlobalId("502380");
- service.setUserRolesAndAttributes(user);
- assertTrue(user.isSuperUser());
- }
- @Test
- public void testSetUserRolesAndAttributes()
- {
- User user = new User();
- user.setGlobalId("7468");
- service.setUserRolesAndAttributes(user);
- assertFalse(user.isSuperUser());
- assertEquals("PLANNER", user.getRoleList().get(0).getRoleName());
- }
- @Test
- public void testAuthenticationInDB() throws Exception
- {
- service.setLdapUrl("ldaps://ldap.seagate.com:63600");
- User user = service.authenticate("309940", "seagate");
- assertFalse(user.isSuperUser());
- assertEquals("CYCLETIME", user.getRoleList().get(0).getRoleName());
- }
- @Test(expected=AuthenticationException.class)
- public void testAuthentication() throws Exception
- {
- User user = service.authenticate("502380", "x"); // do we got any test user ?
- assertTrue(user.isSuperUser());
- }
- }
Add Comment
Please, Sign In to add comment