3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.setting.ws;
23 import java.io.IOException;
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;
37 import static org.assertj.core.api.Assertions.assertThat;
39 public class GenerateSecretKeyActionTest {
41 public ExpectedException expectedException = ExpectedException.none();
43 public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator();
45 public TemporaryFolder temporaryFolder = new TemporaryFolder();
47 private MapSettings settings = new MapSettings();
48 private Encryption encryption = settings.getEncryption();
49 private GenerateSecretKeyAction underTest = new GenerateSecretKeyAction(settings, userSession);
50 private WsActionTester ws = new WsActionTester(underTest);
53 public void generate_valid_secret_key() throws IOException {
54 GenerateSecretKeyWsResponse result = call();
56 String secretKey = result.getSecretKey();
57 File file = temporaryFolder.newFile();
58 FileUtils.writeStringToFile(file, secretKey);
59 encryption.setPathToSecretKey(file.getAbsolutePath());
60 String encryptedValue = encryption.encrypt("my value");
61 String decryptedValue = encryption.decrypt(encryptedValue);
62 assertThat(decryptedValue).isEqualTo("my value");
66 public void definition() {
67 WebService.Action definition = ws.getDef();
69 assertThat(definition.key()).isEqualTo("generate_secret_key");
70 assertThat(definition.isPost()).isFalse();
71 assertThat(definition.isInternal()).isTrue();
72 assertThat(definition.responseExampleAsString()).isNotEmpty();
73 assertThat(definition.params()).hasSize(0);
77 public void throw_ForbiddenException_if_not_system_administrator() {
78 userSession.logIn().setNonSystemAdministrator();
80 expectedException.expect(ForbiddenException.class);
81 expectedException.expectMessage("Insufficient privileges");
87 private GenerateSecretKeyWsResponse call() {
88 return ws.newRequest()
90 .executeProtobuf(GenerateSecretKeyWsResponse.class);