Search in sources :

Example 76 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class LifeCycleTest method testDelete.

@Test
public void testDelete() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    PersistentResource resource = new PersistentResource(mockModel, "1", scope);
    resource.deleteResource();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRESECURITY));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PREFLUSH));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRECOMMIT));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(POSTCOMMIT));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 77 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class LifeCycleTest method testAttributeUpdate.

@Test
public void testAttributeUpdate() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    PersistentResource resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    resource.updateAttribute("field", "new value");
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRESECURITY), notNull());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PREFLUSH), notNull());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRECOMMIT), notNull());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(POSTCOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 78 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class AggregationStorePermissionExecutorTest method testUserPermissions.

@Test
public void testUserPermissions() {
    @Entity
    @Include
    @Value
    @ReadPermission(expression = "user none or filter check")
    class Model {

        String filterDim;

        long metric;

        long metric2;

        @ReadPermission(expression = "user all")
        public String getFilterDim() {
            return filterDim;
        }

        @ReadPermission(expression = "user none")
        public long getMetric() {
            return metric;
        }
    }
    com.yahoo.elide.core.RequestScope scope = bindAndgetRequestScope(Model.class);
    PermissionExecutor executor = scope.getPermissionExecutor();
    // evaluated expression = user all
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, "filterDim"));
    // evaluated expression = user none -> ForbiddenAccess
    Assertions.assertThrows(ForbiddenAccessException.class, () -> executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, "metric"));
    // evaluated expression = null -> false
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkSpecificFieldPermissions(new PersistentResource(new Model("dim1", 0, 1), "1", scope), null, ReadPermission.class, "metric2"));
    // evaluated expression = (user none or filter check) AND (user all OR user none)
    Assertions.assertEquals(ExpressionResult.DEFERRED, executor.checkUserPermissions(ClassType.of(Model.class), ReadPermission.class, new HashSet<>(Arrays.asList("filterDim", "metric"))));
    // evaluated expression = (user all OR user none)
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkPermission(ReadPermission.class, new PersistentResource(new Model("dim1", 0, 1), "1", scope), new HashSet<>(Arrays.asList("filterDim", "metric"))));
    // evaluated expression = (user none OR null)
    Assertions.assertEquals(ExpressionResult.PASS, executor.checkPermission(ReadPermission.class, new PersistentResource(new Model("dim1", 0, 1), "1", scope), new HashSet<>(Arrays.asList("metric", "metric2"))));
}
Also used : Entity(javax.persistence.Entity) PersistentResource(com.yahoo.elide.core.PersistentResource) Value(lombok.Value) AggregationStorePermissionExecutor(com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 79 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method newResource.

public <T> PersistentResource<T> newResource(T obj, Class<T> cls, boolean markNew) {
    EntityDictionary dictionary = EntityDictionary.builder().checks(TestCheckMappings.MAPPINGS).build();
    dictionary.bindEntity(cls);
    RequestScope requestScope = new RequestScope(null, null, NO_VERSION, null, null, null, null, null, UUID.randomUUID(), getElideSettings(dictionary));
    PersistentResource resource = new PersistentResource<>(obj, requestScope.getUUIDFor(obj), requestScope);
    if (markNew) {
        requestScope.getNewPersistentResources().add(resource);
    }
    return resource;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope)

Example 80 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method testUpdateFieldAwareSuccessAll.

@Test
public void testUpdateFieldAwareSuccessAll() {
    SampleBean sampleBean = new SampleBean();
    sampleBean.id = 1L;
    PersistentResource resource = newResource(sampleBean, SampleBean.class, true);
    RequestScope requestScope = resource.getRequestScope();
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkSpecificFieldPermissions(resource, new ChangeSpec(null, null, null, null), UpdatePermission.class, "allVisible"));
    requestScope.getPermissionExecutor().executeCommitChecks();
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) RequestScope(com.yahoo.elide.core.RequestScope) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) Test(org.junit.jupiter.api.Test)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7