mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
fix and enable junit test createSessionAnonymousTest
This commit is contained in:
@@ -49,7 +49,11 @@ public class ScmUrlProvider
|
||||
public static final String API_PATH = "/api/rest/";
|
||||
|
||||
/** Field description */
|
||||
public static final String URLPART_AUTHENTICATION = "authentication/login";
|
||||
public static final String URLPART_AUTHENTICATION = "authentication";
|
||||
|
||||
/** Field description */
|
||||
public static final String URLPART_AUTHENTICATION_LOGIN =
|
||||
"authentication/login";
|
||||
|
||||
/** Field description */
|
||||
public static final String URLPART_REPOSITORIES = "repositories";
|
||||
@@ -88,6 +92,17 @@ public class ScmUrlProvider
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getAuthenticationLoginUrl()
|
||||
{
|
||||
return getResourceUrl(URLPART_AUTHENTICATION_LOGIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -32,15 +32,23 @@
|
||||
<dependency>
|
||||
<groupId>org.sonatype.spice</groupId>
|
||||
<artifactId>jersey-ahc-client</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.1-patch1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scope -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jul-to-slf4j</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>0.9.28</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -35,7 +35,10 @@ package sonia.scm.client;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import com.sun.jersey.api.client.Client;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import com.sun.jersey.api.client.filter.LoggingFilter;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -57,4 +60,41 @@ public class ClientUtil
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param client
|
||||
* @param url
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static WebResource createResource(Client client, String url)
|
||||
{
|
||||
return createResource(client, url, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param client
|
||||
* @param url
|
||||
* @param enableLogging
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static WebResource createResource(Client client, String url,
|
||||
boolean enableLogging)
|
||||
{
|
||||
WebResource resource = client.resource(url);
|
||||
|
||||
if (enableLogging)
|
||||
{
|
||||
resource.addFilter(new LoggingFilter());
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ package sonia.scm.client;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient;
|
||||
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
|
||||
|
||||
import sonia.scm.ScmState;
|
||||
@@ -46,7 +47,6 @@ import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import com.sun.jersey.api.client.Client;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
@@ -64,6 +64,25 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(JerseyClientProvider.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public JerseyClientProvider() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param enableLogging
|
||||
*/
|
||||
public JerseyClientProvider(boolean enableLogging)
|
||||
{
|
||||
this.enableLogging = enableLogging;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -99,12 +118,15 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
|
||||
ScmUrlProvider urlProvider = new ScmUrlProvider(url);
|
||||
DefaultAhcConfig config = new DefaultAhcConfig();
|
||||
Client client = Client.create(config);
|
||||
WebResource resource = client.resource(urlProvider.getAuthenticationUrl());
|
||||
AhcHttpClient client = AhcHttpClient.create(config);
|
||||
ClientResponse response = null;
|
||||
|
||||
if (Util.isNotEmpty(username) && Util.isNotEmpty(password))
|
||||
{
|
||||
WebResource resource = ClientUtil.createResource(client,
|
||||
urlProvider.getAuthenticationLoginUrl(),
|
||||
enableLogging);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("try login for {}", username);
|
||||
@@ -119,6 +141,10 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
}
|
||||
else
|
||||
{
|
||||
WebResource resource = ClientUtil.createResource(client,
|
||||
urlProvider.getAuthenticationUrl(),
|
||||
enableLogging);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("try anonymous login");
|
||||
@@ -164,4 +190,9 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
|
||||
return new JerseyClientSession(client, urlProvider, state);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private boolean enableLogging = false;
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ package sonia.scm.client.it;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import sonia.scm.client.ClientUtil;
|
||||
import sonia.scm.client.JerseyClientProvider;
|
||||
import sonia.scm.client.JerseyClientSession;
|
||||
import sonia.scm.client.ScmClientException;
|
||||
@@ -77,7 +77,6 @@ public class JerseyClientProviderITCase
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void createSessionAnonymousTest() throws ScmClientException
|
||||
{
|
||||
JerseyClientSession adminSession = createSession("scmadmin", "scmadmin");
|
||||
@@ -85,7 +84,8 @@ public class JerseyClientProviderITCase
|
||||
// enable anonymous access
|
||||
ScmUrlProvider up = adminSession.getUrlProvider();
|
||||
Client client = adminSession.getClient();
|
||||
WebResource resource = client.resource(up.getResourceUrl("config"));
|
||||
WebResource resource = ClientUtil.createResource(client,
|
||||
up.getResourceUrl("config"), true);
|
||||
ScmConfiguration config = resource.get(ScmConfiguration.class);
|
||||
|
||||
config.setAnonymousAccessEnabled(true);
|
||||
@@ -154,7 +154,7 @@ public class JerseyClientProviderITCase
|
||||
private JerseyClientSession createSession(String username, String password)
|
||||
throws ScmClientException
|
||||
{
|
||||
JerseyClientProvider provider = new JerseyClientProvider();
|
||||
JerseyClientProvider provider = new JerseyClientProvider(true);
|
||||
|
||||
return provider.createSession("http://localhost:8081/scm", username,
|
||||
password);
|
||||
|
||||
Reference in New Issue
Block a user