use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testProcessDomainWithUpdateNullTags.
@Test
public void testProcessDomainWithUpdateNullTags() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
ObjectStore savedStore = zms.dbService.store;
Map<String, TagValueList> domainTags = new HashMap<>();
domainTags.put("tagToBeRemoved", new TagValueList().setList(Collections.singletonList("val0")));
domainTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
Domain domain = new Domain().setName("newDomain").setTags(domainTags);
Mockito.when(conn.insertDomain(domain)).thenReturn(true);
Mockito.when(conn.insertDomainTags("newDomain", domainTags)).thenReturn(true);
Mockito.when(conn.insertRole(anyString(), any(Role.class))).thenReturn(true);
Mockito.when(conn.insertRoleMember(any(), any(), any(), any(), any())).thenReturn(true);
Mockito.when(conn.insertPolicy(any(), any())).thenReturn(true);
Mockito.when(conn.insertAssertion(any(), any(), any(), any())).thenReturn(true);
Mockito.when(mockObjStore.getConnection(false, true)).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn);
zms.dbService.store = mockObjStore;
Domain createdDomain = zms.dbService.makeDomain(mockDomRsrcCtx, domain, Collections.singletonList(adminUser), null, auditRef);
assertEquals(createdDomain.getTags(), domainTags);
Mockito.when(conn.updateDomain(any(Domain.class))).thenReturn(true);
Mockito.when(conn.deleteDomainTags(anyString(), anySet())).thenReturn(true);
Mockito.when(conn.insertDomainTags(anyString(), anyMap())).thenReturn(true);
Mockito.when(conn.getDomain("newDomain")).thenReturn(domain);
Mockito.when(mockObjStore.getConnection(false, true)).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn);
// update domain meta
DomainMeta meta = new DomainMeta().setTags(null);
zms.dbService.executePutDomainMeta(mockDomRsrcCtx, domain, meta, null, false, auditRef, "putDomainMeta");
assertEquals(createdDomain.getTags(), domainTags);
zms.dbService.store = savedStore;
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testRoleSameTagKeyValues.
@Test
public void testRoleSameTagKeyValues() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Map<String, TagValueList> roleTags = Collections.singletonMap("tagKey", new TagValueList().setList(Collections.singletonList("tagVal")));
Role role = new Role().setName("role").setTags(roleTags);
Mockito.when(conn.insertRole(anyString(), any())).thenReturn(true);
Mockito.when(conn.insertRoleTags(anyString(), anyString(), any())).thenReturn(true);
StringBuilder auditDetails = new StringBuilder("testAudit");
boolean success = zms.dbService.processRole(conn, null, "sys.auth", "newRole", role, adminUser, auditRef, false, auditDetails);
assertTrue(success);
// process the same role again with the same tags
Role newRole = new Role().setName("role").setTags(roleTags);
Mockito.when(conn.updateRole("sys.auth", newRole)).thenReturn(true);
Mockito.when(conn.deleteRoleTags(anyString(), anyString(), anySet())).thenReturn(true);
Mockito.when(conn.insertRoleTags(anyString(), anyString(), anyMap())).thenReturn(true);
success = zms.dbService.processRole(conn, role, "sys.auth", "newRole", newRole, adminUser, auditRef, false, auditDetails);
assertTrue(success);
// assert tags to remove should be empty
ArgumentCaptor<Set<String>> tagCapture = ArgumentCaptor.forClass(Set.class);
ArgumentCaptor<String> roleCapture = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> domainCapture = ArgumentCaptor.forClass(String.class);
Mockito.verify(conn, times(1)).deleteRoleTags(roleCapture.capture(), domainCapture.capture(), tagCapture.capture());
assertEquals("newRole", roleCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
assertTrue(tagCapture.getValue().isEmpty());
// assert tags to add should be empty
ArgumentCaptor<Map<String, TagValueList>> tagInsertCapture = ArgumentCaptor.forClass(Map.class);
Mockito.verify(conn, times(2)).insertRoleTags(roleCapture.capture(), domainCapture.capture(), tagInsertCapture.capture());
assertEquals("newRole", roleCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
Map<String, TagValueList> resultInsertTags = tagInsertCapture.getAllValues().get(1);
assertTrue(resultInsertTags.isEmpty());
// asert first tag insertion
Map<String, TagValueList> resultFirstInsertTags = tagInsertCapture.getAllValues().get(0);
assertTrue(resultFirstInsertTags.containsKey("tagKey"));
assertTrue(resultFirstInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).contains("tagVal"));
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testRemovePrincipalFromAllRolesExceptions.
@Test
public void testRemovePrincipalFromAllRolesExceptions() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(conn.getPrincipalRoles("user.joe", null)).thenThrow(new ResourceException(404)).thenThrow(new ResourceException(501));
// no exception if store returns 404
zms.dbService.removePrincipalFromAllRoles(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
try {
zms.dbService.removePrincipalFromAllRoles(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
fail();
} catch (ResourceException ex) {
assertEquals(501, ex.getCode());
}
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testUpdateDomainMembersExpirationFailure.
@Test
public void testUpdateDomainMembersExpirationFailure() {
final String domainName = "expiration-failure";
Domain domain = new Domain().setName(domainName).setMemberExpiryDays(100).setModified(Timestamp.fromCurrentTime());
Domain updateDomain = new Domain().setName(domainName).setMemberExpiryDays(50);
ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
// we're going to make sure to throw an exception here
// since this should never be called
Mockito.when(mockConn.getAthenzDomain(domainName)).thenThrow(new ResourceException(400));
Authority savedAuthority = zms.dbService.zmsConfig.getUserAuthority();
Authority authority = Mockito.mock(Authority.class);
zms.dbService.zmsConfig.setUserAuthority(authority);
zms.dbService.updateDomainMembersExpiration(mockDomRsrcCtx, mockConn, domain, updateDomain, auditRef, "testUpdateMdomainMembersExpirationFailure");
zms.dbService.zmsConfig.setUserAuthority(savedAuthority);
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testRemovePrincipalFromAllGroupsDeleteUserException.
@Test
public void testRemovePrincipalFromAllGroupsDeleteUserException() {
DomainGroupMember roles = new DomainGroupMember();
roles.setMemberGroups(new ArrayList<>());
GroupMember group1 = new GroupMember();
group1.setDomainName("dom1");
group1.setGroupName("group1");
roles.getMemberGroups().add(group1);
GroupMember group2 = new GroupMember();
group2.setDomainName("dom1");
group2.setGroupName("group2");
roles.getMemberGroups().add(group2);
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(conn.getPrincipalGroups("user.joe", null)).thenReturn(roles);
Mockito.when(conn.deleteGroupMember("dom1", "group1", "user.joe", adminUser, "unittest")).thenReturn(true);
Mockito.when(conn.deleteGroupMember("dom1", "group2", "user.joe", adminUser, "unittest")).thenThrow(new ResourceException(501));
// we should handle the exception without any errors
zms.dbService.removePrincipalFromAllGroups(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
}
Aggregations