+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.organization.ws;
-
-import java.util.Optional;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.alm.OrganizationAlmBindingDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
-import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
-
-public class SetMembersSyncAction implements OrganizationsWsAction {
-
- private static final String ENABLED = "enabled";
- private DbClient dbClient;
- private UserSession userSession;
-
- public SetMembersSyncAction(DbClient dbClient, UserSession userSession) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- }
-
- @Override
- public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("set_members_sync")
- .setDescription("Enable or disable organization members synchronization.<br/>" +
- "Requires 'Administer System' permission on the specified organization.")
- .setSince("7.7")
- .setPost(true)
- .setInternal(true)
- .setHandler(this);
-
- action.createParam(PARAM_ORGANIZATION)
- .setDescription("Organization key")
- .setInternal(true)
- .setRequired(true);
-
- action.createParam(ENABLED)
- .setDescription("True to enable members sync, false otherwise.")
- .setInternal(true)
- .setRequired(true)
- .setBooleanPossibleValues();
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION);
-
- try (DbSession dbSession = dbClient.openSession(false)) {
-
- OrganizationDto organization = checkFoundWithOptional(dbClient.organizationDao().selectByKey(dbSession, organizationKey),
- "Organization '%s' does not exist", organizationKey);
-
- userSession.checkPermission(ADMINISTER, organization);
-
- Optional<OrganizationAlmBindingDto> orgAlmBindingDto = dbClient.organizationAlmBindingDao().selectByOrganization(dbSession, organization);
- checkArgument(orgAlmBindingDto.isPresent(), "Organization '%s' is not bound to an ALM", organization.getKey());
-
- dbClient.organizationAlmBindingDao().updateMembersSync(dbSession, orgAlmBindingDto.get(), request.mandatoryParamAsBoolean(ENABLED));
-
- dbSession.commit();
- }
-
- response.noContent();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.organization.ws;
-
-import java.util.Optional;
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.alm.OrganizationAlmBindingDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.TestRequest;
-import org.sonar.server.ws.TestResponse;
-import org.sonar.server.ws.WsActionTester;
-
-import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
-import static java.util.Optional.ofNullable;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.AssertionsForClassTypes.tuple;
-import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
-
-public class SetMembersSyncActionTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone().logIn().setRoot();
- @Rule
- public DbTester db = DbTester.create();
-
- private DbClient dbClient = db.getDbClient();
-
- private DbSession dbSession = db.getSession();
-
- private WsActionTester ws = new WsActionTester(new SetMembersSyncAction(dbClient, userSession));
-
- @Test
- public void definition() {
- OrganizationDto organization = db.organizations().insert();
- db.alm().insertOrganizationAlmBinding(organization, db.alm().insertAlmAppInstall(), true);
-
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("set_members_sync");
- assertThat(definition.since()).isEqualTo("7.7");
- assertThat(definition.isPost()).isTrue();
- assertThat(definition.isInternal()).isTrue();
- assertThat(definition.params())
- .extracting(WebService.Param::key, WebService.Param::isRequired)
- .containsExactlyInAnyOrder(tuple("organization", true), tuple("enabled", true));
- }
-
- @Test
- public void update_members_sync() {
- OrganizationDto organization = db.organizations().insert();
- db.alm().insertOrganizationAlmBinding(organization, db.alm().insertAlmAppInstall(), true);
-
- sendRequest(organization.getKey(), true);
-
- Optional<OrganizationAlmBindingDto> dto = dbClient.organizationAlmBindingDao().selectByOrganization(dbSession, organization);
- assertThat(dto).isPresent();
- assertThat(dto.get().isMembersSyncEnable()).isTrue();
- }
-
- @Test
- public void returns_no_content() {
- OrganizationDto organization = db.organizations().insert();
- db.alm().insertOrganizationAlmBinding(organization, db.alm().insertAlmAppInstall(), true);
-
- TestResponse result = sendRequest(organization.getKey(), true);
-
- assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
- assertThat(result.getInput()).isEmpty();
- }
-
- @Test
- public void fail_if_org_is_not_admin_of_the_org() {
- OrganizationDto organization = db.organizations().insert();
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- sendRequest(organization.getKey(), true);
- }
-
- @Test
- public void fail_if_org_is_not_bound_to_an_alm() {
- OrganizationDto organization = db.organizations().insert();
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(String.format("Organization '%s' is not bound to an ALM", organization.getKey()));
-
- sendRequest(organization.getKey(), true);
- }
-
- @Test
- public void fail_if_org_does_not_exist() {
- OrganizationDto organization = db.organizations().insert();
- db.alm().insertOrganizationAlmBinding(organization, db.alm().insertAlmAppInstall(), true);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Organization '1234' does not exist");
-
- sendRequest("1234", true);
- }
-
- private TestResponse sendRequest(@Nullable String organizationKey, @Nullable Boolean enabled) {
- TestRequest request = ws.newRequest();
- ofNullable(organizationKey).ifPresent(o -> request.setParam(PARAM_ORGANIZATION, o));
- ofNullable(enabled).ifPresent(e -> request.setParam("enabled", String.valueOf(e)));
-
- return request.execute();
- }
-}