]> source.dussan.org Git - sonarqube.git/blob
ec620433777072a888f424c6a748c3392ae6c228
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info 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.platform.db.migration.version.v00;
21
22 import java.sql.SQLException;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Random;
27 import java.util.stream.Stream;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.SonarRuntime;
33 import org.sonar.api.utils.System2;
34 import org.sonar.api.utils.Version;
35 import org.sonar.core.util.UuidFactory;
36 import org.sonar.core.util.UuidFactoryFast;
37 import org.sonar.core.util.stream.MoreCollectors;
38 import org.sonar.db.CoreDbTester;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43
44 public class PopulateInitialSchemaTest {
45
46   private static final long NOW = 1_500L;
47
48   @Rule
49   public ExpectedException expectedException = ExpectedException.none();
50
51   private final Random random = new Random();
52   private final Version version = Version.create(1 + random.nextInt(10), 1 + random.nextInt(10), random.nextInt(10));
53   private UuidFactory uuidFactory = UuidFactoryFast.getInstance();
54   private System2 system2 = mock(System2.class);
55   private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
56
57   @Rule
58   public CoreDbTester db = CoreDbTester.createForSchema(PopulateInitialSchemaTest.class, "v79.sql");
59
60   private PopulateInitialSchema underTest = new PopulateInitialSchema(db.database(), system2, uuidFactory, sonarRuntime);
61
62   @Before
63   public void setUp() {
64     when(sonarRuntime.getApiVersion()).thenReturn(version);
65   }
66
67   @Test
68   public void migration_inserts_users_and_groups() throws SQLException {
69     when(system2.now()).thenReturn(NOW);
70
71     underTest.execute();
72
73     verifyAdminUser();
74     long userGroupId = verifyGroup("sonar-users", "Any new users created will automatically join this group");
75     verifyGroup("sonar-administrators", "System administrators");
76     String qgUuid = verifyQualityGate();
77     String orgUuid = verifyDefaultOrganization(userGroupId, qgUuid);
78     verifyOrgQualityGate(orgUuid, qgUuid);
79     verifyInternalProperties(orgUuid);
80     verifyRolesOfAdminsGroup();
81     verifyRolesOfUsersGroup();
82     verifyRolesOfAnyone();
83     verifyMembershipOfAdminUser();
84   }
85
86   private void verifyAdminUser() {
87     Map<String, Object> cols = db.selectFirst("select " +
88       "login as \"LOGIN\", " +
89       "name as \"NAME\", " +
90       "email as \"EMAIL\", " +
91       "external_id as \"EXTERNAL_ID\", " +
92       "external_login as \"EXTERNAL_LOGIN\", " +
93       "external_identity_provider as \"EXT_IDENT_PROVIDER\", " +
94       "user_local as \"USER_LOCAL\", " +
95       "crypted_password as \"CRYPTED_PASSWORD\", " +
96       "salt as \"SALT\", " +
97       "hash_method as \"HASH_METHOD\", " +
98       "is_root as \"IS_ROOT\", " +
99       "onboarded as \"ONBOARDED\", " +
100       "created_at as \"CREATED_AT\", " +
101       "updated_at as \"UPDATED_AT\" " +
102       "from users where login='admin'");
103     assertThat(cols.get("LOGIN")).isEqualTo("admin");
104     assertThat(cols.get("NAME")).isEqualTo("Administrator");
105     assertThat(cols.get("EMAIL")).isNull();
106     assertThat(cols.get("EXTERNAL_ID")).isEqualTo("admin");
107     assertThat(cols.get("EXTERNAL_LOGIN")).isEqualTo("admin");
108     assertThat(cols.get("EXT_IDENT_PROVIDER")).isEqualTo("sonarqube");
109     assertThat(cols.get("USER_LOCAL")).isEqualTo(true);
110     assertThat(cols.get("CRYPTED_PASSWORD")).isEqualTo("$2a$12$uCkkXmhW5ThVK8mpBvnXOOJRLd64LJeHTeCkSuB3lfaR2N0AYBaSi");
111     assertThat(cols.get("SALT")).isNull();
112     assertThat(cols.get("HASH_METHOD")).isEqualTo("BCRYPT");
113     assertThat(cols.get("IS_ROOT")).isEqualTo(false);
114     assertThat(cols.get("ONBOARDED")).isEqualTo(true);
115     assertThat(cols.get("CREATED_AT")).isEqualTo(NOW);
116     assertThat(cols.get("UPDATED_AT")).isEqualTo(NOW);
117   }
118
119   private long verifyGroup(String expectedName, String expectedDescription) {
120     List<Map<String, Object>> rows = db.select("select " +
121       "id as \"ID\"," +
122       "name as \"name\", " +
123       "description as \"description\", " +
124       "created_at as \"CREATED_AT\", " +
125       "updated_at as \"UPDATED_AT\" " +
126       "from groups where name='" + expectedName + "'");
127     assertThat(rows).hasSize(1);
128
129     Map<String, Object> row = rows.get(0);
130     assertThat(row.get("name")).isEqualTo(expectedName);
131     assertThat(row.get("description")).isEqualTo(expectedDescription);
132     assertThat(((Date) row.get("CREATED_AT")).getTime()).isEqualTo(NOW);
133     assertThat(((Date) row.get("UPDATED_AT")).getTime()).isEqualTo(NOW);
134
135     return (long) row.get("ID");
136   }
137
138   private String verifyQualityGate() {
139     List<Map<String, Object>> rows = db.select("select " +
140       "uuid as \"UUID\", " +
141       "name as \"NAME\", " +
142       "is_built_in as \"BUILTIN\"," +
143       "created_at as \"CREATED_AT\", " +
144       "updated_at as \"UPDATED_AT\"" +
145       " from quality_gates");
146     assertThat(rows).hasSize(1);
147
148     Map<String, Object> row = rows.get(0);
149     assertThat(row.get("NAME")).isEqualTo("Sonar way");
150     assertThat(row.get("BUILTIN")).isEqualTo(true);
151     assertThat(((Date) row.get("CREATED_AT")).getTime()).isEqualTo(NOW);
152     assertThat(((Date) row.get("UPDATED_AT")).getTime()).isEqualTo(NOW);
153     return (String) row.get("UUID");
154   }
155
156   private String verifyDefaultOrganization(long userGroupId, String defaultQQUuid) {
157     List<Map<String, Object>> rows = db.select("select " +
158       "uuid as \"UUID\", " +
159       "kee as \"KEE\", " +
160       "name as \"NAME\", " +
161       "guarded as \"GUARDED\", " +
162       "new_project_private as \"PRIVATE\", " +
163       "default_group_id as \"GROUP_ID\", " +
164       "default_quality_gate_uuid as \"QG_UUID\", " +
165       "subscription as \"SUBSCRIPTION\", " +
166       "created_at as \"CREATED_AT\", " +
167       "updated_at as \"UPDATED_AT\"" +
168       " from organizations");
169     assertThat(rows).hasSize(1);
170
171     Map<String, Object> row = rows.get(0);
172     assertThat(row.get("KEE")).isEqualTo("default-organization");
173     assertThat(row.get("NAME")).isEqualTo("Default Organization");
174     assertThat(row.get("GUARDED")).isEqualTo(true);
175     assertThat(row.get("PRIVATE")).isEqualTo(false);
176     assertThat(row.get("GROUP_ID")).isEqualTo(userGroupId);
177     assertThat(row.get("QG_UUID")).isEqualTo(defaultQQUuid);
178     assertThat(row.get("SUBSCRIPTION")).isEqualTo("SONARQUBE");
179     assertThat(row.get("CREATED_AT")).isEqualTo(NOW);
180     assertThat(row.get("UPDATED_AT")).isEqualTo(NOW);
181     return (String) row.get("UUID");
182   }
183
184   private void verifyOrgQualityGate(String orgUuid, String qgUuid) {
185     List<Map<String, Object>> rows = db.select("select " +
186       "uuid as \"UUID\", " +
187       "organization_uuid as \"ORG\", " +
188       "quality_gate_uuid as \"QG\"" +
189       " from org_quality_gates");
190     assertThat(rows).hasSize(1);
191
192     Map<String, Object> row = rows.get(0);
193     assertThat(row.get("UUID")).isNotNull();
194     assertThat(row.get("ORG")).isEqualTo(orgUuid);
195     assertThat(row.get("QG")).isEqualTo(qgUuid);
196   }
197
198   private void verifyInternalProperties(String orgUuid) {
199     List<Map<String, Object>> rows = db.select("select " +
200       "kee as \"KEE\", " +
201       "is_empty as \"EMPTY\", " +
202       "text_value as \"VAL\"," +
203       "created_at as \"CREATED_AT\" " +
204       " from internal_properties");
205     assertThat(rows).hasSize(3);
206
207     Map<String, Map<String, Object>> rowsByKey = rows.stream().collect(MoreCollectors.uniqueIndex(t -> (String) t.get("KEE")));
208     verifyInternalProperty(rowsByKey, "organization.default", orgUuid);
209     verifyInternalProperty(rowsByKey, "installation.date", String.valueOf(system2.now()));
210     verifyInternalProperty(rowsByKey, "installation.version", version.toString());
211   }
212
213   private static void verifyInternalProperty(Map<String, Map<String, Object>> rowsByKey, String key, String val) {
214     Map<String, Object> row = rowsByKey.get(key);
215     assertThat(row.get("KEE")).isEqualTo(key);
216     assertThat(row.get("EMPTY")).isEqualTo(false);
217     assertThat(row.get("VAL")).isEqualTo(val);
218     assertThat(row.get("CREATED_AT")).isEqualTo(NOW);
219   }
220
221   private void verifyRolesOfAdminsGroup() {
222     assertThat(selectRoles("sonar-administrators")).containsOnly("admin", "profileadmin", "gateadmin", "provisioning", "applicationcreator", "portfoliocreator");
223   }
224
225   private void verifyRolesOfUsersGroup() {
226     assertThat(selectRoles("sonar-users")).isEmpty();
227   }
228
229   private void verifyRolesOfAnyone() {
230     List<Map<String, Object>> rows = db.select("select gr.role as \"role\" " +
231       "from group_roles gr where gr.group_id is null");
232     Stream<String> roles = rows.stream()
233       .map(row -> (String) row.get("role"));
234     assertThat(roles).containsOnly("provisioning", "scan");
235   }
236
237   private Stream<String> selectRoles(String groupName) {
238     List<Map<String, Object>> rows = db.select("select gr.role as \"role\" " +
239       "from group_roles gr " +
240       "inner join groups g on gr.group_id = g.id " +
241       "where g.name='" + groupName + "'");
242     return rows.stream()
243       .map(row -> (String) row.get("role"));
244   }
245
246   private void verifyMembershipOfAdminUser() {
247     List<Map<String, Object>> rows = db.select("select g.name as \"groupName\" from groups g " +
248       "inner join groups_users gu on gu.group_id = g.id " +
249       "inner join users u on gu.user_id = u.id " +
250       "where u.login='admin'");
251     List<String> groupNames = rows.stream()
252       .map(row -> (String) row.get("groupName"))
253       .collect(MoreCollectors.toArrayList());
254     assertThat(groupNames).containsOnly("sonar-administrators", "sonar-users");
255   }
256
257 }