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

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