]> source.dussan.org Git - sonarqube.git/blob
b9440a088169f63989312f0bb811ca38539f551e
[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.List;
23 import java.util.Properties;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.config.PropertyDefinitions;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.resources.ResourceType;
32 import org.sonar.api.resources.ResourceTypes;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.permission.PermissionTemplateDto;
38 import org.sonar.db.permission.PermissionTemplateTesting;
39 import org.sonar.server.component.ComponentFinder;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.ForbiddenException;
42 import org.sonar.server.exceptions.NotFoundException;
43 import org.sonar.server.exceptions.UnauthorizedException;
44 import org.sonar.server.i18n.I18nRule;
45 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
46 import org.sonar.server.platform.PersistentSettings;
47 import org.sonar.server.platform.WebServerSettings;
48 import org.sonar.server.tester.UserSessionRule;
49 import org.sonar.server.usergroups.ws.UserGroupFinder;
50 import org.sonar.server.ws.TestRequest;
51 import org.sonar.server.ws.WsActionTester;
52
53 import static java.util.Arrays.asList;
54 import static java.util.Collections.singletonList;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
58 import static org.sonar.api.resources.Qualifiers.PROJECT;
59 import static org.sonar.api.resources.Qualifiers.VIEW;
60 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
61 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
62 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
63 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
64 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
65
66
67 public class SetDefaultTemplateActionTest {
68   @Rule
69   public DbTester db = DbTester.create(System2.INSTANCE);
70   @Rule
71   public UserSessionRule userSession = UserSessionRule.standalone();
72   @Rule
73   public ExpectedException expectedException = ExpectedException.none();
74   I18nRule i18n = new I18nRule();
75
76   WsActionTester ws;
77   PersistentSettings persistentSettings;
78   ResourceTypes resourceTypes = mock(ResourceTypes.class);
79
80   PermissionTemplateDto template;
81
82   @Before
83   public void setUp() {
84     DbClient dbClient = db.getDbClient();
85     persistentSettings = new PersistentSettings(dbClient.propertiesDao(), new WebServerSettings(new PropertyDefinitions(), new Properties()));
86     persistentSettings.saveProperty(DEFAULT_TEMPLATE_PROPERTY, "any-template-uuid");
87     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
88     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
89     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
90     when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
91     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
92
93     ws = new WsActionTester(new SetDefaultTemplateAction(
94       dbClient,
95       new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes),
96       resourceTypes,
97       persistentSettings,
98       userSession, i18n));
99
100     template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
101   }
102
103   @Test
104   public void update_settings_for_project_qualifier() {
105     // default value is project qualifier's value
106     String result = newRequest(template.getUuid(), null);
107
108     assertThat(result).isEmpty();
109     assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
110     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
111     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
112     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
113   }
114
115   @Test
116   public void update_settings_for_project_qualifier_by_template_name() {
117     ws.newRequest()
118       .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
119       .execute();
120     db.getSession().commit();
121
122     assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
123     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
124     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
125     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
126   }
127
128   @Test
129   public void update_settings_of_views_property() {
130     newRequest(template.getUuid(), VIEW);
131
132     assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
133     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
134     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
135     assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
136   }
137
138   @Test
139   public void fail_if_anonymous() {
140     expectedException.expect(UnauthorizedException.class);
141     userSession.anonymous();
142
143     newRequest(template.getUuid(), PROJECT);
144   }
145
146   @Test
147   public void fail_if_not_admin() {
148     expectedException.expect(ForbiddenException.class);
149     userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
150
151     newRequest(template.getUuid(), PROJECT);
152   }
153
154   @Test
155   public void fail_if_template_not_provided() {
156     expectedException.expect(BadRequestException.class);
157
158     newRequest(null, PROJECT);
159   }
160
161   @Test
162   public void fail_if_template_does_not_exist() {
163     expectedException.expect(NotFoundException.class);
164
165     newRequest("unknown-template-uuid", PROJECT);
166   }
167
168   @Test
169   public void fail_if_qualifier_is_not_root() {
170     expectedException.expect(BadRequestException.class);
171     when(resourceTypes.getRoots()).thenReturn(singletonList(ResourceType.builder(PROJECT).build()));
172
173     newRequest(template.getUuid(), VIEW);
174   }
175
176   private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) {
177     TestRequest request = ws.newRequest();
178     if (templateUuid != null) {
179       request.setParam(PARAM_TEMPLATE_ID, templateUuid);
180     }
181     if (qualifier != null) {
182       request.setParam(PARAM_QUALIFIER, qualifier);
183     }
184
185     return request.execute().getInput();
186   }
187
188   private static List<ResourceType> rootResourceTypes() {
189     ResourceType project = ResourceType.builder(PROJECT).build();
190     ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
191     ResourceType dev = ResourceType.builder("DEV").build();
192
193     return asList(project, view, dev);
194   }
195 }