]> source.dussan.org Git - sonarqube.git/blob
4789e934557e192442e130096680283363f56cc2
[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 javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.core.permission.GlobalPermissions;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.permission.PermissionTemplateDto;
34 import org.sonar.server.exceptions.BadRequestException;
35 import org.sonar.server.exceptions.ForbiddenException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.TestResponse;
40 import org.sonar.server.ws.WsActionTester;
41
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
46 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_DESCRIPTION;
47 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_NAME;
48 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PATTERN;
49 import static org.sonar.test.JsonAssert.assertJson;
50
51 public class CreateTemplateActionTest {
52
53   @Rule
54   public DbTester db = DbTester.create(System2.INSTANCE);
55   @Rule
56   public UserSessionRule userSession = UserSessionRule.standalone();
57   @Rule
58   public ExpectedException expectedException = ExpectedException.none();
59
60   WsActionTester ws;
61   DbClient dbClient;
62   DbSession dbSession;
63   System2 system = mock(System2.class);
64
65   @Before
66   public void setUp() {
67     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
68     when(system.now()).thenReturn(1440512328743L);
69
70     dbClient = db.getDbClient();
71     dbSession = db.getSession();
72     ws = new WsActionTester(new CreateTemplateAction(dbClient, userSession, system));
73   }
74
75   @Test
76   public void create_full_permission_template() {
77     TestResponse result = newRequest("Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
78
79     assertJson(result.getInput())
80       .ignoreFields("id")
81       .isSimilarTo(getClass().getResource("create_template-example.json"));
82     PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
83     assertThat(finance.getName()).isEqualTo("Finance");
84     assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
85     assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
86     assertThat(finance.getUuid()).isNotEmpty();
87     assertThat(finance.getCreatedAt().getTime()).isEqualTo(1440512328743L);
88     assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
89   }
90
91   @Test
92   public void create_minimalist_permission_template() {
93     newRequest("Finance", null, null);
94
95     PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
96     assertThat(finance.getName()).isEqualTo("Finance");
97     assertThat(finance.getDescription()).isNullOrEmpty();
98     assertThat(finance.getKeyPattern()).isNullOrEmpty();
99     assertThat(finance.getUuid()).isNotEmpty();
100     assertThat(finance.getCreatedAt().getTime()).isEqualTo(1440512328743L);
101     assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
102   }
103
104   @Test
105   public void fail_if_name_not_provided() {
106     expectedException.expect(IllegalArgumentException.class);
107
108     newRequest(null, null, null);
109   }
110
111   @Test
112   public void fail_if_name_empty() {
113     expectedException.expect(BadRequestException.class);
114     expectedException.expectMessage("The template name must not be blank");
115
116     newRequest("", null, null);
117   }
118
119   @Test
120   public void fail_if_regexp_if_not_valid() {
121     expectedException.expect(BadRequestException.class);
122     expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
123
124     newRequest("Finance", null, "[azerty");
125   }
126
127   @Test
128   public void fail_if_name_already_exists_in_database_case_insensitive() {
129     expectedException.expect(BadRequestException.class);
130     expectedException.expectMessage("A template with the name 'Finance' already exists (case insensitive).");
131     insertTemplate(newPermissionTemplateDto().setName("finance"));
132     commit();
133
134     newRequest("Finance", null, null);
135   }
136
137   @Test
138   public void fail_if_not_logged_in() {
139     expectedException.expect(UnauthorizedException.class);
140     userSession.anonymous();
141
142     newRequest("Finance", null, null);
143   }
144
145   @Test
146   public void fail_if_not_admin() {
147     expectedException.expect(ForbiddenException.class);
148     userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
149
150     newRequest("Finance", null, null);
151   }
152
153   private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
154     return dbClient.permissionTemplateDao().insert(dbSession, template);
155   }
156
157   private void commit() {
158     dbSession.commit();
159   }
160
161   private TestResponse newRequest(@Nullable String name, @Nullable String description, @Nullable String projectPattern) {
162     TestRequest request = ws.newRequest();
163     if (name != null) {
164       request.setParam(PARAM_NAME, name);
165     }
166     if (description != null) {
167       request.setParam(PARAM_DESCRIPTION, description);
168     }
169     if (projectPattern != null) {
170       request.setParam(PARAM_PATTERN, projectPattern);
171     }
172
173     return request.execute();
174   }
175 }