]> source.dussan.org Git - sonarqube.git/blob
92e29cb833c74f1d27c61f54c26376e24d2c8803
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.management;
21
22 import java.util.Map;
23 import java.util.Set;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.sonar.db.DbSession;
29
30 import static java.util.Collections.emptySet;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
33 import static org.assertj.core.api.Assertions.assertThatNoException;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.ArgumentMatchers.anyString;
36 import static org.mockito.Mockito.doReturn;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.spy;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42 import static org.mockito.Mockito.withSettings;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class DelegatingManagedServicesTest {
46
47   private static final DelegatingManagedServices NO_MANAGED_SERVICES = new DelegatingManagedServices(emptySet());
48
49   @Mock
50   private DbSession dbSession;
51
52   @Test
53   public void getProviderName_whenNotManaged_shouldThrow() {
54     assertThatThrownBy(NO_MANAGED_SERVICES::getProviderName)
55       .isInstanceOf(IllegalStateException.class)
56       .hasMessage("This instance is not managed.");
57   }
58
59   @Test
60   public void getProviderName_whenManaged_shouldReturnName() {
61     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new AlwaysManagedInstanceService()));
62
63     assertThat(managedInstanceService.getProviderName()).isEqualTo("Always");
64   }
65
66   @Test
67   public void isInstanceExternallyManaged_whenNoManagedInstanceService_returnsFalse() {
68     assertThat(NO_MANAGED_SERVICES.isInstanceExternallyManaged()).isFalse();
69   }
70
71   @Test
72   public void isInstanceExternallyManaged_whenAllManagedInstanceServiceReturnsFalse_returnsFalse() {
73     Set<ManagedInstanceService> delegates = Set.of(new NeverManagedInstanceService(), new NeverManagedInstanceService());
74     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
75
76     assertThat(managedInstanceService.isInstanceExternallyManaged()).isFalse();
77   }
78
79   @Test
80   public void isInstanceExternallyManaged_whenOneManagedInstanceServiceReturnsTrue_returnsTrue() {
81     Set<ManagedInstanceService> delegates = Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService());
82     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
83
84     assertThat(managedInstanceService.isInstanceExternallyManaged()).isTrue();
85   }
86
87   @Test
88   public void getUserUuidToManaged_whenNoDelegates_setAllUsersAsNonManaged() {
89     Set<String> userUuids = Set.of("a", "b");
90
91     Map<String, Boolean> userUuidToManaged = NO_MANAGED_SERVICES.getUserUuidToManaged(dbSession, userUuids);
92
93     assertThat(userUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
94   }
95
96   @Test
97   public void getUserUuidToManaged_delegatesToRightService_andPropagateAnswer() {
98     Set<String> userUuids = Set.of("a", "b");
99     Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
100
101     ManagedInstanceService anotherManagedInstanceService = getManagedInstanceService(userUuids, serviceResponse);
102     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedInstanceService));
103
104     Map<String, Boolean> userUuidToManaged = managedInstanceService.getUserUuidToManaged(dbSession, userUuids);
105
106     assertThat(userUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
107   }
108
109   @Test
110   public void getGroupUuidToManaged_whenNoDelegates_setAllUsersAsNonManaged() {
111     Set<String> groupUuids = Set.of("a", "b");
112     DelegatingManagedServices managedInstanceService = NO_MANAGED_SERVICES;
113
114     Map<String, Boolean> groupUuidToManaged = managedInstanceService.getGroupUuidToManaged(dbSession, groupUuids);
115
116     assertThat(groupUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
117   }
118
119   @Test
120   public void isUserManaged_delegatesToRightService_andPropagateAnswer() {
121     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
122
123     assertThat(managedInstanceService.isUserManaged(dbSession, "whatever")).isTrue();
124   }
125
126   @Test
127   public void isUserManaged_whenNoDelegates_returnsFalse() {
128     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of());
129
130     assertThat(managedInstanceService.isUserManaged(dbSession, "whatever")).isFalse();
131   }
132
133   @Test
134   public void isGroupManaged_delegatesToRightService_andPropagateAnswer() {
135     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
136
137     assertThat(managedInstanceService.isGroupManaged(dbSession, "whatever")).isTrue();
138   }
139
140   @Test
141   public void isGroupManaged_whenNoDelegates_returnsFalse() {
142     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of());
143
144     assertThat(managedInstanceService.isGroupManaged(dbSession, "whatever")).isFalse();
145   }
146
147   @Test
148   public void getGroupUuidToManaged_delegatesToRightService_andPropagateAnswer() {
149     Set<String> groupUuids = Set.of("a", "b");
150     Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
151
152     ManagedInstanceService anotherManagedInstanceService = getManagedInstanceService(groupUuids, serviceResponse);
153     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedInstanceService));
154
155     Map<String, Boolean> groupUuidToManaged = managedInstanceService.getGroupUuidToManaged(dbSession, groupUuids);
156
157     assertThat(groupUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
158   }
159
160   @Test
161   public void getGroupUuidToManaged_ifMoreThanOneDelegatesActivated_throws() {
162     Set<ManagedInstanceService> managedInstanceServices = Set.of(new AlwaysManagedInstanceService(), new AlwaysManagedInstanceService());
163     DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
164     assertThatIllegalStateException()
165       .isThrownBy(() -> delegatingManagedServices.getGroupUuidToManaged(dbSession, Set.of("a")))
166       .withMessage("The instance can't be managed by more than one identity provider and 2 were found.");
167   }
168
169   @Test
170   public void getUserUuidToManaged_ifMoreThanOneDelegatesActivated_throws() {
171     Set<ManagedInstanceService> managedInstanceServices = Set.of(new AlwaysManagedInstanceService(), new AlwaysManagedInstanceService());
172     DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
173     assertThatIllegalStateException()
174       .isThrownBy(() -> delegatingManagedServices.getUserUuidToManaged(dbSession, Set.of("a")))
175       .withMessage("The instance can't be managed by more than one identity provider and 2 were found.");
176   }
177
178   @Test
179   public void getManagedUsersSqlFilter_whenNoDelegates_throws() {
180     Set<ManagedInstanceService> managedInstanceServices = emptySet();
181     DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
182     assertThatIllegalStateException()
183       .isThrownBy(() -> delegatingManagedServices.getManagedUsersSqlFilter(true))
184       .withMessage("This instance is not managed.");
185   }
186
187   @Test
188   public void getManagedUsersSqlFilter_delegatesToRightService_andPropagateAnswer() {
189     AlwaysManagedInstanceService alwaysManagedInstanceService = new AlwaysManagedInstanceService();
190     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), alwaysManagedInstanceService));
191
192     assertThat(managedInstanceService.getManagedUsersSqlFilter(true)).isNotNull().isEqualTo(alwaysManagedInstanceService.getManagedUsersSqlFilter(
193       true));
194   }
195
196   @Test
197   public void getManagedGroupsSqlFilter_whenNoDelegates_throws() {
198     Set<ManagedInstanceService> managedInstanceServices = emptySet();
199     DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
200     assertThatIllegalStateException()
201       .isThrownBy(() -> delegatingManagedServices.getManagedGroupsSqlFilter(true))
202       .withMessage("This instance is not managed.");
203   }
204
205   @Test
206   public void getManagedGroupsSqlFilter_delegatesToRightService_andPropagateAnswer() {
207     AlwaysManagedInstanceService alwaysManagedInstanceService = new AlwaysManagedInstanceService();
208     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), alwaysManagedInstanceService));
209
210     assertThat(managedInstanceService.getManagedGroupsSqlFilter(true)).isNotNull().isEqualTo(alwaysManagedInstanceService.getManagedGroupsSqlFilter(
211       true));
212   }
213
214   private ManagedInstanceService getManagedInstanceService(Set<String> userUuids, Map<String, Boolean> uuidToManaged) {
215     ManagedInstanceService anotherManagedInstanceService = mock(ManagedInstanceService.class);
216     when(anotherManagedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
217     when(anotherManagedInstanceService.getGroupUuidToManaged(dbSession, userUuids)).thenReturn(uuidToManaged);
218     when(anotherManagedInstanceService.getUserUuidToManaged(dbSession, userUuids)).thenReturn(uuidToManaged);
219     return anotherManagedInstanceService;
220   }
221
222   @Test
223   public void getProjectUuidToManaged_whenNoDelegates_setAllProjectsAsNonManaged() {
224     Set<String> projectUuids = Set.of("a", "b");
225     DelegatingManagedServices managedInstanceService = NO_MANAGED_SERVICES;
226
227     Map<String, Boolean> projectUuidToManaged = managedInstanceService.getProjectUuidToManaged(dbSession, projectUuids);
228
229     assertThat(projectUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
230   }
231
232   @Test
233   public void getProjectUuidToManaged_delegatesToRightService_andPropagateAnswer() {
234     Set<String> projectUuids = Set.of("a", "b");
235     Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
236
237     ManagedInstanceService anotherManagedProjectService = getManagedProjectService(projectUuids, serviceResponse);
238     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedProjectService));
239
240     Map<String, Boolean> projectUuidToManaged = managedInstanceService.getProjectUuidToManaged(dbSession, projectUuids);
241
242     assertThat(projectUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
243   }
244
245   private ManagedInstanceService getManagedProjectService(Set<String> projectUuids, Map<String, Boolean> uuidsToManaged) {
246     ManagedInstanceService anotherManagedProjectService = mock(ManagedInstanceService.class, withSettings().extraInterfaces(ManagedProjectService.class));
247     when(anotherManagedProjectService.isInstanceExternallyManaged()).thenReturn(true);
248     doReturn(uuidsToManaged).when((ManagedProjectService) anotherManagedProjectService).getProjectUuidToManaged(dbSession, projectUuids);
249     return anotherManagedProjectService;
250   }
251
252   @Test
253   public void isProjectManaged_whenManagedInstanceServices_shouldDelegatesToRightService() {
254     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
255
256     assertThat(managedInstanceService.isProjectManaged(dbSession, "whatever")).isTrue();
257   }
258
259   @Test
260   public void isProjectManaged_whenManagedNoInstanceServices_returnsFalse() {
261     assertThat(NO_MANAGED_SERVICES.isProjectManaged(dbSession, "whatever")).isFalse();
262   }
263
264   @Test
265   public void queuePermissionSyncTask_whenManagedNoInstanceServices_doesNotFail() {
266     assertThatNoException().isThrownBy(() -> NO_MANAGED_SERVICES.queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid"));
267   }
268
269   @Test
270   public void queuePermissionSyncTask_whenManagedInstanceServices_shouldDelegatesToRightService() {
271     NeverManagedInstanceService neverManagedInstanceService = spy(new NeverManagedInstanceService());
272     AlwaysManagedInstanceService alwaysManagedInstanceService = spy(new AlwaysManagedInstanceService());
273     Set<ManagedInstanceService> delegates = Set.of(neverManagedInstanceService, alwaysManagedInstanceService);
274     DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
275
276     managedInstanceService.queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid");
277     verify(neverManagedInstanceService, never()).queuePermissionSyncTask(anyString(), anyString(), anyString());
278     verify(alwaysManagedInstanceService).queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid");
279   }
280
281   private static class NeverManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
282
283     @Override
284     public boolean isInstanceExternallyManaged() {
285       return false;
286     }
287
288     @Override
289     public String getProviderName() {
290       return "Never";
291     }
292
293     @Override
294     public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
295       return null;
296     }
297
298     @Override
299     public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) {
300       return null;
301     }
302
303     @Override
304     public String getManagedUsersSqlFilter(boolean filterByManaged) {
305       return null;
306     }
307
308     @Override
309     public String getManagedGroupsSqlFilter(boolean filterByManaged) {
310       return null;
311     }
312
313     @Override
314     public boolean isUserManaged(DbSession dbSession, String userUuid) {
315       return false;
316     }
317
318     @Override
319     public boolean isGroupManaged(DbSession dbSession, String groupUuid) {
320       return false;
321     }
322
323     @Override
324     public Map<String, Boolean> getProjectUuidToManaged(DbSession dbSession, Set<String> projectUuids) {
325       return null;
326     }
327
328     @Override
329     public boolean isProjectManaged(DbSession dbSession, String projectUuid) {
330       return false;
331     }
332
333     @Override
334     public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
335
336     }
337   }
338
339   private static class AlwaysManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
340
341     @Override
342     public boolean isInstanceExternallyManaged() {
343       return true;
344     }
345
346     @Override
347     public String getProviderName() {
348       return "Always";
349     }
350
351     @Override
352     public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
353       return null;
354     }
355
356     @Override
357     public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) {
358       return null;
359     }
360
361     @Override
362     public String getManagedUsersSqlFilter(boolean filterByManaged) {
363       return "any filter";
364     }
365
366     @Override
367     public String getManagedGroupsSqlFilter(boolean filterByManaged) {
368       return "any filter";
369     }
370
371     @Override
372     public boolean isUserManaged(DbSession dbSession, String userUuid) {
373       return true;
374     }
375
376     @Override
377     public boolean isGroupManaged(DbSession dbSession, String groupUuid) {
378       return true;
379     }
380
381     @Override
382     public Map<String, Boolean> getProjectUuidToManaged(DbSession dbSession, Set<String> projectUuids) {
383       return null;
384     }
385
386     @Override
387     public boolean isProjectManaged(DbSession dbSession, String projectUuid) {
388       return true;
389     }
390
391     @Override
392     public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
393
394     }
395   }
396
397 }