-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
403 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...e/infrastructure/kubernetes/namespace/configurator/GitconfigUserdataConfiguratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (c) 2012-2022 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator; | ||
|
||
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.GIT_USERDATA_CONFIGMAP_NAME; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesServer; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.eclipse.che.api.factory.server.scm.GitUserData; | ||
import org.eclipse.che.api.factory.server.scm.GitUserDataFetcher; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException; | ||
import org.eclipse.che.api.workspace.server.spi.InfrastructureException; | ||
import org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext; | ||
import org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientFactory; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class GitconfigUserdataConfiguratorTest { | ||
|
||
private NamespaceConfigurator configurator; | ||
|
||
@Mock private KubernetesClientFactory clientFactory; | ||
@Mock private GitUserDataFetcher gitUserDataFetcher; | ||
private KubernetesServer serverMock; | ||
|
||
private NamespaceResolutionContext namespaceResolutionContext; | ||
private final String TEST_NAMESPACE_NAME = "namespace123"; | ||
private final String TEST_WORKSPACE_ID = "workspace123"; | ||
private final String TEST_USER_ID = "user123"; | ||
private final String TEST_USERNAME = "jondoe"; | ||
|
||
@BeforeMethod | ||
public void setUp() | ||
throws InfrastructureException, ScmCommunicationException, ScmUnauthorizedException { | ||
configurator = new GitconfigUserDataConfigurator(clientFactory, Set.of(gitUserDataFetcher)); | ||
|
||
serverMock = new KubernetesServer(true, true); | ||
serverMock.before(); | ||
KubernetesClient client = spy(serverMock.getClient()); | ||
when(clientFactory.create()).thenReturn(client); | ||
|
||
namespaceResolutionContext = | ||
new NamespaceResolutionContext(TEST_WORKSPACE_ID, TEST_USER_ID, TEST_USERNAME); | ||
} | ||
|
||
@Test | ||
public void createUserdataConfigmapWhenDoesNotExist() | ||
throws ScmCommunicationException, ScmUnauthorizedException, InfrastructureException, | ||
InterruptedException { | ||
// given | ||
when(gitUserDataFetcher.fetchGitUserData()).thenReturn(new GitUserData("gitUser", "gitEmail")); | ||
|
||
// when | ||
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); | ||
|
||
// then create a secret | ||
Assert.assertEquals(serverMock.getLastRequest().getMethod(), "POST"); | ||
Assert.assertNotNull( | ||
serverMock | ||
.getClient() | ||
.configMaps() | ||
.inNamespace(TEST_NAMESPACE_NAME) | ||
.withName(GIT_USERDATA_CONFIGMAP_NAME) | ||
.get()); | ||
} | ||
|
||
@Test | ||
public void doNothingWhenGitUserDataIsNull() | ||
throws InfrastructureException, InterruptedException { | ||
// when | ||
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); | ||
|
||
// then - don't create the configmap | ||
var configMaps = | ||
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).list().getItems(); | ||
Assert.assertEquals(configMaps.size(), 0); | ||
} | ||
|
||
@Test | ||
public void doNothingWhenSecretAlreadyExists() | ||
throws InfrastructureException, InterruptedException, ScmCommunicationException, | ||
ScmUnauthorizedException { | ||
// given | ||
when(gitUserDataFetcher.fetchGitUserData()).thenReturn(new GitUserData("gitUser", "gitEmail")); | ||
Map<String, String> annotations = | ||
ImmutableMap.of( | ||
"controller.devfile.io/mount-as", | ||
"subpath", | ||
"controller.devfile.io/mount-path", | ||
"/etc/", | ||
"already", | ||
"created"); | ||
Map<String, String> labels = | ||
ImmutableMap.of( | ||
"controller.devfile.io/mount-to-devworkspace", | ||
"true", | ||
"controller.devfile.io/watch-configmap", | ||
"true", | ||
"already", | ||
"created"); | ||
ConfigMap configMap = | ||
new ConfigMapBuilder() | ||
.withNewMetadata() | ||
.withName(GIT_USERDATA_CONFIGMAP_NAME) | ||
.withLabels(labels) | ||
.withAnnotations(annotations) | ||
.endMetadata() | ||
.build(); | ||
configMap.setData(Collections.singletonMap("gitconfig", "empty")); | ||
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(configMap); | ||
|
||
// when | ||
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); | ||
|
||
// then - don't create the configmap | ||
Assert.assertEquals(serverMock.getLastRequest().getMethod(), "GET"); | ||
var configMaps = | ||
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).list().getItems(); | ||
Assert.assertEquals(configMaps.size(), 1); | ||
Assert.assertEquals(configMaps.get(0).getMetadata().getAnnotations().get("already"), "created"); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerUserDataFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2012-2022 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.factory.server.bitbucket; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.net.MalformedURLException; | ||
import org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApiClient; | ||
import org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser; | ||
import org.eclipse.che.api.factory.server.scm.GitUserData; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException; | ||
import org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException; | ||
import org.eclipse.che.commons.env.EnvironmentContext; | ||
import org.eclipse.che.commons.subject.Subject; | ||
import org.eclipse.che.commons.subject.SubjectImpl; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class BitbucketServerUserDataFetcherTest { | ||
String someBitbucketURL = "https://some.bitbucketserver.com"; | ||
Subject subject; | ||
@Mock BitbucketServerApiClient bitbucketServerApiClient; | ||
BitbucketUser bitbucketUser; | ||
BitbucketServerUserDataFetcher fetcher; | ||
|
||
@BeforeMethod | ||
public void setup() throws MalformedURLException { | ||
subject = new SubjectImpl("another_user", "user987", "token111", false); | ||
bitbucketUser = | ||
new BitbucketUser("User", "user", 32423523, "NORMAL", true, "user", "user@users.com"); | ||
fetcher = new BitbucketServerUserDataFetcher(bitbucketServerApiClient, someBitbucketURL); | ||
EnvironmentContext context = new EnvironmentContext(); | ||
context.setSubject(subject); | ||
EnvironmentContext.setCurrent(context); | ||
} | ||
|
||
@Test | ||
public void shouldBeAbleToFetchPersonalAccessToken() | ||
throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException, | ||
ScmBadRequestException { | ||
// given | ||
when(bitbucketServerApiClient.isConnected(eq(someBitbucketURL))).thenReturn(true); | ||
when(bitbucketServerApiClient.getUser(eq(subject))).thenReturn(bitbucketUser); | ||
// when | ||
GitUserData gitUserData = fetcher.fetchGitUserData(); | ||
// then | ||
assertEquals(gitUserData.getScmUsername(), "user"); | ||
assertEquals(gitUserData.getScmUserEmail(), "user@users.com"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...src/test/java/org/eclipse/che/api/factory/server/github/GithubGitUserDataFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2012-2022 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.factory.server.github; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.eclipse.che.dto.server.DtoFactory.newDto; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.github.tomakehurst.wiremock.common.Slf4jNotifier; | ||
import com.google.common.net.HttpHeaders; | ||
import org.eclipse.che.api.auth.shared.dto.OAuthToken; | ||
import org.eclipse.che.api.factory.server.scm.GitUserData; | ||
import org.eclipse.che.security.oauth.OAuthAPI; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class GithubGitUserDataFetcherTest { | ||
|
||
@Mock OAuthAPI oAuthAPI; | ||
GithubUserDataFetcher githubGUDFetcher; | ||
|
||
final int httpPort = 3301; | ||
WireMockServer wireMockServer; | ||
WireMock wireMock; | ||
|
||
final String githubOauthToken = "gho_token1"; | ||
|
||
@BeforeMethod | ||
void start() { | ||
wireMockServer = | ||
new WireMockServer(wireMockConfig().notifier(new Slf4jNotifier(false)).port(httpPort)); | ||
wireMockServer.start(); | ||
WireMock.configureFor("localhost", httpPort); | ||
wireMock = new WireMock("localhost", httpPort); | ||
githubGUDFetcher = | ||
new GithubUserDataFetcher( | ||
"http://che.api", oAuthAPI, new GithubApiClient(wireMockServer.url("/"))); | ||
stubFor( | ||
get(urlEqualTo("/user")) | ||
.withHeader(HttpHeaders.AUTHORIZATION, equalTo("token " + githubOauthToken)) | ||
.willReturn( | ||
aResponse() | ||
.withHeader("Content-Type", "application/json; charset=utf-8") | ||
.withHeader(GithubApiClient.GITHUB_OAUTH_SCOPES_HEADER, "repo") | ||
.withBodyFile("github/rest/user/response.json"))); | ||
} | ||
|
||
@AfterMethod | ||
void stop() { | ||
wireMockServer.stop(); | ||
} | ||
|
||
@Test | ||
public void shouldFetchGitUserData() throws Exception { | ||
OAuthToken oAuthToken = newDto(OAuthToken.class).withToken(githubOauthToken).withScope("repo"); | ||
when(oAuthAPI.getToken(anyString())).thenReturn(oAuthToken); | ||
|
||
GitUserData gitUserData = githubGUDFetcher.fetchGitUserData(); | ||
|
||
assertEquals(gitUserData.getScmUsername(), "Github User"); | ||
assertEquals(gitUserData.getScmUserEmail(), "github-user@acme.com"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.