3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.common.github.permissions;
22 import java.util.List;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.core.util.SequenceUuidFactory;
28 import org.sonar.core.util.UuidFactory;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.audit.AuditPersister;
32 import org.sonar.db.provisioning.GithubPermissionsMappingDao;
33 import org.sonar.db.provisioning.GithubPermissionsMappingDto;
34 import org.sonar.server.common.permission.Operation;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.sonar.server.common.github.permissions.GithubPermissionsMappingService.ADMIN_GITHUB_ROLE;
39 import static org.sonar.server.common.github.permissions.GithubPermissionsMappingService.MAINTAIN_GITHUB_ROLE;
40 import static org.sonar.server.common.github.permissions.GithubPermissionsMappingService.READ_GITHUB_ROLE;
41 import static org.sonar.server.common.github.permissions.GithubPermissionsMappingService.TRIAGE_GITHUB_ROLE;
42 import static org.sonar.server.common.github.permissions.GithubPermissionsMappingService.WRITE_GITHUB_ROLE;
44 public class GithubPermissionsMappingServiceIT {
46 private static final SonarqubePermissions NO_SQ_PERMISSIONS = new SonarqubePermissions(false, false, false, false, false, false);
48 public DbTester db = DbTester.create();
49 private final DbSession dbSession = db.getSession();
51 private final AuditPersister auditPersister = mock();
52 private final GithubPermissionsMappingDao githubPermissionsMappingDao = new GithubPermissionsMappingDao(auditPersister);
54 private final UuidFactory uuidFactory = new SequenceUuidFactory();
56 private final GithubPermissionsMappingService underTest = new GithubPermissionsMappingService(db.getDbClient(), githubPermissionsMappingDao, uuidFactory);
59 public void getPermissionsMapping_whenMappingNotDefined_returnMappingEntirelyFalse() {
60 List<GithubPermissionsMapping> actualPermissionsMapping = underTest.getPermissionsMapping();
62 List<GithubPermissionsMapping> expectedPermissionsMapping = List.of(
63 new GithubPermissionsMapping(READ_GITHUB_ROLE, NO_SQ_PERMISSIONS),
64 new GithubPermissionsMapping(TRIAGE_GITHUB_ROLE, NO_SQ_PERMISSIONS),
65 new GithubPermissionsMapping(WRITE_GITHUB_ROLE, NO_SQ_PERMISSIONS),
66 new GithubPermissionsMapping(MAINTAIN_GITHUB_ROLE, NO_SQ_PERMISSIONS),
67 new GithubPermissionsMapping(ADMIN_GITHUB_ROLE, NO_SQ_PERMISSIONS));
69 assertThat(actualPermissionsMapping).containsAll(expectedPermissionsMapping);
73 public void getPermissionsMapping_whenMappingDefined_returnMapping() {
74 Map<String, Set<String>> githubRolesToSqPermissions = Map.of(
75 READ_GITHUB_ROLE, Set.of("user", "codeviewer"),
76 WRITE_GITHUB_ROLE, Set.of("user", "codeviewer", "issueadmin", "securityhotspotadmin", "admin", "scan"));
77 persistGithubPermissionsMapping(githubRolesToSqPermissions);
79 List<GithubPermissionsMapping> actualPermissionsMapping = underTest.getPermissionsMapping();
81 List<GithubPermissionsMapping> expectedPermissionsMapping = List.of(
82 new GithubPermissionsMapping(READ_GITHUB_ROLE, new SonarqubePermissions(true, true, false, false, false, false)),
83 new GithubPermissionsMapping(TRIAGE_GITHUB_ROLE, NO_SQ_PERMISSIONS),
84 new GithubPermissionsMapping(WRITE_GITHUB_ROLE, new SonarqubePermissions(true, true, true, true, true, true)),
85 new GithubPermissionsMapping(MAINTAIN_GITHUB_ROLE, NO_SQ_PERMISSIONS),
86 new GithubPermissionsMapping(ADMIN_GITHUB_ROLE, NO_SQ_PERMISSIONS));
88 assertThat(actualPermissionsMapping).containsAll(expectedPermissionsMapping);
91 private void persistGithubPermissionsMapping(Map<String, Set<String>> githubRolesToSonarqubePermissions) {
92 for (Map.Entry<String, Set<String>> githubRoleToSonarqubePermissions : githubRolesToSonarqubePermissions.entrySet()) {
93 String githubRole = githubRoleToSonarqubePermissions.getKey();
94 githubRoleToSonarqubePermissions.getValue()
95 .forEach(permission -> githubPermissionsMappingDao.insert(
97 new GithubPermissionsMappingDto("uuid_" + githubRole + "_" + permission, githubRole, permission)));
103 public void updatePermissionsMappings_shouldAddAndRemovePermissions() {
104 Map<String, Set<String>> githubRolesToSqPermissions = Map.of(READ_GITHUB_ROLE, Set.of("user", "codeviewer"));
105 persistGithubPermissionsMapping(githubRolesToSqPermissions);
107 PermissionMappingChange permToAdd1 = new PermissionMappingChange(READ_GITHUB_ROLE, "issueadmin", Operation.ADD);
108 PermissionMappingChange permToAdd2 = new PermissionMappingChange(READ_GITHUB_ROLE, "scan", Operation.ADD);
109 PermissionMappingChange permToRemove1 = new PermissionMappingChange(READ_GITHUB_ROLE, "user", Operation.REMOVE);
110 PermissionMappingChange permToRemove2 = new PermissionMappingChange(READ_GITHUB_ROLE, "codeviewer", Operation.REMOVE);
112 underTest.updatePermissionsMappings(Set.of(permToAdd1, permToAdd2, permToRemove1, permToRemove2));
114 GithubPermissionsMapping updatedPermissionsMapping = underTest.getPermissionsMappingForGithubRole(READ_GITHUB_ROLE);
116 GithubPermissionsMapping expectedPermissionsMapping = new GithubPermissionsMapping(READ_GITHUB_ROLE, new SonarqubePermissions(false, false, true, false, false, true));
117 assertThat(updatedPermissionsMapping).isEqualTo(expectedPermissionsMapping);
121 public void updatePermissionsMappings_whenRemovingNonExistingPermission_isNoOp() {
122 PermissionMappingChange permToRemove1 = new PermissionMappingChange(READ_GITHUB_ROLE, "user", Operation.REMOVE);
124 underTest.updatePermissionsMappings(Set.of(permToRemove1));
126 GithubPermissionsMapping updatedPermissionsMapping = underTest.getPermissionsMappingForGithubRole(READ_GITHUB_ROLE);
128 GithubPermissionsMapping expectedPermissionsMapping = new GithubPermissionsMapping(READ_GITHUB_ROLE, NO_SQ_PERMISSIONS);
129 assertThat(updatedPermissionsMapping).isEqualTo(expectedPermissionsMapping);
133 public void updatePermissionsMappings_whenAddingAlreadyExistingPermission_isNoOp() {
134 Map<String, Set<String>> githubRolesToSqPermissions = Map.of(READ_GITHUB_ROLE, Set.of("user", "codeviewer"));
135 persistGithubPermissionsMapping(githubRolesToSqPermissions);
136 PermissionMappingChange permToAdd1 = new PermissionMappingChange(READ_GITHUB_ROLE, "user", Operation.ADD);
138 underTest.updatePermissionsMappings(Set.of(permToAdd1));
140 GithubPermissionsMapping updatedPermissionsMapping = underTest.getPermissionsMappingForGithubRole(READ_GITHUB_ROLE);
142 GithubPermissionsMapping expectedPermissionsMapping = new GithubPermissionsMapping(READ_GITHUB_ROLE, new SonarqubePermissions(true, true, false, false, false, false));
143 assertThat(updatedPermissionsMapping).isEqualTo(expectedPermissionsMapping);
147 public void updatePermissionsMappings_handlesUpdatesForDifferentRoles() {
148 PermissionMappingChange permToAdd1 = new PermissionMappingChange(READ_GITHUB_ROLE, "user", Operation.ADD);
149 PermissionMappingChange permToAdd2 = new PermissionMappingChange(WRITE_GITHUB_ROLE, "user", Operation.ADD);
151 underTest.updatePermissionsMappings(Set.of(permToAdd1, permToAdd2));
153 SonarqubePermissions userOnlySqPermission = new SonarqubePermissions(true, false, false, false, false, false);
155 GithubPermissionsMapping updatedPermissionsMapping = underTest.getPermissionsMappingForGithubRole(READ_GITHUB_ROLE);
156 assertThat(updatedPermissionsMapping).isEqualTo(new GithubPermissionsMapping(READ_GITHUB_ROLE, userOnlySqPermission));
158 updatedPermissionsMapping = underTest.getPermissionsMappingForGithubRole(WRITE_GITHUB_ROLE);
159 assertThat(updatedPermissionsMapping).isEqualTo(new GithubPermissionsMapping(WRITE_GITHUB_ROLE, userOnlySqPermission));
163 public void getPermissionsMappingForGithubRole_shouldReturnMappingOnlyForRole() {
164 Map<String, Set<String>> githubRolesToSqPermissions = Map.of(
165 READ_GITHUB_ROLE, Set.of("user", "codeviewer"),
166 WRITE_GITHUB_ROLE, Set.of("user", "codeviewer", "issueadmin", "securityhotspotadmin", "admin", "scan"));
167 persistGithubPermissionsMapping(githubRolesToSqPermissions);
169 GithubPermissionsMapping actualPermissionsMapping = underTest.getPermissionsMappingForGithubRole(READ_GITHUB_ROLE);
171 GithubPermissionsMapping expectedPermissionsMapping = new GithubPermissionsMapping(READ_GITHUB_ROLE, new SonarqubePermissions(true, true, false, false, false, false));
173 assertThat(actualPermissionsMapping).isEqualTo(expectedPermissionsMapping);