Additional tests for role service v2

This commit is contained in:
Martin Stockhammer 2020-11-25 20:29:43 +01:00
parent 86670fc649
commit 1ce5d0be47
3 changed files with 196 additions and 51 deletions

View File

@ -285,7 +285,7 @@ Response removeTemplatedRole( @PathParam( "templateId" ) String templateId,
* @param roleId
* @param userId
*/
@Path( "{roleId}/assign/{userId}" )
@Path( "{roleId}/user/{userId}" )
@PUT
@Produces( { APPLICATION_JSON } )
@RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION )
@ -313,10 +313,10 @@ RoleInfo assignRole( @PathParam( "roleId" ) String roleId, @PathParam( "userId"
*
* @param templateId
* @param resource
* @param principal
* @param userId
*/
@Path( "template/{templateId}/{resource}/assign/{userId}" )
@POST
@Path( "template/{templateId}/{resource}/user/{userId}" )
@PUT
@Produces( { APPLICATION_JSON } )
@RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION )
@Operation( summary = "Assigns a template role instance to a given user",
@ -333,19 +333,19 @@ RoleInfo assignRole( @PathParam( "roleId" ) String roleId, @PathParam( "userId"
content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = RedbackRestError.class )) )
}
)
RoleInfo assignTemplatedRole( @QueryParam( "templateId" ) String templateId,
@QueryParam( "resource" ) String resource,
@QueryParam( "principal" ) String principal )
RoleInfo assignTemplatedRole( @PathParam( "templateId" ) String templateId,
@PathParam( "resource" ) String resource,
@PathParam( "userId" ) String userId )
throws RedbackServiceException;
/**
* Unassigns the role indicated by the role id from the given principal
*
* @param roleId
* @param principal
* @param userId
* @throws RedbackServiceException
*/
@Path( "{roleId}/{userId}" )
@Path( "{roleId}/user/{userId}" )
@DELETE
@Produces( { APPLICATION_JSON } )
@RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION )
@ -363,7 +363,7 @@ RoleInfo assignTemplatedRole( @QueryParam( "templateId" ) String templateId,
content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = RedbackRestError.class )) )
}
)
RoleInfo unassignRole( @QueryParam( "roleId" ) String roleId, @QueryParam( "principal" ) String principal )
RoleInfo unassignRole( @PathParam( "roleId" ) String roleId, @PathParam( "userId" ) String userId )
throws RedbackServiceException;

View File

@ -40,6 +40,7 @@
import org.apache.archiva.redback.role.RoleManagerException;
import org.apache.archiva.redback.role.RoleNotFoundException;
import org.apache.archiva.redback.role.model.ModelTemplate;
import org.apache.archiva.redback.role.util.RoleModelUtils;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserManager;
import org.apache.archiva.redback.users.UserManagerException;
@ -358,33 +359,75 @@ public RoleInfo assignRole( String roleId, String userId )
@Override
public RoleInfo assignTemplatedRole( String templateId, String resource, String principal )
public RoleInfo assignTemplatedRole( String templateId, String resource, String userId )
throws RedbackServiceException
{
try
{
roleManager.assignTemplatedRole( templateId, resource, principal );
userManager.findUser( userId );
roleManager.assignTemplatedRole( templateId, resource, userId );
String roleId = RoleModelUtils.getRoleId( templateId, resource );
return getRoleInfo( rbacManager.getRoleById( roleId ) );
}
catch ( RoleNotFoundException e ) {
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_ROLE_NOT_FOUND, e.getMessage( ) ), 404 );
}
catch ( RoleManagerException e )
{
throw new RedbackServiceException( e.getMessage() );
}
return null;
catch ( UserNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_NOT_FOUND, e.getMessage( ) ), 404 );
}
catch ( UserManagerException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ) );
}
catch ( RbacObjectNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_RBACMANAGER_FAIL, e.getMessage( ) ) );
}
catch ( RbacManagerException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_RBACMANAGER_FAIL, e.getMessage( ) ) );
}
}
@Override
public RoleInfo unassignRole( String roleId, String principal )
public RoleInfo unassignRole( String roleId, String userId )
throws RedbackServiceException
{
try
{
roleManager.unassignRole( roleId, principal );
userManager.findUser( userId );
roleManager.unassignRole( roleId, userId );
return getRoleInfo( rbacManager.getRoleById( roleId ) );
}
catch ( RoleNotFoundException e ) {
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_ROLE_NOT_FOUND, e.getMessage( ) ), 404 );
}
catch ( RoleManagerException e )
{
throw new RedbackServiceException( e.getMessage() );
}
return null;
catch ( UserNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_NOT_FOUND, e.getMessage( ) ), 404 );
}
catch ( UserManagerException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ) );
}
catch ( RbacObjectNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_RBACMANAGER_FAIL, e.getMessage( ) ) );
}
catch ( RbacManagerException e )
{
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_RBACMANAGER_FAIL, e.getMessage( ) ) );
}
}
@Override

View File

@ -140,8 +140,6 @@ void createTemplatedRoleWithNonexistentTemplate( )
void deleteTemplatedRole( )
{
String token = getAdminToken( );
try
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository05" )
@ -158,15 +156,11 @@ void deleteTemplatedRole( )
.when( )
.delete( "template/archiva-repository-manager/repository05" )
.then( ).statusCode( 404 );
}
finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository05" )
.then( ).statusCode( 200 );
}
}
@Test
@ -175,25 +169,25 @@ void checkTemplatedRole( )
String token = getAdminToken( );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-observer/repository01" )
.put( "template/archiva-repository-observer/repository06" )
.then( ).statusCode( 201 );
try
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.head( "template/archiva-repository-observer/repository01" )
.head( "template/archiva-repository-observer/repository06" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.head( "archiva-repository-observer.repository01" )
.head( "archiva-repository-observer.repository06" )
.then( ).statusCode( 200 );
}
finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository01" )
.delete( "template/archiva-repository-observer/repository06" )
.then( ).statusCode( 200 );
}
@ -382,39 +376,39 @@ void moveRole( )
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository01" )
.put( "template/archiva-repository-manager/repository07" )
.then( ).statusCode( 201 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).head( "template/archiva-repository-observer/repository01" ).then( ).statusCode( 200 );
.when( ).head( "template/archiva-repository-observer/repository07" ).then( ).statusCode( 200 );
Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).post( "template/archiva-repository-manager/repository01/moveto/repository02" ).then( ).statusCode( 201 ).extract( ).response( );
.when( ).post( "template/archiva-repository-manager/repository07/moveto/repository08" ).then( ).statusCode( 201 ).extract( ).response( );
RoleInfo role = response.getBody( ).jsonPath( ).getObject( "", RoleInfo.class );
assertNotNull( role );
assertEquals( "archiva-repository-manager.repository02", role.getId( ) );
assertEquals( "repository02", role.getResource( ) );
assertEquals( "archiva-repository-manager.repository08", role.getId( ) );
assertEquals( "repository08", role.getResource( ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).head( "template/archiva-repository-manager/repository01" ).then( ).statusCode( 404 );
.when( ).head( "template/archiva-repository-manager/repository07" ).then( ).statusCode( 404 );
// Child templates are copied and not moved
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).head( "template/archiva-repository-observer/repository01" ).then( ).statusCode( 200 );
.when( ).head( "template/archiva-repository-observer/repository07" ).then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).head( "template/archiva-repository-observer/repository02" ).then( ).statusCode( 200 );
.when( ).head( "template/archiva-repository-observer/repository08" ).then( ).statusCode( 200 );
}
finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-manager/repository02" )
.delete( "template/archiva-repository-manager/repository08" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository01" )
.delete( "template/archiva-repository-observer/repository07" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository02" )
.delete( "template/archiva-repository-observer/repository08" )
.then( ).statusCode( 200 );
}
@ -429,34 +423,37 @@ void moveRoleToExistingDestination( )
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository01" )
.put( "template/archiva-repository-manager/repository09" )
.then( ).statusCode( 201 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository02" )
.put( "template/archiva-repository-manager/repository10" )
.then( ).statusCode( 201 );
Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).redirects( ).follow( false )
.post( "template/archiva-repository-manager/repository01/moveto/repository02" ).then( ).statusCode( 303 )
.post( "template/archiva-repository-manager/repository09/moveto/repository10" ).then( ).statusCode( 303 )
.extract( ).response( );
System.out.println( response.getHeader( "Location" ) );
assertTrue( response.getHeader( "Location" ).endsWith( "/roles/template/archiva-repository-manager/repository02" ) );
assertTrue( response.getHeader( "Location" ).endsWith( "/roles/template/archiva-repository-manager/repository10" ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( ).head( "template/archiva-repository-manager/repository01" ).then( ).statusCode( 200 );
.when( ).head( "template/archiva-repository-manager/repository09" ).then( ).statusCode( 200 );
}
finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-manager/repository01" )
.delete( "template/archiva-repository-manager/repository09" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-manager/repository02" )
.delete( "template/archiva-repository-observer/repository09" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository02" )
.delete( "template/archiva-repository-manager/repository10" )
.then( ).statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository10" )
.then( ).statusCode( 200 );
}
@ -490,7 +487,7 @@ void assignRole( )
assertFalse( roles.stream( ).filter( role -> "system-administrator".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "system-administrator/assign/aragorn" )
.put( "system-administrator/user/aragorn" )
.prettyPeek( )
.then( ).statusCode( 200 );
response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
@ -504,7 +501,7 @@ void assignRole( )
{
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.delete( "aragorn" ).getBody( );
.delete( "aragorn" ).then().statusCode( 200 );
}
}
@ -534,7 +531,7 @@ void assignNonexistentRole( )
assertFalse( roles.stream( ).filter( role -> "abcdefg".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "abcdefg/assign/aragorn" )
.put( "abcdefg/user/aragorn" )
.prettyPeek( )
.then( ).statusCode( 404 );
response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
@ -548,7 +545,7 @@ void assignNonexistentRole( )
{
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.delete( "aragorn" ).getBody( );
.delete( "aragorn" ).then().statusCode( 200 );
}
}
@ -558,9 +555,114 @@ void assignRoleToNonexistentUser( )
String token = getAdminToken( );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "system-administrator/assign/aragorn" )
.put( "system-administrator/user/aragorn" )
.prettyPeek( )
.then( ).statusCode( 404 );
}
@Test
void assignTemplatedRole( )
{
String token = getAdminToken( );
Map<String, Object> jsonAsMap = new HashMap<>( );
jsonAsMap.put( "user_id", "aragorn" );
jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
jsonAsMap.put( "full_name", "Aragorn King of Gondor " );
jsonAsMap.put( "password", "pAssw0rD" );
try
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository11" )
.then( ).statusCode( 201 );
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.body( jsonAsMap )
.when( )
.post( )
.then( ).statusCode( 201 );
Response response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.get( "aragorn/roles" )
.then( ).statusCode( 200 ).extract( ).response( );
List<RoleInfo> roles = response.getBody( ).jsonPath( ).getList( "", RoleInfo.class );
assertFalse( roles.stream( ).filter( role -> "archiva-repository-manager.repository11".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "template/archiva-repository-manager/repository11/user/aragorn" )
.prettyPeek( )
.then( ).statusCode( 200 );
response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.get( "aragorn/roles" )
.then( ).statusCode( 200 ).extract( ).response( );
roles = response.getBody( ).jsonPath( ).getList( "", RoleInfo.class );
assertTrue( roles.stream( ).filter( role -> "archiva-repository-manager.repository11".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
}
finally
{
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.delete( "aragorn" ).then().statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-manager/repository11" ).then().statusCode( 200 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "template/archiva-repository-observer/repository11" ).then().statusCode( 200 );
}
}
@Test
void unAssignRole( )
{
String token = getAdminToken( );
Map<String, Object> jsonAsMap = new HashMap<>( );
jsonAsMap.put( "user_id", "aragorn" );
jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
jsonAsMap.put( "full_name", "Aragorn King of Gondor " );
jsonAsMap.put( "password", "pAssw0rD" );
try
{
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.body( jsonAsMap )
.when( )
.post( )
.then( ).statusCode( 201 );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.put( "system-administrator/user/aragorn" )
.then( ).statusCode( 200 );
Response response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.get( "aragorn/roles" )
.then( ).statusCode( 200 ).extract( ).response( );
List<RoleInfo> roles = response.getBody( ).jsonPath( ).getList( "", RoleInfo.class );
assertTrue( roles.stream( ).filter( role -> "system-administrator".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.delete( "system-administrator/user/aragorn" )
.then( ).statusCode( 200 );
response = given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.get( "aragorn/roles" )
.then( ).statusCode( 200 ).extract( ).response( );
roles = response.getBody( ).jsonPath( ).getList( "", RoleInfo.class );
assertFalse( roles.stream( ).filter( role -> "system-administrator".equals( role.getId( ) ) ).findAny( ).isPresent( ) );
}
finally
{
given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
.when( )
.delete( "aragorn" ).then().statusCode( 200 );
}
}
}