Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(graphql): add graphql unit tests #1060

Merged
merged 7 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,41 @@
package io.cryostat.net.web.http.api.v2.graph;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import javax.inject.Inject;

import io.cryostat.net.AuthManager;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.http.api.v2.graph.labels.LabelSelectorMatcher;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.rules.ArchivedRecordingInfo;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

class AllArchivedRecordingsFetcher implements DataFetcher<List<ArchivedRecordingInfo>> {
class AllArchivedRecordingsFetcher
extends AbstractPermissionedDataFetcher<List<ArchivedRecordingInfo>> {

private final RecordingArchiveHelper archiveHelper;

@Inject
AllArchivedRecordingsFetcher(RecordingArchiveHelper archiveHelper) {
AllArchivedRecordingsFetcher(AuthManager auth, RecordingArchiveHelper archiveHelper) {
super(auth);
this.archiveHelper = archiveHelper;
}

public List<ArchivedRecordingInfo> get(DataFetchingEnvironment environment) throws Exception {
@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_RECORDING);
}

@Override
List<ArchivedRecordingInfo> getAuthenticated(DataFetchingEnvironment environment)
throws Exception {
FilterInput filter = FilterInput.from(environment);
List<ArchivedRecordingInfo> result = new ArrayList<>();
if (filter.contains(FilterInput.Key.SOURCE_TARGET)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ static ActiveRecordingsFetcher provideActiveRecordingsFetcher(AuthManager auth)
return new ActiveRecordingsFetcher(auth);
}

@Provides
static AllArchivedRecordingsFetcher provideAllArchivedRecordingsFetcher(
AuthManager auth, RecordingArchiveHelper recordingArchiveHelper) {
return new AllArchivedRecordingsFetcher(auth, recordingArchiveHelper);
}

@Provides
static ArchivedRecordingsFetcher provideArchivedRecordingsFetcher(AuthManager auth) {
return new ArchivedRecordingsFetcher(auth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

class RecordingsFetcher extends AbstractPermissionedDataFetcher<Recordings> {

private final TargetConnectionManager tcm;
private final TargetConnectionManager targetConnectionManager;
private final RecordingArchiveHelper archiveHelper;
private final CredentialsManager credentialsManager;
private final RecordingMetadataManager metadataManager;
Expand All @@ -80,14 +80,14 @@ class RecordingsFetcher extends AbstractPermissionedDataFetcher<Recordings> {
@Inject
RecordingsFetcher(
AuthManager auth,
TargetConnectionManager tcm,
TargetConnectionManager targetConnectionManager,
RecordingArchiveHelper archiveHelper,
CredentialsManager credentialsManager,
RecordingMetadataManager metadataManager,
Provider<WebServer> webServer,
Logger logger) {
super(auth);
this.tcm = tcm;
this.targetConnectionManager = targetConnectionManager;
this.archiveHelper = archiveHelper;
this.credentialsManager = credentialsManager;
this.metadataManager = metadataManager;
Expand Down Expand Up @@ -126,7 +126,7 @@ public Recordings getAuthenticated(DataFetchingEnvironment environment) throws E
// FIXME populating these two struct members are each async tasks. we should do them in
// parallel
recordings.active =
tcm.executeConnectedTask(
targetConnectionManager.executeConnectedTask(
cd,
conn -> {
return conn.getService().getAvailableRecordings().stream()
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/io/cryostat/UnknownNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat;

import io.cryostat.platform.discovery.AbstractNode;

public class UnknownNode extends AbstractNode {
protected UnknownNode(AbstractNode other) {
super(other);
}

@Override
public String getName() {
return "unknown";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.net.web.http.api.v2.graph;

import static org.mockito.Mockito.when;

import java.util.Set;
import java.util.concurrent.CompletableFuture;

import io.cryostat.net.AuthManager;
import io.cryostat.net.AuthorizationErrorException;
import io.cryostat.net.security.ResourceAction;

import graphql.GraphQLContext;
import graphql.schema.DataFetchingEnvironment;
import io.vertx.ext.web.RoutingContext;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AbstractPermissionedDataFetcherTest {
AbstractPermissionedDataFetcher<?> fetcher;

@Mock AuthManager auth;

@Mock DataFetchingEnvironment env;
@Mock GraphQLContext graphCtx;
@Mock RoutingContext ctx;
@Mock FilterInput filter;

@BeforeEach
void setup() {
this.fetcher = new PermissionedDataFetcher(auth);
}

@Test
void shouldHaveExpectedRequiredPermissions() {
MatcherAssert.assertThat(fetcher.resourceActions(), Matchers.equalTo(ResourceAction.NONE));
}

@Test
void shouldThrowAuthorizationError() throws Exception {
when(env.getGraphQlContext()).thenReturn(graphCtx);

when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(false));

AuthorizationErrorException ex =
Assertions.assertThrows(AuthorizationErrorException.class, () -> fetcher.get(env));
MatcherAssert.assertThat(ex.getMessage(), Matchers.equalTo("Unauthorized"));
}

static class PermissionedDataFetcher extends AbstractPermissionedDataFetcher<String> {
PermissionedDataFetcher(AuthManager auth) {
super(auth);
}

@Override
public Set<ResourceAction> resourceActions() {
return ResourceAction.NONE;
}

@Override
String getAuthenticated(DataFetchingEnvironment environment) throws Exception {
return null;
}
}
}
Loading