use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class AttributeRepositorySecurityDecoratorTest method countQueryUser.
@WithMockUser(username = USERNAME)
@Test
public void countQueryUser() throws Exception {
String entityType0Name = "entity0";
EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
String entityType1Name = "entity1";
EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn(entityType1Name).getMock();
String attr0Name = "entity0attr0";
Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
when(attr0.getEntity()).thenReturn(entityType0);
String attr1Name = "entity1attr0";
Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn(attr1Name).getMock();
when(attr1.getEntity()).thenReturn(entityType1);
Query<Attribute> q = new QueryImpl<>();
@SuppressWarnings("unchecked") ArgumentCaptor<Query<Attribute>> queryCaptor = forClass(Query.class);
when(delegateRepository.findAll(queryCaptor.capture())).thenReturn(Stream.of(attr0, attr1));
when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
when(permissionService.hasPermission(new EntityTypeIdentity(entityType1Name), EntityTypePermission.COUNT)).thenReturn(true);
assertEquals(repo.count(q), 1L);
assertEquals(queryCaptor.getValue().getOffset(), 0);
assertEquals(queryCaptor.getValue().getPageSize(), Integer.MAX_VALUE);
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class AttributeRepositorySecurityDecoratorTest method forEachBatchedUser.
@WithMockUser(username = USERNAME)
@Test
public void forEachBatchedUser() throws Exception {
List<Attribute> attributes = newArrayList();
Attribute attribute1 = mock(Attribute.class);
Attribute attribute2 = mock(Attribute.class);
Attribute attribute3 = mock(Attribute.class);
Attribute attribute4 = mock(Attribute.class);
EntityType entityType1 = mock(EntityType.class);
EntityType entityType2 = mock(EntityType.class);
EntityType entityType3 = mock(EntityType.class);
EntityType entityType4 = mock(EntityType.class);
when(attribute1.getEntity()).thenReturn(entityType1);
when(attribute2.getEntity()).thenReturn(entityType2);
when(attribute3.getEntity()).thenReturn(entityType3);
when(attribute4.getEntity()).thenReturn(entityType4);
when(entityType1.getId()).thenReturn("EntityType1");
when(entityType2.getId()).thenReturn("EntityType2");
when(entityType3.getId()).thenReturn("EntityType3");
when(entityType4.getId()).thenReturn("EntityType4");
repo.forEachBatched(attributes::addAll, 2);
when(permissionService.hasPermission(new EntityTypeIdentity("EntityType1"), EntityTypePermission.COUNT)).thenReturn(true);
when(permissionService.hasPermission(new EntityTypeIdentity("EntityType2"), EntityTypePermission.COUNT)).thenReturn(false);
when(permissionService.hasPermission(new EntityTypeIdentity("EntityType3"), EntityTypePermission.COUNT)).thenReturn(false);
when(permissionService.hasPermission(new EntityTypeIdentity("EntityType4"), EntityTypePermission.COUNT)).thenReturn(true);
// Decorated repo returns two batches of two entityTypes
verify(delegateRepository).forEachBatched(eq(null), consumerCaptor.capture(), eq(2));
consumerCaptor.getValue().accept(Lists.newArrayList(attribute1, attribute2));
consumerCaptor.getValue().accept(Lists.newArrayList(attribute3, attribute4));
assertEquals(attributes, newArrayList(attribute1, attribute4));
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class AttributeRepositorySecurityDecoratorTest method countUser.
@WithMockUser(username = USERNAME)
@Test
public void countUser() throws Exception {
String entityType0Name = "entity0";
EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
String entityType1Name = "entity1";
EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn(entityType1Name).getMock();
String attr0Name = "entity0attr0";
Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
when(attr0.getEntity()).thenReturn(entityType0);
String attr1Name = "entity1attr0";
Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn(attr1Name).getMock();
when(attr1.getEntity()).thenReturn(entityType1);
when(delegateRepository.spliterator()).thenReturn(asList(attr0, attr1).spliterator());
when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
when(permissionService.hasPermission(new EntityTypeIdentity(entityType1Name), EntityTypePermission.COUNT)).thenReturn(true);
assertEquals(repo.count(), 1L);
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class RestControllerTest method retrieveEntityTypeWritable.
@Test
public void retrieveEntityTypeWritable() throws Exception {
when(permissionService.hasPermission(new EntityTypeIdentity(ENTITY_NAME), EntityTypePermission.WRITE)).thenReturn(true);
when(dataService.getCapabilities(ENTITY_NAME)).thenReturn(new HashSet<>(singletonList(RepositoryCapability.WRITABLE)));
mockMvc.perform(get(HREF_ENTITY_META)).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(content().json("{\"href\":\"" + HREF_ENTITY_META + "\",\"hrefCollection\":\"/api/v1/Person\",\"name\":\"" + ENTITY_NAME + "\",\"attributes\":{\"name\":{\"href\":\"" + HREF_ENTITY_META + "/name\"},\"id\":{\"href\":\"/api/v1/Person/meta/id\"},\"enum\":{\"href\":\"/api/v1/Person/meta/enum\"},\"int\":{\"href\":\"/api/v1/Person/meta/int\"}},\"idAttribute\":\"id\",\"isAbstract\":false,\"languageCode\":\"en\",\"writable\":true}"));
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class RestControllerV2 method copyEntity.
/**
* Copy an entity.
*
* @param entityTypeId name of the entity that will be copied.
* @param request CopyEntityRequestV2
* @param response HttpServletResponse
* @return String name of the new entity
*/
@Transactional
@PostMapping(value = "copy/{entityTypeId}", produces = APPLICATION_JSON_VALUE)
public String copyEntity(@PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid CopyEntityRequestV2 request, HttpServletResponse response) throws Exception {
// No repo
if (!dataService.hasRepository(entityTypeId))
throw createUnknownEntityException(entityTypeId);
Repository<Entity> repositoryToCopyFrom = dataService.getRepository(entityTypeId);
// Validate the new name
NameValidator.validateEntityName(request.getNewEntityName());
// Check if the entity already exists
String newFullName = EntityTypeUtils.buildFullName(repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
if (dataService.hasRepository(newFullName))
throw createDuplicateEntityException(newFullName);
// Permission
boolean readPermission = permissionService.hasPermission(new EntityTypeIdentity(repositoryToCopyFrom.getName()), EntityTypePermission.READ);
if (!readPermission)
throw createNoReadPermissionOnEntityException(entityTypeId);
// Capabilities
boolean writableCapabilities = dataService.getCapabilities(repositoryToCopyFrom.getName()).contains(RepositoryCapability.WRITABLE);
if (!writableCapabilities)
throw createNoWriteCapabilitiesOnEntityException(entityTypeId);
// Copy
Repository<Entity> repository = this.copyRepositoryRunAsSystem(repositoryToCopyFrom, request.getNewEntityName(), repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
// Retrieve new repo
permissionSystemService.giveUserWriteMetaPermissions(repository.getEntityType());
response.addHeader("Location", Href.concatMetaEntityHrefV2(RestControllerV2.BASE_URI, repository.getName()));
response.setStatus(HttpServletResponse.SC_CREATED);
return repository.getName();
}
Aggregations