Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DefaultLivenessControllerIT.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.v2.controller;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.mockito.Mockito;
  25. import org.sonar.server.platform.ws.LivenessChecker;
  26. import org.sonar.server.user.SystemPasscode;
  27. import org.sonar.server.user.UserSession;
  28. import org.sonar.server.v2.common.ControllerIT;
  29. import static org.mockito.Mockito.when;
  30. import static org.sonar.server.user.SystemPasscodeImpl.PASSCODE_HTTP_HEADER;
  31. import static org.sonar.server.v2.WebApiEndpoints.LIVENESS_ENDPOINT;
  32. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  33. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  34. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  35. public class DefaultLivenessControllerIT extends ControllerIT {
  36. private static final String VALID_PASSCODE = "valid_passcode";
  37. private static final String INVALID_PASSCODE = "invalid_passcode";
  38. @Before
  39. public void setup() {
  40. super.setup();
  41. when(webAppContext.getBean(LivenessChecker.class).liveness()).thenReturn(true);
  42. }
  43. @After
  44. public void resetUsedMocks() {
  45. Mockito.reset(webAppContext.getBean(SystemPasscode.class));
  46. Mockito.reset(webAppContext.getBean(UserSession.class));
  47. }
  48. @Test
  49. public void getSystemLiveness_whenValidPasscode_shouldSucceed() throws Exception {
  50. when(webAppContext.getBean(SystemPasscode.class).isValidPasscode(VALID_PASSCODE)).thenReturn(true);
  51. mockMvc.perform(get(LIVENESS_ENDPOINT).header(PASSCODE_HTTP_HEADER, VALID_PASSCODE))
  52. .andExpect(status().isNoContent());
  53. }
  54. @Test
  55. public void getSystemLiveness_whenAdminCredential_shouldSucceed() throws Exception {
  56. when(webAppContext.getBean(UserSession.class).isSystemAdministrator()).thenReturn(true);
  57. mockMvc.perform(get(LIVENESS_ENDPOINT))
  58. .andExpect(status().isNoContent());
  59. }
  60. @Test
  61. public void getSystemLiveness_whenNoUserSessionAndNoPasscode_shouldReturnForbidden() throws Exception {
  62. mockMvc.perform(get(LIVENESS_ENDPOINT))
  63. .andExpectAll(
  64. status().isForbidden(),
  65. content().json("{\"message\":\"Insufficient privileges\"}"));
  66. }
  67. @Test
  68. public void getSystemLiveness_whenInvalidPasscodeAndNoAdminCredentials_shouldReturnForbidden() throws Exception {
  69. when(webAppContext.getBean(SystemPasscode.class).isValidPasscode(INVALID_PASSCODE)).thenReturn(false);
  70. when(webAppContext.getBean(UserSession.class).isSystemAdministrator()).thenReturn(false);
  71. mockMvc.perform(get(LIVENESS_ENDPOINT).header(PASSCODE_HTTP_HEADER, INVALID_PASSCODE))
  72. .andExpectAll(
  73. status().isForbidden(),
  74. content().json("{\"message\":\"Insufficient privileges\"}"));
  75. }
  76. @Test
  77. public void getSystemLiveness_whenLivenessCheckFails_shouldReturnServerError() throws Exception {
  78. when(webAppContext.getBean(SystemPasscode.class).isValidPasscode(VALID_PASSCODE)).thenReturn(true);
  79. when(webAppContext.getBean(LivenessChecker.class).liveness()).thenReturn(false);
  80. mockMvc.perform(get(LIVENESS_ENDPOINT).header(PASSCODE_HTTP_HEADER, VALID_PASSCODE))
  81. .andExpectAll(
  82. status().isInternalServerError(),
  83. content().json("{\"message\":\"Liveness check failed\"}"));
  84. }
  85. }