small fixes and cleanup after review

This commit is contained in:
Eduard Heimbuch
2019-10-17 09:33:54 +02:00
parent 63f773a206
commit e91a3eac6a
6 changed files with 9 additions and 51 deletions

View File

@@ -20,6 +20,10 @@ public class ScmConfigurationChangedListener {
@Subscribe @Subscribe
public void handleEvent(ScmConfigurationChangedEvent event) { public void handleEvent(ScmConfigurationChangedEvent event) {
createAnonymousUserIfRequired(event);
}
private void createAnonymousUserIfRequired(ScmConfigurationChangedEvent event) {
if (event.getConfiguration().isAnonymousAccessEnabled() && !userManager.contains(SCMContext.USER_ANONYMOUS)) { if (event.getConfiguration().isAnonymousAccessEnabled() && !userManager.contains(SCMContext.USER_ANONYMOUS)) {
userManager.create(SCMContext.ANONYMOUS); userManager.create(SCMContext.ANONYMOUS);
} }

View File

@@ -6,10 +6,10 @@ import sonia.scm.SCMContext;
public class Authentications { public class Authentications {
public static boolean isAuthenticatedSubjectAnonymous() { public static boolean isAuthenticatedSubjectAnonymous() {
return SecurityUtils.getSubject().getPrincipal().equals(SCMContext.USER_ANONYMOUS); return isSubjectAnonymous((String) SecurityUtils.getSubject().getPrincipal());
} }
public static boolean isSubjectAnonymous(String principal) { public static boolean isSubjectAnonymous(String principal) {
return principal.equals(SCMContext.USER_ANONYMOUS); return SCMContext.USER_ANONYMOUS.equals(principal);
} }
} }

View File

@@ -49,14 +49,9 @@ import sonia.scm.TransformFilter;
import sonia.scm.search.SearchRequest; import sonia.scm.search.SearchRequest;
import sonia.scm.search.SearchUtil; import sonia.scm.search.SearchUtil;
import sonia.scm.util.CollectionAppender; import sonia.scm.util.CollectionAppender;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util; import sonia.scm.util.Util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;

View File

@@ -114,42 +114,6 @@ public class ConfigResourceTest {
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config")); assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config"));
} }
@Test
@SubjectAware(username = "readWrite")
public void shouldUpdateConfigAndCreateAnonymousUser() throws URISyntaxException, IOException {
MockHttpRequest request = post("sonia/scm/api/v2/config-test-update-with-anonymous-access.json");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
request = MockHttpRequest.get("/" + ConfigResource.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"));
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config"));
}
@Test
@SubjectAware(username = "readWrite")
public void shouldUpdateConfigAndNotCreateAnonymousUserIfAlreadyExists() throws URISyntaxException, IOException {
MockHttpRequest request = post("sonia/scm/api/v2/config-test-update-with-anonymous-access.json");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
request = MockHttpRequest.get("/" + ConfigResource.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"));
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config"));
}
@Test @Test
@SubjectAware(username = "readOnly") @SubjectAware(username = "readOnly")
public void shouldNotUpdateConfigWhenNotAuthorized() throws URISyntaxException, IOException { public void shouldNotUpdateConfigWhenNotAuthorized() throws URISyntaxException, IOException {
@@ -188,7 +152,6 @@ public class ConfigResourceTest {
private static ScmConfiguration createConfiguration() { private static ScmConfiguration createConfiguration() {
ScmConfiguration scmConfiguration = new ScmConfiguration(); ScmConfiguration scmConfiguration = new ScmConfiguration();
scmConfiguration.setProxyPassword("heartOfGold"); scmConfiguration.setProxyPassword("heartOfGold");
scmConfiguration.setAnonymousAccessEnabled(true);
return scmConfiguration; return scmConfiguration;
} }

View File

@@ -1,4 +0,0 @@
{
"proxyPassword": "newPassword",
"anonymousAccessEnabled": "true"
}