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 org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.utils.System2;
25 import org.sonar.db.DbTester;
26 import org.sonar.db.alm.setting.AlmSettingDto;
27 import org.sonar.db.permission.GlobalPermission;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.exceptions.NotFoundException;
30 import org.sonar.server.exceptions.UnauthorizedException;
31 import org.sonar.server.tester.UserSessionRule;
32 import org.sonar.server.ws.TestRequest;
33 import org.sonar.server.ws.WsActionTester;
34 import org.sonarqube.ws.AlmIntegrations;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38 import static org.mockito.Mockito.mock;
39 import static org.sonar.server.tester.UserSessionRule.standalone;
41 public class GetGithubClientIdActionTest {
44 public UserSessionRule userSession = standalone();
46 private final System2 system2 = mock(System2.class);
49 public DbTester db = DbTester.create(system2);
51 private final WsActionTester ws = new WsActionTester(new GetGithubClientIdAction(db.getDbClient(), userSession));
54 public void get_client_id() {
55 UserDto user = db.users().insertUser();
56 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
57 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));
59 AlmIntegrations.GithubClientIdWsResponse response = ws.newRequest().setParam(GetGithubClientIdAction.PARAM_ALM_SETTING, githubAlmSetting.getKey())
60 .executeProtobuf(AlmIntegrations.GithubClientIdWsResponse.class);
62 assertThat(response.getClientId()).isEqualTo(githubAlmSetting.getClientId());
66 public void fail_when_missing_create_project_permission() {
67 TestRequest request = ws.newRequest();
68 assertThatThrownBy(request::execute)
69 .isInstanceOf(UnauthorizedException.class);
73 public void fail_when_almSetting_does_not_exist() {
74 UserDto user = db.users().insertUser();
75 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
77 TestRequest request = ws.newRequest().setParam(GetGithubClientIdAction.PARAM_ALM_SETTING, "unknown");
78 assertThatThrownBy(request::execute)
79 .isInstanceOf(NotFoundException.class)
80 .hasMessage("Github ALM Setting 'unknown' not found");
84 public void fail_when_client_id_does_not_exist() {
85 UserDto user = db.users().insertUser();
86 userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
87 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
89 TestRequest request = ws.newRequest()
90 .setParam(GetGithubClientIdAction.PARAM_ALM_SETTING, githubAlmSetting.getKey());
91 assertThatThrownBy(request::execute)
92 .isInstanceOf(NotFoundException.class)
93 .hasMessage("No client ID for setting with key '%s'", githubAlmSetting.getKey());