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.

GenerateSecretKeyActionTest.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 java.nio.charset.StandardCharsets;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.api.config.internal.Encryption;
  30. import org.sonar.api.config.internal.MapSettings;
  31. import org.sonar.api.server.ws.WebService;
  32. import org.sonar.server.exceptions.ForbiddenException;
  33. import org.sonar.server.tester.UserSessionRule;
  34. import org.sonar.server.ws.WsActionTester;
  35. import org.sonarqube.ws.Settings.GenerateSecretKeyWsResponse;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. public class GenerateSecretKeyActionTest {
  38. @Rule
  39. public ExpectedException expectedException = ExpectedException.none();
  40. @Rule
  41. public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator();
  42. @Rule
  43. public TemporaryFolder temporaryFolder = new TemporaryFolder();
  44. private MapSettings settings = new MapSettings();
  45. private Encryption encryption = settings.getEncryption();
  46. private GenerateSecretKeyAction underTest = new GenerateSecretKeyAction(settings, userSession);
  47. private WsActionTester ws = new WsActionTester(underTest);
  48. @Test
  49. public void generate_valid_secret_key() throws IOException {
  50. GenerateSecretKeyWsResponse result = call();
  51. String secretKey = result.getSecretKey();
  52. File file = temporaryFolder.newFile();
  53. FileUtils.writeStringToFile(file, secretKey, StandardCharsets.UTF_8);
  54. encryption.setPathToSecretKey(file.getAbsolutePath());
  55. String encryptedValue = encryption.encrypt("my value");
  56. String decryptedValue = encryption.decrypt(encryptedValue);
  57. assertThat(decryptedValue).isEqualTo("my value");
  58. }
  59. @Test
  60. public void definition() {
  61. WebService.Action definition = ws.getDef();
  62. assertThat(definition.key()).isEqualTo("generate_secret_key");
  63. assertThat(definition.isPost()).isFalse();
  64. assertThat(definition.isInternal()).isTrue();
  65. assertThat(definition.responseExampleAsString()).isNotEmpty();
  66. assertThat(definition.params()).hasSize(0);
  67. }
  68. @Test
  69. public void throw_ForbiddenException_if_not_system_administrator() {
  70. userSession.logIn().setNonSystemAdministrator();
  71. expectedException.expect(ForbiddenException.class);
  72. expectedException.expectMessage("Insufficient privileges");
  73. call();
  74. }
  75. private GenerateSecretKeyWsResponse call() {
  76. return ws.newRequest()
  77. .setMethod("GET")
  78. .executeProtobuf(GenerateSecretKeyWsResponse.class);
  79. }
  80. }