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.

EncryptActionTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.nio.charset.StandardCharsets;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.config.internal.Encryption;
  31. import org.sonar.api.config.internal.MapSettings;
  32. import org.sonar.api.server.ws.WebService;
  33. import org.sonar.server.exceptions.BadRequestException;
  34. import org.sonar.server.exceptions.ForbiddenException;
  35. import org.sonar.server.tester.UserSessionRule;
  36. import org.sonar.server.ws.TestRequest;
  37. import org.sonar.server.ws.WsActionTester;
  38. import org.sonarqube.ws.Settings.EncryptWsResponse;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_VALUE;
  41. import static org.sonar.test.JsonAssert.assertJson;
  42. public class EncryptActionTest {
  43. @Rule
  44. public ExpectedException expectedException = ExpectedException.none();
  45. @Rule
  46. public UserSessionRule userSession = UserSessionRule.standalone();
  47. @Rule
  48. public TemporaryFolder folder = new TemporaryFolder();
  49. private MapSettings settings = new MapSettings();
  50. private Encryption encryption = settings.getEncryption();
  51. private EncryptAction underTest = new EncryptAction(userSession, settings);
  52. private WsActionTester ws = new WsActionTester(underTest);
  53. @Before
  54. public void setUpSecretKey() throws Exception {
  55. logInAsSystemAdministrator();
  56. File secretKeyFile = folder.newFile();
  57. FileUtils.writeStringToFile(secretKeyFile, "fCVFf/JHRi8Qwu5KLNva7g==", StandardCharsets.UTF_8);
  58. encryption.setPathToSecretKey(secretKeyFile.getAbsolutePath());
  59. }
  60. @Test
  61. public void json_example() {
  62. logInAsSystemAdministrator();
  63. String result = ws.newRequest().setParam("value", "my value").execute().getInput();
  64. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  65. }
  66. @Test
  67. public void encrypt() {
  68. logInAsSystemAdministrator();
  69. EncryptWsResponse result = call("my value!");
  70. assertThat(result.getEncryptedValue()).isEqualTo("{aes}NoofntibpMBdhkMfXQxYcA==");
  71. }
  72. @Test
  73. public void definition() {
  74. WebService.Action definition = ws.getDef();
  75. assertThat(definition.key()).isEqualTo("encrypt");
  76. assertThat(definition.isPost()).isFalse();
  77. assertThat(definition.isInternal()).isTrue();
  78. assertThat(definition.responseExampleAsString()).isNotEmpty();
  79. assertThat(definition.params()).hasSize(1);
  80. }
  81. @Test
  82. public void throw_ForbiddenException_if_not_system_administrator() {
  83. userSession.logIn().setNonSystemAdministrator();
  84. expectedException.expect(ForbiddenException.class);
  85. expectedException.expectMessage("Insufficient privileges");
  86. call("my value");
  87. }
  88. @Test
  89. public void fail_if_value_is_not_provided() {
  90. logInAsSystemAdministrator();
  91. expectedException.expect(IllegalArgumentException.class);
  92. call(null);
  93. }
  94. @Test
  95. public void fail_if_value_is_empty() {
  96. logInAsSystemAdministrator();
  97. expectedException.expect(IllegalArgumentException.class);
  98. expectedException.expectMessage("The 'value' parameter is missing");
  99. call(" ");
  100. }
  101. @Test
  102. public void fail_if_no_secret_key_available() {
  103. logInAsSystemAdministrator();
  104. encryption.setPathToSecretKey("unknown/path/to/secret/key");
  105. expectedException.expect(BadRequestException.class);
  106. expectedException.expectMessage("No secret key available");
  107. call("my value");
  108. }
  109. private EncryptWsResponse call(@Nullable String value) {
  110. TestRequest request = ws.newRequest()
  111. .setMethod("POST");
  112. if (value != null) {
  113. request.setParam(PARAM_VALUE, value);
  114. }
  115. return request.executeProtobuf(EncryptWsResponse.class);
  116. }
  117. private void logInAsSystemAdministrator() {
  118. userSession.logIn().setSystemAdministrator();
  119. }
  120. }