SvnPlugin Config: Adds v2 endpoint

This commit is contained in:
Johannes Schnatterer
2018-08-02 13:04:06 +02:00
parent 8b16a0251a
commit 54354590ac
11 changed files with 497 additions and 22 deletions

View File

@@ -0,0 +1,42 @@
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.Compatibility;
import sonia.scm.repository.SvnConfig;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
public class SvnConfigDtoToSvnConfigMapperTest {
@InjectMocks
private SvnConfigDtoToSvnConfigMapperImpl mapper;
@Test
public void shouldMapFields() {
SvnConfigDto dto = createDefaultDto();
SvnConfig config = mapper.map(dto);
assertTrue(config.isDisabled());
assertEquals("repository/directory", config.getRepositoryDirectory().getPath());
assertEquals(Compatibility.PRE15, config.getCompatibility());
assertTrue(config.isEnabledGZip());
}
private SvnConfigDto createDefaultDto() {
SvnConfigDto configDto = new SvnConfigDto();
configDto.setDisabled(true);
configDto.setRepositoryDirectory(new File("repository/directory"));
configDto.setCompatibility(Compatibility.PRE15);
configDto.setEnabledGZip(true);
return configDto;
}
}

View File

@@ -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.SvnConfig;
import sonia.scm.repository.SvnRepositoryHandler;
import sonia.scm.web.SvnVndMediaType;
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 SvnConfigResourceTest {
@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 SvnConfigDtoToSvnConfigMapperImpl dtoToConfigMapper;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UriInfoStore uriInfoStore;
@InjectMocks
private SvnConfigToSvnConfigDtoMapperImpl configToDtoMapper;
@Mock
private SvnRepositoryHandler repositoryHandler;
@Before
public void prepareEnvironment() {
SvnConfig gitConfig = createConfiguration();
when(repositoryHandler.getConfig()).thenReturn(gitConfig);
SvnConfigResource gitConfigResource = new SvnConfigResource(dtoToConfigMapper, configToDtoMapper, repositoryHandler);
dispatcher.getRegistry().addSingletonResource(gitConfigResource);
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
}
@Test
@SubjectAware(username = "readWrite")
public void shouldGetSvnConfig() 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/svn"));
assertTrue(responseString.contains("\"update\":{\"href\":\"/v2/config/svn"));
}
@Test
@SubjectAware(username = "readWrite")
public void shouldGetSvnConfigEvenWhenItsEmpty() 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 shouldGetSvnConfigWithoutUpdateLink() throws URISyntaxException {
MockHttpResponse response = get();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertFalse(response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config/svn"));
}
@Test
@SubjectAware(username = "writeOnly")
public void shouldGetConfigOnlyWhenAuthorized() throws URISyntaxException {
thrown.expectMessage("Subject does not have permission [configuration:read:svn]");
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 {
thrown.expectMessage("Subject does not have permission [configuration:write:svn]");
put();
}
private MockHttpResponse get() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/" + SvnConfigResource.SVN_CONFIG_PATH_V2);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
private MockHttpResponse put() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.put("/" + SvnConfigResource.SVN_CONFIG_PATH_V2)
.contentType(SvnVndMediaType.SVN_CONFIG)
.content("{\"disabled\":true}".getBytes());
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
private SvnConfig createConfiguration() {
SvnConfig config = new SvnConfig();
config.setDisabled(false);
config.setRepositoryDirectory(new File("repository/directory"));
return config;
}
}

View File

@@ -0,0 +1,95 @@
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.Compatibility;
import sonia.scm.repository.SvnConfig;
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 SvnConfigToSvnConfigDtoMapperTest {
private URI baseUri = URI.create("http://example.com/base/");
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UriInfoStore uriInfoStore;
@InjectMocks
private SvnConfigToSvnConfigDtoMapperImpl 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(SvnConfigResource.SVN_CONFIG_PATH_V2);
subjectThreadState.bind();
ThreadContext.bind(subject);
}
@After
public void unbindSubject() {
ThreadContext.unbindSubject();
}
@Test
public void shouldMapFields() {
SvnConfig config = createConfiguration();
when(subject.isPermitted("configuration:write:svn")).thenReturn(true);
SvnConfigDto dto = mapper.map(config);
assertTrue(dto.isDisabled());
assertEquals("repository/directory", dto.getRepositoryDirectory().getPath());
assertEquals(Compatibility.PRE15, dto.getCompatibility());
assertTrue(dto.isEnabledGZip());
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("update").get().getHref());
}
@Test
public void shouldMapFieldsWithoutUpdate() {
SvnConfig config = createConfiguration();
when(subject.isPermitted("configuration:write:svn")).thenReturn(false);
SvnConfigDto dto = mapper.map(config);
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
assertFalse(dto.getLinks().hasLink("update"));
}
private SvnConfig createConfiguration() {
SvnConfig config = new SvnConfig();
config.setDisabled(true);
config.setRepositoryDirectory(new File("repository/directory"));
config.setCompatibility(Compatibility.PRE15);
config.setEnabledGZip(true);
return config;
}
}

View File

@@ -0,0 +1,9 @@
[users]
readOnly = secret, reader
writeOnly = secret, writer
readWrite = secret, readerWriter
[roles]
reader = configuration:read:svn
writer = configuration:write:svn
readerWriter = configuration:*:svn