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