Search in sources :

Example 71 with EntityTypeIdentity

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);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Entity(org.molgenis.data.Entity) QuestionnaireResponse(org.molgenis.questionnaires.response.QuestionnaireResponse) Test(org.testng.annotations.Test)

Example 72 with EntityTypeIdentity

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;
}
Also used : EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Package(org.molgenis.data.meta.model.Package)

Example 73 with EntityTypeIdentity

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);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity)

Example 74 with EntityTypeIdentity

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));
}
Also used : EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Test(org.testng.annotations.Test)

Example 75 with EntityTypeIdentity

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));
    }
}
Also used : Entity(org.molgenis.data.Entity) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity)

Aggregations

EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)75 Test (org.testng.annotations.Test)57 EntityType (org.molgenis.data.meta.model.EntityType)40 WithMockUser (org.springframework.security.test.context.support.WithMockUser)39 Attribute (org.molgenis.data.meta.model.Attribute)16 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)8 EntityTypePermission (org.molgenis.data.security.EntityTypePermission)6 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 PrincipalSid (org.springframework.security.acls.domain.PrincipalSid)6 Sid (org.springframework.security.acls.model.Sid)6 Entity (org.molgenis.data.Entity)5 Package (org.molgenis.data.meta.model.Package)5 EntityTypePermissionUtils.getCumulativePermission (org.molgenis.data.security.EntityTypePermissionUtils.getCumulativePermission)4 QueryImpl (org.molgenis.data.support.QueryImpl)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 File (java.io.File)3 Map (java.util.Map)3 ADD (org.molgenis.data.DatabaseAction.ADD)3 FileRepositoryCollection (org.molgenis.data.file.support.FileRepositoryCollection)3