mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 10:41:06 +01:00
Implement global config endpoint v2
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
public class GlobalConfigDtoToScmConfigurationMapperTest {
|
||||
|
||||
@InjectMocks
|
||||
private GlobalConfigDtoToScmConfigurationMapperImpl mapper;
|
||||
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
GlobalConfigDto dto = createDefaultDto();
|
||||
ScmConfiguration config = mapper.map(dto);
|
||||
assertEquals("baseurl" , config.getBaseUrl());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
initMocks(this);
|
||||
}
|
||||
|
||||
private GlobalConfigDto createDefaultDto() {
|
||||
GlobalConfigDto globalConfigDto = new GlobalConfigDto();
|
||||
globalConfigDto.setBaseUrl("baseurl");
|
||||
return globalConfigDto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
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.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
@SubjectAware(
|
||||
username = "trillian",
|
||||
password = "secret",
|
||||
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||
)
|
||||
public class GlobalConfigResourceTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private ResourceLinks resourceLinks;
|
||||
|
||||
@InjectMocks
|
||||
private GlobalConfigDtoToScmConfigurationMapperImpl dtoToConfigMapper;
|
||||
@InjectMocks
|
||||
private ScmConfigurationToGlobalConfigDtoMapperImpl configToDtoMapper;
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws IOException {
|
||||
initMocks(this);
|
||||
|
||||
ResourceLinksMock.initMock(resourceLinks, URI.create("/"));
|
||||
|
||||
GlobalConfigResource globalConfigResource = new GlobalConfigResource(dtoToConfigMapper,
|
||||
configToDtoMapper, createConfiguration());
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(globalConfigResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetGlobalConfig() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"proxyPassword\":\"heartOfGold\""));
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/config/global"));
|
||||
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config/global"));
|
||||
}
|
||||
|
||||
@SubjectAware(
|
||||
username = "dent"
|
||||
)
|
||||
@Test
|
||||
public void shouldGetForbiddenGlobalConfig() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdateGlobalConfig() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/globalConfig-test-update.json");
|
||||
byte[] configJson = Resources.toByteArray(url);
|
||||
MockHttpRequest request = MockHttpRequest.put("/" + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2)
|
||||
.contentType(VndMediaType.GLOBAL_CONFIG)
|
||||
.content(configJson);
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
|
||||
|
||||
request = MockHttpRequest.get("/" + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2);
|
||||
response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"proxyPassword\":\"newPassword\""));
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/config/global"));
|
||||
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config/global"));
|
||||
|
||||
}
|
||||
|
||||
@SubjectAware(
|
||||
username = "dent"
|
||||
)
|
||||
@Test
|
||||
public void shouldUpdateForbiddenGlobalConfig() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/globalConfig-test-update.json");
|
||||
byte[] configJson = Resources.toByteArray(url);
|
||||
MockHttpRequest request = MockHttpRequest.put("/" + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2)
|
||||
.contentType(VndMediaType.GLOBAL_CONFIG)
|
||||
.content(configJson);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
|
||||
}
|
||||
|
||||
public static ScmConfiguration createConfiguration() {
|
||||
ScmConfiguration scmConfiguration = new ScmConfiguration();
|
||||
scmConfiguration.setProxyPassword("heartOfGold");
|
||||
scmConfiguration.setProxyPort(1234);
|
||||
scmConfiguration.setProxyServer("proxyserver");
|
||||
scmConfiguration.setProxyUser("trillian");
|
||||
scmConfiguration.setEnableProxy(true);
|
||||
scmConfiguration.setRealmDescription("description");
|
||||
scmConfiguration.setEnableRepositoryArchive(true);
|
||||
scmConfiguration.setDisableGroupingGrid(true);
|
||||
scmConfiguration.setDateFormat("dd");
|
||||
scmConfiguration.setAnonymousAccessEnabled(true);
|
||||
scmConfiguration.setAdminGroups(new HashSet<>(Arrays.asList("group")));
|
||||
scmConfiguration.setAdminUsers(new HashSet<>(Arrays.asList("user1")));
|
||||
scmConfiguration.setBaseUrl("baseurl");
|
||||
scmConfiguration.setForceBaseUrl(true);
|
||||
scmConfiguration.setLoginAttemptLimit(1);
|
||||
scmConfiguration.setProxyExcludes(new HashSet<>(Arrays.asList("arthur", "dent")));
|
||||
scmConfiguration.setSkipFailedAuthenticators(true);
|
||||
scmConfiguration.setPluginUrl("pluginurl");
|
||||
scmConfiguration.setLoginAttemptLimitTimeout(2);
|
||||
scmConfiguration.setEnabledXsrfProtection(true);
|
||||
return scmConfiguration;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.net.URI;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static sonia.scm.api.v2.resources.GlobalConfigResource.GLOBAL_CONFIG_PATH_V2;
|
||||
import static sonia.scm.api.v2.resources.GroupRootResource.GROUPS_PATH_V2;
|
||||
import static sonia.scm.api.v2.resources.UserRootResource.USERS_PATH_V2;
|
||||
|
||||
@@ -22,5 +23,8 @@ public class ResourceLinksMock {
|
||||
|
||||
when(resourceLinks.groupCollection().self()).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2);
|
||||
when(resourceLinks.groupCollection().create()).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2);
|
||||
|
||||
when(resourceLinks.globalConfig().self()).thenAnswer(invocation -> baseUri + GLOBAL_CONFIG_PATH_V2);
|
||||
when(resourceLinks.globalConfig().update()).thenAnswer(invocation -> baseUri + GLOBAL_CONFIG_PATH_V2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,18 @@ public class ResourceLinksTest {
|
||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectGlobalConfigSelfUrl() {
|
||||
String url = resourceLinks.globalConfig().self();
|
||||
assertEquals(BASE_URL + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2, url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectGlobalConfigUpdateUrl() {
|
||||
String url = resourceLinks.globalConfig().update();
|
||||
assertEquals(BASE_URL + GlobalConfigResource.GLOBAL_CONFIG_PATH_V2, url);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initUriInfo() {
|
||||
initMocks(this);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
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.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.security.Role;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.api.v2.resources.GlobalConfigResourceTest.createConfiguration;
|
||||
|
||||
public class ScmConfigurationToGlobalConfigDtoMapperTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private ResourceLinks resourceLinks;
|
||||
|
||||
@InjectMocks
|
||||
private ScmConfigurationToGlobalConfigDtoMapperImpl mapper;
|
||||
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
private URI expectedBaseUri;
|
||||
|
||||
@Before
|
||||
public void init() throws URISyntaxException {
|
||||
initMocks(this);
|
||||
URI baseUri = new URI("http://example.com/base/");
|
||||
expectedBaseUri = baseUri.resolve(GlobalConfigResource.GLOBAL_CONFIG_PATH_V2);
|
||||
subjectThreadState.bind();
|
||||
ResourceLinksMock.initMock(resourceLinks, baseUri);
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
ScmConfiguration config = createConfiguration();
|
||||
|
||||
when(subject.hasRole(Role.ADMIN)).thenReturn(true);
|
||||
GlobalConfigDto dto = mapper.map(config);
|
||||
|
||||
assertEquals("baseurl", dto.getBaseUrl());
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("update").get().getHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFieldsWithoutUpdate() {
|
||||
ScmConfiguration config = createConfiguration();
|
||||
|
||||
when(subject.hasRole(Role.ADMIN)).thenReturn(false);
|
||||
GlobalConfigDto dto = mapper.map(config);
|
||||
|
||||
assertEquals("baseurl", dto.getBaseUrl());
|
||||
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertFalse(dto.getLinks().hasLink("update"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user