diff --git a/wsmaster/che-core-api-factory-azure-devops/src/main/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcher.java b/wsmaster/che-core-api-factory-azure-devops/src/main/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcher.java index 2606666eaac..4199bab25d8 100644 --- a/wsmaster/che-core-api-factory-azure-devops/src/main/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcher.java +++ b/wsmaster/che-core-api-factory-azure-devops/src/main/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcher.java @@ -49,7 +49,7 @@ public class AzureDevOpsPersonalAccessTokenFetcher implements PersonalAccessToke private static final Logger LOG = LoggerFactory.getLogger(AzureDevOpsPersonalAccessTokenFetcher.class); private final String cheApiEndpoint; - private final String azureDevOpsApiEndpoint; + private final String azureDevOpsScmApiEndpoint; private final OAuthAPI oAuthAPI; private final String[] scopes; @@ -58,12 +58,12 @@ public class AzureDevOpsPersonalAccessTokenFetcher implements PersonalAccessToke @Inject public AzureDevOpsPersonalAccessTokenFetcher( @Named("che.api") String cheApiEndpoint, - @Named("che.integration.azure.devops.api_endpoint") String azureDevOpsApiEndpoint, + @Named("che.integration.azure.devops.scm.api_endpoint") String azureDevOpsScmApiEndpoint, @Named("che.integration.azure.devops.application_scopes") String[] scopes, AzureDevOpsApiClient azureDevOpsApiClient, OAuthAPI oAuthAPI) { this.cheApiEndpoint = cheApiEndpoint; - this.azureDevOpsApiEndpoint = trimEnd(azureDevOpsApiEndpoint, '/'); + this.azureDevOpsScmApiEndpoint = trimEnd(azureDevOpsScmApiEndpoint, '/'); this.oAuthAPI = oAuthAPI; this.scopes = scopes; this.azureDevOpsApiClient = azureDevOpsApiClient; @@ -74,7 +74,7 @@ public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String s throws ScmUnauthorizedException, ScmCommunicationException, UnknownScmProviderException { OAuthToken oAuthToken; - if (isValidScmServerUrl(scmServerUrl)) { + if (!isValidScmServerUrl(scmServerUrl)) { LOG.debug("not a valid url {} for current fetcher ", scmServerUrl); return null; } @@ -96,7 +96,7 @@ public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String s if (valid.isEmpty()) { throw new ScmCommunicationException( "Unable to verify if current token is a valid Azure DevOps token. Token's scm-url needs to be '" - + azureDevOpsApiEndpoint + + azureDevOpsScmApiEndpoint + "' and was '" + token.getScmProviderUrl() + "'"); @@ -130,7 +130,7 @@ public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String s @Override public Optional isValid(PersonalAccessToken personalAccessToken) { - if (isValidScmServerUrl(personalAccessToken.getScmProviderUrl())) { + if (!isValidScmServerUrl(personalAccessToken.getScmProviderUrl())) { LOG.debug("not a valid url {} for current fetcher ", personalAccessToken.getScmProviderUrl()); return Optional.empty(); } @@ -160,6 +160,6 @@ private String getLocalAuthenticateUrl() { } private Boolean isValidScmServerUrl(String scmServerUrl) { - return azureDevOpsApiEndpoint.equals(trimEnd(scmServerUrl, '/')); + return azureDevOpsScmApiEndpoint.equals(trimEnd(scmServerUrl, '/')); } } diff --git a/wsmaster/che-core-api-factory-azure-devops/src/main/test/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcherTest.java b/wsmaster/che-core-api-factory-azure-devops/src/main/test/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcherTest.java new file mode 100644 index 00000000000..eccbbf544db --- /dev/null +++ b/wsmaster/che-core-api-factory-azure-devops/src/main/test/java/org/eclipse/che/api/factory/server/azure/devops/AzureDevOpsPersonalAccessTokenFetcherTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2012-2023 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.azure.devops; + +import org.eclipse.che.api.auth.shared.dto.OAuthToken; +import org.eclipse.che.api.factory.server.scm.PersonalAccessToken; +import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider; +import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; +import org.eclipse.che.commons.subject.Subject; +import org.eclipse.che.security.oauth.OAuthAPI; +import org.mockito.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.testng.Assert.*; + +/** + * @author Anatalii Bazko + */ +@Listeners(MockitoTestNGListener.class) +public class AzureDevOpsPersonalAccessTokenFetcherTest { + + @Mock + private AzureDevOpsApiClient azureDevOpsApiClient; + @Mock + private OAuthAPI oAuthAPI; + @Mock + private OAuthToken oAuthToken; + @Mock + private AzureDevOpsUser azureDevOpsUser; + private AzureDevOpsPersonalAccessTokenFetcher personalAccessTokenFetcher; + + @BeforeMethod + protected void start() { + personalAccessTokenFetcher = new AzureDevOpsPersonalAccessTokenFetcher( + "localhost", + "https://dev.azure.com", + new String[]{}, + azureDevOpsApiClient, + oAuthAPI); + } + + @Test + public void fetchPersonalAccessTokenShouldReturnNullIfScmServerUrlIsNotAzureDevOps() throws Exception { + PersonalAccessToken personalAccessToken = personalAccessTokenFetcher.fetchPersonalAccessToken(mock(Subject.class), "https://eclipse.org"); + + assertNull(personalAccessToken); + } + + @Test + public void fetchPersonalAccessTokenShouldReturnToken() throws Exception { + when(oAuthAPI.getToken(AzureDevOps.PROVIDER_NAME)).thenReturn(oAuthToken); + when(azureDevOpsApiClient.getUserWithOAuthToken(any())).thenReturn(azureDevOpsUser); + when(azureDevOpsApiClient.getTokenScopes(any())).thenReturn(new String[]{"vso.code_full"}); + + PersonalAccessToken personalAccessToken = personalAccessTokenFetcher.fetchPersonalAccessToken(mock(Subject.class), "https://dev.azure.com/"); + + assertNotNull(personalAccessToken); + } +}