Updating and testing lock methods V2 REST user service

This commit is contained in:
Martin Stockhammer 2020-08-04 18:42:58 +02:00
parent bcdc1f24b6
commit 1609b4eaeb
3 changed files with 142 additions and 18 deletions

View File

@ -173,16 +173,32 @@ public interface UserService
@POST @POST
@Produces( { MediaType.APPLICATION_JSON } ) @Produces( { MediaType.APPLICATION_JSON } )
@RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION ) @RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION )
ActionStatus lockUser( @PathParam( "userId" ) String userId ) @io.swagger.v3.oas.annotations.Operation( summary = "Creates a user",
responses = {
@ApiResponse( responseCode = "200",
description = "If locking was successful"
),
@ApiResponse( responseCode = "404", description = "User does not exist" ),
}
)
void lockUser( @PathParam( "userId" ) String userId )
throws RedbackServiceException; throws RedbackServiceException;
/** /**
*/ */
@Path( "{userId}/unlock" ) @Path( "{userId}/unlock" )
@GET @POST
@Produces( { MediaType.APPLICATION_JSON } ) @Produces( { MediaType.APPLICATION_JSON } )
@RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION ) @RedbackAuthorization( permissions = RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION )
ActionStatus unlockUser( @PathParam( "userId" ) String userId ) @io.swagger.v3.oas.annotations.Operation( summary = "Creates a user",
responses = {
@ApiResponse( responseCode = "200",
description = "If locking was successful"
),
@ApiResponse( responseCode = "404", description = "User does not exist" ),
}
)
void unlockUser( @PathParam( "userId" ) String userId )
throws RedbackServiceException; throws RedbackServiceException;

View File

@ -1035,31 +1035,55 @@ public class DefaultUserService
} }
@Override @Override
public ActionStatus unlockUser( String userId ) public void unlockUser( String userId )
throws RedbackServiceException throws RedbackServiceException
{ {
User user = getUser( userId ); try
if ( user != null )
{ {
user.setLocked( false ); org.apache.archiva.redback.users.User rawUser = userManager.findUser( userId, false );
updateUser( user.getUserId(), user ); if ( rawUser != null )
return ActionStatus.SUCCESS; {
rawUser.setLocked( false );
userManager.updateUser( rawUser, false );
} else {
throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND, userId ), 404 );
} }
return ActionStatus.FAIL; }
catch ( UserNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND ), 404 );
}
catch ( UserManagerException e )
{
throw new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
}
httpServletResponse.setStatus( 200 );
} }
@Override @Override
public ActionStatus lockUser( String userId ) public void lockUser( String userId )
throws RedbackServiceException throws RedbackServiceException
{ {
User user = getUser( userId ); try
if ( user != null )
{ {
user.setLocked( true ); org.apache.archiva.redback.users.User rawUser = userManager.findUser( userId, false );
updateUser( user.getUserId(), user ); if ( rawUser != null )
return ActionStatus.SUCCESS; {
rawUser.setLocked( true );
userManager.updateUser( rawUser, false );
} else {
throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND, userId ), 404 );
} }
return ActionStatus.FAIL; }
catch ( UserNotFoundException e )
{
throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND ), 404 );
}
catch ( UserManagerException e )
{
throw new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
}
httpServletResponse.setStatus( 200 );
} }
@Override @Override

View File

@ -380,7 +380,7 @@ public class NativeUserServiceTest extends AbstractNativeRestServices
} }
@Test @Test
void updateUserPasswordViolation() { void updateUserWithPasswordViolation() {
String token = getAdminToken( ); String token = getAdminToken( );
Map<String, Object> jsonAsMap = new HashMap<>( ); Map<String, Object> jsonAsMap = new HashMap<>( );
jsonAsMap.put( "user_id", "aragorn" ); jsonAsMap.put( "user_id", "aragorn" );
@ -415,4 +415,88 @@ public class NativeUserServiceTest extends AbstractNativeRestServices
} }
} }
@Test
void lockUser() {
String token = getAdminToken( );
Map<String, Object> jsonAsMap = new HashMap<>( );
jsonAsMap.put( "user_id", "aragorn" );
jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
jsonAsMap.put( "fullName", "Aragorn King of Gondor" );
jsonAsMap.put( "locked", false );
jsonAsMap.put( "password", "pAssw0rD" );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.body( jsonAsMap )
.when( )
.post( )
.then( ).statusCode( 201 );
try
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.post( "aragorn/lock" )
.then( ).statusCode( 200 );
Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.get( "aragorn" )
.then( ).statusCode( 200 ).extract( ).response( );
assertTrue( response.getBody( ).jsonPath( ).getBoolean( "locked" ) );
} finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.delete( "aragorn" )
.then( ).statusCode( 200 );
}
}
@Test
void lockUnknownUser() {
String token = getAdminToken( );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.post( "aragorn/lock" )
.then( ).statusCode( 404 );
}
@Test
void unlockUser() {
String token = getAdminToken( );
Map<String, Object> jsonAsMap = new HashMap<>( );
jsonAsMap.put( "user_id", "aragorn" );
jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
jsonAsMap.put( "fullName", "Aragorn King of Gondor" );
jsonAsMap.put( "locked", true );
jsonAsMap.put( "password", "pAssw0rD" );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.body( jsonAsMap )
.when( )
.post( )
.then( ).statusCode( 201 );
Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.get( "aragorn" )
.then( ).statusCode( 200 ).extract( ).response( );
assertTrue( response.getBody( ).jsonPath( ).getBoolean( "locked" ) );
try
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.post( "aragorn/unlock" )
.then( ).statusCode( 200 );
response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.get( "aragorn" )
.then( ).statusCode( 200 ).extract( ).response( );
assertFalse( response.getBody( ).jsonPath( ).getBoolean( "locked" ) );
} finally
{
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.delete( "aragorn" )
.then( ).statusCode( 200 );
}
}
@Test
void unlockUnknownUser() {
String token = getAdminToken( );
given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.post( "aragorn/unlock" )
.then( ).statusCode( 404 );
}
} }