123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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.server.monitoring;
-
- import org.assertj.core.api.Assertions;
- import org.junit.Rule;
- import org.junit.Test;
- import org.sonar.api.server.ws.WebService;
- import org.sonar.db.DbTester;
- import org.sonar.server.exceptions.ForbiddenException;
- import org.sonar.server.tester.UserSessionRule;
- import org.sonar.server.user.BearerPasscode;
- import org.sonar.server.user.SystemPasscode;
- import org.sonar.server.ws.TestRequest;
- import org.sonar.server.ws.TestResponse;
- import org.sonar.server.ws.WsActionTester;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.mockito.Mockito.any;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.when;
- import static org.sonar.server.tester.UserSessionRule.standalone;
-
- public class MetricsActionTest {
-
- @Rule
- public UserSessionRule userSession = standalone();
-
- @Rule
- public DbTester db = DbTester.create();
-
- private final BearerPasscode bearerPasscode = mock(BearerPasscode.class);
- private final SystemPasscode systemPasscode = mock(SystemPasscode.class);
-
- private final MetricsAction underTest = new MetricsAction(systemPasscode, bearerPasscode, userSession);
- private final WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
- assertThat(definition.isInternal()).isFalse();
- assertThat(definition.isPost()).isFalse();
- assertThat(definition.responseExampleAsString()).isNotEmpty();
- assertThat(definition.params()).isEmpty();
- }
-
- @Test
- public void no_authentication_throw_insufficient_privileges_error() {
- TestRequest request = ws.newRequest();
- Assertions.assertThatThrownBy(request::execute)
- .hasMessage("Insufficient privileges")
- .isInstanceOf(ForbiddenException.class);
- }
-
- @Test
- public void authenticated_non_global_admin_is_forbidden() {
- userSession.logIn();
-
- TestRequest testRequest = ws.newRequest();
- Assertions.assertThatThrownBy(testRequest::execute)
- .hasMessage("Insufficient privileges")
- .isInstanceOf(ForbiddenException.class);
- }
-
- @Test
- public void authentication_passcode_is_allowed() {
- when(systemPasscode.isValid(any())).thenReturn(true);
-
- TestResponse response = ws.newRequest().execute();
- String content = response.getInput();
- assertThat(content)
- .contains("# HELP is_web_up Tells whether web service is up")
- .contains("# TYPE is_web_up gauge")
- .contains("is_web_up 0.0");
- }
-
- @Test
- public void authentication_bearer_passcode_is_allowed() {
- when(bearerPasscode.isValid(any())).thenReturn(true);
-
- TestResponse response = ws.newRequest().execute();
- String content = response.getInput();
- assertThat(content)
- .contains("# HELP is_web_up Tells whether web service is up")
- .contains("# TYPE is_web_up gauge")
- .contains("is_web_up 0.0");
- }
-
- @Test
- public void authenticated_global_admin_is_allowed() {
- userSession.logIn().setSystemAdministrator();
-
- TestResponse response = ws.newRequest().execute();
- String content = response.getInput();
- assertThat(content)
- .contains("# HELP is_web_up Tells whether web service is up")
- .contains("# TYPE is_web_up gauge")
- .contains("is_web_up 0.0");
- }
-
- }
|