You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetricsActionTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.monitoring;
  21. import org.assertj.core.api.Assertions;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.server.ws.WebService;
  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;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.mockito.Mockito.any;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. import static org.sonar.server.tester.UserSessionRule.standalone;
  38. public class MetricsActionTest {
  39. @Rule
  40. public UserSessionRule userSession = standalone();
  41. @Rule
  42. public DbTester db = DbTester.create();
  43. private final BearerPasscode bearerPasscode = mock(BearerPasscode.class);
  44. private final SystemPasscode systemPasscode = mock(SystemPasscode.class);
  45. private final MetricsAction underTest = new MetricsAction(systemPasscode, bearerPasscode, userSession);
  46. private final WsActionTester ws = new WsActionTester(underTest);
  47. @Test
  48. public void test_definition() {
  49. WebService.Action definition = ws.getDef();
  50. assertThat(definition.isInternal()).isFalse();
  51. assertThat(definition.isPost()).isFalse();
  52. assertThat(definition.responseExampleAsString()).isNotEmpty();
  53. assertThat(definition.params()).isEmpty();
  54. }
  55. @Test
  56. public void no_authentication_throw_insufficient_privileges_error() {
  57. TestRequest request = ws.newRequest();
  58. Assertions.assertThatThrownBy(request::execute)
  59. .hasMessage("Insufficient privileges")
  60. .isInstanceOf(ForbiddenException.class);
  61. }
  62. @Test
  63. public void authenticated_non_global_admin_is_forbidden() {
  64. userSession.logIn();
  65. TestRequest testRequest = ws.newRequest();
  66. Assertions.assertThatThrownBy(testRequest::execute)
  67. .hasMessage("Insufficient privileges")
  68. .isInstanceOf(ForbiddenException.class);
  69. }
  70. @Test
  71. public void authentication_passcode_is_allowed() {
  72. when(systemPasscode.isValid(any())).thenReturn(true);
  73. TestResponse response = ws.newRequest().execute();
  74. String content = response.getInput();
  75. assertThat(content)
  76. .contains("# HELP is_web_up Tells whether web service is up")
  77. .contains("# TYPE is_web_up gauge")
  78. .contains("is_web_up 0.0");
  79. }
  80. @Test
  81. public void authentication_bearer_passcode_is_allowed() {
  82. when(bearerPasscode.isValid(any())).thenReturn(true);
  83. TestResponse response = ws.newRequest().execute();
  84. String content = response.getInput();
  85. assertThat(content)
  86. .contains("# HELP is_web_up Tells whether web service is up")
  87. .contains("# TYPE is_web_up gauge")
  88. .contains("is_web_up 0.0");
  89. }
  90. @Test
  91. public void authenticated_global_admin_is_allowed() {
  92. userSession.logIn().setSystemAdministrator();
  93. TestResponse response = ws.newRequest().execute();
  94. String content = response.getInput();
  95. assertThat(content)
  96. .contains("# HELP is_web_up Tells whether web service is up")
  97. .contains("# TYPE is_web_up gauge")
  98. .contains("is_web_up 0.0");
  99. }
  100. }