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