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 d1ec53d commit 2bdf1d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
11 changes: 1 addition & 10 deletions src/main/java/io/cryostat/net/OpenShiftAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,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: 8 additions & 5 deletions src/test/java/io/cryostat/net/OpenShiftAuthManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.security.ResourceType;
import io.cryostat.net.security.ResourceVerb;
import io.cryostat.net.UserInfo;

import com.google.gson.Gson;
import io.fabric8.kubernetes.api.model.authentication.TokenReview;
Expand All @@ -72,6 +73,7 @@
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.openshift.api.model.OAuthAccessToken;
import io.fabric8.openshift.api.model.OAuthAccessTokenList;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.server.mock.EnableOpenShiftMockClient;
import io.fabric8.openshift.client.server.mock.OpenShiftMockServer;
Expand Down Expand Up @@ -445,7 +447,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 +516,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 +698,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 2bdf1d7

Please sign in to comment.