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.server.platform.ws;
22 import org.assertj.core.api.Assertions;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.db.DbTester;
26 import org.sonar.server.exceptions.ForbiddenException;
27 import org.sonar.server.tester.UserSessionRule;
28 import org.sonar.server.user.BearerPasscode;
29 import org.sonar.server.user.SystemPasscode;
30 import org.sonar.server.ws.TestRequest;
31 import org.sonar.server.ws.TestResponse;
32 import org.sonar.server.ws.WsActionTester;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38 import static org.sonar.server.tester.UserSessionRule.standalone;
40 public class SafeModeMonitoringMetricActionIT {
43 public UserSessionRule userSession = standalone();
46 public DbTester db = DbTester.create();
48 private final BearerPasscode bearerPasscode = mock(BearerPasscode.class);
49 private final SystemPasscode systemPasscode = mock(SystemPasscode.class);
51 private final SafeModeMonitoringMetricAction safeModeMonitoringMetricAction = new SafeModeMonitoringMetricAction(systemPasscode, bearerPasscode);
52 private final WsActionTester ws = new WsActionTester(safeModeMonitoringMetricAction);
55 public void no_authentication_throw_insufficient_privileges_error() {
56 TestRequest request = ws.newRequest();
57 Assertions.assertThatThrownBy(request::execute)
58 .hasMessage("Insufficient privileges")
59 .isInstanceOf(ForbiddenException.class);
63 public void authenticated_non_global_admin_is_forbidden() {
66 TestRequest testRequest = ws.newRequest();
67 Assertions.assertThatThrownBy(testRequest::execute)
68 .hasMessage("Insufficient privileges")
69 .isInstanceOf(ForbiddenException.class);
73 public void authentication_passcode_is_allowed() {
74 when(systemPasscode.isValid(any())).thenReturn(true);
76 TestResponse response = ws.newRequest().execute();
77 String content = response.getInput();
79 .contains("# HELP sonarqube_health_web_status Tells whether Web process is up or down. 1 for up, 0 for down")
80 .contains("# TYPE sonarqube_health_web_status gauge")
81 .contains("sonarqube_health_web_status 1.0");
85 public void authentication_bearer_passcode_is_allowed() {
86 when(bearerPasscode.isValid(any())).thenReturn(true);
88 TestResponse response = ws.newRequest().execute();
89 String content = response.getInput();
91 .contains("# HELP sonarqube_health_web_status Tells whether Web process is up or down. 1 for up, 0 for down")
92 .contains("# TYPE sonarqube_health_web_status gauge")
93 .contains("sonarqube_health_web_status 1.0");
97 public void authenticated_global_admin_is_not_allowed_in_safe_mode() {
98 userSession.logIn().setSystemAdministrator();
100 TestRequest testRequest = ws.newRequest();
101 Assertions.assertThatThrownBy(testRequest::execute)
102 .hasMessage("Insufficient privileges")
103 .isInstanceOf(ForbiddenException.class);