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.

SystemPasscodeImplTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.user;
  21. import org.junit.After;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.config.internal.MapSettings;
  25. import org.sonar.api.impl.ws.SimpleGetRequest;
  26. import org.sonar.api.utils.log.LogTester;
  27. import org.sonar.api.utils.log.LoggerLevel;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. public class SystemPasscodeImplTest {
  30. @Rule
  31. public LogTester logTester = new LogTester();
  32. private MapSettings settings = new MapSettings();
  33. private SystemPasscodeImpl underTest = new SystemPasscodeImpl(settings.asConfig());
  34. @After
  35. public void tearDown() {
  36. underTest.stop();
  37. }
  38. @Test
  39. public void startup_logs_show_that_feature_is_enabled() {
  40. configurePasscode("foo");
  41. underTest.start();
  42. assertThat(logTester.logs(LoggerLevel.INFO)).contains("System authentication by passcode is enabled");
  43. }
  44. @Test
  45. public void startup_logs_show_that_feature_is_disabled() {
  46. underTest.start();
  47. assertThat(logTester.logs(LoggerLevel.INFO)).contains("System authentication by passcode is disabled");
  48. }
  49. @Test
  50. public void passcode_is_disabled_if_blank_configuration() {
  51. configurePasscode("");
  52. underTest.start();
  53. assertThat(logTester.logs(LoggerLevel.INFO)).contains("System authentication by passcode is disabled");
  54. }
  55. @Test
  56. public void isValid_is_true_if_request_header_matches_configured_passcode() {
  57. verifyIsValid(true, "foo", "foo");
  58. }
  59. @Test
  60. public void isValid_is_false_if_request_header_matches_configured_passcode_with_different_case() {
  61. verifyIsValid(false, "foo", "FOO");
  62. }
  63. @Test
  64. public void isValid_is_false_if_request_header_does_not_match_configured_passcode() {
  65. verifyIsValid(false, "foo", "bar");
  66. }
  67. @Test
  68. public void isValid_is_false_if_request_header_is_defined_but_passcode_is_not_configured() {
  69. verifyIsValid(false, null, "foo");
  70. }
  71. @Test
  72. public void isValid_is_false_if_request_header_is_empty() {
  73. verifyIsValid(false, "foo", "");
  74. }
  75. private void verifyIsValid(boolean expectedResult, String configuredPasscode, String header) {
  76. configurePasscode(configuredPasscode);
  77. SimpleGetRequest request = new SimpleGetRequest();
  78. request.setHeader("X-Sonar-Passcode", header);
  79. assertThat(underTest.isValid(request)).isEqualTo(expectedResult);
  80. }
  81. private void configurePasscode(String propertyValue) {
  82. settings.setProperty("sonar.web.systemPasscode", propertyValue);
  83. underTest.start();
  84. }
  85. }