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 javax.annotation.Nullable;
23 import org.junit.Test;
24 import org.sonar.api.resources.Qualifiers;
25 import org.sonar.core.permission.GlobalPermissions;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.organization.DefaultTemplates;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.db.permission.template.PermissionTemplateTesting;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.exceptions.UnauthorizedException;
36 import org.sonar.server.i18n.I18nRule;
37 import org.sonar.server.permission.ws.BasePermissionWsTest;
38 import org.sonar.server.ws.TestRequest;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.api.resources.Qualifiers.PROJECT;
42 import static org.sonar.api.resources.Qualifiers.VIEW;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
48 public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaultTemplateAction> {
50 private DbClient dbClient = db.getDbClient();
51 private I18nRule i18n = new I18nRule();
54 protected SetDefaultTemplateAction buildWsAction() {
55 return new SetDefaultTemplateAction(db.getDbClient(), newPermissionWsSupport(), newRootResourceTypes(), userSession, i18n);
59 public void update_project_default_template() throws Exception {
60 db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "project-template-uuid", "view-template-uuid");
61 PermissionTemplateDto template = insertTemplate(db.getDefaultOrganization());
62 loginAsAdmin(db.getDefaultOrganization());
64 newRequest(template.getUuid(), Qualifiers.PROJECT);
66 assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), "view-template-uuid");
70 public void update_project_default_template_without_qualifier_param() throws Exception {
71 OrganizationDto organization = db.organizations().insert();
72 db.organizations().setDefaultTemplates(organization, "any-project-template-uuid", "any-view-template-uuid");
73 PermissionTemplateDto template = insertTemplate(organization);
74 loginAsAdmin(organization);
76 // default value is project qualifier's value
77 newRequest(template.getUuid(), null);
79 assertDefaultTemplates(organization, template.getUuid(), "any-view-template-uuid");
83 public void update_project_default_template_by_template_name() throws Exception {
84 OrganizationDto organization = db.organizations().insert();
85 db.organizations().setDefaultTemplates(organization, "bar", "roh");
86 PermissionTemplateDto template = insertTemplate(organization);
87 loginAsAdmin(organization);
90 .setParam(PARAM_ORGANIZATION_KEY, organization.getKey())
91 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
93 db.getSession().commit();
95 assertDefaultTemplates(organization, template.getUuid(), "roh");
99 public void update_view_default_template() throws Exception {
100 OrganizationDto organization = db.organizations().insert();
101 db.organizations().setDefaultTemplates(organization, "foo", null);
102 PermissionTemplateDto template = insertTemplate(organization);
103 loginAsAdmin(organization);
105 newRequest(template.getUuid(), VIEW);
107 assertDefaultTemplates(organization, "foo", template.getUuid());
111 public void fail_if_anonymous() throws Exception {
112 OrganizationDto organization = db.organizations().insert();
113 PermissionTemplateDto template = insertTemplate(organization);
114 userSession.anonymous();
116 expectedException.expect(UnauthorizedException.class);
118 newRequest(template.getUuid(), PROJECT);
122 public void fail_if_not_admin() throws Exception {
123 OrganizationDto organization = db.organizations().insert();
124 PermissionTemplateDto template = insertTemplate(organization);
125 userSession.logIn().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
127 expectedException.expect(ForbiddenException.class);
129 newRequest(template.getUuid(), null);
133 public void fail_if_template_not_provided() throws Exception {
134 expectedException.expect(BadRequestException.class);
136 newRequest(null, PROJECT);
140 public void fail_if_template_does_not_exist() throws Exception {
141 expectedException.expect(NotFoundException.class);
143 newRequest("unknown-template-uuid", PROJECT);
147 public void fail_if_qualifier_is_not_root() throws Exception {
148 OrganizationDto organization = db.organizations().insert();
149 PermissionTemplateDto template = insertTemplate(organization);
150 loginAsAdmin(organization);
152 expectedException.expect(IllegalArgumentException.class);
153 expectedException.expectMessage("Value of parameter 'qualifier' (FIL) must be one of: [TRK, VW]");
155 newRequest(template.getUuid(), Qualifiers.FILE);
159 public void fail_if_organization_has_no_default_templates() throws Exception {
160 OrganizationDto organization = db.organizations().insert();
161 PermissionTemplateDto template = insertTemplate(organization);
162 loginAsAdmin(organization);
164 expectedException.expect(NotFoundException.class);
165 expectedException.expectMessage("No Default templates for organization with uuid '" + organization.getUuid() + "'");
167 newRequest(template.getUuid(), null);
170 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) throws Exception {
171 TestRequest request = newRequest();
172 if (templateUuid != null) {
173 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
175 if (qualifier != null) {
176 request.setParam(PARAM_QUALIFIER, qualifier);
179 return request.execute().getInput();
182 private PermissionTemplateDto insertTemplate(OrganizationDto organization) {
183 PermissionTemplateDto res = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto()
184 .setOrganizationUuid(organization.getUuid())
185 .setUuid("permission-template-uuid"));
190 private void assertDefaultTemplates(OrganizationDto organizationDto,
191 @Nullable String projectDefaultTemplateUuid, @Nullable String viewDefaultTemplateUuid) {
192 DbSession dbSession = db.getSession();
193 DefaultTemplates defaultTemplates = db.getDbClient().organizationDao().getDefaultTemplates(dbSession, organizationDto.getUuid())
194 .orElseThrow(() -> new IllegalStateException("No default templates for organization with uuid '" + organizationDto.getUuid() + "'"));
196 assertThat(defaultTemplates.getProjectUuid()).isEqualTo(projectDefaultTemplateUuid);
197 assertThat(defaultTemplates.getViewUuid()).isEqualTo(viewDefaultTemplateUuid);