Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue # 7  fix to be able to test system-users. #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import javax.jcr.RepositoryException;

import org.apache.jackrabbit.api.security.user.AuthorizableExistsException;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
Expand All @@ -34,7 +34,7 @@
*
*/
public class AcTestSet {

private final Logger LOG = LoggerFactory.getLogger(AcTestSet.class);


Expand All @@ -48,7 +48,7 @@ public class AcTestSet {

/**
* Constructor
*
*
* @param authorizableID user/group id
*/
public AcTestSet(String authorizableID, String pathToTestfile) {
Expand All @@ -59,56 +59,65 @@ public AcTestSet(String authorizableID, String pathToTestfile) {
public void addAcTestCase(Testable testable){
this.acTestCase.add(testable);
}

public String getAuthorizableID(){
return this.authorizableID;
}

/**
* creates the needed testuser and resolver needed for the testcases, executes the tests and cleans up afterwards
* @param serviceResourceResolverService
* @param serviceResourceResolverService
* @return
* @throws RepositoryException
* @throws LoginException
*/
public List<TestResult> isOk(ServiceResourceResolverService serviceResourceResolverService) throws RepositoryException, LoginException {
List<TestResult> resultList = new ArrayList<>();
User testuser = null;
Group testGroup = null;
Authorizable authorizableToTest = null;
ResourceResolver serviceResourcerResolver = null;
ResourceResolver testUserResolver = null;
try {
// create authorizables
// create authorizables
serviceResourcerResolver = serviceResourceResolverService.getServiceResourceResolver();

UserManager userManager = getUserManager(serviceResourcerResolver);

boolean isSystemUser = !userManager.getAuthorizable(authorizableID).isGroup();

if (isSystemUser) {
testUserResolver = serviceResourceResolverService.getServiceResourceResolver(authorizableID);
authorizableToTest = userManager.getAuthorizable(authorizableID);

} else {

testuser = userManager.createUser(ACVALIDATOR_TESTUSER_ID, ACVALIDATOR_TESTUSER_PASSWORD);
testGroup = getTestGroup(getUserManager(serviceResourcerResolver), authorizableID, testuser);
authorizableToTest = getTestGroup(getUserManager(serviceResourcerResolver), authorizableID, testuser);

// we need to persist the created testuser in order to be able to get a resolver for him
// we need to persist the created testuser in order to be able to get a resolver for him
serviceResourcerResolver.commit();
LOG.debug("comitting serviceResourcerResolver to persist testuser");
// create ResourceResolver for the testuser based on his permissions
testUserResolver = serviceResourceResolverService.getTestUserResourceResolver(ACVALIDATOR_TESTUSER_ID, ACVALIDATOR_TESTUSER_PASSWORD);
}

// execute all testcases for the testuser
for(Testable testable: acTestCase){
resultList.add(testable.isOk(serviceResourcerResolver, testUserResolver, testGroup));
resultList.add(testable.isOk(serviceResourcerResolver, testUserResolver, authorizableToTest));
}
} catch (PersistenceException e) {
throw new RepositoryException(e);
}finally{
} finally {
// clean up temporary testuser
if(testGroup != null && testuser != null){
testGroup.removeMember(testuser);
if(authorizableToTest != null && authorizableToTest instanceof Group && testuser != null){
((Group)authorizableToTest).removeMember(testuser);
}
if(testuser != null){
testuser.remove();
}

// close resolvers

if(testUserResolver != null){
testUserResolver.revert();
testUserResolver.close();
Expand All @@ -128,7 +137,7 @@ public List<TestResult> isOk(ServiceResourceResolverService serviceResourceResol
return resultList;
}

private Group getTestGroup(UserManager userManager, String authorizableID, User testuser) throws AuthorizableExistsException, RepositoryException{
private Group getTestGroup(UserManager userManager, String authorizableID, User testuser) throws RepositoryException{
Group group = (Group) userManager.getAuthorizable(authorizableID);
group.addMember(testuser);
return group;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public interface ServiceResourceResolverService {
* @throws LoginException error getting resource resolver
*/
ResourceResolver getServiceResourceResolver() throws LoginException;


ResourceResolver getServiceResourceResolver(String authorizableID) throws LoginException;


Session getUserSession(SimpleCredentials credentials) throws javax.jcr.LoginException, RepositoryException;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public ResourceResolver getServiceResourceResolver() throws LoginException {
return resourceResolverFactory.getServiceResourceResolver(authenticationInfo);
}

@Override
public ResourceResolver getServiceResourceResolver(String authorizableID) throws LoginException {
final Map<String, Object> authenticationInfo = new HashMap<>();
authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, authorizableID);
return resourceResolverFactory.getServiceResourceResolver(authenticationInfo);
}

@Override
public Session getUserSession(SimpleCredentials credentials) throws javax.jcr.LoginException, RepositoryException {
javax.jcr.Session session = repository.login(credentials);
Expand Down