3 * Copyright (C) 2009-2024 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.alm.client.github;
22 import java.util.Arrays;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.junit.runners.Suite;
28 import org.sonar.auth.github.GsonRepositoryPermissions;
29 import org.sonar.db.provisioning.GithubPermissionsMappingDto;
31 import static org.assertj.core.api.Assertions.assertThat;
35 GithubPermissionConverterTest.ToSonarqubeRolesForDefaultRepositoryPermissionTest.class,
36 GithubPermissionConverterTest.ToSonarqubeRolesWithFallbackOnRepositoryPermissionsTest.class
38 public class GithubPermissionConverterTest {
40 private static final Set<GithubPermissionsMappingDto> ALL_PERMISSIONS_MAPPING_FROM_DB = Set.of(
41 new GithubPermissionsMappingDto("uuid1", "read", "roleRead"),
42 new GithubPermissionsMappingDto("uuid2", "triage", "roleTriage"),
43 new GithubPermissionsMappingDto("uuid3", "write", "roleWrite"),
44 new GithubPermissionsMappingDto("uuid4", "maintain", "roleMaintain"),
45 new GithubPermissionsMappingDto("uuid5", "admin", "roleAdmin")
48 private static final GsonRepositoryPermissions NO_PERMS = new GsonRepositoryPermissions(false, false, false, false, false);
49 private static final GsonRepositoryPermissions READ_PERMS = new GsonRepositoryPermissions(false, false, false, false, true);
50 private static final GsonRepositoryPermissions TRIAGE_PERMS = new GsonRepositoryPermissions(false, false, false, true, true);
51 private static final GsonRepositoryPermissions WRITE_PERMS = new GsonRepositoryPermissions(false, false, true, true, true);
52 private static final GsonRepositoryPermissions MAINTAIN_PERMS = new GsonRepositoryPermissions(false, true, true, true, true);
53 private static final GsonRepositoryPermissions ADMIN_PERMS = new GsonRepositoryPermissions(true, true, true, true, true);
55 @RunWith(Parameterized.class)
56 public static class ToSonarqubeRolesWithFallbackOnRepositoryPermissionsTest {
57 private final GithubPermissionConverter githubPermissionConverter = new GithubPermissionConverter();
58 private final String role;
59 private final GsonRepositoryPermissions repositoryPermissions;
60 private final Set<String> expectedSqPermissions;
62 @Parameterized.Parameters(name = "GH role:{0}, GH perms:{1}, Expected SQ perms:{2}")
63 public static Iterable<Object[]> testData() {
64 return Arrays.asList(new Object[][] {
65 {"none", NO_PERMS, Set.of()},
66 {"read", NO_PERMS, Set.of("roleRead")},
67 {"read", READ_PERMS, Set.of("roleRead")},
68 {"pull", NO_PERMS, Set.of("roleRead")},
69 {"triage", NO_PERMS, Set.of("roleTriage")},
70 {"write", NO_PERMS, Set.of("roleWrite")},
71 {"push", NO_PERMS, Set.of("roleWrite")},
72 {"maintain", NO_PERMS, Set.of("roleMaintain")},
73 {"admin", NO_PERMS, Set.of("roleAdmin")},
74 {"custom_role_extending_read", READ_PERMS, Set.of("roleRead")},
75 {"custom_role_extending_triage", TRIAGE_PERMS, Set.of("roleTriage")},
76 {"custom_role_extending_write", WRITE_PERMS, Set.of("roleWrite")},
77 {"custom_role_extending_maintain", MAINTAIN_PERMS, Set.of("roleMaintain")},
78 {"custom_role_extending_admin", ADMIN_PERMS, Set.of("roleAdmin")},
82 public ToSonarqubeRolesWithFallbackOnRepositoryPermissionsTest(String role, GsonRepositoryPermissions repositoryPermissions, Set<String> expectedSqPermissions) {
84 this.repositoryPermissions = repositoryPermissions;
85 this.expectedSqPermissions = expectedSqPermissions;
89 public void toGithubRepositoryPermissions_convertsCorrectly() {
90 Set<String> actualPermissions = githubPermissionConverter.toSonarqubeRolesWithFallbackOnRepositoryPermissions(ALL_PERMISSIONS_MAPPING_FROM_DB, role, repositoryPermissions);
91 assertThat(actualPermissions).isEqualTo(expectedSqPermissions);
95 @RunWith(Parameterized.class)
96 public static class ToSonarqubeRolesForDefaultRepositoryPermissionTest {
97 private final GithubPermissionConverter githubPermissionConverter = new GithubPermissionConverter();
98 private final String role;
99 private final Set<String> expectedSqPermissions;
101 @Parameterized.Parameters(name = "GH role:{0}, GH perms:{1}, Expected SQ perms:{2}")
102 public static Iterable<Object[]> testData() {
103 return Arrays.asList(new Object[][] {
105 {"read", Set.of("roleRead")},
106 {"triage", Set.of("roleTriage")},
107 {"write", Set.of("roleWrite")},
108 {"maintain", Set.of("roleMaintain")},
109 {"admin", Set.of("roleAdmin")}
113 public ToSonarqubeRolesForDefaultRepositoryPermissionTest(String role, Set<String> expectedSqPermissions) {
115 this.expectedSqPermissions = expectedSqPermissions;
119 public void toGithubRepositoryPermissions_convertsCorrectly() {
120 Set<String> actualPermissions = githubPermissionConverter.toSonarqubeRolesForDefaultRepositoryPermission(ALL_PERMISSIONS_MAPPING_FROM_DB, role);
121 assertThat(actualPermissions).isEqualTo(expectedSqPermissions);