Guest User

Untitled

a guest
Aug 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. package com.seagate.rd.auth;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.beans.PropertyVetoException;
  6. import java.util.List;
  7.  
  8. import javax.naming.AuthenticationException;
  9. import javax.sql.DataSource;
  10.  
  11. import org.junit.AfterClass;
  12. import org.junit.Before;
  13. import org.junit.BeforeClass;
  14. import org.junit.Test;
  15.  
  16. import com.mchange.v2.c3p0.ComboPooledDataSource;
  17. import com.seagate.rd.auth.AuthService;
  18. import com.seagate.rd.auth.Permission;
  19. import com.seagate.rd.auth.Role;
  20. import com.seagate.rd.auth.UserAttribute;
  21.  
  22. public class AuthServiceTest {
  23.  
  24. private AuthService service;
  25.  
  26. private static ComboPooledDataSource ds;
  27.  
  28. @BeforeClass
  29. public static void init() throws PropertyVetoException {
  30. ds = new ComboPooledDataSource();
  31. ds.setJdbcUrl( "jdbc:oracle:thin:@nrmlnhd1.nrm.minn.seagate.com:1521:nrmlnhd1" );
  32. ds.setUser("hismes");
  33. ds.setPassword("Seagate123");
  34. }
  35.  
  36. @AfterClass
  37. public static void shutDown() {
  38. ds.close();
  39. }
  40.  
  41. @Before
  42. public void setUp() {
  43.  
  44. service = new AuthService() {
  45.  
  46. @Override
  47. public DataSource getDataSource(String jndiName) {
  48. return ds;
  49. }
  50. };
  51. }
  52.  
  53. @Test(expected=AuthenticationException.class)
  54. public void testLdapAuthentication() throws Exception
  55. {
  56. boolean ok = service.ldapAuthenticate("502380", "password"); // don't put your real password
  57.  
  58. assertTrue(ok);
  59. }
  60.  
  61. @Test
  62. public void testGetPropertyFromDB()
  63. {
  64. String url = service.getPropertyFromDB("LDAP_PROVIDER_URL");
  65.  
  66. assertEquals("ldaps://ldap.seagate.com:636", url);
  67. }
  68.  
  69.  
  70. @Test
  71. public void testAuthenticateWithDBDefaultPassword()
  72. {
  73. boolean ok = service.authenticateWithDB("17093", "seagate");
  74.  
  75. assertTrue(ok);
  76. }
  77.  
  78.  
  79. @Test
  80. public void testAuthenticateWithDB()
  81. {
  82. boolean ok = service.authenticateWithDB("502380", "nopassword");
  83.  
  84. assertFalse(ok);
  85. }
  86.  
  87. @Test
  88. public void testGetUserName()
  89. {
  90. String name = service.getUserName("17093");
  91.  
  92. assertEquals("Jarunee Saksung", name);
  93. }
  94.  
  95. @Test
  96. public void testGetRoleAndPermissionGUEST()
  97. {
  98. List<Role> list = service.getRoleAndPermission("guest");
  99.  
  100. assertFalse(list.isEmpty());
  101. assertEquals(10, list.get(0).getPermissionList().size());
  102.  
  103. for (Role role : list) {
  104. for (Permission p : role.getPermissionList()) {
  105.  
  106. if(p.getPermissionName().equals("VIEW_BO")) {
  107.  
  108. String attr = p.getPermissionScopeAttributeNameList().get(0);
  109.  
  110. assertEquals("FACTORY", attr);
  111. }
  112. }
  113. }
  114. }
  115.  
  116. @Test
  117. public void testGetRoleAndPermission()
  118. {
  119. List<Role> list = service.getRoleAndPermission("502380");
  120.  
  121. assertFalse(list.isEmpty());
  122. assertEquals(3, list.size());
  123. }
  124.  
  125. @Test
  126. public void testGetSuperUserAttributes()
  127. {
  128. List<UserAttribute> list = service.getSuperUserAttributes();
  129.  
  130. assertFalse(list.isEmpty());
  131. assertEquals(1, list.size());
  132. assertEquals("ALL", list.get(0).getUserAttributeName());
  133. }
  134.  
  135. @Test
  136. public void testGetNormalUserAttributes()
  137. {
  138. List<UserAttribute> list = service.getUserAttributes("7468");
  139.  
  140. assertFalse(list.isEmpty());
  141. assertEquals(3, list.size());
  142. assertEquals("FACTORY", list.get(0).getUserAttributeName());
  143. }
  144.  
  145. @Test
  146. public void testSHA()
  147. {
  148. String hash = service.sha("password");
  149.  
  150. assertEquals("Gn%�K`SOh����yPS", hash);
  151. }
  152.  
  153. @Test
  154. public void testSetUserRolesAndAttributesSuperUser()
  155. {
  156. User user = new User();
  157. user.setGlobalId("502380");
  158.  
  159. service.setUserRolesAndAttributes(user);
  160.  
  161. assertTrue(user.isSuperUser());
  162. }
  163.  
  164. @Test
  165. public void testSetUserRolesAndAttributes()
  166. {
  167. User user = new User();
  168. user.setGlobalId("7468");
  169.  
  170. service.setUserRolesAndAttributes(user);
  171.  
  172. assertFalse(user.isSuperUser());
  173. assertEquals("PLANNER", user.getRoleList().get(0).getRoleName());
  174. }
  175.  
  176. @Test
  177. public void testAuthenticationInDB() throws Exception
  178. {
  179. service.setLdapUrl("ldaps://ldap.seagate.com:63600");
  180.  
  181. User user = service.authenticate("309940", "seagate");
  182.  
  183. assertFalse(user.isSuperUser());
  184. assertEquals("CYCLETIME", user.getRoleList().get(0).getRoleName());
  185. }
  186.  
  187. @Test(expected=AuthenticationException.class)
  188. public void testAuthentication() throws Exception
  189. {
  190. User user = service.authenticate("502380", "x"); // do we got any test user ?
  191.  
  192. assertTrue(user.isSuperUser());
  193. }
  194.  
  195.  
  196. }
Add Comment
Please, Sign In to add comment