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.Optional;
23 import org.junit.Before;
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.server.ws.WebService;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.i18n.I18n;
31 import org.sonar.core.util.SequenceUuidFactory;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.alm.setting.AlmSettingDto;
34 import org.sonar.db.permission.GlobalPermission;
35 import org.sonar.db.project.ProjectDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.almintegration.ws.ImportHelper;
38 import org.sonar.server.component.ComponentUpdater;
39 import org.sonar.server.es.TestProjectIndexers;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.NotFoundException;
42 import org.sonar.server.exceptions.UnauthorizedException;
43 import org.sonar.server.favorite.FavoriteUpdater;
44 import org.sonar.server.permission.PermissionTemplateService;
45 import org.sonar.server.project.ProjectDefaultVisibility;
46 import org.sonar.server.project.Visibility;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.ws.TestRequest;
49 import org.sonar.server.ws.WsActionTester;
50 import org.sonarqube.ws.Projects;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.assertj.core.api.Assertions.assertThatThrownBy;
54 import static org.assertj.core.api.Assertions.tuple;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
58 import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
59 import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_ORGANIZATION;
60 import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_REPOSITORY_KEY;
61 import static org.sonar.server.tester.UserSessionRule.standalone;
63 public class ImportGithubProjectActionTest {
66 public UserSessionRule userSession = standalone();
68 private final System2 system2 = mock(System2.class);
69 private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
72 public DbTester db = DbTester.create(system2);
74 private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
75 mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory());
77 private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
78 private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
79 private final WsActionTester ws = new WsActionTester(new ImportGithubProjectAction(db.getDbClient(), userSession,
80 projectDefaultVisibility, appClient, componentUpdater, importHelper));
83 public void before() {
84 when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
88 public void import_project() {
89 AlmSettingDto githubAlmSetting = setupAlm();
90 db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
92 GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, "Hello-World", false, "octocat/Hello-World",
93 "https://github.sonarsource.com/api/v3/repos/octocat/Hello-World");
94 when(appClient.getRepository(any(), any(), any(), any()))
95 .thenReturn(Optional.of(repository));
97 Projects.CreateWsResponse response = ws.newRequest()
98 .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
99 .setParam(PARAM_ORGANIZATION, "octocat")
100 .setParam(PARAM_REPOSITORY_KEY, "octocat/Hello-World")
101 .executeProtobuf(Projects.CreateWsResponse.class);
103 Projects.CreateWsResponse.Project result = response.getProject();
104 assertThat(result.getKey()).isEqualTo(repository.getFullName().replace("/", "_"));
105 assertThat(result.getName()).isEqualTo(repository.getName());
107 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
108 assertThat(projectDto).isPresent();
109 assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
113 public void fail_project_already_exist() {
114 AlmSettingDto githubAlmSetting = setupAlm();
115 db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
116 db.components().insertPublicProject(p -> p.setDbKey("octocat_Hello-World"));
118 GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, "Hello-World", false, "octocat/Hello-World",
119 "https://github.sonarsource.com/api/v3/repos/octocat/Hello-World");
120 when(appClient.getRepository(any(), any(), any(), any()))
121 .thenReturn(Optional.of(repository));
123 TestRequest request = ws.newRequest()
124 .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
125 .setParam(PARAM_ORGANIZATION, "octocat")
126 .setParam(PARAM_REPOSITORY_KEY, "octocat/Hello-World");
127 assertThatThrownBy(() -> request.execute())
128 .isInstanceOf(BadRequestException.class)
129 .hasMessage("Could not create null, key already exists: octocat_Hello-World");
133 public void fail_when_not_logged_in() {
134 TestRequest request = ws.newRequest()
135 .setParam(PARAM_ALM_SETTING, "asdfghjkl")
136 .setParam(PARAM_ORGANIZATION, "test")
137 .setParam(PARAM_REPOSITORY_KEY, "test/repo");
138 assertThatThrownBy(() -> request
140 .isInstanceOf(UnauthorizedException.class);
144 public void fail_when_missing_create_project_permission() {
145 TestRequest request = ws.newRequest();
146 assertThatThrownBy(() -> request.execute())
147 .isInstanceOf(UnauthorizedException.class);
151 public void fail_when_almSetting_does_not_exist() {
152 UserDto user = db.users().insertUser();
153 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
155 TestRequest request = ws.newRequest()
156 .setParam(PARAM_ALM_SETTING, "unknown")
157 .setParam(PARAM_ORGANIZATION, "test")
158 .setParam(PARAM_REPOSITORY_KEY, "test/repo");
159 assertThatThrownBy(() -> request
161 .isInstanceOf(NotFoundException.class)
162 .hasMessage("ALM Setting 'unknown' not found");
166 public void fail_when_personal_access_token_doesnt_exist() {
167 AlmSettingDto githubAlmSetting = setupAlm();
169 TestRequest request = ws.newRequest()
170 .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
171 .setParam(PARAM_ORGANIZATION, "test")
172 .setParam(PARAM_REPOSITORY_KEY, "test/repo");
173 assertThatThrownBy(() -> request.execute())
174 .isInstanceOf(IllegalArgumentException.class)
175 .hasMessage("No personal access token found");
179 public void definition() {
180 WebService.Action def = ws.getDef();
182 assertThat(def.since()).isEqualTo("8.4");
183 assertThat(def.isPost()).isTrue();
184 assertThat(def.params())
185 .extracting(WebService.Param::key, WebService.Param::isRequired)
186 .containsExactlyInAnyOrder(
187 tuple(PARAM_ALM_SETTING, true),
188 tuple(PARAM_ORGANIZATION, true),
189 tuple(PARAM_REPOSITORY_KEY, true));
192 private AlmSettingDto setupAlm() {
193 UserDto user = db.users().insertUser();
194 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
196 return db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));