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.v2.api.github.permissions.controller;
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import java.util.List;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.server.common.github.permissions.GithubPermissionsMapping;
28 import org.sonar.server.common.github.permissions.GithubPermissionsMappingService;
29 import org.sonar.server.common.github.permissions.SonarqubePermissions;
30 import org.sonar.server.tester.UserSessionRule;
31 import org.sonar.server.v2.api.ControllerTester;
32 import org.sonar.server.v2.api.github.permissions.model.RestGithubPermissionsMapping;
33 import org.sonar.server.v2.api.github.permissions.response.GithubPermissionsMappingRestResponse;
34 import org.springframework.test.web.servlet.MockMvc;
35 import org.springframework.test.web.servlet.MvcResult;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.server.v2.WebApiEndpoints.GITHUB_PERMISSIONS_ENDPOINT;
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
43 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45 public class DefaultGithubPermissionsControllerTest {
47 public UserSessionRule userSession = UserSessionRule.standalone();
48 private final GithubPermissionsMappingService githubPermissionsMappingService = mock();
50 private final MockMvc mockMvc = ControllerTester.getMockMvc(new DefaultGithubPermissionsController(userSession, githubPermissionsMappingService));
52 private static final Gson gson = new GsonBuilder().create();
56 public void fetchMapping_whenUserIsNotAdministrator_shouldReturnForbidden() throws Exception {
57 userSession.logIn().setNonSystemAdministrator();
59 mockMvc.perform(get(GITHUB_PERMISSIONS_ENDPOINT))
61 status().isForbidden(),
62 content().json("{\"message\":\"Insufficient privileges\"}"));
66 public void fetchMapping_whenMappingSet_shouldReturnMapping() throws Exception {
67 userSession.logIn().setSystemAdministrator();
69 List<GithubPermissionsMapping> mapping = List.of(
70 new GithubPermissionsMapping("role1", new SonarqubePermissions(true, false, true, false, true, false)),
71 new GithubPermissionsMapping("role2", new SonarqubePermissions(false, true, false, true, false, true))
73 when(githubPermissionsMappingService.getPermissionsMapping()).thenReturn(mapping);
75 MvcResult mvcResult = mockMvc.perform(get(GITHUB_PERMISSIONS_ENDPOINT))
76 .andExpect(status().isOk())
79 GithubPermissionsMappingRestResponse response = gson.fromJson(mvcResult.getResponse().getContentAsString(), GithubPermissionsMappingRestResponse.class);
80 assertThat(response.githubPermissionsMappings()).isEqualTo(toRestResources(mapping));
83 private static List<RestGithubPermissionsMapping> toRestResources(List<GithubPermissionsMapping> permissionsMapping) {
84 return permissionsMapping.stream()
85 .map(e -> new RestGithubPermissionsMapping(e.roleName(), e.roleName(), e.permissions()))