use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryAuthorizationTest method createGroup.
/**
* Helper to create a group.
* @param name
* @return
* @throws DatastoreException
* @throws InvalidModelException
* @throws NotFoundException
*/
private UserGroup createGroup(String name) throws DatastoreException, InvalidModelException, NotFoundException {
UserGroup group = userGroupDAO.findGroup(name, false);
String id = null;
if (group == null) {
group = new UserGroup();
group.setName(name);
group.setIsIndividual(false);
id = userGroupDAO.create(group);
} else {
id = group.getId();
}
groupsToDelete.add(id);
return userGroupDAO.get(id);
}
use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryAuthorizationTest method createUser.
/**
* Helper for creating a new user.
* @param name
* @param isAdmin
* @return
* @throws DatastoreException
* @throws InvalidModelException
* @throws NotFoundException
*/
private UserInfo createUser(String name, boolean isAdmin) throws DatastoreException, InvalidModelException, NotFoundException {
User user = new User();
user.setUserId(name);
// Create a group for this user
String userGroupName = name + "group";
// new UserGroup();
UserGroup group = userGroupDAO.findGroup(userGroupName, true);
String id = null;
if (group == null) {
group = new UserGroup();
group.setName(userGroupName);
group.setIsIndividual(true);
id = userGroupDAO.create(group);
} else {
id = group.getId();
}
groupsToDelete.add(id);
group = userGroupDAO.get(id);
UserInfo info = new UserInfo(isAdmin);
info.setUser(user);
info.setIndividualGroup(group);
info.setGroups(new ArrayList<UserGroup>());
info.getGroups().add(group);
return info;
}
use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryUnitTest method testAuthorizationSqlNonAdminWithGroups.
@Test
public void testAuthorizationSqlNonAdminWithGroups() throws Exception {
HashMap<String, Object> params = new HashMap<String, Object>();
UserInfo nonAdminUserInfo = Mockito.mock(UserInfo.class);
when(nonAdminUserInfo.isAdmin()).thenReturn(false);
ArrayList<UserGroup> groups = new ArrayList<UserGroup>();
UserGroup group = Mockito.mock(UserGroup.class);
when(group.getId()).thenReturn("123");
groups.add(group);
group = Mockito.mock(UserGroup.class);
when(group.getId()).thenReturn("124");
groups.add(group);
when(nonAdminUserInfo.getGroups()).thenReturn(groups);
// This should build a query with two groups
String sql = QueryUtils.buildAuthorizationFilter(nonAdminUserInfo, params);
assertNotNull(sql);
// It should not be an empty string.
assertFalse("".equals(sql.trim()));
log.info(sql);
log.info(params);
// Check the bind variables.
assertEquals(ACCESS_TYPE.READ.name(), params.get(AuthorizationSqlUtil.ACCESS_TYPE_BIND_VAR));
Long groupBindValue0 = (Long) params.get(AuthorizationSqlUtil.BIND_VAR_PREFIX + "0");
assertNotNull(groupBindValue0);
Long groupBindValue1 = (Long) params.get(AuthorizationSqlUtil.BIND_VAR_PREFIX + "1");
assertNotNull(groupBindValue1);
assertTrue(123L == groupBindValue0.longValue() || 123L == groupBindValue1.longValue());
assertTrue(124L == groupBindValue0.longValue() || 124L == groupBindValue1.longValue());
System.out.print(sql);
}
use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryUnitTest method testAuthorizationSqlNonAdminuserEmptyGroups.
@Test(expected = IllegalArgumentException.class)
public void testAuthorizationSqlNonAdminuserEmptyGroups() throws Exception {
UserInfo nonAdminUserInfo = Mockito.mock(UserInfo.class);
when(nonAdminUserInfo.isAdmin()).thenReturn(false);
when(nonAdminUserInfo.getGroups()).thenReturn(new ArrayList<UserGroup>());
HashMap<String, Object> params = new HashMap<String, Object>();
// Should throw an exception.
String sql = QueryUtils.buildAuthorizationFilter(nonAdminUserInfo, params);
}
use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.
the class UserGroupCacheImplTest method testCache.
@Test
public void testCache() throws DatastoreException, NotFoundException {
assertNotNull(userGroupCache);
// Look up even object using the ID and Odd using the name
for (int i = 0; i < userGroups.size(); i++) {
UserGroup ug = userGroups.get(i);
Long expectedId = KeyFactory.stringToKey(ug.getId());
if (i % 2 == 0) {
Long id = userGroupCache.getIdForUserGroupName(ug.getName());
assertNotNull(id);
assertEquals(id, expectedId);
// Try the other way
String name = userGroupCache.getUserGroupNameForId(expectedId);
assertNotNull(name);
assertEquals(ug.getName(), name);
} else {
String name = userGroupCache.getUserGroupNameForId(expectedId);
assertNotNull(name);
assertEquals(ug.getName(), name);
// try the other way
Long id = userGroupCache.getIdForUserGroupName(ug.getName());
assertNotNull(id);
assertEquals(id, expectedId);
}
}
}
Aggregations