3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.permission.ws.template;
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;
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;
41 public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<AddProjectCreatorToTemplateAction> {
43 private System2 system = spy(System2.INSTANCE);
44 private PermissionTemplateDto template;
47 protected AddProjectCreatorToTemplateAction buildWsAction() {
48 return new AddProjectCreatorToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, system);
53 template = insertTemplate();
54 when(system.now()).thenReturn(2_000_000_000L);
58 public void insert_row_when_no_template_permission() throws Exception {
59 loginAsAdminOnDefaultOrganization();
62 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
63 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
66 assertThatProjectCreatorIsPresentFor(UserRole.ADMIN, template.getId());
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));
80 when(system.now()).thenReturn(3_000_000_000L);
83 .setParam(PARAM_PERMISSION, UserRole.USER)
84 .setParam(PARAM_TEMPLATE_NAME, template.getName())
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);
94 public void fail_when_template_does_not_exist() throws Exception {
95 loginAsAdminOnDefaultOrganization();
97 expectedException.expect(NotFoundException.class);
100 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
101 .setParam(PARAM_TEMPLATE_ID, "42")
106 public void fail_if_permission_is_not_a_project_permission() throws Exception {
107 loginAsAdminOnDefaultOrganization();
109 expectedException.expect(IllegalArgumentException.class);
112 .setParam(PARAM_PERMISSION, QUALITY_GATE_ADMIN)
113 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
118 public void fail_if_not_admin_of_default_organization() throws Exception {
119 userSession.logIn().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_GATE_ADMIN);
121 expectedException.expect(ForbiddenException.class);
124 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
125 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
129 private void assertThatProjectCreatorIsPresentFor(String permission, long templateId) {
130 Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
133 assertThat(templatePermission).isPresent();
134 assertThat(templatePermission.get().getWithProjectCreator()).isTrue();
137 private PermissionTemplateCharacteristicDto reload(PermissionTemplateCharacteristicDto characteristic) {
138 return db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(), characteristic.getPermission(), characteristic.getTemplateId()).get();