You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UninstallPluginsWsActionTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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.
  10. *
  11. * SonarQube 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.
  15. *
  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.
  19. */
  20. package org.sonar.server.plugins.ws;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.core.permission.GlobalPermissions;
  28. import org.sonar.server.exceptions.ForbiddenException;
  29. import org.sonar.server.plugins.ServerPluginRepository;
  30. import org.sonar.server.user.MockUserSession;
  31. import org.sonar.server.ws.WsTester;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.when;
  36. public class UninstallPluginsWsActionTest {
  37. private static final String DUMMY_CONTROLLER_KEY = "dummy";
  38. private static final String CONTROLLER_KEY = "api/plugins";
  39. private static final String ACTION_KEY = "uninstall";
  40. private static final String KEY_PARAM = "key";
  41. private static final String PLUGIN_KEY = "findbugs";
  42. private ServerPluginRepository pluginRepository = mock(ServerPluginRepository.class);
  43. private UninstallPluginsWsAction underTest = new UninstallPluginsWsAction(pluginRepository);
  44. private WsTester wsTester = new WsTester(new PluginsWs(underTest));
  45. private Request invalidRequest = wsTester.newGetRequest(CONTROLLER_KEY, ACTION_KEY);
  46. private Request validRequest = wsTester.newGetRequest(CONTROLLER_KEY, ACTION_KEY).setParam(KEY_PARAM, PLUGIN_KEY);
  47. private WsTester.TestResponse response = new WsTester.TestResponse();
  48. @Rule
  49. public ExpectedException expectedException = ExpectedException.none();
  50. @Before
  51. public void setUp() {
  52. MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
  53. }
  54. @Test
  55. public void user_must_have_system_admin_permission() throws Exception {
  56. expectedException.expect(ForbiddenException.class);
  57. expectedException.expectMessage("Insufficient privileges");
  58. // no permission on user
  59. MockUserSession.set().setGlobalPermissions();
  60. underTest.handle(validRequest, response);
  61. }
  62. @Test
  63. public void action_uninstall_is_defined() {
  64. WsTester wsTester = new WsTester();
  65. WebService.NewController newController = wsTester.context().createController(DUMMY_CONTROLLER_KEY);
  66. underTest.define(newController);
  67. newController.done();
  68. WebService.Controller controller = wsTester.controller(DUMMY_CONTROLLER_KEY);
  69. assertThat(controller.actions()).extracting("key").containsExactly(ACTION_KEY);
  70. WebService.Action action = controller.actions().iterator().next();
  71. assertThat(action.isPost()).isTrue();
  72. assertThat(action.description()).isNotEmpty();
  73. assertThat(action.responseExample()).isNull();
  74. assertThat(action.params()).hasSize(1);
  75. WebService.Param keyParam = action.param(KEY_PARAM);
  76. assertThat(keyParam).isNotNull();
  77. assertThat(keyParam.isRequired()).isTrue();
  78. assertThat(keyParam.description()).isNotNull();
  79. }
  80. @Test
  81. public void IAE_is_raised_when_key_param_is_not_provided() throws Exception {
  82. expectedException.expect(IllegalArgumentException.class);
  83. expectedException.expectMessage("Parameter 'key' is missing");
  84. underTest.handle(invalidRequest, response);
  85. }
  86. @Test
  87. public void IAE_is_raised_when_plugin_is_not_installed() throws Exception {
  88. expectedException.expect(IllegalArgumentException.class);
  89. expectedException.expectMessage("Plugin [findbugs] is not installed");
  90. underTest.handle(validRequest, response);
  91. }
  92. @Test
  93. public void if_plugin_is_installed_uninstallation_is_triggered() throws Exception {
  94. when(pluginRepository.hasPlugin(PLUGIN_KEY)).thenReturn(true);
  95. underTest.handle(validRequest, response);
  96. verify(pluginRepository).uninstall(PLUGIN_KEY);
  97. assertThat(response.outputAsString()).isEmpty();
  98. }
  99. }