mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Add rest method
This commit is contained in:
@@ -8,7 +8,6 @@ import de.otto.edison.hal.Links;
|
|||||||
import sonia.scm.plugin.AvailablePlugin;
|
import sonia.scm.plugin.AvailablePlugin;
|
||||||
import sonia.scm.plugin.InstalledPlugin;
|
import sonia.scm.plugin.InstalledPlugin;
|
||||||
import sonia.scm.plugin.PluginManager;
|
import sonia.scm.plugin.PluginManager;
|
||||||
import sonia.scm.plugin.PluginPermissions;
|
|
||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -46,8 +45,6 @@ public class PendingPluginResource {
|
|||||||
})
|
})
|
||||||
@Produces(VndMediaType.PLUGIN_COLLECTION)
|
@Produces(VndMediaType.PLUGIN_COLLECTION)
|
||||||
public Response getPending() {
|
public Response getPending() {
|
||||||
PluginPermissions.manage().check();
|
|
||||||
|
|
||||||
List<AvailablePlugin> pending = pluginManager
|
List<AvailablePlugin> pending = pluginManager
|
||||||
.getAvailable()
|
.getAvailable()
|
||||||
.stream()
|
.stream()
|
||||||
@@ -106,8 +103,18 @@ public class PendingPluginResource {
|
|||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response executePending() {
|
public Response executePending() {
|
||||||
PluginPermissions.manage().check();
|
|
||||||
pluginManager.executePendingAndRestart();
|
pluginManager.executePendingAndRestart();
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("/cancel")
|
||||||
|
@StatusCodes({
|
||||||
|
@ResponseCode(code = 200, condition = "success"),
|
||||||
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
|
})
|
||||||
|
public Response cancelPending() {
|
||||||
|
pluginManager.cancelPending();
|
||||||
|
return Response.ok().build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,11 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import com.google.inject.util.Providers;
|
import com.google.inject.util.Providers;
|
||||||
import org.apache.shiro.ShiroException;
|
|
||||||
import org.apache.shiro.subject.Subject;
|
|
||||||
import org.apache.shiro.util.ThreadContext;
|
|
||||||
import org.jboss.resteasy.core.Dispatcher;
|
import org.jboss.resteasy.core.Dispatcher;
|
||||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||||
import org.jboss.resteasy.mock.MockHttpRequest;
|
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||||
import org.jboss.resteasy.mock.MockHttpResponse;
|
import org.jboss.resteasy.mock.MockHttpResponse;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Nested;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -24,8 +19,6 @@ import sonia.scm.plugin.PluginInformation;
|
|||||||
import sonia.scm.plugin.PluginManager;
|
import sonia.scm.plugin.PluginManager;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import javax.ws.rs.ext.ExceptionMapper;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
@@ -34,11 +27,8 @@ import static java.util.Collections.emptyList;
|
|||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.doNothing;
|
|
||||||
import static org.mockito.Mockito.doThrow;
|
|
||||||
import static org.mockito.Mockito.lenient;
|
import static org.mockito.Mockito.lenient;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -54,9 +44,6 @@ class PendingPluginResourceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
PluginDtoMapper mapper;
|
PluginDtoMapper mapper;
|
||||||
|
|
||||||
@Mock
|
|
||||||
Subject subject;
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
PendingPluginResource pendingPluginResource;
|
PendingPluginResource pendingPluginResource;
|
||||||
|
|
||||||
@@ -65,7 +52,6 @@ class PendingPluginResourceTest {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
void prepareEnvironment() {
|
void prepareEnvironment() {
|
||||||
dispatcher = MockDispatcherFactory.createDispatcher();
|
dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
dispatcher.getProviderFactory().register(new PermissionExceptionMapper());
|
|
||||||
PluginRootResource pluginRootResource = new PluginRootResource(null, null, Providers.of(pendingPluginResource));
|
PluginRootResource pluginRootResource = new PluginRootResource(null, null, Providers.of(pendingPluginResource));
|
||||||
dispatcher.getRegistry().addSingletonResource(pluginRootResource);
|
dispatcher.getRegistry().addSingletonResource(pluginRootResource);
|
||||||
}
|
}
|
||||||
@@ -84,20 +70,6 @@ class PendingPluginResourceTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
|
||||||
class withAuthorization {
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void bindSubject() {
|
|
||||||
ThreadContext.bind(subject);
|
|
||||||
doNothing().when(subject).checkPermission("plugin:manage");
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void unbindSubject() {
|
|
||||||
ThreadContext.unbindSubject();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldGetEmptyPluginListsWithoutInstallLinkWhenNoPendingPluginsPresent() throws URISyntaxException, UnsupportedEncodingException {
|
void shouldGetEmptyPluginListsWithoutInstallLinkWhenNoPendingPluginsPresent() throws URISyntaxException, UnsupportedEncodingException {
|
||||||
AvailablePlugin availablePlugin = createAvailablePlugin("not-pending-plugin");
|
AvailablePlugin availablePlugin = createAvailablePlugin("not-pending-plugin");
|
||||||
@@ -166,49 +138,15 @@ class PendingPluginResourceTest {
|
|||||||
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
||||||
verify(pluginManager).executePendingAndRestart();
|
verify(pluginManager).executePendingAndRestart();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Nested
|
|
||||||
class WithoutAuthorization {
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void bindSubject() {
|
|
||||||
ThreadContext.bind(subject);
|
|
||||||
doThrow(new ShiroException()).when(subject).checkPermission("plugin:manage");
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void unbindSubject() {
|
|
||||||
ThreadContext.unbindSubject();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldNotListPendingPlugins() throws URISyntaxException {
|
void shouldCancelPendingPlugins() throws URISyntaxException {
|
||||||
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/pending");
|
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/pending/cancel");
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
||||||
verify(pluginManager, never()).executePendingAndRestart();
|
verify(pluginManager).cancelPending();
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldNotExecutePendingPlugins() throws URISyntaxException {
|
|
||||||
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/pending/execute");
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
|
||||||
|
|
||||||
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
||||||
verify(pluginManager, never()).executePendingAndRestart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class PermissionExceptionMapper implements ExceptionMapper<ShiroException> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Response toResponse(ShiroException exception) {
|
|
||||||
return Response.status(401).entity(exception.getMessage()).build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private AvailablePlugin createAvailablePlugin(String name) {
|
private AvailablePlugin createAvailablePlugin(String name) {
|
||||||
|
|||||||
Reference in New Issue
Block a user