Search in sources :

Example 1 with OAuthAccessTokenList

use of io.fabric8.openshift.api.model.OAuthAccessTokenList in project cryostat by cryostatio.

the class OpenShiftAuthManagerTest method shouldReturnLogoutRedirectUrl.

@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)));
    Resource<OAuthAccessToken> token = Mockito.mock(Resource.class);
    NonNamespaceOperation<OAuthAccessToken, OAuthAccessTokenList, Resource<OAuthAccessToken>> tokens = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(client.oAuthAccessTokens()).thenReturn(tokens);
    Mockito.when(tokens.withName(Mockito.anyString())).thenReturn(token);
    Mockito.when(token.delete()).thenReturn(true);
    HttpRequest<Buffer> req = Mockito.mock(HttpRequest.class);
    HttpResponse<Buffer> resp = Mockito.mock(HttpResponse.class);
    Mockito.when(webClient.get(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(req);
    Mockito.when(req.putHeader(Mockito.anyString(), Mockito.anyString())).thenReturn(req);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock args) throws Throwable {
            AsyncResult<HttpResponse<Buffer>> asyncResult = Mockito.mock(AsyncResult.class);
            Mockito.when(asyncResult.result()).thenReturn(resp);
            Mockito.when(resp.bodyAsJsonObject()).thenReturn(OAUTH_METADATA);
            ((Handler<AsyncResult<HttpResponse<Buffer>>>) args.getArgument(0)).handle(asyncResult);
            return null;
        }
    }).when(req).send(Mockito.any());
    String logoutRedirectUrl = mgr.logout(() -> "Bearer myToken").get();
    MatcherAssert.assertThat(logoutRedirectUrl, Matchers.equalTo(EXPECTED_LOGOUT_REDIRECT_URL));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Resource(io.fabric8.kubernetes.client.dsl.Resource) OAuthAccessTokenList(io.fabric8.openshift.api.model.OAuthAccessTokenList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) OAuthAccessToken(io.fabric8.openshift.api.model.OAuthAccessToken) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with OAuthAccessTokenList

use of io.fabric8.openshift.api.model.OAuthAccessTokenList in project cryostat by cryostatio.

the class OpenShiftAuthManagerTest method shouldReturnLogoutRedirectUrl.

@Test
void shouldReturnLogoutRedirectUrl() throws Exception {
    Resource<OAuthAccessToken> token = Mockito.mock(Resource.class);
    NonNamespaceOperation<OAuthAccessToken, OAuthAccessTokenList, Resource<OAuthAccessToken>> tokens = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(client.oAuthAccessTokens()).thenReturn(tokens);
    Mockito.when(tokens.withName(Mockito.anyString())).thenReturn(token);
    Mockito.when(token.delete()).thenReturn(true);
    Mockito.when(client.adapt(OkHttpClient.class)).thenReturn(httpClient);
    Mockito.when(client.getMasterUrl()).thenReturn(new URL("https://example.com"));
    Response resp = Mockito.mock(Response.class);
    ResponseBody body = Mockito.mock(ResponseBody.class);
    Mockito.when(body.string()).thenReturn(OAUTH_METADATA);
    Mockito.when(resp.body()).thenReturn(body);
    Call call = Mockito.mock(Call.class);
    Mockito.when(httpClient.newCall(Mockito.any(Request.class))).thenReturn(call);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock args) throws Throwable {
            Callback callback = args.getArgument(0);
            callback.onResponse(call, resp);
            return null;
        }
    }).when(call).enqueue(Mockito.any(Callback.class));
    String logoutRedirectUrl = mgr.logout(() -> "Bearer myToken").get();
    MatcherAssert.assertThat(logoutRedirectUrl, Matchers.equalTo(EXPECTED_LOGOUT_REDIRECT_URL));
}
Also used : Call(okhttp3.Call) GroupResource(io.cryostat.net.openshift.OpenShiftAuthManager.GroupResource) Resource(io.fabric8.kubernetes.client.dsl.Resource) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) URL(java.net.URL) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) OAuthAccessTokenList(io.fabric8.openshift.api.model.OAuthAccessTokenList) Callback(okhttp3.Callback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OAuthAccessToken(io.fabric8.openshift.api.model.OAuthAccessToken) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with OAuthAccessTokenList

use of io.fabric8.openshift.api.model.OAuthAccessTokenList in project cryostat by cryostatio.

the class OpenShiftAuthManagerTest method shouldThrowWhenTokenDeletionFailsOnLogout.

@ParameterizedTest
@NullSource
@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)));
    Resource<OAuthAccessToken> token = Mockito.mock(Resource.class);
    NonNamespaceOperation<OAuthAccessToken, OAuthAccessTokenList, Resource<OAuthAccessToken>> tokens = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(client.oAuthAccessTokens()).thenReturn(tokens);
    Mockito.when(tokens.withName(Mockito.anyString())).thenReturn(token);
    Mockito.when(token.delete()).thenReturn(deletionFailure);
    Assertions.assertThrows(TokenNotFoundException.class, () -> mgr.logout(() -> "Bearer myToken").get());
}
Also used : OAuthAccessTokenList(io.fabric8.openshift.api.model.OAuthAccessTokenList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Resource(io.fabric8.kubernetes.client.dsl.Resource) OAuthAccessToken(io.fabric8.openshift.api.model.OAuthAccessToken) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) NullSource(org.junit.jupiter.params.provider.NullSource)

Aggregations

Resource (io.fabric8.kubernetes.client.dsl.Resource)3 OAuthAccessToken (io.fabric8.openshift.api.model.OAuthAccessToken)3 OAuthAccessTokenList (io.fabric8.openshift.api.model.OAuthAccessTokenList)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BufferedReader (java.io.BufferedReader)2 StringReader (java.io.StringReader)2 Test (org.junit.jupiter.api.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 GroupResource (io.cryostat.net.openshift.OpenShiftAuthManager.GroupResource)1 AsyncResult (io.vertx.core.AsyncResult)1 Buffer (io.vertx.core.buffer.Buffer)1 URL (java.net.URL)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 NullSource (org.junit.jupiter.params.provider.NullSource)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1