diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2023-05-03 17:39:11 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-05-11 20:03:14 +0000 |
commit | bfacb4c8eab9949a52afb6e467504c6afb150a31 (patch) | |
tree | e516ba1dc0735e78d9189c1572b16b1b39339f9c /server/sonar-auth-github | |
parent | ca468111e1e8425cdbc9b9c23e80c3ab563f6115 (diff) | |
download | sonarqube-bfacb4c8eab9949a52afb6e467504c6afb150a31.tar.gz sonarqube-bfacb4c8eab9949a52afb6e467504c6afb150a31.zip |
SONAR-19084 Add GitHubManagedInstanceService to web server context
Diffstat (limited to 'server/sonar-auth-github')
2 files changed, 0 insertions, 224 deletions
diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubManagedInstanceService.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubManagedInstanceService.java deleted file mode 100644 index 7921892fe92..00000000000 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubManagedInstanceService.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.auth.github; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.api.server.ServerSide; -import org.sonar.db.DbSession; -import org.sonar.db.user.UserDao; -import org.sonar.db.user.UserDto; -import org.sonar.db.user.UserQuery; -import org.sonar.server.management.ManagedInstanceService; - -import static java.util.stream.Collectors.toMap; -import static java.util.stream.Collectors.toSet; - -@ServerSide -@ComputeEngineSide -public class GitHubManagedInstanceService implements ManagedInstanceService { - - private final GitHubSettings gitHubSettings; - private final UserDao userDao; - - public GitHubManagedInstanceService(GitHubSettings gitHubSettings, UserDao userDao) { - this.gitHubSettings = gitHubSettings; - this.userDao = userDao; - } - - @Override - public boolean isInstanceExternallyManaged() { - return gitHubSettings.isProvisioningEnabled(); - } - - @Override - public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) { - Set<String> gitHubUserUuids = findManagedUserUuids(dbSession, userUuids); - - return userUuids.stream() - .collect(toMap(Function.identity(), gitHubUserUuids::contains)); - } - - private Set<String> findManagedUserUuids(DbSession dbSession, Set<String> userUuids) { - List<UserDto> userDtos = findManagedUsers(dbSession, userUuids); - - return userDtos.stream() - .map(UserDto::getUuid) - .collect(toSet()); - } - - private List<UserDto> findManagedUsers(DbSession dbSession, Set<String> userUuids) { - UserQuery managedUsersQuery = UserQuery.builder() - .userUuids(userUuids) - .isManagedClause(getManagedUsersSqlFilter(true)) - .build(); - - return userDao.selectUsers(dbSession, managedUsersQuery); - } - - @Override - public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) { - throw new IllegalStateException("Not implemented."); - } - - @Override - public String getManagedUsersSqlFilter(boolean filterByManaged) { - String operator = filterByManaged ? "=" : "<>"; - return String.format("external_identity_provider %s '%s'", operator, GitHubIdentityProvider.KEY); - } - - @Override - public String getManagedGroupsSqlFilter(boolean filterByManaged) { - throw new IllegalStateException("Not implemented."); - } -} diff --git a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubManagedInstanceServiceTest.java b/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubManagedInstanceServiceTest.java deleted file mode 100644 index 7d09ab883c0..00000000000 --- a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubManagedInstanceServiceTest.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.auth.github; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.sonar.db.DbSession; -import org.sonar.db.user.UserDao; -import org.sonar.db.user.UserDto; -import org.sonar.db.user.UserQuery; - -import static java.util.Collections.emptyList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class GitHubManagedInstanceServiceTest { - - @Mock - private GitHubSettings gitHubSettings; - - @Mock - private UserDao userDao; - - @Mock - private DbSession dbSession; - - @Captor - private ArgumentCaptor<UserQuery> userQueryCaptor; - - @InjectMocks - private GitHubManagedInstanceService gitHubManagedInstanceService; - - @Test - public void isInstanceExternallyManaged_whenFalse_returnsFalse() { - when(gitHubSettings.isProvisioningEnabled()).thenReturn(false); - assertThat(gitHubManagedInstanceService.isInstanceExternallyManaged()).isFalse(); - } - - @Test - public void isInstanceExternallyManaged_whenTrue_returnsTrue() { - when(gitHubSettings.isProvisioningEnabled()).thenReturn(true); - assertThat(gitHubManagedInstanceService.isInstanceExternallyManaged()).isTrue(); - } - - @Test - public void getManagedUsersSqlFilter_whenTrue_returnsFilterByGithub() { - String managedUsersSqlFilter = gitHubManagedInstanceService.getManagedUsersSqlFilter(true); - assertThat(managedUsersSqlFilter).isEqualTo("external_identity_provider = 'github'"); - } - - @Test - public void getManagedUsersSqlFilter_whenFalse_returnsFilterByNotGithub() { - String managedUsersSqlFilter = gitHubManagedInstanceService.getManagedUsersSqlFilter(false); - assertThat(managedUsersSqlFilter).isEqualTo("external_identity_provider <> 'github'"); - } - - @Test - public void getUserUuidToManaged_whenNoUsers_returnsFalseForAllInput() { - Set<String> uuids = Set.of("uuid1", "uuid2"); - Map<String, Boolean> userUuidToManaged = gitHubManagedInstanceService.getUserUuidToManaged(dbSession, uuids); - - assertThat(userUuidToManaged) - .hasSize(2) - .containsEntry("uuid1", false) - .containsEntry("uuid2", false); - } - - @Test - public void getUserUuidToManaged_whenOneUserManaged_returnsTrueForIt() { - String managedUserUuid = "managedUserUuid"; - Set<String> uuids = Set.of("uuid1", managedUserUuid); - - UserDto user2dto = mock(UserDto.class); - when(user2dto.getUuid()).thenReturn(managedUserUuid); - - when(userDao.selectUsers(eq(dbSession), userQueryCaptor.capture())).thenReturn(List.of(user2dto)); - - Map<String, Boolean> userUuidToManaged = gitHubManagedInstanceService.getUserUuidToManaged(dbSession, uuids); - - assertThat(userUuidToManaged) - .hasSize(2) - .containsEntry("uuid1", false) - .containsEntry(managedUserUuid, true); - } - - @Test - public void getUserUuidToManaged_sendsTheRightQueryToUserDao() { - Set<String> uuids = Set.of("uuid1", "uuid2"); - - when(userDao.selectUsers(eq(dbSession), userQueryCaptor.capture())).thenReturn(emptyList()); - - gitHubManagedInstanceService.getUserUuidToManaged(dbSession, uuids); - - UserQuery capturedQuery = userQueryCaptor.getValue(); - UserQuery expectedQuery = UserQuery.builder() - .userUuids(uuids) - .isManagedClause(gitHubManagedInstanceService.getManagedUsersSqlFilter(true)) - .build(); - assertThat(capturedQuery).usingRecursiveComparison().isEqualTo(expectedQuery); - } - -} |