Add rest method

This commit is contained in:
Rene Pfeuffer
2019-09-27 11:49:14 +02:00
parent 3b34cb5278
commit fd4070b1b1
2 changed files with 81 additions and 136 deletions

View File

@@ -8,7 +8,6 @@ import de.otto.edison.hal.Links;
import sonia.scm.plugin.AvailablePlugin;
import sonia.scm.plugin.InstalledPlugin;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.PluginPermissions;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
@@ -46,8 +45,6 @@ public class PendingPluginResource {
})
@Produces(VndMediaType.PLUGIN_COLLECTION)
public Response getPending() {
PluginPermissions.manage().check();
List<AvailablePlugin> pending = pluginManager
.getAvailable()
.stream()
@@ -106,8 +103,18 @@ public class PendingPluginResource {
@ResponseCode(code = 500, condition = "internal server error")
})
public Response executePending() {
PluginPermissions.manage().check();
pluginManager.executePendingAndRestart();
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();
}
}

View File

@@ -1,16 +1,11 @@
package sonia.scm.api.v2.resources;
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.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -24,8 +19,6 @@ import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginManager;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
@@ -34,11 +27,8 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
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.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -54,9 +44,6 @@ class PendingPluginResourceTest {
@Mock
PluginDtoMapper mapper;
@Mock
Subject subject;
@InjectMocks
PendingPluginResource pendingPluginResource;
@@ -65,7 +52,6 @@ class PendingPluginResourceTest {
@BeforeEach
void prepareEnvironment() {
dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getProviderFactory().register(new PermissionExceptionMapper());
PluginRootResource pluginRootResource = new PluginRootResource(null, null, Providers.of(pendingPluginResource));
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
void shouldGetEmptyPluginListsWithoutInstallLinkWhenNoPendingPluginsPresent() throws URISyntaxException, UnsupportedEncodingException {
AvailablePlugin availablePlugin = createAvailablePlugin("not-pending-plugin");
@@ -166,49 +138,15 @@ class PendingPluginResourceTest {
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
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
void shouldNotListPendingPlugins() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/pending");
void shouldCancelPendingPlugins() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/pending/cancel");
dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
verify(pluginManager, never()).executePendingAndRestart();
}
@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();
}
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
verify(pluginManager).cancelPending();
}
private AvailablePlugin createAvailablePlugin(String name) {