Search in sources :

Example 1 with UserAccessToken

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

the class GithubApplicationClientImplTest method getRepository_returns_empty_when_repository_doesnt_exist.

@Test
public void getRepository_returns_empty_when_repository_doesnt_exist() throws IOException {
    when(httpClient.get(any(), any(), any())).thenReturn(new Response(404, null));
    Optional<GithubApplicationClient.Repository> repository = underTest.getRepository(appUrl, new UserAccessToken("temp"), "octocat", "octocat/Hello-World");
    assertThat(repository).isEmpty();
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 2 with UserAccessToken

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

the class GithubApplicationClientImplTest method getRepository_returns_repository.

@Test
public void getRepository_returns_repository() throws IOException {
    String appUrl = "https://github.sonarsource.com";
    AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
    String responseJson = "{\n" + "  \"id\": 1296269,\n" + "  \"node_id\": \"MDEwOlJlcG9zaXRvcnkxMjk2MjY5\",\n" + "  \"name\": \"Hello-World\",\n" + "  \"full_name\": \"octocat/Hello-World\",\n" + "  \"owner\": {\n" + "    \"login\": \"octocat\",\n" + "    \"id\": 1,\n" + "    \"node_id\": \"MDQ6VXNlcjE=\",\n" + "    \"avatar_url\": \"https://github.sonarsource.com/images/error/octocat_happy.gif\",\n" + "    \"gravatar_id\": \"\",\n" + "    \"url\": \"https://github.sonarsource.com/api/v3/users/octocat\",\n" + "    \"html_url\": \"https://github.com/octocat\",\n" + "    \"followers_url\": \"https://github.sonarsource.com/api/v3/users/octocat/followers\",\n" + "    \"following_url\": \"https://github.sonarsource.com/api/v3/users/octocat/following{/other_user}\",\n" + "    \"gists_url\": \"https://github.sonarsource.com/api/v3/users/octocat/gists{/gist_id}\",\n" + "    \"starred_url\": \"https://github.sonarsource.com/api/v3/users/octocat/starred{/owner}{/repo}\",\n" + "    \"subscriptions_url\": \"https://github.sonarsource.com/api/v3/users/octocat/subscriptions\",\n" + "    \"organizations_url\": \"https://github.sonarsource.com/api/v3/users/octocat/orgs\",\n" + "    \"repos_url\": \"https://github.sonarsource.com/api/v3/users/octocat/repos\",\n" + "    \"events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/events{/privacy}\",\n" + "    \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/received_events\",\n" + "    \"type\": \"User\",\n" + "    \"site_admin\": false\n" + "  },\n" + "  \"private\": false,\n" + "  \"html_url\": \"https://github.com/octocat/Hello-World\",\n" + "  \"description\": \"This your first repo!\",\n" + "  \"fork\": false,\n" + "  \"url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World\",\n" + "  \"archive_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/{archive_format}{/ref}\",\n" + "  \"assignees_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/assignees{/user}\",\n" + "  \"blobs_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/git/blobs{/sha}\",\n" + "  \"branches_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/branches{/branch}\",\n" + "  \"collaborators_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/collaborators{/collaborator}\",\n" + "  \"comments_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/comments{/number}\",\n" + "  \"commits_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/commits{/sha}\",\n" + "  \"compare_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/compare/{base}...{head}\",\n" + "  \"contents_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/contents/{+path}\",\n" + "  \"contributors_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/contributors\",\n" + "  \"deployments_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/deployments\",\n" + "  \"downloads_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/downloads\",\n" + "  \"events_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/events\",\n" + "  \"forks_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/forks\",\n" + "  \"git_commits_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/git/commits{/sha}\",\n" + "  \"git_refs_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/git/refs{/sha}\",\n" + "  \"git_tags_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/git/tags{/sha}\",\n" + "  \"git_url\": \"git:github.com/octocat/Hello-World.git\",\n" + "  \"issue_comment_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/issues/comments{/number}\",\n" + "  \"issue_events_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/issues/events{/number}\",\n" + "  \"issues_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/issues{/number}\",\n" + "  \"keys_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/keys{/key_id}\",\n" + "  \"labels_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/labels{/name}\",\n" + "  \"languages_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/languages\",\n" + "  \"merges_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/merges\",\n" + "  \"milestones_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/milestones{/number}\",\n" + "  \"notifications_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/notifications{?since,all,participating}\",\n" + "  \"pulls_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/pulls{/number}\",\n" + "  \"releases_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/releases{/id}\",\n" + "  \"ssh_url\": \"git@github.com:octocat/Hello-World.git\",\n" + "  \"stargazers_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/stargazers\",\n" + "  \"statuses_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/statuses/{sha}\",\n" + "  \"subscribers_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/subscribers\",\n" + "  \"subscription_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/subscription\",\n" + "  \"tags_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/tags\",\n" + "  \"teams_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/teams\",\n" + "  \"trees_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/git/trees{/sha}\",\n" + "  \"clone_url\": \"https://github.com/octocat/Hello-World.git\",\n" + "  \"mirror_url\": \"git:git.example.com/octocat/Hello-World\",\n" + "  \"hooks_url\": \"https://github.sonarsource.com/api/v3/repos/octocat/Hello-World/hooks\",\n" + "  \"svn_url\": \"https://svn.github.com/octocat/Hello-World\",\n" + "  \"homepage\": \"https://github.com\",\n" + "  \"language\": null,\n" + "  \"forks_count\": 9,\n" + "  \"stargazers_count\": 80,\n" + "  \"watchers_count\": 80,\n" + "  \"size\": 108,\n" + "  \"default_branch\": \"master\",\n" + "  \"open_issues_count\": 0,\n" + "  \"is_template\": true,\n" + "  \"topics\": [\n" + "    \"octocat\",\n" + "    \"atom\",\n" + "    \"electron\",\n" + "    \"api\"\n" + "  ],\n" + "  \"has_issues\": true,\n" + "  \"has_projects\": true,\n" + "  \"has_wiki\": true,\n" + "  \"has_pages\": false,\n" + "  \"has_downloads\": true,\n" + "  \"archived\": false,\n" + "  \"disabled\": false,\n" + "  \"visibility\": \"public\",\n" + "  \"pushed_at\": \"2011-01-26T19:06:43Z\",\n" + "  \"created_at\": \"2011-01-26T19:01:12Z\",\n" + "  \"updated_at\": \"2011-01-26T19:14:43Z\",\n" + "  \"permissions\": {\n" + "    \"admin\": false,\n" + "    \"push\": false,\n" + "    \"pull\": true\n" + "  },\n" + "  \"allow_rebase_merge\": true,\n" + "  \"template_repository\": null,\n" + "  \"allow_squash_merge\": true,\n" + "  \"allow_merge_commit\": true,\n" + "  \"subscribers_count\": 42,\n" + "  \"network_count\": 0,\n" + "  \"anonymous_access_enabled\": false,\n" + "  \"license\": {\n" + "    \"key\": \"mit\",\n" + "    \"name\": \"MIT License\",\n" + "    \"spdx_id\": \"MIT\",\n" + "    \"url\": \"https://github.sonarsource.com/api/v3/licenses/mit\",\n" + "    \"node_id\": \"MDc6TGljZW5zZW1pdA==\"\n" + "  },\n" + "  \"organization\": {\n" + "    \"login\": \"octocat\",\n" + "    \"id\": 1,\n" + "    \"node_id\": \"MDQ6VXNlcjE=\",\n" + "    \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n" + "    \"gravatar_id\": \"\",\n" + "    \"url\": \"https://github.sonarsource.com/api/v3/users/octocat\",\n" + "    \"html_url\": \"https://github.com/octocat\",\n" + "    \"followers_url\": \"https://github.sonarsource.com/api/v3/users/octocat/followers\",\n" + "    \"following_url\": \"https://github.sonarsource.com/api/v3/users/octocat/following{/other_user}\",\n" + "    \"gists_url\": \"https://github.sonarsource.com/api/v3/users/octocat/gists{/gist_id}\",\n" + "    \"starred_url\": \"https://github.sonarsource.com/api/v3/users/octocat/starred{/owner}{/repo}\",\n" + "    \"subscriptions_url\": \"https://github.sonarsource.com/api/v3/users/octocat/subscriptions\",\n" + "    \"organizations_url\": \"https://github.sonarsource.com/api/v3/users/octocat/orgs\",\n" + "    \"repos_url\": \"https://github.sonarsource.com/api/v3/users/octocat/repos\",\n" + "    \"events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/events{/privacy}\",\n" + "    \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/received_events\",\n" + "    \"type\": \"Organization\",\n" + "    \"site_admin\": false\n" + "  }" + "}";
    when(httpClient.get(appUrl, accessToken, "/repos/octocat/Hello-World")).thenReturn(new GithubApplicationHttpClient.GetResponse() {

        @Override
        public Optional<String> getNextEndPoint() {
            return Optional.empty();
        }

        @Override
        public int getCode() {
            return 200;
        }

        @Override
        public Optional<String> getContent() {
            return Optional.of(responseJson);
        }
    });
    Optional<GithubApplicationClient.Repository> repository = underTest.getRepository(appUrl, accessToken, "octocat", "octocat/Hello-World");
    assertThat(repository).isPresent().get().extracting(GithubApplicationClient.Repository::getId, GithubApplicationClient.Repository::getName, GithubApplicationClient.Repository::getFullName, GithubApplicationClient.Repository::getUrl, GithubApplicationClient.Repository::isPrivate, GithubApplicationClient.Repository::getDefaultBranch).containsOnly(1296269L, "Hello-World", "octocat/Hello-World", "https://github.com/octocat/Hello-World", false, "master");
}
Also used : Optional(java.util.Optional) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AccessToken(org.sonar.alm.client.github.security.AccessToken) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 3 with UserAccessToken

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

the class GithubApplicationClientImplTest method createUserAccessToken_from_authorization_code_returns_access_token.

@Test
@UseDataProvider("githubServers")
public void createUserAccessToken_from_authorization_code_returns_access_token(String apiUrl, String appUrl) throws IOException {
    String token = randomAlphanumeric(10);
    when(httpClient.post(appUrl, null, "/login/oauth/access_token?client_id=clientId&client_secret=clientSecret&code=code")).thenReturn(new OkGetResponse("access_token=" + token + "&status="));
    UserAccessToken userAccessToken = underTest.createUserAccessToken(apiUrl, "clientId", "clientSecret", "code");
    assertThat(userAccessToken).extracting(UserAccessToken::getValue, UserAccessToken::getAuthorizationHeaderPrefix).containsOnly(token, "token");
    verify(httpClient).post(appUrl, null, "/login/oauth/access_token?client_id=clientId&client_secret=clientSecret&code=code");
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 4 with UserAccessToken

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

the class GithubApplicationClientImplTest method listOrganizations_fail_if_pageIndex_out_of_bounds.

@Test
public void listOrganizations_fail_if_pageIndex_out_of_bounds() {
    UserAccessToken token = new UserAccessToken("token");
    assertThatThrownBy(() -> underTest.listOrganizations(appUrl, token, 0, 100)).isInstanceOf(IllegalArgumentException.class).hasMessage("'page' must be larger than 0.");
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 5 with UserAccessToken

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

the class GithubApplicationClientImplTest method listOrganizations_fail_if_pageSize_out_of_bounds.

@Test
public void listOrganizations_fail_if_pageSize_out_of_bounds() {
    UserAccessToken token = new UserAccessToken("token");
    assertThatThrownBy(() -> underTest.listOrganizations(appUrl, token, 1, 0)).isInstanceOf(IllegalArgumentException.class).hasMessage("'pageSize' must be a value larger than 0 and smaller or equal to 100.");
    assertThatThrownBy(() -> underTest.listOrganizations("", token, 1, 101)).isInstanceOf(IllegalArgumentException.class).hasMessage("'pageSize' must be a value larger than 0 and smaller or equal to 100.");
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Aggregations

UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)21 Test (org.junit.Test)18 AccessToken (org.sonar.alm.client.github.security.AccessToken)11 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)6 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)4 NotFoundException (org.sonar.server.exceptions.NotFoundException)4 ListGithubOrganizationsWsResponse (org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)4 IOException (java.io.IOException)3 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)3 DbSession (org.sonar.db.DbSession)3 Common (org.sonarqube.ws.Common)3 Optional (java.util.Optional)2 Repository (org.sonar.alm.client.github.GithubApplicationClient.Repository)2 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assertions.tuple (org.assertj.core.api.Assertions.tuple)1