Search in sources :

Example 1 with Book

use of graphqlEndpointTestModels.Book in project elide by yahoo.

the class GraphQLEndpointTest method testLifeCycleHooks.

@Test
void testLifeCycleHooks() throws Exception {
    /* Separate user 1 so it doesn't interfere */
    SecurityContext user = Mockito.mock(SecurityContext.class);
    User principal = new User().withName("1");
    Mockito.when(user.getUserPrincipal()).thenReturn(principal);
    Book book = new Book();
    book.setId(1);
    book.setTitle("my new book!");
    String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
    String expected = document(selection(field("book", selections(field("id", "1"), field("title", "my new book!"))))).toResponse();
    Response response = endpoint.post(uriInfo, requestHeaders, user, graphQLRequestToJSON(graphQLRequest));
    assert200EqualBody(response, expected);
    String expectedLog = "On Title Update Pre Security\nOn Title Update Pre Commit\nOn Title Update Post Commit\n";
    assertEquals(principal.getLog(), expectedLog);
}
Also used : Response(javax.ws.rs.core.Response) Book(graphqlEndpointTestModels.Book) SecurityContext(javax.ws.rs.core.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 2 with Book

use of graphqlEndpointTestModels.Book in project elide by yahoo.

the class GraphQLEndpointTest method testFailedCommitCheck.

@Test
void testFailedCommitCheck() throws IOException {
    Book book = new Book();
    book.setId(1);
    book.setTitle("update title");
    // NOTE: User 3 cannot update books.
    String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
    Response response = endpoint.post(uriInfo, requestHeaders, user3, graphQLRequestToJSON(graphQLRequest));
    assertHasErrors(response);
    verify(elide).mapError(any());
}
Also used : Response(javax.ws.rs.core.Response) Book(graphqlEndpointTestModels.Book) Test(org.junit.jupiter.api.Test)

Example 3 with Book

use of graphqlEndpointTestModels.Book in project elide by yahoo.

the class GraphQLEndpointTest method testAuditLogging.

@Test
void testAuditLogging() throws IOException {
    Mockito.reset(audit);
    Book book = new Book();
    book.setTitle("my new book!");
    String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
    endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
    verify(audit, Mockito.times(1)).log(Mockito.any());
    verify(audit, Mockito.times(1)).commit();
    verify(audit, Mockito.times(1)).clear();
}
Also used : Book(graphqlEndpointTestModels.Book) Test(org.junit.jupiter.api.Test)

Example 4 with Book

use of graphqlEndpointTestModels.Book in project elide by yahoo.

the class GraphQLEndpointTest method setupTest.

@BeforeEach
public void setupTest() throws Exception {
    HashMapDataStore inMemoryStore = new HashMapDataStore(DefaultClassScanner.getInstance(), Book.class.getPackage());
    Map<String, Class<? extends Check>> checkMappings = new HashMap<>();
    checkMappings.put(UserChecks.IS_USER_1, UserChecks.IsUserId.One.class);
    checkMappings.put(UserChecks.IS_USER_2, UserChecks.IsUserId.Two.class);
    checkMappings.put(CommitChecks.IS_NOT_USER_3, CommitChecks.IsNotUser3.class);
    elide = spy(new Elide(new ElideSettingsBuilder(inMemoryStore).withEntityDictionary(EntityDictionary.builder().checks(checkMappings).build()).withAuditLogger(audit).build()));
    elide.doScans();
    endpoint = new GraphQLEndpoint(elide);
    DataStoreTransaction tx = inMemoryStore.beginTransaction();
    // Initial data
    Book book1 = new Book();
    Author author1 = new Author();
    Author author2 = new Author();
    DisallowTransfer noShare = new DisallowTransfer();
    book1.setId(1L);
    book1.setTitle("My first book");
    book1.setAuthors(Sets.newHashSet(author1));
    author1.setId(1L);
    author1.setName("Ricky Carmichael");
    author1.setBooks(Sets.newHashSet(book1));
    author1.setBookTitlesAndAwards(Stream.of(new AbstractMap.SimpleImmutableEntry<>("Bookz", "Pulitzer Prize"), new AbstractMap.SimpleImmutableEntry<>("Lost in the Data", "PEN/Faulkner Award")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
    author2.setId(2L);
    author2.setName("The Silent Author");
    author2.setBookTitlesAndAwards(Stream.of(new AbstractMap.SimpleImmutableEntry<>("Working Hard or Hardly Working", "Booker Prize")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
    noShare.setId(1L);
    tx.createObject(book1, null);
    tx.createObject(author1, null);
    tx.createObject(author2, null);
    tx.createObject(noShare, null);
    tx.save(book1, null);
    tx.save(author1, null);
    tx.save(author2, null);
    tx.save(noShare, null);
    tx.commit(null);
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Check(com.yahoo.elide.core.security.checks.Check) DisallowTransfer(graphqlEndpointTestModels.DisallowTransfer) AbstractMap(java.util.AbstractMap) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Book(graphqlEndpointTestModels.Book) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Author(graphqlEndpointTestModels.Author) Elide(com.yahoo.elide.Elide) CommitChecks(graphqlEndpointTestModels.security.CommitChecks) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with Book

use of graphqlEndpointTestModels.Book in project elide by yahoo.

the class GraphQLEndpointTest method testSuccessfulMutation.

@Test
void testSuccessfulMutation() throws JSONException {
    Author author = new Author();
    author.setId(2L);
    Book book = new Book();
    book.setId(123);
    book.setTitle("my new book!");
    book.setAuthors(Sets.newHashSet(author));
    String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title"), field("user1SecretField")))))).toQuery();
    String expected = document(selection(field("book", selections(field("id", "2"), field("title", "my new book!"), field("user1SecretField", "this is a secret for user 1 only1"))))).toResponse();
    Response response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
    assert200EqualBody(response, expected);
    graphQLRequest = document(selection(field("book", selections(field("id"), field("title"), field("authors", selections(field("id"), field("name"))))))).toQuery();
    expected = document(selection(field("book", selections(field("id", "1"), field("title", "My first book"), field("authors", selections(field("id", "1"), field("name", "Ricky Carmichael")))), selections(field("id", "2"), field("title", "my new book!"), field("authors", selections(field("id", "2"), field("name", "The Silent Author"))))))).toResponse();
    response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
    assert200EqualBody(response, expected);
}
Also used : Response(javax.ws.rs.core.Response) Book(graphqlEndpointTestModels.Book) Author(graphqlEndpointTestModels.Author) Test(org.junit.jupiter.api.Test)

Aggregations

Book (graphqlEndpointTestModels.Book)6 Test (org.junit.jupiter.api.Test)5 Response (javax.ws.rs.core.Response)4 Author (graphqlEndpointTestModels.Author)3 Elide (com.yahoo.elide.Elide)1 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)1 Check (com.yahoo.elide.core.security.checks.Check)1 DisallowTransfer (graphqlEndpointTestModels.DisallowTransfer)1 CommitChecks (graphqlEndpointTestModels.security.CommitChecks)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 SecurityContext (javax.ws.rs.core.SecurityContext)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1