use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class QuestionnaireServiceTest method testGetQuestionnairesWithNoExistingRow.
@Test
public void testGetQuestionnairesWithNoExistingRow() {
// =========== Setup ===========
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn(QUESTIONNAIRE_ID);
when(entityType.getLabel()).thenReturn("label");
when(entityType.getDescription()).thenReturn("description");
Query<EntityType> typedQuery = mock(Query.class);
Query<EntityType> query = mock(Query.class);
when(typedQuery.eq(EntityTypeMetadata.EXTENDS, QUESTIONNAIRE)).thenReturn(query);
when(dataService.query(ENTITY_TYPE_META_DATA, EntityType.class)).thenReturn(typedQuery);
when(query.findAll()).thenReturn(Stream.of(entityType));
when(userPermissionEvaluator.hasPermission(new EntityTypeIdentity(QUESTIONNAIRE_ID), WRITE)).thenReturn(true);
Entity entity = null;
when(dataService.findOne(QUESTIONNAIRE_ID, EQ(OWNER_USERNAME, null))).thenReturn(entity);
when(questionnaireFactory.create(entity)).thenReturn(null);
// =========== Test ===========
List<QuestionnaireResponse> actual = questionnaireService.getQuestionnaires();
QuestionnaireResponse questionnaireResponse = QuestionnaireResponse.create(QUESTIONNAIRE_ID, "label", "description", NOT_STARTED);
List<QuestionnaireResponse> expected = newArrayList(questionnaireResponse);
assertEquals(actual, expected);
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class RestControllerV2Test method mocksForCopyEntitySuccess.
private Package mocksForCopyEntitySuccess(Repository<Entity> repositoryToCopy) {
Package pack = mock(Package.class);
when(pack.getId()).thenReturn("org_molgenis_blah");
when(dataService.hasRepository("entity")).thenReturn(true);
when(dataService.hasRepository("org_molgenis_blah_duplicateEntity")).thenReturn(true);
when(dataService.hasRepository("org_molgenis_blah_newEntity")).thenReturn(false);
when(dataService.getRepository("entity")).thenReturn(repositoryToCopy);
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("entityTypeId");
when(repositoryToCopy.getEntityType()).thenReturn(entityType);
when(entityType.getPackage()).thenReturn(pack);
when(repositoryToCopy.getName()).thenReturn("entity");
when(permissionService.hasPermission(new EntityTypeIdentity("entity"), EntityTypePermission.READ)).thenReturn(true);
Set<RepositoryCapability> capabilities = Sets.newHashSet(RepositoryCapability.WRITABLE);
when(dataService.getCapabilities("entity")).thenReturn(capabilities);
@SuppressWarnings("unchecked") Repository<Entity> repository = mock(Repository.class);
when(repository.getName()).thenReturn("org_molgenis_blah_newEntity");
when(dataService.getRepository("org_molgenis_blah_newEntity")).thenReturn(repository);
when(repoCopier.copyRepository(repositoryToCopy, "newEntity", pack, "newEntity")).thenReturn(repository);
doNothing().when(permissionSystemService).giveUserWriteMetaPermissions(any(EntityType.class));
return pack;
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class RestControllerV2Test method testCopyEntityNoReadPermissions.
@Test
public void testCopyEntityNoReadPermissions() throws Exception {
@SuppressWarnings("unchecked") Repository<Entity> repositoryToCopy = mock(Repository.class);
mocksForCopyEntitySuccess(repositoryToCopy);
// Override mock
when(permissionService.hasPermission(new EntityTypeIdentity("entity"), EntityTypePermission.READ)).thenReturn(false);
String content = "{newEntityName: 'newEntity'}";
mockMvc.perform(post(HREF_COPY_ENTITY).content(content).contentType(APPLICATION_JSON)).andExpect(status().isUnauthorized()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(status().isUnauthorized()).andExpect(jsonPath(FIRST_ERROR_MESSAGE, is("No read permission on entity entity")));
verifyZeroInteractions(repoCopier);
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class RestControllerTest method retrieveEntityTypeNotWritable.
@Test
public void retrieveEntityTypeNotWritable() throws Exception {
when(permissionService.hasPermission(new EntityTypeIdentity(ENTITY_NAME), EntityTypePermission.WRITE)).thenReturn(true);
when(dataService.getCapabilities(ENTITY_NAME)).thenReturn(new HashSet<>(singletonList(RepositoryCapability.QUERYABLE)));
mockMvc.perform(get(HREF_ENTITY_META)).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(content().json(ENTITY_META_RESPONSE_STRING));
}
use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.
the class PluginInterceptor method postHandle.
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (modelAndView != null) {
PluginController molgenisPlugin = validateHandler(handler);
String pluginId = molgenisPlugin.getId();
// allow controllers that handle multiple plugins to set their plugin id
if (!modelAndView.getModel().containsKey(PluginAttributes.KEY_PLUGIN_ID)) {
modelAndView.addObject(PluginAttributes.KEY_PLUGIN_ID, pluginId);
}
Entity pluginSettings = molgenisPlugin.getPluginSettings();
boolean pluginSettingsCanWrite;
if (pluginSettings != null) {
String pluginSettingsEntityName = pluginSettings.getEntityType().getId();
pluginSettingsCanWrite = permissionService.hasPermission(new EntityTypeIdentity(pluginSettingsEntityName), EntityTypePermission.WRITE);
modelAndView.addObject(PluginAttributes.KEY_PLUGIN_SETTINGS, pluginSettings);
} else {
pluginSettingsCanWrite = false;
}
modelAndView.addObject(PluginAttributes.KEY_PLUGIN_SHOW_SETTINGS_COG, pluginSettingsCanWrite);
modelAndView.addObject(PluginAttributes.KEY_MOLGENIS_UI, molgenisUi);
modelAndView.addObject(PluginAttributes.KEY_AUTHENTICATED, SecurityUtils.currentUserIsAuthenticated());
modelAndView.addObject(PluginAttributes.KEY_PLUGIN_ID_WITH_QUERY_STRING, getPluginIdWithQueryString(request, pluginId));
}
}
Aggregations