use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.
the class HttpBitbucketServerApiClientTest method shouldBeAbleToCreatePAT.
@Test
public void shouldBeAbleToCreatePAT() throws ScmCommunicationException, ScmBadRequestException, ScmUnauthorizedException {
// given
stubFor(put(urlPathEqualTo("/rest/access-tokens/1.0/users/ksmster")).withHeader(HttpHeaders.AUTHORIZATION, equalTo(AUTHORIZATION_TOKEN)).withHeader(HttpHeaders.ACCEPT, equalTo(MediaType.APPLICATION_JSON)).withHeader(HttpHeaders.CONTENT_TYPE, equalTo(MediaType.APPLICATION_JSON)).withHeader(HttpHeaders.CONTENT_LENGTH, equalTo("63")).willReturn(ok().withBodyFile("bitbucket/rest/access-tokens/1.0/users/ksmster/newtoken.json")));
// when
BitbucketPersonalAccessToken result = bitbucketServer.createPersonalAccessTokens("ksmster", "myToKen", ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE"));
// then
assertNotNull(result);
assertEquals(result.getToken(), "MTU4OTEwNTMyOTA5Ohc88HcY8k7gWOzl2mP5TtdtY5Qs");
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
try {
BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
LOG.debug("Current bitbucket user {} ", user);
// cleanup existed
List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
for (BitbucketPersonalAccessToken existedToken : existingTokens) {
LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
}
BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
} catch (ScmBadRequestException | ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcherTest method setup.
@BeforeMethod
public void setup() throws MalformedURLException {
URL apiEndpoint = new URL("https://che.server.com");
subject = new SubjectImpl("another_user", "user987", "token111", false);
bitbucketUser = new BitbucketUser("User", "user", 32423523, "NORMAL", true, "user", "user@users.com");
bitbucketPersonalAccessToken = new BitbucketPersonalAccessToken(234234, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "2340590skdf3<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE"));
bitbucketPersonalAccessToken2 = new BitbucketPersonalAccessToken(3647456, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "34545<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("REPO_READ"));
bitbucketPersonalAccessToken3 = new BitbucketPersonalAccessToken(132423, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "3456\\<0>945//i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_READ", "REPO_READ"));
fetcher = new BitbucketServerPersonalAccessTokenFetcher(bitbucketServerApiClient, apiEndpoint);
EnvironmentContext context = new EnvironmentContext();
context.setSubject(subject);
EnvironmentContext.setCurrent(context);
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcherTest method shouldBeAbleToFetchPersonalAccessToken.
@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(bitbucketServerApiClient.getPersonalAccessTokens(eq(bitbucketUser.getSlug()))).thenReturn(Collections.emptyList());
when(bitbucketServerApiClient.createPersonalAccessTokens(eq(bitbucketUser.getSlug()), eq("che-token-<user987>-<che.server.com>"), eq(ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE")))).thenReturn(bitbucketPersonalAccessToken);
// when
PersonalAccessToken result = fetcher.fetchPersonalAccessToken(subject, someBitbucketURL);
// then
assertNotNull(result);
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcherTest method shouldDeleteExistedCheTokenBeforeCreatingNew.
@Test
public void shouldDeleteExistedCheTokenBeforeCreatingNew() throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException, ScmBadRequestException {
when(bitbucketServerApiClient.isConnected(eq(someBitbucketURL))).thenReturn(true);
when(bitbucketServerApiClient.getUser(eq(subject))).thenReturn(bitbucketUser);
when(bitbucketServerApiClient.getPersonalAccessTokens(eq(bitbucketUser.getSlug()))).thenReturn(ImmutableList.of(bitbucketPersonalAccessToken, bitbucketPersonalAccessToken2));
when(bitbucketServerApiClient.createPersonalAccessTokens(eq(bitbucketUser.getSlug()), eq("che-token-<user987>-<che.server.com>"), eq(ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE")))).thenReturn(bitbucketPersonalAccessToken3);
// when
PersonalAccessToken result = fetcher.fetchPersonalAccessToken(subject, someBitbucketURL);
// then
assertNotNull(result);
verify(bitbucketServerApiClient).deletePersonalAccessTokens(eq(bitbucketUser.getSlug()), eq(bitbucketPersonalAccessToken.getId()));
verify(bitbucketServerApiClient).deletePersonalAccessTokens(eq(bitbucketUser.getSlug()), eq(bitbucketPersonalAccessToken2.getId()));
}
Aggregations