use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.
the class JDBCConnectionTest method testUpdateQuotaException.
@Test
public void testUpdateQuotaException() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Quota quota = new Quota().setName("athenz").setAssertion(10).setEntity(11).setPolicy(12).setPublicKey(13).setRole(14).setRoleMember(15).setService(16).setServiceHost(17).setSubdomain(18);
Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.doReturn(5).when(mockResultSet).getInt(1);
try {
jdbcConn.updateQuota("athenz", quota);
fail();
} catch (Exception ex) {
assertTrue(true);
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.
the class JDBCConnectionTest method testDeletePrincipalDomainFailure.
@Test
public void testDeletePrincipalDomainFailure() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
// domain delete is failure, but sub-domain is success
// thus the result must be successful
Mockito.when(mockPrepStmt.executeUpdate()).thenReturn(0).thenReturn(1);
Mockito.when(mockResultSet.next()).thenReturn(true);
boolean requestSuccess = jdbcConn.deletePrincipal("user.jake", true);
assertTrue(requestSuccess);
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.jake");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.jake.%");
jdbcConn.close();
}
use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.
the class JDBCConnectionTest method testListResourceAccessAws.
@Test
public void testListResourceAccessAws() throws SQLException {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(// up to here is role principals
false).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(// up to here is role assertions
false).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(// up to here standard trusted roles
false).thenReturn(// up to here wildcard trusted roles
false).thenReturn(true).thenReturn(true).thenReturn(// up to here is aws domains
false);
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_NAME)).thenReturn("user.user1").thenReturn("user.user2").thenReturn(// up to here is role principals
"user.user3.service").thenReturn("dom1").thenReturn("dom2").thenReturn(// up to here is role assertions
"dom3").thenReturn("trole1").thenReturn("trole2").thenReturn(// up to here trusted roles
"trole3").thenReturn("dom1").thenReturn("dom2");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_DOMAIN_ID)).thenReturn("101").thenReturn("102").thenReturn(// up to here is role principals
"103").thenReturn("101").thenReturn("102").thenReturn(// up to here role assertions
"103").thenReturn("101").thenReturn("102").thenReturn(// up to here trusted roles
"103");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ROLE_NAME)).thenReturn("role1").thenReturn("role2").thenReturn("role3");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ROLE)).thenReturn("role1").thenReturn("role2").thenReturn(// up to here role assertions
"role3").thenReturn("role1").thenReturn("role2").thenReturn(// up to here trusted roles
"role3");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_RESOURCE)).thenReturn("dom1:role1").thenReturn("dom2:role2").thenReturn("resource3");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ACTION)).thenReturn("assume_aws_role");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_EFFECT)).thenReturn("ALLOW");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ACCOUNT)).thenReturn("12345").thenReturn("12346");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ASSERT_DOMAIN_ID)).thenReturn("101").thenReturn("102").thenReturn("103");
ResourceAccessList resourceAccessList = jdbcConn.listResourceAccess(null, "assume_aws_role", "user");
List<ResourceAccess> resources = resourceAccessList.getResources();
assertEquals(2, resources.size());
boolean userUser1 = false;
boolean userUser2 = false;
// must be skipped
boolean userUser3 = false;
for (ResourceAccess rsrcAccess : resources) {
switch(rsrcAccess.getPrincipal()) {
case "user.user1":
userUser1 = true;
assertEquals(1, rsrcAccess.getAssertions().size());
assertEquals("arn:aws:iam::12345:role/role1", rsrcAccess.getAssertions().get(0).getResource());
break;
case "user.user2":
userUser2 = true;
assertEquals(1, rsrcAccess.getAssertions().size());
assertEquals("arn:aws:iam::12346:role/role2", rsrcAccess.getAssertions().get(0).getResource());
break;
case "user.user3.service":
userUser3 = true;
break;
}
}
assertTrue(userUser1);
assertTrue(userUser2);
assertFalse(userUser3);
jdbcConn.close();
}
use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.
the class JDBCConnectionTest method testDeletePrincipalDomainException.
@Test
public void testDeletePrincipalDomainException() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
try {
jdbcConn.deletePrincipal("user.jake", true);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), ResourceException.INTERNAL_SERVER_ERROR);
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.
the class JDBCConnectionTest method testListPrincipalRoles.
@Test
public void testListPrincipalRoles() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// principal id
5);
// principal roles
Mockito.when(mockResultSet.next()).thenReturn(// get principal id
true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_NAME)).thenReturn("coretech").thenReturn("sports").thenReturn("sports").thenReturn("weather");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ROLE_NAME)).thenReturn("admin").thenReturn("reader").thenReturn("writer").thenReturn("reader");
List<PrincipalRole> roles = jdbcConn.listPrincipalRoles("user.joe");
assertEquals(4, roles.size());
// get principal id
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.joe");
// get role list
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
boolean coretech_admin = false;
boolean sports_reader = false;
boolean sports_writer = false;
boolean weather_reader = false;
for (PrincipalRole role : roles) {
if (role.getDomainName().equals("coretech") && role.getRoleName().equals("admin")) {
coretech_admin = true;
} else if (role.getDomainName().equals("sports") && role.getRoleName().equals("reader")) {
sports_reader = true;
} else if (role.getDomainName().equals("sports") && role.getRoleName().equals("writer")) {
sports_writer = true;
} else if (role.getDomainName().equals("weather") && role.getRoleName().equals("reader")) {
weather_reader = true;
}
}
assertTrue(coretech_admin);
assertTrue(sports_reader);
assertTrue(sports_writer);
assertTrue(weather_reader);
jdbcConn.close();
}
Aggregations