mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
HgPlugin Config: Adds v2 endpoint
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class HgConfigDto extends HalRepresentation {
|
||||
|
||||
private boolean disabled;
|
||||
private File repositoryDirectory;
|
||||
|
||||
private String encoding;
|
||||
private String hgBinary;
|
||||
private String pythonBinary;
|
||||
private String pythonPath;
|
||||
private boolean useOptimizedBytecode;
|
||||
private boolean showRevisionInId;
|
||||
|
||||
@Override
|
||||
protected HalRepresentation add(Links links) {
|
||||
return super.add(links);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
|
||||
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||
@SuppressWarnings("squid:S3306")
|
||||
@Mapper
|
||||
public abstract class HgConfigDtoToHgConfigMapper {
|
||||
public abstract HgConfig map(HgConfigDto dto);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.config.ConfigurationPermissions;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.HgVndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
/**
|
||||
* RESTful Web Service Resource to manage the configuration of the hg plugin.
|
||||
*/
|
||||
@Path(HgConfigResource.GIT_CONFIG_PATH_V2)
|
||||
public class HgConfigResource {
|
||||
|
||||
static final String GIT_CONFIG_PATH_V2 = "v2/config/hg";
|
||||
private final HgConfigDtoToHgConfigMapper dtoToConfigMapper;
|
||||
private final HgConfigToHgConfigDtoMapper configToDtoMapper;
|
||||
private final HgRepositoryHandler repositoryHandler;
|
||||
|
||||
@Inject
|
||||
public HgConfigResource(HgConfigDtoToHgConfigMapper dtoToConfigMapper, HgConfigToHgConfigDtoMapper configToDtoMapper,
|
||||
HgRepositoryHandler repositoryHandler) {
|
||||
this.dtoToConfigMapper = dtoToConfigMapper;
|
||||
this.configToDtoMapper = configToDtoMapper;
|
||||
this.repositoryHandler = repositoryHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hg config.
|
||||
*/
|
||||
@GET
|
||||
@Path("")
|
||||
@Produces(HgVndMediaType.HG_CONFIG)
|
||||
@TypeHint(HgConfigDto.class)
|
||||
@StatusCodes({
|
||||
@ResponseCode(code = 200, condition = "success"),
|
||||
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
|
||||
@ResponseCode(code = 403, condition = "not authorized, the current user has no privileges to read the git config"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response get() {
|
||||
|
||||
HgConfig config = repositoryHandler.getConfig();
|
||||
|
||||
if (config == null) {
|
||||
config = new HgConfig();
|
||||
repositoryHandler.setConfig(config);
|
||||
}
|
||||
|
||||
ConfigurationPermissions.read(config).check();
|
||||
|
||||
return Response.ok(configToDtoMapper.map(config)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the hg config.
|
||||
*
|
||||
* @param configDto new git configuration as DTO
|
||||
*/
|
||||
@PUT
|
||||
@Path("")
|
||||
@Consumes(HgVndMediaType.HG_CONFIG)
|
||||
@StatusCodes({
|
||||
@ResponseCode(code = 204, condition = "update success"),
|
||||
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
|
||||
@ResponseCode(code = 403, condition = "not authorized, the current user has no privileges to update the git config"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||
public Response update(@Context UriInfo uriInfo, HgConfigDto configDto) {
|
||||
|
||||
HgConfig config = dtoToConfigMapper.map(configDto);
|
||||
|
||||
ConfigurationPermissions.write(config).check();
|
||||
|
||||
repositoryHandler.setConfig(config);
|
||||
repositoryHandler.storeConfig();
|
||||
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
// TODO
|
||||
//* `auto-configuration`
|
||||
// * `packages`
|
||||
// * `packages/{pkgId}`
|
||||
// * `installations/hg`
|
||||
// * `installations/python
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Links;
|
||||
import org.mapstruct.AfterMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import sonia.scm.config.ConfigurationPermissions;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
import static de.otto.edison.hal.Links.linkingTo;
|
||||
|
||||
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||
@SuppressWarnings("squid:S3306")
|
||||
@Mapper
|
||||
public abstract class HgConfigToHgConfigDtoMapper {
|
||||
|
||||
@Inject
|
||||
private UriInfoStore uriInfoStore;
|
||||
|
||||
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
|
||||
public abstract HgConfigDto map(HgConfig config);
|
||||
|
||||
@AfterMapping
|
||||
void appendLinks(HgConfig config, @MappingTarget HgConfigDto target) {
|
||||
Links.Builder linksBuilder = linkingTo().self(self());
|
||||
if (ConfigurationPermissions.write(config).isPermitted()) {
|
||||
linksBuilder.single(link("update", update()));
|
||||
}
|
||||
target.add(linksBuilder.build());
|
||||
}
|
||||
|
||||
private String self() {
|
||||
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigResource.class);
|
||||
return linkBuilder.method("get").parameters().href();
|
||||
}
|
||||
|
||||
private String update() {
|
||||
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigResource.class);
|
||||
return linkBuilder.method("update").parameters().href();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
public class HgVndMediaType {
|
||||
public static final String HG_CONFIG = VndMediaType.PREFIX + "hgConfig" + VndMediaType.SUFFIX;
|
||||
}
|
||||
@@ -36,7 +36,9 @@ package sonia.scm.web;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.servlet.ServletModule;
|
||||
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.api.v2.resources.HgConfigDtoToHgConfigMapper;
|
||||
import sonia.scm.api.v2.resources.HgConfigToHgConfigDtoMapper;
|
||||
import sonia.scm.installer.HgPackageReader;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.repository.HgContext;
|
||||
@@ -70,6 +72,9 @@ public class HgServletModule extends ServletModule
|
||||
bind(HgHookManager.class);
|
||||
bind(HgPackageReader.class);
|
||||
|
||||
bind(HgConfigDtoToHgConfigMapper.class).to(Mappers.getMapper(HgConfigDtoToHgConfigMapper.class).getClass());
|
||||
bind(HgConfigToHgConfigDtoMapper.class).to(Mappers.getMapper(HgConfigToHgConfigDtoMapper.class).getClass());
|
||||
|
||||
// bind servlets
|
||||
serve(MAPPING_HOOK).with(HgHookCallbackServlet.class);
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
[users]
|
||||
readOnly = secret, reader
|
||||
writeOnly = secret, writer
|
||||
readWrite = secret, readerWriter
|
||||
|
||||
[roles]
|
||||
reader = configuration:read:hg
|
||||
writer = configuration:write:hg
|
||||
readerWriter = configuration:*:hg
|
||||
@@ -0,0 +1,49 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HgConfigDtoToHgConfigMapperTest {
|
||||
|
||||
@InjectMocks
|
||||
private HgConfigDtoToHgConfigMapperImpl mapper;
|
||||
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
HgConfigDto dto = createDefaultDto();
|
||||
HgConfig config = mapper.map(dto);
|
||||
|
||||
assertTrue(config.isDisabled());
|
||||
assertEquals("repository/directory", config.getRepositoryDirectory().getPath());
|
||||
|
||||
assertEquals("ABC", config.getEncoding());
|
||||
assertEquals("/etc/hg", config.getHgBinary());
|
||||
assertEquals("/py", config.getPythonBinary());
|
||||
assertEquals("/etc/", config.getPythonPath());
|
||||
assertTrue(config.isShowRevisionInId());
|
||||
assertTrue(config.isUseOptimizedBytecode());
|
||||
}
|
||||
|
||||
private HgConfigDto createDefaultDto() {
|
||||
HgConfigDto configDto = new HgConfigDto();
|
||||
configDto.setDisabled(true);
|
||||
configDto.setRepositoryDirectory(new File("repository/directory"));
|
||||
configDto.setEncoding("ABC");
|
||||
configDto.setHgBinary("/etc/hg");
|
||||
configDto.setPythonBinary("/py");
|
||||
configDto.setPythonPath("/etc/");
|
||||
configDto.setShowRevisionInId(true);
|
||||
configDto.setUseOptimizedBytecode(true);
|
||||
|
||||
return configDto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
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.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.HgVndMediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SubjectAware(
|
||||
configuration = "classpath:sonia/scm/configuration/shiro.ini",
|
||||
password = "secret"
|
||||
)
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HgConfigResourceTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
private final URI baseUri = URI.create("/");
|
||||
|
||||
@InjectMocks
|
||||
private HgConfigDtoToHgConfigMapperImpl dtoToConfigMapper;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private UriInfoStore uriInfoStore;
|
||||
|
||||
@InjectMocks
|
||||
private HgConfigToHgConfigDtoMapperImpl configToDtoMapper;
|
||||
|
||||
@Mock
|
||||
private HgRepositoryHandler repositoryHandler;
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
HgConfig gitConfig = createConfiguration();
|
||||
when(repositoryHandler.getConfig()).thenReturn(gitConfig);
|
||||
HgConfigResource gitConfigResource = new HgConfigResource(dtoToConfigMapper, configToDtoMapper, repositoryHandler);
|
||||
dispatcher.getRegistry().addSingletonResource(gitConfigResource);
|
||||
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readWrite")
|
||||
public void shouldGetHgConfig() throws URISyntaxException, IOException {
|
||||
MockHttpResponse response = get();
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
|
||||
String responseString = response.getContentAsString();
|
||||
ObjectNode responseJson = new ObjectMapper().readValue(responseString, ObjectNode.class);
|
||||
|
||||
assertTrue(responseString.contains("\"disabled\":false"));
|
||||
assertTrue(responseJson.get("repositoryDirectory").asText().endsWith("repository/directory"));
|
||||
assertTrue(responseString.contains("\"self\":{\"href\":\"/v2/config/hg"));
|
||||
assertTrue(responseString.contains("\"update\":{\"href\":\"/v2/config/hg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readWrite")
|
||||
public void shouldGetHgConfigEvenWhenItsEmpty() throws URISyntaxException, IOException {
|
||||
when(repositoryHandler.getConfig()).thenReturn(null);
|
||||
|
||||
MockHttpResponse response = get();
|
||||
String responseString = response.getContentAsString();
|
||||
|
||||
assertTrue(responseString.contains("\"disabled\":false"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readOnly")
|
||||
public void shouldGetHgConfigWithoutUpdateLink() throws URISyntaxException {
|
||||
MockHttpResponse response = get();
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
|
||||
assertFalse(response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config/hg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "writeOnly")
|
||||
public void shouldGetConfigOnlyWhenAuthorized() throws URISyntaxException {
|
||||
thrown.expectMessage("Subject does not have permission [configuration:read:hg]");
|
||||
|
||||
get();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "writeOnly")
|
||||
public void shouldUpdateConfig() throws URISyntaxException {
|
||||
MockHttpResponse response = put();
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readOnly")
|
||||
public void shouldUpdateConfigOnlyWhenAuthorized() throws URISyntaxException, IOException {
|
||||
thrown.expectMessage("Subject does not have permission [configuration:write:hg]");
|
||||
|
||||
put();
|
||||
}
|
||||
|
||||
private MockHttpResponse get() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + HgConfigResource.GIT_CONFIG_PATH_V2);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private MockHttpResponse put() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.put("/" + HgConfigResource.GIT_CONFIG_PATH_V2)
|
||||
.contentType(HgVndMediaType.HG_CONFIG)
|
||||
.content("{\"disabled\":true}".getBytes());
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private HgConfig createConfiguration() {
|
||||
HgConfig config = new HgConfig();
|
||||
config.setDisabled(false);
|
||||
config.setRepositoryDirectory(new File("repository/directory"));
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HgConfigToHgConfigDtoMapperTest {
|
||||
|
||||
private URI baseUri = URI.create("http://example.com/base/");
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private UriInfoStore uriInfoStore;
|
||||
|
||||
@InjectMocks
|
||||
private HgConfigToHgConfigDtoMapperImpl mapper;
|
||||
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
private URI expectedBaseUri;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
|
||||
expectedBaseUri = baseUri.resolve(HgConfigResource.GIT_CONFIG_PATH_V2);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbindSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
HgConfig config = createConfiguration();
|
||||
|
||||
when(subject.isPermitted("configuration:write:hg")).thenReturn(true);
|
||||
HgConfigDto dto = mapper.map(config);
|
||||
|
||||
assertTrue(dto.isDisabled());
|
||||
assertEquals("repository/directory", dto.getRepositoryDirectory().getPath());
|
||||
|
||||
assertEquals("ABC", dto.getEncoding());
|
||||
assertEquals("/etc/hg", dto.getHgBinary());
|
||||
assertEquals("/py", dto.getPythonBinary());
|
||||
assertEquals("/etc/", dto.getPythonPath());
|
||||
assertTrue(dto.isShowRevisionInId());
|
||||
assertTrue(dto.isUseOptimizedBytecode());
|
||||
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("update").get().getHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFieldsWithoutUpdate() {
|
||||
HgConfig config = createConfiguration();
|
||||
|
||||
when(subject.isPermitted("configuration:write:hg")).thenReturn(false);
|
||||
HgConfigDto dto = mapper.map(config);
|
||||
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertFalse(dto.getLinks().hasLink("update"));
|
||||
}
|
||||
|
||||
private HgConfig createConfiguration() {
|
||||
HgConfig config = new HgConfig();
|
||||
config.setDisabled(true);
|
||||
config.setRepositoryDirectory(new File("repository/directory"));
|
||||
|
||||
config.setEncoding("ABC");
|
||||
config.setHgBinary("/etc/hg");
|
||||
config.setPythonBinary("/py");
|
||||
config.setPythonPath("/etc/");
|
||||
config.setShowRevisionInId(true);
|
||||
config.setUseOptimizedBytecode(true);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user