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