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;
32 import org.sonar.server.ws.WsTester;
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;
43 public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<AddProjectCreatorToTemplateAction> {
45 private System2 system = spy(System2.INSTANCE);
46 private PermissionTemplateDto template;
49 protected AddProjectCreatorToTemplateAction buildWsAction() {
50 return new AddProjectCreatorToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, system);
55 template = insertTemplate();
56 when(system.now()).thenReturn(2_000_000_000L);
60 public void insert_row_when_no_template_permission() throws Exception {
61 loginAsAdminOnDefaultOrganization();
64 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
65 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
68 assertThatProjectCreatorIsPresentFor(UserRole.ADMIN, template.getId());
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));
82 when(system.now()).thenReturn(3_000_000_000L);
85 .setParam(PARAM_PERMISSION, UserRole.USER)
86 .setParam(PARAM_TEMPLATE_NAME, template.getName())
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);
96 public void fail_when_template_does_not_exist() throws Exception {
97 loginAsAdminOnDefaultOrganization();
99 expectedException.expect(NotFoundException.class);
102 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
103 .setParam(PARAM_TEMPLATE_ID, "42")
108 public void fail_if_permission_is_not_a_project_permission() throws Exception {
109 loginAsAdminOnDefaultOrganization();
111 expectedException.expect(IllegalArgumentException.class);
114 .setParam(PARAM_PERMISSION, QUALITY_GATE_ADMIN)
115 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
120 public void fail_if_not_admin_of_default_organization() throws Exception {
121 userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_GATE_ADMIN);
123 expectedException.expect(ForbiddenException.class);
126 .setParam(PARAM_PERMISSION, UserRole.ADMIN)
127 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
131 private void assertThatProjectCreatorIsPresentFor(String permission, long templateId) {
132 Optional<PermissionTemplateCharacteristicDto> templatePermission = db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(),
135 assertThat(templatePermission).isPresent();
136 assertThat(templatePermission.get().getWithProjectCreator()).isTrue();
139 private WsTester.TestRequest newRequest() {
140 return wsTester.newPostRequest(CONTROLLER, "add_project_creator_to_template");
143 private PermissionTemplateCharacteristicDto reload(PermissionTemplateCharacteristicDto characteristic) {
144 return db.getDbClient().permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(db.getSession(), characteristic.getPermission(), characteristic.getTemplateId()).get();