]> source.dussan.org Git - sonarqube.git/blob
66fd47a2ad2208e8dbbb5a159d6f3df427977e28
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.almintegration.ws.github;
21
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;
41
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;
54
55 public class ListGithubRepositoriesActionTest {
56
57   @Rule
58   public UserSessionRule userSession = standalone();
59
60   private final System2 system2 = mock(System2.class);
61   private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
62
63   @Rule
64   public DbTester db = DbTester.create(system2);
65
66   private final WsActionTester ws = new WsActionTester(new ListGithubRepositoriesAction(db.getDbClient(), userSession, appClient));
67
68   @Test
69   public void fail_when_missing_create_project_permission() {
70     TestRequest request = ws.newRequest();
71     assertThatThrownBy(request::execute)
72       .isInstanceOf(UnauthorizedException.class);
73   }
74
75   @Test
76   public void fail_when_almSetting_does_not_exist() {
77     UserDto user = db.users().insertUser();
78     userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
79
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");
86   }
87
88   @Test
89   public void fail_when_personal_access_token_doesnt_exist() {
90     AlmSettingDto githubAlmSetting = setupAlm();
91
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");
98   }
99
100   @Test
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()));
104
105     when(appClient.listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1),
106       eq(100)))
107         .thenReturn(new GithubApplicationClient.Repositories()
108           .setTotal(2)
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())));
113
114     ProjectDto project = db.components().insertPrivateProjectDto(componentDto -> componentDto.setDbKey("github_HelloWorld"));
115
116     ListGithubRepositoriesWsResponse response = ws.newRequest()
117       .setParam(ListGithubRepositoriesAction.PARAM_ALM_SETTING, githubAlmSettings.getKey())
118       .setParam(ListGithubRepositoriesAction.PARAM_ORGANIZATION, "github")
119       .executeProtobuf(ListGithubRepositoriesWsResponse.class);
120
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", ""));
128
129     verify(appClient).listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1),
130       eq(100));
131   }
132
133   private AlmSettingDto setupAlm() {
134     UserDto user = db.users().insertUser();
135     userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
136
137     return db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));
138   }
139 }