+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-import org.sonar.api.server.ws.WebService;
-
-public class RootWs implements WebService {
- private final RootWsAction[] actions;
-
- public RootWs(RootWsAction... actions) {
- this.actions = actions;
- }
-
- @Override
- public void define(Context context) {
- NewController controller = context.createController("api/root")
- .setSince("6.2")
- .setDescription("Manage root users");
-
- for (RootWsAction action : actions) {
- action.define(controller);
- }
-
- controller.done();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-import org.sonar.server.ws.WsAction;
-
-public interface RootWsAction extends WsAction {
-
-}
public class RootWsModule extends Module {
@Override
protected void configureModule() {
- add(RootWs.class,
- SetRootWsAction.class,
- UnsetRootWsAction.class,
+ add(RootsWs.class,
+ SetRootAction.class,
+ UnsetRootAction.class,
SearchAction.class);
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+import org.sonar.api.server.ws.WebService;
+
+public class RootsWs implements WebService {
+ private final RootsWsAction[] actions;
+
+ public RootsWs(RootsWsAction... actions) {
+ this.actions = actions;
+ }
+
+ @Override
+ public void define(Context context) {
+ NewController controller = context.createController("api/roots")
+ .setSince("6.2")
+ .setDescription("Manage root users");
+
+ for (RootsWsAction action : actions) {
+ action.define(controller);
+ }
+
+ controller.done();
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+import org.sonar.server.ws.WsAction;
+
+public interface RootsWsAction extends WsAction {
+
+}
import static org.sonar.server.ws.WsUtils.writeProtobuf;
-public class SearchAction implements RootWsAction {
+public class SearchAction implements RootsWsAction {
private final UserSession userSession;
private final DbClient dbClient;
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+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.user.UserDto;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+
+import static java.lang.String.format;
+
+public class SetRootAction implements RootsWsAction {
+ private static final String PARAM_LOGIN = "login";
+
+ private final UserSession userSession;
+ private final DbClient dbClient;
+
+ public SetRootAction(UserSession userSession, DbClient dbClient) {
+ this.userSession = userSession;
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ WebService.NewAction action = controller.createAction("set_root")
+ .setInternal(true)
+ .setPost(true)
+ .setDescription("Make the specified user root.<br/>" +
+ "Requires to be root.")
+ .setSince("6.2")
+ .setHandler(this);
+
+ action.createParam(PARAM_LOGIN)
+ .setDescription("A user login")
+ .setExampleValue("admin")
+ .setRequired(true)
+ .setSince("6.2");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ userSession.checkIsRoot();
+
+ String login = request.mandatoryParam(PARAM_LOGIN);
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
+ if (userDto == null || !userDto.isActive()) {
+ throw new NotFoundException(format("User with login '%s' not found", login));
+ }
+
+ if (!userDto.isRoot()) {
+ dbClient.userDao().setRoot(dbSession, login, true);
+ dbSession.commit();
+ }
+ }
+ response.noContent();
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-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.user.UserDto;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.user.UserSession;
-
-import static java.lang.String.format;
-
-public class SetRootWsAction implements RootWsAction {
- private static final String PARAM_LOGIN = "login";
-
- private final UserSession userSession;
- private final DbClient dbClient;
-
- public SetRootWsAction(UserSession userSession, DbClient dbClient) {
- this.userSession = userSession;
- this.dbClient = dbClient;
- }
-
- @Override
- public void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction("set_root")
- .setInternal(true)
- .setPost(true)
- .setDescription("Make the specified user root.<br/>" +
- "Requires to be root.")
- .setSince("6.2")
- .setHandler(this);
-
- action.createParam(PARAM_LOGIN)
- .setDescription("A user login")
- .setExampleValue("admin")
- .setRequired(true)
- .setSince("6.2");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- userSession.checkIsRoot();
-
- String login = request.mandatoryParam(PARAM_LOGIN);
- try (DbSession dbSession = dbClient.openSession(false)) {
- UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
- if (userDto == null || !userDto.isActive()) {
- throw new NotFoundException(format("User with login '%s' not found", login));
- }
-
- if (!userDto.isRoot()) {
- dbClient.userDao().setRoot(dbSession, login, true);
- dbSession.commit();
- }
- }
- response.noContent();
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+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.user.UserDto;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+
+import static java.lang.String.format;
+
+public class UnsetRootAction implements RootsWsAction {
+ private static final String PARAM_LOGIN = "login";
+
+ private final UserSession userSession;
+ private final DbClient dbClient;
+
+ public UnsetRootAction(UserSession userSession, DbClient dbClient) {
+ this.userSession = userSession;
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ WebService.NewAction action = controller.createAction("unset_root")
+ .setInternal(true)
+ .setPost(true)
+ .setDescription("Make the specified user not root.<br/>" +
+ "Requires to be root.")
+ .setSince("6.2")
+ .setHandler(this);
+
+ action.createParam(PARAM_LOGIN)
+ .setDescription("A user login")
+ .setExampleValue("admin")
+ .setRequired(true)
+ .setSince("6.2");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ userSession.checkIsRoot();
+
+ String login = request.mandatoryParam(PARAM_LOGIN);
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
+ if (userDto == null || !userDto.isActive()) {
+ throw new NotFoundException(format("User with login '%s' not found", login));
+ }
+
+ if (dbClient.userDao().countRootUsersButLogin(dbSession, login) == 0) {
+ throw new BadRequestException("Last root can't be unset");
+ }
+ if (userDto.isRoot()) {
+ dbClient.userDao().setRoot(dbSession, login, false);
+ dbSession.commit();
+ }
+ }
+ response.noContent();
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-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.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.user.UserSession;
-
-import static java.lang.String.format;
-
-public class UnsetRootWsAction implements RootWsAction {
- private static final String PARAM_LOGIN = "login";
-
- private final UserSession userSession;
- private final DbClient dbClient;
-
- public UnsetRootWsAction(UserSession userSession, DbClient dbClient) {
- this.userSession = userSession;
- this.dbClient = dbClient;
- }
-
- @Override
- public void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction("unset_root")
- .setInternal(true)
- .setPost(true)
- .setDescription("Make the specified user not root.<br/>" +
- "Requires to be root.")
- .setSince("6.2")
- .setHandler(this);
-
- action.createParam(PARAM_LOGIN)
- .setDescription("A user login")
- .setExampleValue("admin")
- .setRequired(true)
- .setSince("6.2");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- userSession.checkIsRoot();
-
- String login = request.mandatoryParam(PARAM_LOGIN);
- try (DbSession dbSession = dbClient.openSession(false)) {
- UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
- if (userDto == null || !userDto.isActive()) {
- throw new NotFoundException(format("User with login '%s' not found", login));
- }
-
- if (dbClient.userDao().countRootUsersButLogin(dbSession, login) == 0) {
- throw new BadRequestException("Last root can't be unset");
- }
- if (userDto.isRoot()) {
- dbClient.userDao().setRoot(dbSession, login, false);
- dbSession.commit();
- }
- }
- response.noContent();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-import org.junit.Test;
-import org.sonar.core.platform.ComponentContainer;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RootWsModuleTest {
- private static final int ADAPTERS_IN_EMPTY_CONTAINER = 2;
-
- private RootWsModule underTest = new RootWsModule();
-
- @Test
- public void verify_number_of_components_added_by_module() {
- ComponentContainer container = new ComponentContainer();
-
- underTest.configure(container);
-
- assertThat(container.getPicoContainer().getComponentAdapters()).hasSize(ADAPTERS_IN_EMPTY_CONTAINER + 4);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-import org.junit.Test;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.server.ws.WsTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RootWsTest {
- private RootWs underTest = new RootWs(new DummyRootWsAction());
- private WsTester wsTester = new WsTester(underTest);
-
- @Test
- public void verify_definition() {
- assertThat(wsTester.context().controllers()).hasSize(1);
- WebService.Controller controller = wsTester.context().controller("api/root");
- assertThat(controller.description()).isEqualTo("Manage root users");
- assertThat(controller.since()).isEqualTo("6.2");
- }
-
- private static class DummyRootWsAction implements RootWsAction {
- @Override
- public void define(WebService.NewController context) {
- context.createAction("ooo").setHandler(this);
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
-
- }
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+import org.junit.Test;
+import org.sonar.core.platform.ComponentContainer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RootsWsModuleTest {
+ private static final int ADAPTERS_IN_EMPTY_CONTAINER = 2;
+
+ private RootWsModule underTest = new RootWsModule();
+
+ @Test
+ public void verify_number_of_components_added_by_module() {
+ ComponentContainer container = new ComponentContainer();
+
+ underTest.configure(container);
+
+ assertThat(container.getPicoContainer().getComponentAdapters()).hasSize(ADAPTERS_IN_EMPTY_CONTAINER + 4);
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.ws.WsTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RootsWsTest {
+ private RootsWs underTest = new RootsWs(new DummyRootsWsAction());
+ private WsTester wsTester = new WsTester(underTest);
+
+ @Test
+ public void verify_definition() {
+ assertThat(wsTester.context().controllers()).hasSize(1);
+ WebService.Controller controller = wsTester.context().controller("api/roots");
+ assertThat(controller.description()).isEqualTo("Manage root users");
+ assertThat(controller.since()).isEqualTo("6.2");
+ }
+
+ private static class DummyRootsWsAction implements RootsWsAction {
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction("ooo").setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+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.api.utils.System2;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.UserDao;
+import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
+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.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SetRootActionTest {
+ private static final String SOME_LOGIN = "johndoe";
+
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ @Rule
+ public UserSessionRule userSessionRule = UserSessionRule.standalone();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private UserDao userDao = dbTester.getDbClient().userDao();
+ private DbSession dbSession = dbTester.getSession();
+ private SetRootAction underTest = new SetRootAction(userSessionRule, dbTester.getDbClient());
+ private WsActionTester wsTester = new WsActionTester(underTest);
+
+ @Test
+ public void verify_definition() {
+ WebService.Action action = wsTester.getDef();
+ assertThat(action.key()).isEqualTo("set_root");
+ assertThat(action.isInternal()).isTrue();
+ assertThat(action.isPost()).isTrue();
+ assertThat(action.since()).isEqualTo("6.2");
+ assertThat(action.description()).isEqualTo("Make the specified user root.<br/>" +
+ "Requires to be root.");
+ assertThat(action.responseExample()).isNull();
+ assertThat(action.deprecatedKey()).isNull();
+ assertThat(action.deprecatedSince()).isNull();
+ assertThat(action.handler()).isSameAs(underTest);
+ assertThat(action.params()).hasSize(1);
+
+ WebService.Param param = action.param("login");
+ assertThat(param.isRequired()).isTrue();
+ assertThat(param.description()).isEqualTo("A user login");
+ assertThat(param.defaultValue()).isNull();
+ assertThat(param.deprecatedSince()).isNull();
+ assertThat(param.deprecatedKey()).isNull();
+ assertThat(param.exampleValue()).isEqualTo("admin");
+ }
+
+ @Test
+ public void execute_fails_with_ForbiddenException_when_user_is_not_logged_in() {
+ expectInsufficientPrivilegesForbiddenException();
+
+ executeRequest(SOME_LOGIN);
+ }
+
+ @Test
+ public void execute_fails_with_ForbiddenException_when_user_is_not_root() {
+ userSessionRule.login();
+
+ expectInsufficientPrivilegesForbiddenException();
+
+ executeRequest(SOME_LOGIN);
+ }
+
+ @Test
+ public void execute_fails_with_IAE_when_login_param_is_not_provided() {
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The 'login' parameter is missing");
+
+ executeRequest(null);
+ }
+
+ @Test
+ public void execute_makes_user_with_specified_login_root_when_it_is_not() {
+ UserDto otherUser = UserTesting.newUserDto();
+ userDao.insert(dbSession, otherUser);
+ userDao.insert(dbSession, UserTesting.newUserDto(SOME_LOGIN, "name", "email"));
+ dbSession.commit();
+ makeAuthenticatedUserRoot();
+
+ executeRequest(SOME_LOGIN);
+
+ assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isTrue();
+ assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isFalse();
+ }
+
+ @Test
+ public void execute_has_no_effect_when_user_is_already_root() {
+ UserDto otherUser = UserTesting.newUserDto();
+ userDao.insert(dbSession, otherUser);
+ userDao.insert(dbSession, UserTesting.newUserDto(SOME_LOGIN, "name", "email"));
+ userDao.setRoot(dbSession, SOME_LOGIN, true);
+ dbSession.commit();
+ makeAuthenticatedUserRoot();
+
+ executeRequest(SOME_LOGIN);
+
+ assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isTrue();
+ assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isFalse();
+ }
+
+ @Test
+ public void execute_fails_with_NotFoundException_when_user_for_specified_login_does_not_exist() {
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("User with login 'foo_bar' not found");
+
+ executeRequest("foo_bar");
+ }
+
+ @Test
+ public void execute_fails_with_NotFoundException_when_user_for_specified_login_is_not_active() {
+ UserDto userDto = UserTesting.newUserDto().setActive(false);
+ userDao.insert(dbSession, userDto);
+ dbSession.commit();
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("User with login '" + userDto.getLogin() + "' not found");
+
+ executeRequest(userDto.getLogin());
+ }
+
+ private void makeAuthenticatedUserRoot() {
+ userSessionRule.login().setRoot();
+ }
+
+ private void expectInsufficientPrivilegesForbiddenException() {
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
+ }
+
+ private int executeRequest(@Nullable String login) {
+ TestRequest request = wsTester.newRequest();
+ if (login != null) {
+ request.setParam("login", login);
+ }
+ return request
+ .execute()
+ .getStatus();
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-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.api.utils.System2;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.user.UserDao;
-import org.sonar.db.user.UserDto;
-import org.sonar.db.user.UserTesting;
-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.WsActionTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class SetRootWsActionTest {
- private static final String SOME_LOGIN = "johndoe";
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private UserDao userDao = dbTester.getDbClient().userDao();
- private DbSession dbSession = dbTester.getSession();
- private SetRootWsAction underTest = new SetRootWsAction(userSessionRule, dbTester.getDbClient());
- private WsActionTester wsTester = new WsActionTester(underTest);
-
- @Test
- public void verify_definition() {
- WebService.Action action = wsTester.getDef();
- assertThat(action.key()).isEqualTo("set_root");
- assertThat(action.isInternal()).isTrue();
- assertThat(action.isPost()).isTrue();
- assertThat(action.since()).isEqualTo("6.2");
- assertThat(action.description()).isEqualTo("Make the specified user root.<br/>" +
- "Requires to be root.");
- assertThat(action.responseExample()).isNull();
- assertThat(action.deprecatedKey()).isNull();
- assertThat(action.deprecatedSince()).isNull();
- assertThat(action.handler()).isSameAs(underTest);
- assertThat(action.params()).hasSize(1);
-
- WebService.Param param = action.param("login");
- assertThat(param.isRequired()).isTrue();
- assertThat(param.description()).isEqualTo("A user login");
- assertThat(param.defaultValue()).isNull();
- assertThat(param.deprecatedSince()).isNull();
- assertThat(param.deprecatedKey()).isNull();
- assertThat(param.exampleValue()).isEqualTo("admin");
- }
-
- @Test
- public void execute_fails_with_ForbiddenException_when_user_is_not_logged_in() {
- expectInsufficientPrivilegesForbiddenException();
-
- executeRequest(SOME_LOGIN);
- }
-
- @Test
- public void execute_fails_with_ForbiddenException_when_user_is_not_root() {
- userSessionRule.login();
-
- expectInsufficientPrivilegesForbiddenException();
-
- executeRequest(SOME_LOGIN);
- }
-
- @Test
- public void execute_fails_with_IAE_when_login_param_is_not_provided() {
- makeAuthenticatedUserRoot();
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'login' parameter is missing");
-
- executeRequest(null);
- }
-
- @Test
- public void execute_makes_user_with_specified_login_root_when_it_is_not() {
- UserDto otherUser = UserTesting.newUserDto();
- userDao.insert(dbSession, otherUser);
- userDao.insert(dbSession, UserTesting.newUserDto(SOME_LOGIN, "name", "email"));
- dbSession.commit();
- makeAuthenticatedUserRoot();
-
- executeRequest(SOME_LOGIN);
-
- assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isTrue();
- assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isFalse();
- }
-
- @Test
- public void execute_has_no_effect_when_user_is_already_root() {
- UserDto otherUser = UserTesting.newUserDto();
- userDao.insert(dbSession, otherUser);
- userDao.insert(dbSession, UserTesting.newUserDto(SOME_LOGIN, "name", "email"));
- userDao.setRoot(dbSession, SOME_LOGIN, true);
- dbSession.commit();
- makeAuthenticatedUserRoot();
-
- executeRequest(SOME_LOGIN);
-
- assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isTrue();
- assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isFalse();
- }
-
- @Test
- public void execute_fails_with_NotFoundException_when_user_for_specified_login_does_not_exist() {
- makeAuthenticatedUserRoot();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("User with login 'foo_bar' not found");
-
- executeRequest("foo_bar");
- }
-
- @Test
- public void execute_fails_with_NotFoundException_when_user_for_specified_login_is_not_active() {
- UserDto userDto = UserTesting.newUserDto().setActive(false);
- userDao.insert(dbSession, userDto);
- dbSession.commit();
- makeAuthenticatedUserRoot();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("User with login '" + userDto.getLogin() + "' not found");
-
- executeRequest(userDto.getLogin());
- }
-
- private void makeAuthenticatedUserRoot() {
- userSessionRule.login().setRoot();
- }
-
- private void expectInsufficientPrivilegesForbiddenException() {
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
- }
-
- private int executeRequest(@Nullable String login) {
- TestRequest request = wsTester.newRequest();
- if (login != null) {
- request.setParam("login", login);
- }
- return request
- .execute()
- .getStatus();
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.root.ws;
+
+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.api.utils.System2;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.UserDao;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.exceptions.BadRequestException;
+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.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.user.UserTesting.newUserDto;
+
+public class UnsetRootActionTest {
+ private static final String SOME_LOGIN = "johndoe";
+
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ @Rule
+ public UserSessionRule userSessionRule = UserSessionRule.standalone();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private UserDao userDao = dbTester.getDbClient().userDao();
+ private DbSession dbSession = dbTester.getSession();
+ private UnsetRootAction underTest = new UnsetRootAction(userSessionRule, dbTester.getDbClient());
+ private WsActionTester wsTester = new WsActionTester(underTest);
+
+ @Test
+ public void verify_definition() {
+ WebService.Action action = wsTester.getDef();
+ assertThat(action.key()).isEqualTo("unset_root");
+ assertThat(action.isInternal()).isTrue();
+ assertThat(action.isPost()).isTrue();
+ assertThat(action.since()).isEqualTo("6.2");
+ assertThat(action.description()).isEqualTo("Make the specified user not root.<br/>" +
+ "Requires to be root.");
+ assertThat(action.responseExample()).isNull();
+ assertThat(action.deprecatedKey()).isNull();
+ assertThat(action.deprecatedSince()).isNull();
+ assertThat(action.handler()).isSameAs(underTest);
+ assertThat(action.params()).hasSize(1);
+
+ WebService.Param param = action.param("login");
+ assertThat(param.isRequired()).isTrue();
+ assertThat(param.description()).isEqualTo("A user login");
+ assertThat(param.defaultValue()).isNull();
+ assertThat(param.deprecatedSince()).isNull();
+ assertThat(param.deprecatedKey()).isNull();
+ assertThat(param.exampleValue()).isEqualTo("admin");
+ }
+
+ @Test
+ public void execute_fails_with_ForbiddenException_when_user_is_not_logged_in() {
+ expectInsufficientPrivilegesForbiddenException();
+
+ executeRequest(SOME_LOGIN);
+ }
+
+ @Test
+ public void execute_fails_with_ForbiddenException_when_user_is_not_root() {
+ userSessionRule.login();
+
+ expectInsufficientPrivilegesForbiddenException();
+
+ executeRequest(SOME_LOGIN);
+ }
+
+ @Test
+ public void execute_fails_with_IAE_when_login_param_is_not_provided() {
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The 'login' parameter is missing");
+
+ executeRequest(null);
+ }
+
+ @Test
+ public void execute_makes_user_with_specified_login_not_root_when_it_is() {
+ UserDto otherUser = insertRootUser(newUserDto());
+ insertRootUser(newUserDto(SOME_LOGIN, "name", "email"));
+ makeAuthenticatedUserRoot();
+
+ executeRequest(SOME_LOGIN);
+
+ assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse();
+ assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue();
+ }
+
+ @Test
+ public void execute_has_no_effect_when_user_is_already_not_root() {
+ UserDto otherUser = insertRootUser(newUserDto());
+ insertNonRootUser(newUserDto(SOME_LOGIN, "name", "email"));
+ makeAuthenticatedUserRoot();
+
+ executeRequest(SOME_LOGIN);
+
+ assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse();
+ assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue();
+ }
+
+ @Test
+ public void execute_fails_with_BadRequestException_when_attempting_to_unset_root_on_last_root_user() {
+ insertRootUser(newUserDto(SOME_LOGIN, "name", "email"));
+ insertNonRootUser(newUserDto());
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("Last root can't be unset");
+
+ executeRequest(SOME_LOGIN);
+ }
+
+ @Test
+ public void execute_fails_with_BadRequestException_when_attempting_to_unset_non_root_and_there_is_no_root_at_all() {
+ UserDto userDto1 = newUserDto(SOME_LOGIN, "name", "email");
+ insertNonRootUser(userDto1);
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("Last root can't be unset");
+
+ executeRequest(userDto1.getLogin());
+ }
+
+ @Test
+ public void execute_fails_with_NotFoundException_when_user_for_specified_login_does_not_exist() {
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("User with login 'bar_foo' not found");
+
+ executeRequest("bar_foo");
+ }
+
+ @Test
+ public void execute_fails_with_NotFoundException_when_user_for_specified_login_is_inactive() {
+ UserDto userDto = insertRootUser(newUserDto().setActive(false));
+ makeAuthenticatedUserRoot();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("User with login '" + userDto.getLogin() + "' not found");
+
+ executeRequest(userDto.getLogin());
+ }
+
+ private UserDto insertNonRootUser(UserDto dto) {
+ userDao.insert(dbSession, dto);
+ dbSession.commit();
+ return dto;
+ }
+
+ private UserDto insertRootUser(UserDto dto) {
+ insertNonRootUser(dto);
+ userDao.setRoot(dbSession, dto.getLogin(), true);
+ dbSession.commit();
+ return dto;
+ }
+
+ private void makeAuthenticatedUserRoot() {
+ userSessionRule.login().setRoot();
+ }
+
+ private void expectInsufficientPrivilegesForbiddenException() {
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
+ }
+
+ private int executeRequest(@Nullable String login) {
+ TestRequest request = wsTester.newRequest();
+ if (login != null) {
+ request.setParam("login", login);
+ }
+ return request
+ .execute()
+ .getStatus();
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.root.ws;
-
-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.api.utils.System2;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.user.UserDao;
-import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
-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.WsActionTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.user.UserTesting.newUserDto;
-
-public class UnsetRootWsActionTest {
- private static final String SOME_LOGIN = "johndoe";
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private UserDao userDao = dbTester.getDbClient().userDao();
- private DbSession dbSession = dbTester.getSession();
- private UnsetRootWsAction underTest = new UnsetRootWsAction(userSessionRule, dbTester.getDbClient());
- private WsActionTester wsTester = new WsActionTester(underTest);
-
- @Test
- public void verify_definition() {
- WebService.Action action = wsTester.getDef();
- assertThat(action.key()).isEqualTo("unset_root");
- assertThat(action.isInternal()).isTrue();
- assertThat(action.isPost()).isTrue();
- assertThat(action.since()).isEqualTo("6.2");
- assertThat(action.description()).isEqualTo("Make the specified user not root.<br/>" +
- "Requires to be root.");
- assertThat(action.responseExample()).isNull();
- assertThat(action.deprecatedKey()).isNull();
- assertThat(action.deprecatedSince()).isNull();
- assertThat(action.handler()).isSameAs(underTest);
- assertThat(action.params()).hasSize(1);
-
- WebService.Param param = action.param("login");
- assertThat(param.isRequired()).isTrue();
- assertThat(param.description()).isEqualTo("A user login");
- assertThat(param.defaultValue()).isNull();
- assertThat(param.deprecatedSince()).isNull();
- assertThat(param.deprecatedKey()).isNull();
- assertThat(param.exampleValue()).isEqualTo("admin");
- }
-
- @Test
- public void execute_fails_with_ForbiddenException_when_user_is_not_logged_in() {
- expectInsufficientPrivilegesForbiddenException();
-
- executeRequest(SOME_LOGIN);
- }
-
- @Test
- public void execute_fails_with_ForbiddenException_when_user_is_not_root() {
- userSessionRule.login();
-
- expectInsufficientPrivilegesForbiddenException();
-
- executeRequest(SOME_LOGIN);
- }
-
- @Test
- public void execute_fails_with_IAE_when_login_param_is_not_provided() {
- makeAuthenticatedUserRoot();
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'login' parameter is missing");
-
- executeRequest(null);
- }
-
- @Test
- public void execute_makes_user_with_specified_login_not_root_when_it_is() {
- UserDto otherUser = insertRootUser(newUserDto());
- insertRootUser(newUserDto(SOME_LOGIN, "name", "email"));
- makeAuthenticatedUserRoot();
-
- executeRequest(SOME_LOGIN);
-
- assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse();
- assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue();
- }
-
- @Test
- public void execute_has_no_effect_when_user_is_already_not_root() {
- UserDto otherUser = insertRootUser(newUserDto());
- insertNonRootUser(newUserDto(SOME_LOGIN, "name", "email"));
- makeAuthenticatedUserRoot();
-
- executeRequest(SOME_LOGIN);
-
- assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse();
- assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue();
- }
-
- @Test
- public void execute_fails_with_BadRequestException_when_attempting_to_unset_root_on_last_root_user() {
- insertRootUser(newUserDto(SOME_LOGIN, "name", "email"));
- insertNonRootUser(newUserDto());
- makeAuthenticatedUserRoot();
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Last root can't be unset");
-
- executeRequest(SOME_LOGIN);
- }
-
- @Test
- public void execute_fails_with_BadRequestException_when_attempting_to_unset_non_root_and_there_is_no_root_at_all() {
- UserDto userDto1 = newUserDto(SOME_LOGIN, "name", "email");
- insertNonRootUser(userDto1);
- makeAuthenticatedUserRoot();
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Last root can't be unset");
-
- executeRequest(userDto1.getLogin());
- }
-
- @Test
- public void execute_fails_with_NotFoundException_when_user_for_specified_login_does_not_exist() {
- makeAuthenticatedUserRoot();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("User with login 'bar_foo' not found");
-
- executeRequest("bar_foo");
- }
-
- @Test
- public void execute_fails_with_NotFoundException_when_user_for_specified_login_is_inactive() {
- UserDto userDto = insertRootUser(newUserDto().setActive(false));
- makeAuthenticatedUserRoot();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("User with login '" + userDto.getLogin() + "' not found");
-
- executeRequest(userDto.getLogin());
- }
-
- private UserDto insertNonRootUser(UserDto dto) {
- userDao.insert(dbSession, dto);
- dbSession.commit();
- return dto;
- }
-
- private UserDto insertRootUser(UserDto dto) {
- insertNonRootUser(dto);
- userDao.setRoot(dbSession, dto.getLogin(), true);
- dbSession.commit();
- return dto;
- }
-
- private void makeAuthenticatedUserRoot() {
- userSessionRule.login().setRoot();
- }
-
- private void expectInsufficientPrivilegesForbiddenException() {
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
- }
-
- private int executeRequest(@Nullable String login) {
- TestRequest request = wsTester.newRequest();
- if (login != null) {
- request.setParam("login", login);
- }
- return request
- .execute()
- .getStatus();
- }
-
-}
import org.sonarqube.ws.client.projectlinks.ProjectLinksService;
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
-import org.sonarqube.ws.client.root.RootService;
+import org.sonarqube.ws.client.root.RootsService;
import org.sonarqube.ws.client.rule.RulesService;
import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
private final ProjectsService projectsService;
private final ProjectLinksService projectLinksService;
private final SettingsService settingsService;
- private final RootService rootService;
+ private final RootsService rootsService;
DefaultWsClient(WsConnector wsConnector) {
this.wsConnector = wsConnector;
this.projectsService = new ProjectsService(wsConnector);
this.projectLinksService = new ProjectLinksService(wsConnector);
this.settingsService = new SettingsService(wsConnector);
- this.rootService = new RootService(wsConnector);
+ this.rootsService = new RootsService(wsConnector);
}
@Override
}
@Override
- public RootService rootService() {
- return rootService;
+ public RootsService rootService() {
+ return rootsService;
}
}
import org.sonarqube.ws.client.projectlinks.ProjectLinksService;
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
-import org.sonarqube.ws.client.root.RootService;
+import org.sonarqube.ws.client.root.RootsService;
import org.sonarqube.ws.client.rule.RulesService;
import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
/**
* @since 6.2
*/
- RootService rootService();
+ RootsService rootService();
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.sonarqube.ws.client.root;
-
-import org.sonarqube.ws.WsRoot;
-import org.sonarqube.ws.client.BaseService;
-import org.sonarqube.ws.client.GetRequest;
-import org.sonarqube.ws.client.PostRequest;
-import org.sonarqube.ws.client.WsConnector;
-
-public class RootService extends BaseService {
- public RootService(WsConnector wsConnector) {
- super(wsConnector, "api/root");
- }
-
- public WsRoot.SearchWsResponse search() {
- return call(new GetRequest(path("search")), WsRoot.SearchWsResponse.parser());
- }
-
- public void setRoot(String login) {
- PostRequest post = new PostRequest(path("set_root"))
- .setParam("login", login);
-
- call(post);
- }
-
- public void unsetRoot(String login) {
- PostRequest post = new PostRequest(path("unset_root"))
- .setParam("login", login);
-
- call(post);
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonarqube.ws.client.root;
+
+import org.sonarqube.ws.WsRoot;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.WsConnector;
+
+public class RootsService extends BaseService {
+ public RootsService(WsConnector wsConnector) {
+ super(wsConnector, "api/roots");
+ }
+
+ public WsRoot.SearchWsResponse search() {
+ return call(new GetRequest(path("search")), WsRoot.SearchWsResponse.parser());
+ }
+
+ public void setRoot(String login) {
+ PostRequest post = new PostRequest(path("set_root"))
+ .setParam("login", login);
+
+ call(post);
+ }
+
+ public void unsetRoot(String login) {
+ PostRequest post = new PostRequest(path("unset_root"))
+ .setParam("login", login);
+
+ call(post);
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.sonarqube.ws.client.root;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonarqube.ws.client.ServiceTester;
-import org.sonarqube.ws.client.WsConnector;
-
-import static org.mockito.Mockito.mock;
-
-public class RootServiceTest {
- private static final String SOME_LOGIN = "johnDoe";
-
- @Rule
- public ServiceTester<RootService> serviceTester = new ServiceTester<>(new RootService(mock(WsConnector.class)));
-
- private RootService underTest = serviceTester.getInstanceUnderTest();
-
- @Test
- public void search() {
- underTest.search();
-
- serviceTester.assertThat(serviceTester.getGetRequest())
- .hasPath("search")
- .andNoOtherParam();
- }
-
- @Test
- public void setRoot() {
- underTest.setRoot(SOME_LOGIN);
-
- serviceTester.assertThat(serviceTester.getPostRequest())
- .hasPath("set_root")
- .hasParam("login", SOME_LOGIN)
- .andNoOtherParam();
- }
-
- @Test
- public void unsetRoot() {
- underTest.unsetRoot(SOME_LOGIN);
-
- serviceTester.assertThat(serviceTester.getPostRequest())
- .hasPath("unset_root")
- .hasParam("login", SOME_LOGIN)
- .andNoOtherParam();
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonarqube.ws.client.root;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonarqube.ws.client.ServiceTester;
+import org.sonarqube.ws.client.WsConnector;
+
+import static org.mockito.Mockito.mock;
+
+public class RootsServiceTest {
+ private static final String SOME_LOGIN = "johnDoe";
+
+ @Rule
+ public ServiceTester<RootsService> serviceTester = new ServiceTester<>(new RootsService(mock(WsConnector.class)));
+
+ private RootsService underTest = serviceTester.getInstanceUnderTest();
+
+ @Test
+ public void search() {
+ underTest.search();
+
+ serviceTester.assertThat(serviceTester.getGetRequest())
+ .hasPath("search")
+ .andNoOtherParam();
+ }
+
+ @Test
+ public void setRoot() {
+ underTest.setRoot(SOME_LOGIN);
+
+ serviceTester.assertThat(serviceTester.getPostRequest())
+ .hasPath("set_root")
+ .hasParam("login", SOME_LOGIN)
+ .andNoOtherParam();
+ }
+
+ @Test
+ public void unsetRoot() {
+ underTest.unsetRoot(SOME_LOGIN);
+
+ serviceTester.assertThat(serviceTester.getPostRequest())
+ .hasPath("unset_root")
+ .hasParam("login", SOME_LOGIN)
+ .andNoOtherParam();
+ }
+}