diff --git a/CHANGELOG.md b/CHANGELOG.md index 69a4cf4bd83..21230dd9e4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ### 6.6-SNAPSHOT #### Bugs - +* Fix #4963: Openshift Client return 403 when use websocket #### Improvements #### Dependency Upgrade diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java b/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java index 358877e3bbc..de73903aedb 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java @@ -24,7 +24,6 @@ import io.fabric8.kubernetes.client.http.BasicBuilder; import io.fabric8.kubernetes.client.http.HttpClient; import io.fabric8.kubernetes.client.http.HttpRequest; -import io.fabric8.kubernetes.client.http.HttpRequest.Builder; import io.fabric8.kubernetes.client.http.HttpResponse; import io.fabric8.kubernetes.client.http.Interceptor; import io.fabric8.kubernetes.client.utils.HttpClientUtils; @@ -93,7 +92,7 @@ public void before(BasicBuilder builder, HttpRequest httpRequest, RequestTags ta } @Override - public CompletableFuture afterFailure(Builder builder, HttpResponse response, RequestTags tags) { + public CompletableFuture afterFailure(BasicBuilder builder, HttpResponse response, RequestTags tags) { if (shouldProceed(response.request(), response)) { return CompletableFuture.completedFuture(false); } @@ -121,7 +120,7 @@ public CompletableFuture afterFailure(Builder builder, HttpResponse return CompletableFuture.completedFuture(refreshFromConfig(builder)); } - private boolean refreshFromConfig(Builder builder) { + private boolean refreshFromConfig(BasicBuilder builder) { Config newestConfig = config.refresh(); // does some i/o work, but for now we'll consider this non-blocking String oauthToken = newestConfig.getOauthToken(); if (oauthToken != null) { diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/AdaptTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/AdaptTest.java deleted file mode 100644 index 5497812a7f4..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/AdaptTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.dsl; - -import io.fabric8.kubernetes.client.impl.KubernetesClientImpl; -import io.fabric8.openshift.client.OpenShiftClient; -import io.fabric8.openshift.client.OpenShiftConfigBuilder; -import io.fabric8.openshift.client.impl.OpenShiftClientImpl; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class AdaptTest { - - @Test - void testAdaptDisabledCheck() { - // Given - OpenShiftClient client = new OpenShiftClientImpl(new OpenShiftConfigBuilder().withDisableApiGroupCheck(true).build()); - - // When + Then - assertTrue(client.isAdaptable(OpenShiftClient.class)); - } - - @Test - void testAdaptDSLs() { - // Given - OpenShiftClient client = new OpenShiftClientImpl( - new OpenShiftConfigBuilder().withDisableApiGroupCheck(true).build()); - - assertNotNull(client.v1()); - assertNotNull(client.apps()); - assertNotNull(client.autoscaling()); - assertNotNull(client.batch()); - assertNotNull(client.buildConfigs()); - assertNotNull(client.builds()); - assertNotNull(client.clusterRoleBindings()); - assertNotNull(client.deploymentConfigs()); - assertNotNull(client.extensions()); - assertNotNull(client.groups()); - assertNotNull(client.imageStreams()); - assertNotNull(client.imageStreamTags()); - assertNotNull(client.network()); - assertNotNull(client.oAuthAccessTokens()); - assertNotNull(client.oAuthAuthorizeTokens()); - assertNotNull(client.oAuthClients()); - assertNotNull(client.projectrequests()); - assertNotNull(client.projects()); - assertNotNull(client.pods()); - assertNotNull(client.rbac()); - assertNotNull(client.roleBindings()); - assertNotNull(client.storage()); - assertNotNull(client.templates()); - assertNotNull(client.users()); - } - - @Test - void testNamespacePreservation() { - // Given - KubernetesClientImpl client = new KubernetesClientImpl(); - OpenShiftClient client1 = client.inNamespace("x").adapt(OpenShiftClient.class); - - // When + Then - assertEquals("x", client1.getNamespace()); - } -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/base/TestUnmarshal.java b/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/base/TestUnmarshal.java deleted file mode 100644 index 45db1603e2c..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/base/TestUnmarshal.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.dsl.base; - -import io.fabric8.kubernetes.client.utils.Serialization; -import io.fabric8.openshift.api.model.Template; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class TestUnmarshal { - - @Test - void testUnmarshalJSONTemplate() { - Template t = Serialization.unmarshal(getClass().getResourceAsStream("/test-template.json"), Template.class); - assertEquals("eap6-basic-sti", t.getMetadata().getName()); - } - - @Test - void testUnmarshalYAMLTemplate() { - Template t = Serialization.unmarshal(getClass().getResourceAsStream("/test-template.yml"), Template.class); - assertEquals("eap6-basic-sti", t.getMetadata().getName()); - } - -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/apps/OpenShiftDeploymentConfigPodOperationTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/apps/OpenShiftDeploymentConfigPodOperationTest.java deleted file mode 100644 index 7d960e12634..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/apps/OpenShiftDeploymentConfigPodOperationTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.dsl.internal.apps; - -import io.fabric8.openshift.api.model.DeploymentConfig; -import io.fabric8.openshift.api.model.DeploymentConfigBuilder; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static io.fabric8.openshift.client.dsl.internal.apps.DeploymentConfigOperationsImpl.OPENSHIFT_IO_DEPLOYMENT_CONFIG_NAME; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -class OpenShiftDeploymentConfigPodOperationTest { - @Test - void testDeploymentConfigSelectorLabels() { - // Given - DeploymentConfig deploymentConfig = new DeploymentConfigBuilder() - .withNewMetadata().withName("dc1").endMetadata() - .build(); - - // When - Map labels = DeploymentConfigOperationsImpl.getDeploymentConfigPodLabels(deploymentConfig); - - // Then - assertNotNull(labels); - assertEquals(deploymentConfig.getMetadata().getName(), labels.get(OPENSHIFT_IO_DEPLOYMENT_CONFIG_NAME)); - } - -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/BuildConfigOperationsImplTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/BuildConfigOperationsImplTest.java deleted file mode 100644 index 6a38bafb298..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/BuildConfigOperationsImplTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.dsl.internal.build; - -import io.fabric8.kubernetes.client.Client; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.http.HttpClient; -import io.fabric8.kubernetes.client.http.HttpRequest; -import io.fabric8.kubernetes.client.http.HttpResponse; -import io.fabric8.kubernetes.client.impl.BaseClient; -import io.fabric8.openshift.client.OpenShiftConfigBuilder; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.URI; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class BuildConfigOperationsImplTest { - - private Client client; - private HttpClient httpClient; - - @BeforeEach - public void setUp() { - httpClient = mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS); - - HttpRequest response = mock(HttpRequest.class, Mockito.CALLS_REAL_METHODS); - when(response.method()).thenReturn("POST"); - when(response.uri()).thenReturn(URI.create("https://localhost:8443/")); - - when(httpClient.newBuilder() - .readTimeout(anyLong(), any()) - .writeTimeout(anyLong(), any()) - .build()).thenReturn(httpClient); - when(httpClient.newHttpRequestBuilder() - .post(any(), any(), anyLong()) - .header(any(), any()) - .uri(any(String.class)) - .build()).thenReturn(response); - - client = mock(BaseClient.class, Mockito.RETURNS_SELF); - Mockito.when(client.getHttpClient()).thenReturn(httpClient); - Mockito.when(client.getConfiguration()) - .thenReturn(new OpenShiftConfigBuilder().withMasterUrl("https://localhost:8443/").build()); - } - - @Test - void requestTimeoutDefaultsToZero() { - assertThat(new BuildConfigOperationsImpl(client).getOperationContext().getRequestConfig().getRequestTimeout()) - .isZero(); - } - - @Test - void withTimeoutOverridesRequestTimeout() { - final BuildConfigOperationsImpl buildConfigOperations = new BuildConfigOperationsImpl(client) - .withTimeout(1337, TimeUnit.MILLISECONDS); - assertThat(buildConfigOperations.getOperationContext().getRequestConfig().getRequestTimeout()) - .isEqualTo(1337); - } - - @Test - void withTimeoutInMillisOverridesRequestTimeout() { - final BuildConfigOperationsImpl buildConfigOperations = new BuildConfigOperationsImpl(client) - .withTimeoutInMillis(1337); - assertThat(buildConfigOperations.getOperationContext().getRequestConfig().getRequestTimeout()) - .isEqualTo(1337); - } - - @Test - void testWriteToThrowsExceptionShouldAddEvents() { - // Given - String eventMessage = "FailedScheduling demo-1-7zkjd.1619493da51f6b6f some error"; - - BuildConfigOperationsImpl impl = new BuildConfigOperationsImpl(client) { - @Override - protected String getRecentEvents() { - return eventMessage; - }; - }; - - // When - ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); - CompletableFuture> future = new CompletableFuture<>(); - future.completeExceptionally(new IOException()); - when(httpClient.sendAsync(any(), eq(byte[].class))).thenReturn(future); - - KubernetesClientException exception = assertThrows(KubernetesClientException.class, - () -> impl.submitToApiServer(inputStream, 0)); - - // Then - assertTrue(exception.getMessage().contains(eventMessage)); - } - - @Test - void testWriteShouldCompleteSuccessfully() { - // Given - BuildConfigOperationsImpl impl = new BuildConfigOperationsImpl(client) { - @Override - protected String getRecentEvents() { - throw new AssertionError(); - }; - }; - - HttpResponse response = mock(HttpResponse.class, Mockito.CALLS_REAL_METHODS); - when(response.code()).thenReturn(200); - when(response.body()).thenReturn(new byte[0]); - - when(httpClient.sendAsync(any(), eq(byte[].class))).thenReturn(CompletableFuture.completedFuture(response)); - impl.submitToApiServer(new ByteArrayInputStream(new byte[0]), 0); - - Mockito.verify(response, Mockito.times(1)).body(); - } -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/OpenShiftBuildPodOperationTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/OpenShiftBuildPodOperationTest.java deleted file mode 100644 index d8344a614f5..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/dsl/internal/build/OpenShiftBuildPodOperationTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.dsl.internal.build; - -import io.fabric8.openshift.api.model.Build; -import io.fabric8.openshift.api.model.BuildBuilder; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static io.fabric8.openshift.client.dsl.internal.build.BuildOperationsImpl.OPENSHIFT_IO_BUILD_NAME; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class OpenShiftBuildPodOperationTest { - @Test - void testBuildSelectorLabels() { - // Given - Build build = new BuildBuilder() - .withNewMetadata().withName("dc1").endMetadata() - .build(); - - // When - Map labels = BuildOperationsImpl.getBuildPodLabels(build); - - // Then - assertNotNull(labels); - assertEquals(build.getMetadata().getName(), labels.get(OPENSHIFT_IO_BUILD_NAME)); - } -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftConfigTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftConfigTest.java deleted file mode 100644 index c92872e31a8..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftConfigTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.impl; - -import io.fabric8.kubernetes.client.Config; -import io.fabric8.kubernetes.client.ConfigBuilder; -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.KubernetesClientBuilder; -import io.fabric8.kubernetes.client.utils.Serialization; -import io.fabric8.openshift.client.OpenShiftConfig; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class OpenShiftConfigTest { - - private Config kubernetesConfig; - - private String version; - - @BeforeEach - void setup() { - this.kubernetesConfig = new ConfigBuilder() - .withMasterUrl("https://2.2.2.2") - .build(); - - this.version = "v1"; - System.setProperty(OpenShiftConfig.KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY, this.version); - } - - @AfterEach - void tearDown() { - System.clearProperty(OpenShiftConfig.OPENSHIFT_URL_SYSTEM_PROPERTY); - System.clearProperty(OpenShiftConfig.KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY); - } - - @Test - void testOpenshiftURLAsRoot() { - System.setProperty(OpenShiftConfig.OPENSHIFT_URL_SYSTEM_PROPERTY, "https://1.1.1.1"); - OpenShiftConfig config = new OpenShiftConfig(kubernetesConfig); - assertEquals("https://1.1.1.1/oapi/" + version + "/", config.getOpenShiftUrl()); - } - - @Test - void testOpenshiftURLAsRootWithSlash() { - System.setProperty(OpenShiftConfig.OPENSHIFT_URL_SYSTEM_PROPERTY, "https://1.1.1.1/"); - OpenShiftConfig config = new OpenShiftConfig(kubernetesConfig); - assertEquals("https://1.1.1.1/oapi/" + version + "/", config.getOpenShiftUrl()); - } - - @Test - void testFullOpenshiftURL() { - System.setProperty(OpenShiftConfig.OPENSHIFT_URL_SYSTEM_PROPERTY, "https://1.1.1.1/xxx"); - OpenShiftConfig config = new OpenShiftConfig(kubernetesConfig); - assertEquals("https://1.1.1.1/xxx/", config.getOpenShiftUrl()); - } - - @Test - void testNoOpenshiftURL() { - System.clearProperty(OpenShiftConfig.OPENSHIFT_URL_SYSTEM_PROPERTY); - OpenShiftConfig config = new OpenShiftConfig(kubernetesConfig); - // Use the master URL - assertEquals("https://2.2.2.2/oapi/" + version + "/", config.getOpenShiftUrl()); - } - - @Test - void shouldInstantiateClientUsingSerializeDeserialize() { - OpenShiftClientImpl original = new OpenShiftClientImpl(); - String json = Serialization.asJson(original.getConfiguration()); - KubernetesClient copy = new KubernetesClientBuilder().withConfig(json).build(); - - assertEquals(original.getConfiguration().getMasterUrl(), copy.getConfiguration().getMasterUrl()); - assertEquals(original.getConfiguration().getOauthToken(), copy.getConfiguration().getOauthToken()); - assertEquals(original.getConfiguration().getNamespace(), copy.getConfiguration().getNamespace()); - assertEquals(original.getConfiguration().getUsername(), copy.getConfiguration().getUsername()); - assertEquals(original.getConfiguration().getPassword(), copy.getConfiguration().getPassword()); - } -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftExtensionAdapterTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftExtensionAdapterTest.java deleted file mode 100644 index 9c10c919caf..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/OpenShiftExtensionAdapterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fabric8.openshift.client.impl; - -import io.fabric8.openshift.client.OpenShiftConfig; -import io.fabric8.openshift.client.OpenShiftConfigBuilder; -import org.junit.Assert; -import org.junit.jupiter.api.Test; - -class OpenShiftExtensionAdapterTest { - - @Test - void testHasCustomConfig() { - OpenShiftConfig config = new OpenShiftConfigBuilder() - .withMasterUrl("http://host1:80") - .build(); - - Assert.assertFalse(OpenShiftClientImpl.hasCustomOpenShiftUrl(config)); - - config = new OpenShiftConfigBuilder() - .withMasterUrl("http://host1:80") - .withOpenShiftUrl("http://host2:80/oapi/v1") - .build(); - - Assert.assertTrue(OpenShiftClientImpl.hasCustomOpenShiftUrl(config)); - } - -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/ViewCurrentUser.java b/openshift-client/src/test/java/io/fabric8/openshift/client/impl/ViewCurrentUser.java deleted file mode 100644 index 5046d640a90..00000000000 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/impl/ViewCurrentUser.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (C) 2015 Red Hat, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.fabric8.openshift.client.impl; - -import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.openshift.api.model.User; -import io.fabric8.openshift.client.OpenShiftClient; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -/** - */ -class ViewCurrentUser { - @Test - void testShowCurrentUser() throws Exception { - OpenShiftClient client = new OpenShiftClientImpl(); - User user = client.currentUser(); - - System.out.println("Current user is: " + user); - assertNotNull(user); - ObjectMeta metadata = user.getMetadata(); - assertNotNull(metadata); - String name = metadata.getName(); - System.out.println("User name is: " + name); - assertNotNull(name); - } - -} diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java index 6ce8e15d94b..4d6dfcf64bf 100644 --- a/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java +++ b/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java @@ -19,6 +19,7 @@ import io.fabric8.kubernetes.client.http.HttpClient; import io.fabric8.kubernetes.client.http.StandardHttpRequest; import io.fabric8.kubernetes.client.http.TestHttpResponse; +import io.fabric8.kubernetes.client.http.WebSocket; import io.fabric8.kubernetes.client.utils.TokenRefreshInterceptor; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -71,7 +72,7 @@ void testTokenUsed() { } @Test - void testTokenRefreshedFromConfig() { + void testTokenRefreshedFromConfigForHttpRequestBuilder() { Config config = Mockito.mock(Config.class, RETURNS_DEEP_STUBS); Mockito.when(config.refresh().getOauthToken()).thenReturn("token"); @@ -83,10 +84,27 @@ void testTokenRefreshedFromConfig() { builder.uri("http://localhost"); interceptor.afterFailure(builder, TestHttpResponse.from(HttpURLConnection.HTTP_UNAUTHORIZED, "not for you") - .withRequest(new StandardHttpRequest(null, URI.create("http://localhost"), "GET", null)), null); + .withRequest(new StandardHttpRequest(null, URI.create("http://localhost"), "GET", null)), null); assertEquals(Collections.singletonList("Bearer token"), builder.build().headers(TokenRefreshInterceptor.AUTHORIZATION)); Mockito.verify(config).setOauthToken("token"); } + @Test + void testTokenRefreshedFromConfigForWebSocketBuilder() { + Config config = Mockito.mock(Config.class, RETURNS_DEEP_STUBS); + Mockito.when(config.refresh().getOauthToken()).thenReturn("token"); + + HttpClient client = Mockito.mock(HttpClient.class); + OpenShiftOAuthInterceptor interceptor = new OpenShiftOAuthInterceptor(client, config); + + WebSocket.Builder builder = Mockito.mock(WebSocket.Builder.class, RETURNS_DEEP_STUBS); + + interceptor.afterFailure(builder, TestHttpResponse.from(HttpURLConnection.HTTP_UNAUTHORIZED, "not for you") + .withRequest(new StandardHttpRequest(null, URI.create("http://localhost"), "GET", null)), null); + + Mockito.verify(builder).setHeader(TokenRefreshInterceptor.AUTHORIZATION, "Bearer token"); + Mockito.verify(config).setOauthToken("token"); + } + }