Search in sources :

Example 36 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class VerifyFieldAccessFilterExpressionVisitorTest method testShortCircuitRejectDeferThenFail.

@Test
public void testShortCircuitRejectDeferThenFail() throws Exception {
    RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
    FilterExpression expression = dialect.parseFilterExpression("authors.homeAddress==main", ClassType.of(Book.class), true);
    Book book = new Book();
    Author author = new Author();
    book.setAuthors(Collections.singleton(author));
    author.setBooks(Collections.singleton(book));
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    DataStoreTransaction tx = scope.getTransaction();
    when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS)).thenReturn(ExpressionResult.DEFERRED);
    when(permissionExecutor.checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, HOME)).thenThrow(ForbiddenAccessException.class);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // restricted HOME field
    assertFalse(expression.accept(visitor));
    verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS);
    verify(permissionExecutor, never()).getReadPermissionFilter(ClassType.of(Author.class), null);
    verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, HOME);
    verify(permissionExecutor, never()).checkSpecificFieldPermissions(any(), any(), any(), any());
    verify(permissionExecutor, never()).checkSpecificFieldPermissionsDeferred(any(), any(), any(), any());
    verify(permissionExecutor, times(2)).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, times(1)).handleFilterJoinReject(any(), any(), any());
    verify(tx, never()).getToManyRelation(any(), any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Book(example.Book) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Author(example.Author) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) Test(org.junit.jupiter.api.Test)

Example 37 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class VerifyFieldAccessFilterExpressionVisitorTest method testShortCircuitPass.

@Test
public void testShortCircuitPass() throws Exception {
    RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
    FilterExpression expression = dialect.parseFilterExpression("authors.name==foo", ClassType.of(Book.class), true);
    Book book = new Book();
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    DataStoreTransaction tx = scope.getTransaction();
    when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS)).thenReturn(ExpressionResult.PASS);
    when(permissionExecutor.checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, NAME)).thenReturn(ExpressionResult.PASS);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // restricted HOME field
    assertTrue(expression.accept(visitor));
    verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS);
    verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, NAME);
    verify(permissionExecutor, never()).checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE);
    verify(permissionExecutor, times(2)).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, never()).handleFilterJoinReject(any(), any(), any());
    verify(tx, never()).getToManyRelation(any(), any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Book(example.Book) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Author(example.Author) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) Test(org.junit.jupiter.api.Test)

Example 38 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class ErrorMapperTest method testElideCreateNoErrorMapper.

@Test
public void testElideCreateNoErrorMapper() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, null);
    String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.createNewObject(eq(ClassType.of(FieldTestModel.class)), any())).thenReturn(mockModel);
    doThrow(EXPECTED_EXCEPTION).when(tx).preCommit(any());
    RuntimeException result = assertThrows(RuntimeException.class, () -> elide.post(baseUrl, "/testModel", body, null, NO_VERSION));
    assertEquals(EXPECTED_EXCEPTION, result.getCause());
    verify(tx).close();
}
Also used : FieldTestModel(com.yahoo.elide.core.lifecycle.FieldTestModel) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test)

Example 39 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class ErrorMapperTest method testElideCreateWithErrorMapperMapped.

@Test
public void testElideCreateWithErrorMapperMapped() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_ERROR_MAPPER);
    String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.createNewObject(eq(ClassType.of(FieldTestModel.class)), any())).thenReturn(mockModel);
    doThrow(EXPECTED_EXCEPTION).when(tx).preCommit(any());
    when(MOCK_ERROR_MAPPER.map(EXPECTED_EXCEPTION)).thenReturn(MAPPED_EXCEPTION);
    ElideResponse response = elide.post(baseUrl, "/testModel", body, null, NO_VERSION);
    assertEquals(422, response.getResponseCode());
    assertEquals("{\"errors\":[{\"code\":\"SOME_ERROR\"}]}", response.getBody());
    verify(tx).close();
}
Also used : FieldTestModel(com.yahoo.elide.core.lifecycle.FieldTestModel) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test)

Example 40 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class MultiplexTransaction method updateToOneRelation.

@Override
public <T, R> void updateToOneRelation(DataStoreTransaction tx, T entity, String relationName, R relationshipValue, RequestScope scope) {
    DataStoreTransaction relationTx = getRelationTransaction(entity, relationName);
    DataStoreTransaction entityTransaction = getTransaction(EntityDictionary.getType(entity));
    entityTransaction.updateToOneRelation(relationTx, entity, relationName, relationshipValue, scope);
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction)

Aggregations

DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)139 Test (org.junit.jupiter.api.Test)101 RequestScope (com.yahoo.elide.core.RequestScope)40 DataStore (com.yahoo.elide.core.datastore.DataStore)30 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)28 Elide (com.yahoo.elide.Elide)27 ElideResponse (com.yahoo.elide.ElideResponse)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 PersistentResource (com.yahoo.elide.core.PersistentResource)17 Item (com.yahoo.elide.datastores.search.models.Item)17 Book (example.Book)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Author (example.Author)8 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)7 FirstBean (com.yahoo.elide.example.beans.FirstBean)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)6