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.

CheckSecretKeyActionTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.setting.ws;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import org.apache.commons.io.FileUtils;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.junit.rules.TemporaryFolder;
  28. import org.sonar.api.config.internal.Encryption;
  29. import org.sonar.api.config.internal.MapSettings;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.server.exceptions.ForbiddenException;
  32. import org.sonar.server.tester.UserSessionRule;
  33. import org.sonar.server.ws.WsActionTester;
  34. import org.sonarqube.ws.Settings.CheckSecretKeyWsResponse;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.sonar.test.JsonAssert.assertJson;
  37. public class CheckSecretKeyActionTest {
  38. @Rule
  39. public ExpectedException expectedException = ExpectedException.none();
  40. @Rule
  41. public UserSessionRule userSession = UserSessionRule.standalone();
  42. @Rule
  43. public TemporaryFolder temporaryFolder = new TemporaryFolder();
  44. private MapSettings settings = new MapSettings();
  45. private Encryption encryption = settings.getEncryption();
  46. private CheckSecretKeyAction underTest = new CheckSecretKeyAction(settings, userSession);
  47. private WsActionTester ws = new WsActionTester(underTest);
  48. @Test
  49. public void json_example() throws IOException {
  50. logInAsSystemAdministrator();
  51. File secretKeyFile = temporaryFolder.newFile();
  52. FileUtils.writeStringToFile(secretKeyFile, "fCVFf/JHRi8Qwu5KLNva7g==");
  53. encryption.setPathToSecretKey(secretKeyFile.getAbsolutePath());
  54. String result = ws.newRequest().execute().getInput();
  55. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  56. }
  57. @Test
  58. public void false_when_no_secret_key() {
  59. logInAsSystemAdministrator();
  60. encryption.setPathToSecretKey("unknown/path/to_secret_key.txt");
  61. CheckSecretKeyWsResponse result = call();
  62. assertThat(result.getSecretKeyAvailable()).isFalse();
  63. }
  64. @Test
  65. public void definition() {
  66. WebService.Action definition = ws.getDef();
  67. assertThat(definition.key()).isEqualTo("check_secret_key");
  68. assertThat(definition.isPost()).isFalse();
  69. assertThat(definition.isInternal()).isTrue();
  70. assertThat(definition.since()).isEqualTo("6.1");
  71. assertThat(definition.responseExampleAsString()).isNotEmpty();
  72. assertThat(definition.params()).hasSize(0);
  73. }
  74. @Test
  75. public void throw_ForbiddenException_if_not_system_administrator() {
  76. userSession.logIn().setNonSystemAdministrator();
  77. expectedException.expect(ForbiddenException.class);
  78. expectedException.expectMessage("Insufficient privileges");
  79. call();
  80. }
  81. private CheckSecretKeyWsResponse call() {
  82. return ws.newRequest()
  83. .setMethod("GET")
  84. .executeProtobuf(CheckSecretKeyWsResponse.class);
  85. }
  86. private void logInAsSystemAdministrator() {
  87. userSession.logIn().setSystemAdministrator();
  88. }
  89. }