import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Settings.CheckSecretKeyWsResponse;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class CheckSecretKeyAction implements SettingsWsAction {
@Override
public void handle(Request request, Response response) throws Exception {
- userSession.checkPermission(SYSTEM_ADMIN);
+ userSession.checkLoggedIn().checkIsRoot();
writeProtobuf(CheckSecretKeyWsResponse.newBuilder().setSecretKeyAvailable(settings.getEncryption().hasSecretKey()).build(), request, response);
}
import org.sonar.api.config.Settings;
import org.sonar.api.server.ws.WebService;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Settings.CheckSecretKeyWsResponse;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.test.JsonAssert.assertJson;
public class CheckSecretKeyActionTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public UserSessionRule userSession = UserSessionRule.standalone().setGlobalPermissions(SYSTEM_ADMIN);
+ public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
- Settings settings = new MapSettings();
- Encryption encryption = settings.getEncryption();
-
- CheckSecretKeyAction underTest = new CheckSecretKeyAction(settings, userSession);
-
- WsActionTester ws = new WsActionTester(underTest);
+ private Settings settings = new MapSettings();
+ private Encryption encryption = settings.getEncryption();
+ private CheckSecretKeyAction underTest = new CheckSecretKeyAction(settings, userSession);
+ private WsActionTester ws = new WsActionTester(underTest);
@Test
public void json_example() throws IOException {
+ logInAsRoot();
+
File secretKeyFile = temporaryFolder.newFile();
FileUtils.writeStringToFile(secretKeyFile, "fCVFf/JHRi8Qwu5KLNva7g==");
encryption.setPathToSecretKey(secretKeyFile.getAbsolutePath());
@Test
public void false_when_no_secret_key() {
+ logInAsRoot();
+
encryption.setPathToSecretKey("unknown/path/to_secret_key.txt");
CheckSecretKeyWsResponse result = call();
}
@Test
- public void fail_if_insufficient_permissions() {
- expectedException.expect(ForbiddenException.class);
+ public void throw_UnauthorizedException_if_not_logged_in() {
+ userSession.anonymous();
- userSession.anonymous().setGlobalPermissions(QUALITY_PROFILE_ADMIN);
+ expectedException.expect(UnauthorizedException.class);
+ expectedException.expectMessage("Authentication is required");
+
+ call();
+ }
+
+ @Test
+ public void throw_ForbiddenException_if_not_root() {
+ userSession.login();
+
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
call();
}
}
}
+ private void logInAsRoot() {
+ userSession.login().setRoot();
+ }
}