]> source.dussan.org Git - sonarqube.git/blob
6321c9e68a89be0ba378614c0b3cd795ada775eb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.permission.ws.template;
21
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.resources.Qualifiers;
26 import org.sonar.api.resources.ResourceTypes;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.core.permission.GlobalPermissions;
30 import org.sonar.core.util.Uuids;
31 import org.sonar.db.component.ResourceTypesRule;
32 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
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.permission.PermissionService;
38 import org.sonar.server.permission.PermissionServiceImpl;
39 import org.sonar.server.permission.RequestValidator;
40 import org.sonar.server.permission.ws.BasePermissionWsTest;
41 import org.sonar.server.permission.ws.WsParameters;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.assertj.core.api.Assertions.assertThatThrownBy;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
50
51 public class RemoveProjectCreatorFromTemplateActionTest extends BasePermissionWsTest<RemoveProjectCreatorFromTemplateAction> {
52
53   private System2 system = mock(System2.class);
54   private PermissionTemplateDto template;
55   private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
56   private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
57   private WsParameters wsParameters = new WsParameters(permissionService);
58   private RequestValidator requestValidator = new RequestValidator(permissionService);
59
60   @Override
61   protected RemoveProjectCreatorFromTemplateAction buildWsAction() {
62     return new RemoveProjectCreatorFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, system, wsParameters, requestValidator);
63   }
64
65   @Before
66   public void setUp() {
67     loginAsAdmin();
68     when(system.now()).thenReturn(2_000_000_000L);
69     template = db.permissionTemplates().insertTemplate();
70   }
71
72   @Test
73   public void update_template_permission() {
74     PermissionTemplateCharacteristicDto characteristic = db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(),
75       new PermissionTemplateCharacteristicDto()
76         .setUuid(Uuids.createFast())
77         .setTemplateUuid(template.getUuid())
78         .setPermission(UserRole.USER)
79         .setWithProjectCreator(false)
80         .setCreatedAt(1_000_000_000L)
81         .setUpdatedAt(1_000_000_000L),
82       template.getName());
83     db.commit();
84     when(system.now()).thenReturn(3_000_000_000L);
85
86     newRequest()
87       .setParam(PARAM_PERMISSION, UserRole.USER)
88       .setParam(PARAM_TEMPLATE_NAME, template.getName())
89       .execute();
90
91     assertWithoutProjectCreatorFor(UserRole.USER);
92     PermissionTemplateCharacteristicDto reloaded = reload(characteristic);
93     assertThat(reloaded.getCreatedAt()).isEqualTo(1_000_000_000L);
94     assertThat(reloaded.getUpdatedAt()).isEqualTo(3_000_000_000L);
95   }
96
97   @Test
98   public void do_not_fail_when_no_template_permission() {
99     newRequest()
100       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
101       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
102       .execute();
103
104     assertNoTemplatePermissionFor(UserRole.ADMIN);
105   }
106
107   @Test
108   public void fail_when_template_does_not_exist() {
109     assertThatThrownBy(() ->  {
110       newRequest()
111         .setParam(PARAM_PERMISSION, UserRole.ADMIN)
112         .setParam(PARAM_TEMPLATE_ID, "42")
113         .execute();
114     })
115       .isInstanceOf(NotFoundException.class);
116   }
117
118   @Test
119   public void fail_if_permission_is_not_a_project_permission() {
120     assertThatThrownBy(() ->  {
121       newRequest()
122         .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
123         .setParam(PARAM_TEMPLATE_ID, template.getUuid())
124         .execute();
125     })
126       .isInstanceOf(IllegalArgumentException.class);
127   }
128
129   @Test
130   public void fail_if_not_authenticated() {
131     userSession.anonymous();
132
133     assertThatThrownBy(() ->  {
134       newRequest()
135         .setParam(PARAM_PERMISSION, UserRole.ADMIN)
136         .setParam(PARAM_TEMPLATE_ID, template.getUuid())
137         .execute();
138     })
139       .isInstanceOf(UnauthorizedException.class);
140   }
141
142   @Test
143   public void fail_if_insufficient_privileges() {
144     userSession.logIn();
145
146     assertThatThrownBy(() ->  {
147       newRequest()
148         .setParam(PARAM_PERMISSION, UserRole.ADMIN)
149         .setParam(PARAM_TEMPLATE_ID, template.getUuid())
150         .execute();
151     })
152       .isInstanceOf(ForbiddenException.class);
153   }
154
155   private void assertWithoutProjectCreatorFor(String permission) {
156     Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
157       permission, template.getUuid());
158     assertThat(templatePermission).isPresent();
159     assertThat(templatePermission.get().getWithProjectCreator()).isFalse();
160   }
161
162   private void assertNoTemplatePermissionFor(String permission) {
163     Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
164       permission, template.getUuid());
165     assertThat(templatePermission).isNotPresent();
166   }
167
168   private PermissionTemplateCharacteristicDto reload(PermissionTemplateCharacteristicDto characteristic) {
169     return db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(), characteristic.getPermission(), characteristic.getTemplateUuid())
170       .get();
171   }
172 }