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.3KB

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