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.

BearerPasscodeTest.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.user;
  21. import org.junit.Test;
  22. import org.sonar.api.config.internal.MapSettings;
  23. import org.sonar.api.impl.ws.SimpleGetRequest;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. public class BearerPasscodeTest {
  26. private final MapSettings settings = new MapSettings();
  27. private final BearerPasscode underTest = new BearerPasscode(settings.asConfig());
  28. @Test
  29. public void isValid_is_true_if_request_header_matches_configured_passcode() {
  30. verifyIsValid(true, "foo", "foo");
  31. }
  32. @Test
  33. public void isValid_is_false_if_request_header_matches_configured_passcode_with_different_case() {
  34. verifyIsValid(false, "foo", "FOO");
  35. }
  36. @Test
  37. public void isValid_is_false_if_request_header_does_not_match_configured_passcode() {
  38. verifyIsValid(false, "foo", "bar");
  39. }
  40. @Test
  41. public void isValid_is_false_if_request_header_is_defined_but_passcode_is_not_configured() {
  42. verifyIsValid(false, null, "foo");
  43. }
  44. @Test
  45. public void isValid_is_false_if_request_header_is_empty() {
  46. verifyIsValid(false, "foo", "");
  47. }
  48. private void verifyIsValid(boolean expectedResult, String configuredPasscode, String token) {
  49. configurePasscode(configuredPasscode);
  50. SimpleGetRequest request = new SimpleGetRequest();
  51. request.setHeader("Authorization", "Bearer " + token);
  52. assertThat(underTest.isValid(request)).isEqualTo(expectedResult);
  53. }
  54. private void configurePasscode(String propertyValue) {
  55. settings.setProperty("sonar.web.systemPasscode", propertyValue);
  56. }
  57. }