3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.almintegration.ws.github;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.alm.client.github.GithubApplicationClient;
27 import org.sonar.alm.client.github.GithubApplicationClientImpl;
28 import org.sonar.api.utils.System2;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.alm.pat.AlmPatDto;
31 import org.sonar.db.alm.setting.AlmSettingDto;
32 import org.sonar.db.permission.GlobalPermission;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.WsActionTester;
40 import org.sonarqube.ws.Common;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.assertj.core.api.Assertions.tuple;
45 import static org.mockito.ArgumentMatchers.argThat;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.ArgumentMatchers.isNull;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.verify;
50 import static org.mockito.Mockito.when;
51 import static org.sonar.server.tester.UserSessionRule.standalone;
52 import static org.sonarqube.ws.AlmIntegrations.GithubRepository;
53 import static org.sonarqube.ws.AlmIntegrations.ListGithubRepositoriesWsResponse;
55 public class ListGithubRepositoriesActionTest {
58 public UserSessionRule userSession = standalone();
60 private final System2 system2 = mock(System2.class);
61 private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
64 public DbTester db = DbTester.create(system2);
66 private final WsActionTester ws = new WsActionTester(new ListGithubRepositoriesAction(db.getDbClient(), userSession, appClient));
69 public void fail_when_missing_create_project_permission() {
70 TestRequest request = ws.newRequest();
71 assertThatThrownBy(request::execute)
72 .isInstanceOf(UnauthorizedException.class);
76 public void fail_when_almSetting_does_not_exist() {
77 UserDto user = db.users().insertUser();
78 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
80 TestRequest request = ws.newRequest()
81 .setParam(ListGithubRepositoriesAction.PARAM_ALM_SETTING, "unknown")
82 .setParam(ListGithubRepositoriesAction.PARAM_ORGANIZATION, "test");
83 assertThatThrownBy(request::execute)
84 .isInstanceOf(NotFoundException.class)
85 .hasMessage("GitHub ALM Setting 'unknown' not found");
89 public void fail_when_personal_access_token_doesnt_exist() {
90 AlmSettingDto githubAlmSetting = setupAlm();
92 TestRequest request = ws.newRequest()
93 .setParam(ListGithubRepositoriesAction.PARAM_ALM_SETTING, githubAlmSetting.getKey())
94 .setParam(ListGithubRepositoriesAction.PARAM_ORGANIZATION, "test");
95 assertThatThrownBy(request::execute)
96 .isInstanceOf(IllegalArgumentException.class)
97 .hasMessage("No personal access token found");
101 public void return_repositories_using_existing_personal_access_token() {
102 AlmSettingDto githubAlmSettings = setupAlm();
103 AlmPatDto pat = db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSettings.getUuid()).setUserUuid(userSession.getUuid()));
105 when(appClient.listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1),
107 .thenReturn(new GithubApplicationClient.Repositories()
109 .setRepositories(Stream.of("HelloWorld", "HelloUniverse")
110 .map(name -> new GithubApplicationClient.Repository(name.length(), name, false, "github/" + name,
111 "https://github-enterprise.sonarqube.com/api/v3/github/HelloWorld", "main"))
112 .collect(Collectors.toList())));
114 ProjectDto project = db.components().insertPrivateProjectDto(componentDto -> componentDto.setDbKey("github_HelloWorld"));
116 ListGithubRepositoriesWsResponse response = ws.newRequest()
117 .setParam(ListGithubRepositoriesAction.PARAM_ALM_SETTING, githubAlmSettings.getKey())
118 .setParam(ListGithubRepositoriesAction.PARAM_ORGANIZATION, "github")
119 .executeProtobuf(ListGithubRepositoriesWsResponse.class);
121 assertThat(response.getPaging())
122 .extracting(Common.Paging::getPageIndex, Common.Paging::getPageSize, Common.Paging::getTotal)
123 .containsOnly(1, 100, 2);
124 assertThat(response.getRepositoriesCount()).isEqualTo(2);
125 assertThat(response.getRepositoriesList())
126 .extracting(GithubRepository::getKey, GithubRepository::getName, GithubRepository::getSqProjectKey)
127 .containsOnly(tuple("github/HelloWorld", "HelloWorld", project.getKey()), tuple("github/HelloUniverse", "HelloUniverse", ""));
129 verify(appClient).listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1),
133 private AlmSettingDto setupAlm() {
134 UserDto user = db.users().insertUser();
135 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
137 return db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));