]> source.dussan.org Git - sonarqube.git/blob
16174e399340d681c99398c1753b108af572b507
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.branch.ws;
21
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.BranchDto;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ResourceTypesRule;
33 import org.sonar.server.component.ComponentFinder;
34 import org.sonar.server.exceptions.ForbiddenException;
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.WsActionTester;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.sonar.api.resources.Qualifiers.PROJECT;
43 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
44
45 public class SetAutomaticDeletionProtectionActionTest {
46
47   @Rule
48   public DbTester db = DbTester.create(System2.INSTANCE);
49   @Rule
50   public UserSessionRule userSession = UserSessionRule.standalone();
51
52   private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT);
53   private ComponentFinder componentFinder = new ComponentFinder(db.getDbClient(), resourceTypes);
54   private WsActionTester tester = new WsActionTester(new SetAutomaticDeletionProtectionAction(db.getDbClient(), userSession, componentFinder));
55
56   @Test
57   public void test_definition() {
58     WebService.Action definition = tester.getDef();
59     assertThat(definition.key()).isEqualTo("set_automatic_deletion_protection");
60     assertThat(definition.isPost()).isTrue();
61     assertThat(definition.isInternal()).isFalse();
62     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch", "value");
63     assertThat(definition.since()).isEqualTo("8.1");
64   }
65
66   @Test
67   public void fail_if_missing_project_parameter() {
68     userSession.logIn();
69
70     assertThatThrownBy(() -> tester.newRequest().execute())
71       .isInstanceOf(IllegalArgumentException.class)
72       .hasMessageContaining("The 'project' parameter is missing");
73   }
74
75   @Test
76   public void fail_if_missing_branch_parameter() {
77     userSession.logIn();
78
79     assertThatThrownBy(() -> tester.newRequest()
80       .setParam("project", "projectName")
81       .execute())
82       .isInstanceOf(IllegalArgumentException.class)
83       .hasMessageContaining("The 'branch' parameter is missing");
84   }
85
86   @Test
87   public void fail_if_missing_value_parameter() {
88     userSession.logIn();
89
90     assertThatThrownBy(() -> tester.newRequest()
91       .setParam("project", "projectName")
92       .setParam("branch", "foobar")
93       .execute())
94       .isInstanceOf(IllegalArgumentException.class)
95       .hasMessageContaining("The 'value' parameter is missing");
96   }
97
98   @Test
99   public void fail_if_not_logged_in() {
100     assertThatThrownBy(() -> tester.newRequest().execute())
101       .isInstanceOf(UnauthorizedException.class)
102       .hasMessageContaining("Authentication is required");
103   }
104
105   @Test
106   public void fail_if_no_administer_permission() {
107     userSession.logIn();
108     ComponentDto project = db.components().insertPublicProject();
109
110     assertThatThrownBy(() -> tester.newRequest()
111       .setParam("project", project.getKey())
112       .setParam("branch", "branch1")
113       .setParam("value", "true")
114       .execute())
115       .isInstanceOf(ForbiddenException.class)
116       .hasMessageContaining("Insufficient privileges");
117   }
118
119   @Test
120   public void fail_when_attempting_to_set_main_branch_as_included_in_purge() {
121     userSession.logIn();
122     ComponentDto project = db.components().insertPublicProject();
123     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
124     userSession.addProjectPermission(UserRole.ADMIN, project);
125
126     assertThatThrownBy(() -> tester.newRequest()
127       .setParam("project", project.getKey())
128       .setParam("branch", DEFAULT_PROJECT_MAIN_BRANCH_NAME)
129       .setParam("value", "false")
130       .execute())
131       .isInstanceOf(IllegalArgumentException.class)
132       .hasMessageContaining("Main branch of the project is always excluded from automatic deletion.");
133   }
134
135   @Test
136   public void set_purge_exclusion() {
137     userSession.logIn();
138     ComponentDto project = db.components().insertPublicProject();
139     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
140     userSession.addProjectPermission(UserRole.ADMIN, project);
141
142     tester.newRequest()
143       .setParam("project", project.getKey())
144       .setParam("branch", "branch1")
145       .setParam("value", "true")
146       .execute();
147
148     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
149     Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid());
150     assertThat(mainBranch.get().getKey()).isEqualTo(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
151     assertThat(mainBranch.get().isExcludeFromPurge()).isTrue();
152
153     Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid());
154     assertThat(branchDto.get().getKey()).isEqualTo("branch1");
155     assertThat(branchDto.get().isExcludeFromPurge()).isTrue();
156   }
157
158   @Test
159   public void fail_on_non_boolean_value_parameter() {
160     userSession.logIn();
161     ComponentDto project = db.components().insertPublicProject();
162
163     assertThatThrownBy(() -> tester.newRequest()
164       .setParam("project", project.getKey())
165       .setParam("branch", "branch1")
166       .setParam("value", "foobar")
167       .execute())
168       .isInstanceOf(IllegalArgumentException.class)
169       .hasMessageContaining("Value of parameter 'value' (foobar) must be one of: [true, false, yes, no]");
170   }
171
172   @Test
173   public void fail_if_project_does_not_exist() {
174     userSession.logIn();
175
176     assertThatThrownBy(() -> tester.newRequest()
177       .setParam("project", "foo")
178       .setParam("branch", "branch1")
179       .setParam("value", "true")
180       .execute())
181       .isInstanceOf(NotFoundException.class)
182       .hasMessageContaining("Project 'foo' not found");
183   }
184
185   @Test
186   public void fail_if_branch_does_not_exist() {
187     userSession.logIn();
188     ComponentDto project = db.components().insertPublicProject();
189     userSession.addProjectPermission(UserRole.ADMIN, project);
190
191     assertThatThrownBy(() -> tester.newRequest()
192       .setParam("project", project.getKey())
193       .setParam("branch", "branch1")
194       .setParam("value", "true")
195       .execute())
196       .isInstanceOf(NotFoundException.class)
197       .hasMessageContaining("Branch 'branch1' not found for project '" + project.getKey() + "'");
198   }
199 }