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