You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ma...@apache.org on 2020/07/14 05:52:28 UTC
[archiva-redback-core] 02/02: Improving REST tests
This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git
commit 3f641fe61e1e3964272601012b895620a987134a
Author: Martin Stockhammer <ma...@apache.org>
AuthorDate: Tue Jul 14 07:52:10 2020 +0200
Improving REST tests
---
idea.run.configuration/All Rest Services.run.xml | 22 ++++++++++++++++++++
.../All V2 Rest Services.run.xml | 22 ++++++++++++++++++++
.../All in archiva-rest-services JDK11.run.xml | 18 ++++++++++++++++
.../V2 AuthenticationServiceTest.run.xml | 22 ++++++++++++++++++++
...st.services.v2 in redback-rest-services.run.xml | 22 ++++++++++++++++++++
pom.xml | 1 +
.../interceptors/PermissionsInterceptor.java | 4 +++-
.../services/v2/AbstractNativeRestServices.java | 24 +++++++++++++---------
.../v2/NativeAuthenticationServiceTest.java | 17 ++++++++++++++-
9 files changed, 140 insertions(+), 12 deletions(-)
diff --git a/idea.run.configuration/All Rest Services.run.xml b/idea.run.configuration/All Rest Services.run.xml
new file mode 100644
index 0000000..07152e8
--- /dev/null
+++ b/idea.run.configuration/All Rest Services.run.xml
@@ -0,0 +1,22 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="All Rest Services" type="JUnit" factoryName="JUnit" folderName="Redback">
+ <module name="redback-rest-services" />
+ <useClassPathOnly />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="org.apache.archiva.redback.rest.services.v2.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <option name="PACKAGE_NAME" value="org.apache.archiva.redback.rest.services" />
+ <option name="MAIN_CLASS_NAME" value="" />
+ <option name="METHOD_NAME" value="" />
+ <option name="TEST_OBJECT" value="package" />
+ <option name="VM_PARAMETERS" value="-ea -Dbasedir=$MODULE_DIR$ -DldapPort=10389 -javaagent:$MAVEN_REPOSITORY$/org/apache/openjpa/openjpa/3.1.1/openjpa-3.1.1.jar" />
+ <option name="PARAMETERS" value="" />
+ <option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component>
\ No newline at end of file
diff --git a/idea.run.configuration/All V2 Rest Services.run.xml b/idea.run.configuration/All V2 Rest Services.run.xml
new file mode 100644
index 0000000..817305f
--- /dev/null
+++ b/idea.run.configuration/All V2 Rest Services.run.xml
@@ -0,0 +1,22 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="All V2 Rest Services" type="JUnit" factoryName="JUnit" folderName="Redback">
+ <module name="redback-rest-services" />
+ <useClassPathOnly />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="org.apache.archiva.redback.rest.services.v2.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <option name="PACKAGE_NAME" value="org.apache.archiva.redback.rest.services.v2" />
+ <option name="MAIN_CLASS_NAME" value="" />
+ <option name="METHOD_NAME" value="" />
+ <option name="TEST_OBJECT" value="package" />
+ <option name="VM_PARAMETERS" value="-ea -Dbasedir=$MODULE_DIR$ -DldapPort=10389 -javaagent:$MAVEN_REPOSITORY$/org/apache/openjpa/openjpa/3.1.1/openjpa-3.1.1.jar" />
+ <option name="PARAMETERS" value="" />
+ <option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component>
\ No newline at end of file
diff --git a/idea.run.configuration/All in archiva-rest-services JDK11.run.xml b/idea.run.configuration/All in archiva-rest-services JDK11.run.xml
new file mode 100644
index 0000000..50a49d7
--- /dev/null
+++ b/idea.run.configuration/All in archiva-rest-services JDK11.run.xml
@@ -0,0 +1,18 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="All in archiva-rest-services JDK11" type="JUnit" factoryName="JUnit" folderName="Archiva">
+ <module name="archiva-rest-services" />
+ <useClassPathOnly />
+ <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
+ <option name="ALTERNATIVE_JRE_PATH" value="11" />
+ <option name="PACKAGE_NAME" value="" />
+ <option name="MAIN_CLASS_NAME" value="" />
+ <option name="METHOD_NAME" value="" />
+ <option name="TEST_OBJECT" value="package" />
+ <option name="VM_PARAMETERS" value="-ea -Dbasedir=$MODULE_DIR$ -DldapPort=10389 -javaagent:$MAVEN_REPOSITORY$/org/apache/openjpa/openjpa/3.1.1/openjpa-3.1.1.jar" />
+ <option name="PARAMETERS" value="" />
+ <option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component>
\ No newline at end of file
diff --git a/idea.run.configuration/V2 AuthenticationServiceTest.run.xml b/idea.run.configuration/V2 AuthenticationServiceTest.run.xml
new file mode 100644
index 0000000..2e822b7
--- /dev/null
+++ b/idea.run.configuration/V2 AuthenticationServiceTest.run.xml
@@ -0,0 +1,22 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="V2 AuthenticationServiceTest" type="JUnit" factoryName="JUnit">
+ <module name="redback-rest-services" />
+ <useClassPathOnly />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="org.apache.archiva.redback.rest.services.v2.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <option name="PACKAGE_NAME" value="org.apache.archiva.redback.rest.services.v2" />
+ <option name="MAIN_CLASS_NAME" value="org.apache.archiva.redback.rest.services.v2.AuthenticationServiceTest" />
+ <option name="METHOD_NAME" value="" />
+ <option name="TEST_OBJECT" value="class" />
+ <option name="VM_PARAMETERS" value="-ea -Dbasedir=$MODULE_DIR$ -DldapPort=10389 -javaagent:$MAVEN_REPOSITORY$/org/apache/openjpa/openjpa/3.1.1/openjpa-3.1.1.jar" />
+ <option name="PARAMETERS" value="" />
+ <option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component>
\ No newline at end of file
diff --git a/idea.run.configuration/org.apache.archiva.redback.rest.services.v2 in redback-rest-services.run.xml b/idea.run.configuration/org.apache.archiva.redback.rest.services.v2 in redback-rest-services.run.xml
new file mode 100644
index 0000000..56e3851
--- /dev/null
+++ b/idea.run.configuration/org.apache.archiva.redback.rest.services.v2 in redback-rest-services.run.xml
@@ -0,0 +1,22 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="All V2 Rest Services" type="JUnit" factoryName="JUnit">
+ <module name="redback-rest-services" />
+ <useClassPathOnly />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="org.apache.archiva.redback.rest.services.v2.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <option name="PACKAGE_NAME" value="org.apache.archiva.redback.rest.services.v2" />
+ <option name="MAIN_CLASS_NAME" value="" />
+ <option name="METHOD_NAME" value="" />
+ <option name="TEST_OBJECT" value="package" />
+ <option name="VM_PARAMETERS" value="-ea -Dbasedir=$MODULE_DIR$ -DldapPort=10389 -javaagent:$MAVEN_REPOSITORY$/org/apache/openjpa/openjpa/3.1.1/openjpa-3.1.1.jar" />
+ <option name="PARAMETERS" value="" />
+ <option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component>
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 477b692..7798486 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1033,6 +1033,7 @@
<!-- IntelliJ IDEA files -->
<exclude>.idea/**</exclude>
<exclude>**/*.iml</exclude>
+ <exclude>idea.run.configuration/**</exclude>
<!-- Eclipse files -->
<exclude>.project</exclude>
<exclude>.classpath</exclude>
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/PermissionsInterceptor.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/PermissionsInterceptor.java
index 2ab5fb2..0504a2b 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/PermissionsInterceptor.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/PermissionsInterceptor.java
@@ -170,9 +170,11 @@ public class PermissionsInterceptor
return;
} else {
log.debug( "Path {} is protected and needs authentication. User not authenticated.", requestPath );
- containerRequestContext.abortWith( Response.status( Response.Status.FORBIDDEN ).build() );
+ containerRequestContext.abortWith( Response.status( Response.Status.UNAUTHORIZED ).build() );
+ return;
}
}
+ log.warn( "No permissions defined for the REST method and noPermission=false" );
containerRequestContext.abortWith( Response.status( Response.Status.FORBIDDEN ).build() );
return;
}
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/AbstractNativeRestServices.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/AbstractNativeRestServices.java
index 5677519..1f33566 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/AbstractNativeRestServices.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/AbstractNativeRestServices.java
@@ -23,7 +23,6 @@ import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
import org.apache.archiva.redback.rest.services.BaseSetup;
-import org.apache.archiva.redback.rest.services.FakeCreateAdminServiceImpl;
import org.apache.archiva.redback.role.RoleManager;
import org.apache.archiva.redback.role.RoleManagerException;
import org.apache.archiva.redback.users.User;
@@ -49,8 +48,8 @@ import java.util.concurrent.atomic.AtomicReference;
import static io.restassured.RestAssured.baseURI;
import static io.restassured.RestAssured.port;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.apache.archiva.redback.rest.services.BaseSetup.*;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Native REST tests do not use the JAX-RS client and can be used with a remote
@@ -75,13 +74,11 @@ public abstract class AbstractNativeRestServices
private static AtomicInteger serverStarted = new AtomicInteger( STOPPED );
private UserManager userManager;
private RoleManager roleManager;
- private String adminPwd;
-
public AbstractNativeRestServices( )
{
- this.adminPwd = BaseSetup.getAdminPwd( );
+
}
protected abstract String getServicePath( );
@@ -313,13 +310,20 @@ public abstract class AbstractNativeRestServices
RestAssured.baseURI = "http://localhost";
}
String basePath = getBasePath( );
- RequestSpecBuilder builder = new RequestSpecBuilder( );
- builder.setBaseUri( baseURI )
+ this.requestSpec = getRequestSpecBuilder().build( );
+ RestAssured.basePath = basePath;
+ }
+
+ protected RequestSpecBuilder getRequestSpecBuilder() {
+ return new RequestSpecBuilder().setBaseUri( baseURI )
.setPort( port )
- .setBasePath( basePath )
+ .setBasePath( getBasePath() )
.addHeader( "Origin", RestAssured.baseURI + ":" + RestAssured.port );
- this.requestSpec = builder.build( );
- RestAssured.basePath = basePath;
+
+ }
+
+ protected RequestSpecification getRequestSpec(String bearerToken) {
+ return getRequestSpecBuilder( ).addHeader( "Authorization", "Bearer " + bearerToken ).build();
}
protected void shutdownNative( ) throws Exception
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeAuthenticationServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeAuthenticationServiceTest.java
index a3373ee..3fd9730 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeAuthenticationServiceTest.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeAuthenticationServiceTest.java
@@ -91,6 +91,15 @@ public class NativeAuthenticationServiceTest extends AbstractNativeRestServices
}
@Test
+ void authenticatedPing() {
+ Response result = given( ).spec( getRequestSpec() )
+ .contentType( JSON )
+ .when( ).get( "/ping/authenticated" ).then( ).statusCode( 401 )
+ .extract( ).response( );
+
+ }
+
+ @Test
void tokenLogin() {
Map<String, Object> jsonAsMap = new HashMap<>();
jsonAsMap.put( "grant_type", "authorization_code" );
@@ -101,8 +110,14 @@ public class NativeAuthenticationServiceTest extends AbstractNativeRestServices
.body( jsonAsMap )
.when( ).post( "/authenticate").then( ).statusCode( 200 )
.extract( ).response( );
- assertNotNull( result.body( ).jsonPath( ).getString( "access_token" ) );
+ String accessToken = result.body( ).jsonPath( ).getString( "access_token" );
+ assertNotNull( accessToken );
assertNotNull( result.body( ).jsonPath( ).getString( "refresh_token" ) );
+
+ result = given( ).spec( getRequestSpec( accessToken ) )
+ .contentType( JSON )
+ .when( ).get( "/ping/authenticated" ).then( ).statusCode( 200 )
+ .extract( ).response( );
}
@Test