]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9941 api/plugins/uninstall fails on edition bundled plugin
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 18 Oct 2017 08:15:28 +0000 (10:15 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 23 Oct 2017 15:01:13 +0000 (08:01 -0700)
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UninstallAction.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UninstallActionTest.java

index 7cf974e8d680e7d7263384188437addcd56ba527..027a8738a2e662761da2e523f64fdad4de7bf490 100644 (file)
@@ -22,19 +22,26 @@ package org.sonar.server.plugins.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.core.platform.PluginInfo;
 import org.sonar.server.plugins.PluginUninstaller;
+import org.sonar.server.plugins.ServerPluginRepository;
 import org.sonar.server.user.UserSession;
 
+import static java.lang.String.format;
+import static org.sonar.server.plugins.edition.EditionBundledPlugins.isEditionBundled;
+
 /**
  * Implementation of the {@code uninstall} action for the Plugins WebService.
  */
 public class UninstallAction implements PluginsWsAction {
   private static final String PARAM_KEY = "key";
 
+  private final ServerPluginRepository serverPluginRepository;
   private final PluginUninstaller pluginUninstaller;
   private final UserSession userSession;
 
-  public UninstallAction(PluginUninstaller pluginUninstaller, UserSession userSession) {
+  public UninstallAction(ServerPluginRepository serverPluginRepository, PluginUninstaller pluginUninstaller, UserSession userSession) {
+    this.serverPluginRepository = serverPluginRepository;
     this.pluginUninstaller = pluginUninstaller;
     this.userSession = userSession;
   }
@@ -59,7 +66,15 @@ public class UninstallAction implements PluginsWsAction {
     userSession.checkIsSystemAdministrator();
 
     String key = request.mandatoryParam(PARAM_KEY);
-    pluginUninstaller.uninstall(key);
+    PluginInfo pluginInfo = serverPluginRepository.getPluginInfo(key);
+    if (pluginInfo != null) {
+      if (isEditionBundled(pluginInfo)) {
+        throw new IllegalArgumentException(format(
+          "SonarSource commercial plugin with key '%s' can only be uninstalled as part of a SonarSource edition",
+          pluginInfo.getKey()));
+      }
+      pluginUninstaller.uninstall(key);
+    }
     response.noContent();
   }
 
index 75902e3a98267c309221822192150a31edd7ac10..ddd62123529e07164df2dce589471ff876a742ec 100644 (file)
  */
 package org.sonar.server.plugins.ws;
 
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.core.platform.PluginInfo;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.plugins.PluginUninstaller;
+import org.sonar.server.plugins.ServerPluginRepository;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
 
+@RunWith(DataProviderRunner.class)
 public class UninstallActionTest {
   private static final String DUMMY_CONTROLLER_KEY = "dummy";
   private static final String CONTROLLER_KEY = "api/plugins";
@@ -45,8 +54,9 @@ public class UninstallActionTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  private ServerPluginRepository serverPluginRepository = mock(ServerPluginRepository.class);
   private PluginUninstaller pluginUninstaller = mock(PluginUninstaller.class);
-  private UninstallAction underTest = new UninstallAction(pluginUninstaller, userSessionRule);
+  private UninstallAction underTest = new UninstallAction(serverPluginRepository, pluginUninstaller, userSessionRule);
 
   private WsTester wsTester = new WsTester(new PluginsWs(underTest));
   private Request invalidRequest = wsTester.newGetRequest(CONTROLLER_KEY, ACTION_KEY);
@@ -105,9 +115,44 @@ public class UninstallActionTest {
     underTest.handle(invalidRequest, response);
   }
 
+  @Test
+  public void do_not_attempt_uninstall_if_no_plugin_in_repository_for_specified_key() throws Exception {
+    logInAsSystemAdministrator();
+    when(serverPluginRepository.getPluginInfo(PLUGIN_KEY)).thenReturn(null);
+
+    underTest.handle(validRequest, response);
+
+    verifyZeroInteractions(pluginUninstaller);
+  }
+
+  @Test
+  @UseDataProvider("editionBundledOrganizationAndLicense")
+  public void IAE_is_raised_when_plugin_is_installed_and_is_edition_bundled(String organization, String license) throws Exception {
+    logInAsSystemAdministrator();
+    when(serverPluginRepository.getPluginInfo(PLUGIN_KEY))
+      .thenReturn(new PluginInfo(PLUGIN_KEY)
+        .setOrganizationName(organization)
+        .setLicense(license));
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("SonarSource commercial plugin with key '" + PLUGIN_KEY + "' can only be uninstalled as part of a SonarSource edition");
+
+    underTest.handle(validRequest, response);
+  }
+
+  @DataProvider
+  public static Object[][] editionBundledOrganizationAndLicense() {
+    return new Object[][] {
+      {"SonarSource", "SonarSource"},
+      {"SonarSource", "Commercial"},
+      {"sonarsource", "SOnArSOURCE"}
+    };
+  }
+
   @Test
   public void if_plugin_is_installed_uninstallation_is_triggered() throws Exception {
     logInAsSystemAdministrator();
+    when(serverPluginRepository.getPluginInfo(PLUGIN_KEY)).thenReturn(new PluginInfo(PLUGIN_KEY));
 
     underTest.handle(validRequest, response);