3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.organization.DefaultTemplates;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.db.permission.template.PermissionTemplateTesting;
31 import org.sonar.server.exceptions.BadRequestException;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.exceptions.UnauthorizedException;
35 import org.sonar.server.i18n.I18nRule;
36 import org.sonar.server.permission.ws.BasePermissionWsTest;
37 import org.sonar.server.ws.TestRequest;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.sonar.api.resources.Qualifiers.PROJECT;
41 import static org.sonar.api.resources.Qualifiers.VIEW;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
47 public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaultTemplateAction> {
49 private DbClient dbClient = db.getDbClient();
50 private I18nRule i18n = new I18nRule();
53 protected SetDefaultTemplateAction buildWsAction() {
54 return new SetDefaultTemplateAction(db.getDbClient(), newPermissionWsSupport(), newRootResourceTypes(), userSession, i18n);
58 public void update_project_default_template() throws Exception {
59 PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
60 db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()),
62 PermissionTemplateDto template = insertTemplate(db.getDefaultOrganization());
63 loginAsAdmin(db.getDefaultOrganization());
65 newRequest(template.getUuid(), Qualifiers.PROJECT);
67 assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), viewDefaultTemplate.getUuid());
71 public void update_project_default_template_without_qualifier_param() throws Exception {
72 OrganizationDto organization = db.organizations().insert();
73 db.organizations().setDefaultTemplates(organization, "any-project-template-uuid", "any-view-template-uuid");
74 PermissionTemplateDto template = insertTemplate(organization);
75 loginAsAdmin(organization);
77 // default value is project qualifier's value
78 newRequest(template.getUuid(), null);
80 assertDefaultTemplates(organization, template.getUuid(), "any-view-template-uuid");
84 public void update_project_default_template_by_template_name() throws Exception {
85 OrganizationDto organization = db.organizations().insert();
86 PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
87 db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization),
89 PermissionTemplateDto template = insertTemplate(organization);
90 loginAsAdmin(organization);
93 .setParam(PARAM_ORGANIZATION, organization.getKey())
94 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
96 db.getSession().commit();
98 assertDefaultTemplates(organization, template.getUuid(), viewDefaultTemplate.getUuid());
102 public void update_view_default_template() throws Exception {
103 OrganizationDto organization = db.organizations().insert();
104 PermissionTemplateDto projectDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
105 db.organizations().setDefaultTemplates(projectDefaultTemplate, null);
106 PermissionTemplateDto template = insertTemplate(organization);
107 loginAsAdmin(organization);
109 newRequest(template.getUuid(), VIEW);
111 assertDefaultTemplates(organization, projectDefaultTemplate.getUuid(), template.getUuid());
115 public void fail_if_anonymous() throws Exception {
116 OrganizationDto organization = db.organizations().insert();
117 PermissionTemplateDto template = insertTemplate(organization);
118 userSession.anonymous();
120 expectedException.expect(UnauthorizedException.class);
122 newRequest(template.getUuid(), PROJECT);
126 public void fail_if_not_admin() throws Exception {
127 OrganizationDto organization = db.organizations().insert();
128 PermissionTemplateDto template = insertTemplate(organization);
131 expectedException.expect(ForbiddenException.class);
133 newRequest(template.getUuid(), null);
137 public void fail_if_template_not_provided() throws Exception {
138 expectedException.expect(BadRequestException.class);
140 newRequest(null, PROJECT);
144 public void fail_if_template_does_not_exist() throws Exception {
145 expectedException.expect(NotFoundException.class);
147 newRequest("unknown-template-uuid", PROJECT);
151 public void fail_if_qualifier_is_not_root() throws Exception {
152 OrganizationDto organization = db.organizations().insert();
153 PermissionTemplateDto template = insertTemplate(organization);
154 loginAsAdmin(organization);
156 expectedException.expect(IllegalArgumentException.class);
157 expectedException.expectMessage("Value of parameter 'qualifier' (FIL) must be one of: [TRK, VW]");
159 newRequest(template.getUuid(), Qualifiers.FILE);
163 public void fail_if_organization_has_no_default_templates() throws Exception {
164 OrganizationDto organization = db.organizations().insert();
165 PermissionTemplateDto template = insertTemplate(organization);
166 loginAsAdmin(organization);
168 expectedException.expect(NotFoundException.class);
169 expectedException.expectMessage("No Default templates for organization with uuid '" + organization.getUuid() + "'");
171 newRequest(template.getUuid(), null);
174 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) throws Exception {
175 TestRequest request = newRequest();
176 if (templateUuid != null) {
177 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
179 if (qualifier != null) {
180 request.setParam(PARAM_QUALIFIER, qualifier);
183 return request.execute().getInput();
186 private PermissionTemplateDto insertTemplate(OrganizationDto organization) {
187 PermissionTemplateDto res = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto()
188 .setOrganizationUuid(organization.getUuid())
189 .setUuid("permission-template-uuid"));
194 private void assertDefaultTemplates(OrganizationDto organizationDto,
195 @Nullable String projectDefaultTemplateUuid, @Nullable String viewDefaultTemplateUuid) {
196 DbSession dbSession = db.getSession();
197 DefaultTemplates defaultTemplates = db.getDbClient().organizationDao().getDefaultTemplates(dbSession, organizationDto.getUuid())
198 .orElseThrow(() -> new IllegalStateException("No default templates for organization with uuid '" + organizationDto.getUuid() + "'"));
200 assertThat(defaultTemplates.getProjectUuid()).isEqualTo(projectDefaultTemplateUuid);
201 assertThat(defaultTemplates.getViewUuid()).isEqualTo(viewDefaultTemplateUuid);