Skip to content

Commit

Permalink
Allow nested mock clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Janelle Law committed Dec 17, 2021
1 parent 82fba94 commit b208d42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
12 changes: 1 addition & 11 deletions src/main/java/io/cryostat/net/OpenShiftAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.api.model.OAuthAccessToken;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
Expand Down Expand Up @@ -315,16 +314,7 @@ public Future<Boolean> validateWebSocketSubProtocol(
private Future<Boolean> deleteToken(String token) {
try (OpenShiftClient client = clientProvider.apply(getServiceAccountToken())) {
String serviceAccountAsOAuthClient = this.getServiceAccountName();

// FIXME reuse performTokenReview instead of copying it here
TokenReview review =
new TokenReviewBuilder().withNewSpec().withToken(token).endSpec().build();
review = client.tokenReviews().create(review);
TokenReviewStatus status = review.getStatus();
if (StringUtils.isNotBlank(status.getError())) {
return CompletableFuture.failedFuture(
new AuthorizationErrorException(status.getError()));
}
TokenReviewStatus status = performTokenReview(token).get();
String uid = status.getUser().getUid();

List<OAuthAccessToken> userOauthAccessTokens =
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/io/cryostat/net/OpenShiftAuthManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand All @@ -65,7 +64,6 @@
import com.google.gson.Gson;
import io.fabric8.kubernetes.api.model.authentication.TokenReview;
import io.fabric8.kubernetes.api.model.authentication.TokenReviewBuilder;
import io.fabric8.kubernetes.api.model.authentication.TokenReviewStatus;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReview;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReviewBuilder;
import io.fabric8.kubernetes.client.Config;
Expand Down Expand Up @@ -445,7 +443,9 @@ public Void answer(InvocationOnMock args) throws Throwable {
@Test
void shouldReturnLogoutRedirectUrl() throws Exception {
Mockito.when(fs.readFile(Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH)))
.thenReturn(new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)));
.thenReturn(
new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)),
new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)));
Mockito.when(fs.readFile(Paths.get(Config.KUBERNETES_NAMESPACE_PATH)))
.thenReturn(
new BufferedReader(new StringReader(NAMESPACE)),
Expand Down Expand Up @@ -512,7 +512,9 @@ public Void answer(InvocationOnMock args) throws Throwable {
@ValueSource(booleans = {false})
void shouldThrowWhenTokenDeletionFailsOnLogout(Boolean deletionFailure) throws Exception {
Mockito.when(fs.readFile(Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH)))
.thenReturn(new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)));
.thenReturn(
new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)),
new BufferedReader(new StringReader(SERVICE_ACCOUNT_TOKEN)));
Mockito.when(fs.readFile(Paths.get(Config.KUBERNETES_NAMESPACE_PATH)))
.thenReturn(
new BufferedReader(new StringReader(NAMESPACE)),
Expand Down Expand Up @@ -692,9 +694,6 @@ private static class TokenProvider implements Function<String, OpenShiftClient>

@Override
public OpenShiftClient apply(String token) {
if (this.token != null) {
throw new IllegalStateException("Token was already set!");
}
this.token = token;
return osc;
}
Expand Down

0 comments on commit b208d42

Please sign in to comment.