]> source.dussan.org Git - archiva.git/commitdiff
Adding REST v2 tests
authorMartin Stockhammer <martin_s@apache.org>
Mon, 4 Jan 2021 18:09:06 +0000 (19:09 +0100)
committerMartin Stockhammer <martin_s@apache.org>
Mon, 4 Jan 2021 18:09:06 +0000 (19:09 +0100)
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/SecurityConfigurationService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultSecurityConfigurationService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/ErrorKeys.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeSecurityConfigurationServiceTest.java

index e70f465af60c3bb4b6c1d6941430a316befd6c2a..6c9c83631187432171c31c44f29d3f09950bbcd0 100644 (file)
@@ -98,6 +98,8 @@ public interface SecurityConfigurationService
             @ApiResponse( responseCode = "200",
                 description = "If the configuration was updated"
             ),
+            @ApiResponse( responseCode = "422", description = "Invalid content data",
+                content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ArchivaRestServiceException.class )) ),
             @ApiResponse( responseCode = "403", description = "Authenticated user is not permitted to update the configuration",
                 content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ArchivaRestServiceException.class )) )
         }
@@ -182,6 +184,8 @@ public interface SecurityConfigurationService
             @ApiResponse( responseCode = "200",
                 description = "If the property value was updated."
             ),
+            @ApiResponse( responseCode = "400", description = "The body data is not valid",
+                content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ArchivaRestServiceException.class )) ),
             @ApiResponse( responseCode = "404", description = "The given property name does not exist",
                 content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ArchivaRestServiceException.class )) ),
             @ApiResponse( responseCode = "403", description = "Authenticated user is not permitted to gather the information",
index be76f3164ba9f1c4fcc97a2b0c179a0ea8c4eaf0..d1b4fe1876010ffaa9fc4cac42ed5464d16c97db 100644 (file)
@@ -179,6 +179,9 @@ public class DefaultSecurityConfigurationService implements SecurityConfiguratio
     @Override
     public Response updateConfiguration( SecurityConfiguration newConfiguration ) throws ArchivaRestServiceException
     {
+        if (newConfiguration==null) {
+            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.MISSING_DATA ), 400 );
+        }
         try
         {
             RedbackRuntimeConfiguration conf = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration( );
@@ -321,6 +324,9 @@ public class DefaultSecurityConfigurationService implements SecurityConfiguratio
     @Override
     public Response updateConfigurationProperty( String propertyName, PropertyEntry propertyValue ) throws ArchivaRestServiceException
     {
+        if (propertyValue==null) {
+            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.MISSING_DATA ), 400 );
+        }
         try
         {
             RedbackRuntimeConfiguration conf = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration( );
@@ -403,6 +409,9 @@ public class DefaultSecurityConfigurationService implements SecurityConfiguratio
     @Override
     public Response updateCacheConfiguration( CacheConfiguration cacheConfiguration ) throws ArchivaRestServiceException
     {
+        if (cacheConfiguration==null) {
+            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.MISSING_DATA ), 400 );
+        }
         try
         {
             RedbackRuntimeConfiguration redbackRuntimeConfiguration =
index 610743f5bcf3298d6fc970ddbcb2a940ecb43a85..02d0dc986fe00c426ad8cd7173e1badf6a355ff4 100644 (file)
@@ -22,6 +22,8 @@ import io.restassured.response.Response;
 import org.apache.archiva.components.rest.model.PagedResult;
 import org.apache.archiva.components.rest.model.PropertyEntry;
 import org.apache.archiva.rest.api.model.v2.BeanInformation;
+import org.apache.archiva.rest.api.model.v2.CacheConfiguration;
+import org.apache.archiva.rest.api.model.v2.LdapConfiguration;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.DisplayName;
@@ -34,7 +36,10 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 
+import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static io.restassured.RestAssured.given;
 import static io.restassured.http.ContentType.JSON;
@@ -125,6 +130,54 @@ public class NativeSecurityConfigurationServiceTest extends AbstractNativeRestSe
         assertEquals( 13, response.getBody( ).jsonPath( ).getMap( "properties" ).size( ) );
     }
 
+
+    @Test
+    void testUpdateLdapConfiguration() {
+        String token = getAdminToken( );
+        try
+        {
+            Map<String, Object> jsonMap = new HashMap<>( );
+            jsonMap.put( "host_name", "localhost" );
+            jsonMap.put( "port", 389 );
+            jsonMap.put( "ssl_enabled", false );
+            jsonMap.put( "writable", true );
+            jsonMap.put( "base_dn", "dc=apache,dc=org" );
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/ldap" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "config/ldap" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            LdapConfiguration config = response.getBody( ).jsonPath( ).getObject( "", LdapConfiguration.class );
+            assertEquals( "localhost", config.getHostName( ) );
+            assertEquals( 389, config.getPort( ) );
+            assertFalse( config.isSslEnabled( ) );
+            assertTrue( config.isWritable( ) );
+            assertEquals( "dc=apache,dc=org", config.getBaseDn( ) );
+        } finally
+        {
+            Map<String, Object> jsonMap = new HashMap<>( );
+            jsonMap.put( "host_name", "" );
+            jsonMap.put( "port", -1 );
+            jsonMap.put( "ssl_enabled", false );
+            jsonMap.put( "base_dn", "" );
+            jsonMap.put( "writable", false );
+            given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/ldap" )
+                .then( ).statusCode( 200 );
+
+        }
+    }
+
+
     @Test
     void testGetCacheConfiguration() {
         String token = getAdminToken( );
@@ -135,13 +188,58 @@ public class NativeSecurityConfigurationServiceTest extends AbstractNativeRestSe
         assertNotNull( response );
     }
 
+    @Test
+    void testUpdateCacheConfiguration() {
+        String token = getAdminToken( );
+
+        try
+        {
+            Map<String, Object> jsonMap = new HashMap<>( );
+            jsonMap.put( "time_to_idle_seconds", 1600 );
+            jsonMap.put( "time_to_live_seconds", 12000 );
+            jsonMap.put( "max_entries_in_memory", 500 );
+            jsonMap.put( "max_entries_on_disk", 400 );
+
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/cache" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "config/cache" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            CacheConfiguration config = response.getBody( ).jsonPath( ).getObject( "", CacheConfiguration.class );
+            assertEquals( 1600, config.getTimeToIdleSeconds( ) );
+            assertEquals( 12000, config.getTimeToLiveSeconds( ) );
+            assertEquals( 500, config.getMaxEntriesInMemory( ) );
+            assertEquals( 400, config.getMaxEntriesOnDisk( ) );
+        } finally
+        {
+            Map<String, Object> jsonMap = new HashMap<>( );
+            jsonMap.put( "time_to_idle_seconds", 1800 );
+            jsonMap.put( "time_to_live_seconds", 14400 );
+            jsonMap.put( "max_entries_in_memory", 1000 );
+            jsonMap.put( "max_entries_on_disk", 0 );
+
+            given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/cache" )
+                .then( ).statusCode( 200 );
+
+        }
+    }
+
+
     @Test
     void testGetUserManagers() {
         String token = getAdminToken( );
         Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
             .when( )
             .get( "user_managers" )
-            .prettyPeek()
             .then( ).statusCode( 200 ).extract( ).response( );
         assertNotNull( response );
         List<BeanInformation> usrList = response.getBody( ).jsonPath( ).getList( "", BeanInformation.class );
@@ -156,7 +254,6 @@ public class NativeSecurityConfigurationServiceTest extends AbstractNativeRestSe
         Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
             .when( )
             .get( "rbac_managers" )
-            .prettyPeek()
             .then( ).statusCode( 200 ).extract( ).response( );
         assertNotNull( response );
         List<BeanInformation> rbacList = response.getBody( ).jsonPath( ).getList( "", BeanInformation.class );
@@ -165,4 +262,90 @@ public class NativeSecurityConfigurationServiceTest extends AbstractNativeRestSe
         assertTrue( rbacList.stream( ).anyMatch( bi -> "LDAP RBAC Manager".equals( bi.getDisplayName( ) ) ) );
     }
 
+    @Test
+    void testUpdateConfiguration() {
+        String token = getAdminToken( );
+        try
+        {
+            Map<String, Object> jsonAsMap = new HashMap<>( );
+            jsonAsMap.put( "active_user_managers", Arrays.asList( "jpa", "ldap" ) );
+            jsonAsMap.put( "active_rbac_managers", Arrays.asList( "jpa" ) );
+            jsonAsMap.put( "user_cache_enabled", false );
+            jsonAsMap.put( "ldap_active", false );
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonAsMap )
+                .put( "config" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "config" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            assertEquals( 2, response.getBody( ).jsonPath( ).getList( "active_user_managers" ).size( ) );
+        } finally
+        {
+            Map<String, Object> jsonAsMap = new HashMap<>( );
+            jsonAsMap.put( "active_user_managers", Arrays.asList( "jpa" ) );
+            jsonAsMap.put( "active_rbac_managers", Arrays.asList( "jpa" ) );
+            jsonAsMap.put( "user_cache_enabled", true );
+            jsonAsMap.put( "ldap_active", false );
+            given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonAsMap )
+                .put( "config" )
+                .then( ).statusCode( 200 );
+
+        }
+
+    }
+
+    @Test
+    void testGetConfigProperty() {
+        String token = getAdminToken( );
+        Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+            .when( )
+            .get( "config/properties/rest.csrffilter.absentorigin.deny" )
+            .then( ).statusCode( 200 ).extract( ).response( );
+        assertNotNull( response );
+        assertEquals( "true", response.getBody( ).jsonPath( ).getString( "value" ) );
+    }
+
+    @Test
+    void testUpdateConfigProperty() {
+        String token = getAdminToken( );
+
+        try
+        {
+            Map<String, String> jsonMap = new HashMap<>( );
+            jsonMap.put( "value", "false" );
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/properties/rest.csrffilter.absentorigin.deny" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "config/properties/rest.csrffilter.absentorigin.deny" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            assertEquals( "false", response.getBody( ).jsonPath( ).getString( "value" ) );
+        } finally
+        {
+            Map<String, String> jsonMap = new HashMap<>( );
+            jsonMap.put( "value", "true" );
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonMap )
+                .put( "config/properties/rest.csrffilter.absentorigin.deny" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+        }
+    }
+
+
 }