Search in sources :

Example 1 with AppToken

use of org.sonar.alm.client.github.security.AppToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method checkAppPermissions_IOException.

@Test
public void checkAppPermissions_IOException() throws IOException {
    AppToken appToken = mockAppToken();
    when(httpClient.get(appUrl, appToken, "/app")).thenThrow(new IOException("OOPS"));
    assertThatThrownBy(() -> underTest.checkAppPermissions(githubAppConfiguration)).isInstanceOf(IllegalArgumentException.class).hasMessage("Failed to validate configuration, check URL and Private Key");
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with AppToken

use of org.sonar.alm.client.github.security.AppToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method checkAppPermissions_MissingPermissions.

@Test
public void checkAppPermissions_MissingPermissions() throws IOException {
    AppToken appToken = mockAppToken();
    when(httpClient.get(appUrl, appToken, "/app")).thenReturn(new OkGetResponse("{}"));
    assertThatThrownBy(() -> underTest.checkAppPermissions(githubAppConfiguration)).isInstanceOf(IllegalArgumentException.class).hasMessage("Failed to get app permissions, unexpected response body");
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken) Test(org.junit.Test)

Example 3 with AppToken

use of org.sonar.alm.client.github.security.AppToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method mockAppToken.

private AppToken mockAppToken() {
    String jwt = randomAlphanumeric(5);
    when(appSecurity.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey())).thenReturn(new AppToken(jwt));
    return new AppToken(jwt);
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken)

Example 4 with AppToken

use of org.sonar.alm.client.github.security.AppToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method checkAppPermissions_ErrorCodes.

@Test
@UseDataProvider("checkAppPermissionsErrorCodes")
public void checkAppPermissions_ErrorCodes(int errorCode, String expectedMessage) throws IOException {
    AppToken appToken = mockAppToken();
    when(httpClient.get(appUrl, appToken, "/app")).thenReturn(new ErrorGetResponse(errorCode, null));
    assertThatThrownBy(() -> underTest.checkAppPermissions(githubAppConfiguration)).isInstanceOf(IllegalArgumentException.class).hasMessage(expectedMessage);
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 5 with AppToken

use of org.sonar.alm.client.github.security.AppToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method checkAppPermissions.

@Test
public void checkAppPermissions() throws IOException {
    AppToken appToken = mockAppToken();
    String json = "{" + "      \"permissions\": {\n" + "        \"checks\": \"write\",\n" + "        \"metadata\": \"read\",\n" + "        \"statuses\": \"read\",\n" + "        \"pull_requests\": \"write\"\n" + "      }\n" + "}";
    when(httpClient.get(appUrl, appToken, "/app")).thenReturn(new OkGetResponse(json));
    assertThatCode(() -> underTest.checkAppPermissions(githubAppConfiguration)).isNull();
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken) Test(org.junit.Test)

Aggregations

AppToken (org.sonar.alm.client.github.security.AppToken)7 Test (org.junit.Test)5 IOException (java.io.IOException)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Gson (com.google.gson.Gson)1 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 String.format (java.lang.String.format)1 HTTP_FORBIDDEN (java.net.HttpURLConnection.HTTP_FORBIDDEN)1 HTTP_OK (java.net.HttpURLConnection.HTTP_OK)1 HTTP_UNAUTHORIZED (java.net.HttpURLConnection.HTTP_UNAUTHORIZED)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1