]> source.dussan.org Git - sonarqube.git/blob
64a693ae937062dde02bb13914f905aa01a5d710
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.utils.System2;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.core.permission.GlobalPermissions;
28 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.server.exceptions.ForbiddenException;
31 import org.sonar.server.exceptions.NotFoundException;
32 import org.sonar.server.exceptions.UnauthorizedException;
33 import org.sonar.server.permission.ws.BasePermissionWsTest;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
39 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
40 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
41
42 public class RemoveProjectCreatorFromTemplateActionTest extends BasePermissionWsTest<RemoveProjectCreatorFromTemplateAction> {
43
44   private System2 system = mock(System2.class);
45   private PermissionTemplateDto template;
46
47   @Override
48   protected RemoveProjectCreatorFromTemplateAction buildWsAction() {
49     return new RemoveProjectCreatorFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, system);
50   }
51
52   @Before
53   public void setUp() {
54     loginAsAdminOnDefaultOrganization();
55     when(system.now()).thenReturn(2_000_000_000L);
56     template = insertTemplate();
57   }
58
59   @Test
60   public void update_template_permission() throws Exception {
61     PermissionTemplateCharacteristicDto characteristic = db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(),
62       new PermissionTemplateCharacteristicDto()
63         .setTemplateId(template.getId())
64         .setPermission(UserRole.USER)
65         .setWithProjectCreator(false)
66         .setCreatedAt(1_000_000_000L)
67         .setUpdatedAt(1_000_000_000L));
68     db.commit();
69     when(system.now()).thenReturn(3_000_000_000L);
70
71     newRequest()
72       .setParam(PARAM_PERMISSION, UserRole.USER)
73       .setParam(PARAM_TEMPLATE_NAME, template.getName())
74       .execute();
75
76     assertWithoutProjectCreatorFor(UserRole.USER);
77     PermissionTemplateCharacteristicDto reloaded = reload(characteristic);
78     assertThat(reloaded.getCreatedAt()).isEqualTo(1_000_000_000L);
79     assertThat(reloaded.getUpdatedAt()).isEqualTo(3_000_000_000L);
80   }
81
82   @Test
83   public void do_not_fail_when_no_template_permission() throws Exception {
84     newRequest()
85       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
86       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
87       .execute();
88
89     assertNoTemplatePermissionFor(UserRole.ADMIN);
90   }
91
92   @Test
93   public void fail_when_template_does_not_exist() throws Exception {
94     expectedException.expect(NotFoundException.class);
95
96     newRequest()
97       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
98       .setParam(PARAM_TEMPLATE_ID, "42")
99       .execute();
100   }
101
102   @Test
103   public void fail_if_permission_is_not_a_project_permission() throws Exception {
104     expectedException.expect(IllegalArgumentException.class);
105
106     newRequest()
107       .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
108       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
109       .execute();
110   }
111
112   @Test
113   public void fail_if_not_authenticated() throws Exception {
114     expectedException.expect(UnauthorizedException.class);
115     userSession.anonymous();
116
117     newRequest()
118       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
119       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
120       .execute();
121   }
122
123   @Test
124   public void fail_if_insufficient_privileges() throws Exception {
125     expectedException.expect(ForbiddenException.class);
126     userSession.logIn().setGlobalPermissions(GlobalPermissions.QUALITY_GATE_ADMIN);
127
128     newRequest()
129       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
130       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
131       .execute();
132   }
133
134   private void assertWithoutProjectCreatorFor(String permission) {
135     Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
136       permission, template.getId());
137     assertThat(templatePermission).isPresent();
138     assertThat(templatePermission.get().getWithProjectCreator()).isFalse();
139   }
140
141   private void assertNoTemplatePermissionFor(String permission) {
142     Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
143       permission, template.getId());
144     assertThat(templatePermission).isNotPresent();
145   }
146
147   private PermissionTemplateCharacteristicDto reload(PermissionTemplateCharacteristicDto characteristic) {
148     return db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(), characteristic.getPermission(), characteristic.getTemplateId())
149       .get();
150   }
151 }