Skip to content

Commit

Permalink
fix: typo in AzureDevOpsPersonalAccessTokenFetcher (#457)
Browse files Browse the repository at this point in the history
* fix: typo in AzureDevOpsPersonalAccessTokenFetcher

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

* fixup

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

* fixup

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

* fixup

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

---------

Signed-off-by: Anatolii Bazko <abazko@redhat.com>
  • Loading branch information
tolusha authored Mar 2, 2023
1 parent fb260fd commit f81d126
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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()
+ "'");
Expand Down Expand Up @@ -130,7 +130,7 @@ public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String s

@Override
public Optional<Boolean> 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();
}
Expand Down Expand Up @@ -160,6 +160,6 @@ private String getLocalAuthenticateUrl() {
}

private Boolean isValidScmServerUrl(String scmServerUrl) {
return azureDevOpsApiEndpoint.equals(trimEnd(scmServerUrl, '/'));
return azureDevOpsScmApiEndpoint.equals(trimEnd(scmServerUrl, '/'));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f81d126

Please sign in to comment.