import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.OrganizationFlags;
import org.sonar.server.user.UserSession;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
verifySystemAdministrator();
- verifyFeatureIsDisabled(dbSession);
- flagCurrentUserAsRoot(dbSession);
- enableFeature(dbSession);
- dbSession.commit();
+ if (isSupportDisabled(dbSession)) {
+ flagCurrentUserAsRoot(dbSession);
+ enableFeature(dbSession);
+ dbSession.commit();
+ }
}
response.noContent();
}
userSession.checkLoggedIn().checkOrganizationPermission(defaultOrganizationProvider.get().getUuid(), SYSTEM_ADMIN);
}
- private void verifyFeatureIsDisabled(DbSession dbSession) {
- if (organizationFlags.isEnabled(dbSession)) {
- throw new BadRequestException("Organizations are already enabled");
- }
+ private boolean isSupportDisabled(DbSession dbSession) {
+ return !organizationFlags.isEnabled(dbSession);
}
private void flagCurrentUserAsRoot(DbSession dbSession) {
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbTester;
import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.DefaultOrganizationProvider;
}
@Test
- public void throw_BadRequestException_if_support_is_already_enabled() {
+ public void do_nothing_if_support_is_already_enabled() {
logInAsSystemAdministrator("foo");
call();
+ verifyFeatureEnabled(true);
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Organizations are already enabled");
-
+ // the test could be improved to verify that
+ // the caller user is not flagged as root
+ // if he was not already root
call();
+ verifyFeatureEnabled(true);
}
@Test