Skip to content

Commit

Permalink
ALFREDAPI-562: Replace @BeforeClass by @before
Browse files Browse the repository at this point in the history
  • Loading branch information
WimCrols committed Dec 10, 2024
1 parent d21ea6d commit 032823f
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthorityService;
Expand All @@ -25,8 +27,10 @@
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -66,8 +70,11 @@ public PermissionsTest() {
alfPersonService = getBean("PersonService", PersonService.class);
}

@BeforeClass
public void setupSuite() {
@Before
public void setup() {
// For some reason we cannot use @BeforeClass, since this triggers a
// org.junit.runners.model.InitializationError that cannot be further debugged.

AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
try {
createMainTestFolder(repository.getCompanyHome());
Expand All @@ -83,7 +90,6 @@ public void setupSuite() {
FileInfo documentForbidden = createTestNode(folderForbidden.getNodeRef(), "ForbiddenDocument");
nodeForbidden = documentForbidden.getNodeRef();
alfNodeService.setProperty(nodeForbidden, PROP_QNAME_VERSION_LABEL, PROPERTY_VALUE);

} catch (FileExistsException e) {
logger.warn("Test folder already created. Skipping (" + e.getMessage() + ")");
}
Expand All @@ -96,19 +102,11 @@ public void setupSuite() {
FileInfo documentAllowed = createTestNode(folderAllowed.getNodeRef(), "AllowedDocument");
nodeAllowed = documentAllowed.getNodeRef();
alfNodeService.setProperty(nodeAllowed, PROP_QNAME_VERSION_LABEL, PROPERTY_VALUE);

} catch (FileExistsException e) {
logger.warn("Test folder already created. Skipping (" + e.getMessage() + ")");
}
}

@AfterClass
public void tearDownSuite() {
AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
alfPersonService.deletePerson(USERNAME_NORIGHTS_JOS);
cleanUp();
}

private void createUserAndGroupsWithLimitedRights() {
try {
alfAuthenticationService.createAuthentication(USERNAME_NORIGHTS_JOS, "foobar".toCharArray());
Expand All @@ -119,12 +117,17 @@ private void createUserAndGroupsWithLimitedRights() {
userProperties.put(ContentModel.PROP_EMAIL, "[email protected]");
alfAuthenticationService.getAuthenticationEnabled(USERNAME_NORIGHTS_JOS);
alfPersonService.createPerson(userProperties);
logger.info("User " + USERNAME_NORIGHTS_JOS + " successfully created");
} catch (AuthenticationException e) {
logger.warn("User already created. Skipping (" + e.getMessage() + ")");
}

try {
alfAuthorityService.createAuthority(AuthorityType.GROUP, GROUPNAME);
alfAuthorityService.addAuthority(GROUPID, USERNAME_NORIGHTS_JOS);
} catch (AuthenticationException e) {
// User and groups were already created. Skip.
logger.warn("User and groups already created. Skipping (" + e.getMessage() + ")");
logger.info("Group " + GROUPNAME + " successfully created");
} catch (DuplicateChildNodeNameException e) {
logger.warn("Group already created. Skipping (" + e.getMessage() + ")");
}
}

Expand All @@ -150,10 +153,18 @@ public void testGetNodeMetadata() {
// Switch to non-admin user
AuthenticationUtil.setFullyAuthenticatedUser(USERNAME_NORIGHTS_JOS);

// Allowed case
NodeMetadata result = apixNodeService.getMetadata(new eu.xenit.alfred.api.data.NodeRef(nodeAllowed.toString()));
logger.error("WIM: r:: " + result.getProperties()); //// REMOVEME
Assert.assertFalse(result.getProperties().isEmpty());

// Forbidden case
try {
apixNodeService.getMetadata(new eu.xenit.alfred.api.data.NodeRef(nodeForbidden.toString()));
Assert.fail("Expected AccessDeniedException");
}
catch (AccessDeniedException e) {
}

logger.error("WIM: Your father would be proud, Fox"); // REMOVE ME
}

Expand Down

0 comments on commit 032823f

Please sign in to comment.