use of org.camunda.bpm.engine.authorization.Resource in project camunda-bpm-platform by camunda.
the class AuthorizationScenarioInstance method assertAuthorizationException.
public void assertAuthorizationException(AuthorizationException e) {
if (!missingAuthorizations.isEmpty() && e != null) {
String message = e.getMessage();
String assertionFailureMessage = describeScenarioFailure("Expected an authorization exception but the message was wrong: " + e.getMessage());
List<MissingAuthorization> actualMissingAuthorizations = new ArrayList<MissingAuthorization>(e.getMissingAuthorizations());
List<MissingAuthorization> expectedMissingAuthorizations = MissingAuthorizationMatcher.asMissingAuthorizations(missingAuthorizations);
Assert.assertThat(actualMissingAuthorizations, containsInAnyOrder(MissingAuthorizationMatcher.asMatchers(expectedMissingAuthorizations)));
for (Authorization missingAuthorization : missingAuthorizations) {
Assert.assertTrue(assertionFailureMessage, message.contains(missingAuthorization.getUserId()));
Assert.assertEquals(missingAuthorization.getUserId(), e.getUserId());
for (Permission permission : missingAuthorization.getPermissions(Permissions.values())) {
if (permission != Permissions.NONE) {
Assert.assertTrue(assertionFailureMessage, message.contains(permission.getName()));
}
}
if (!Authorization.ANY.equals(missingAuthorization.getResourceId())) {
// missing ANY authorizations are not explicitly represented in the error message
Assert.assertTrue(assertionFailureMessage, message.contains(missingAuthorization.getResourceId()));
}
Resource resource = AuthorizationTestUtil.getResourceByType(missingAuthorization.getResourceType());
Assert.assertTrue(assertionFailureMessage, message.contains(resource.resourceName()));
}
} else if (missingAuthorizations.isEmpty() && e == null) {
// nothing to do
} else {
if (e != null) {
Assert.fail(describeScenarioFailure("Expected no authorization exception but got one: " + e.getMessage()));
} else {
Assert.fail(describeScenarioFailure("Expected failure due to missing authorizations but code under test was successful"));
}
}
}
use of org.camunda.bpm.engine.authorization.Resource in project camunda-bpm-platform by camunda.
the class AuthorizationQueryTest method testValidQueryCounts.
public void testValidQueryCounts() {
Resource resource1 = new TestResource("resource1", 100);
Resource resource2 = new TestResource("resource2", 101);
Resource nonExisting = new TestResource("non-existing", 102);
// query by user id
assertEquals(2, authorizationService.createAuthorizationQuery().userIdIn("user1").count());
assertEquals(1, authorizationService.createAuthorizationQuery().userIdIn("user2").count());
assertEquals(1, authorizationService.createAuthorizationQuery().userIdIn("user3").count());
assertEquals(3, authorizationService.createAuthorizationQuery().userIdIn("user1", "user2").count());
assertEquals(0, authorizationService.createAuthorizationQuery().userIdIn("non-existing").count());
// query by group id
assertEquals(2, authorizationService.createAuthorizationQuery().groupIdIn("group1").count());
assertEquals(1, authorizationService.createAuthorizationQuery().groupIdIn("group2").count());
assertEquals(1, authorizationService.createAuthorizationQuery().groupIdIn("group3").count());
assertEquals(3, authorizationService.createAuthorizationQuery().groupIdIn("group1", "group2").count());
assertEquals(0, authorizationService.createAuthorizationQuery().groupIdIn("non-existing").count());
// query by resource type
assertEquals(4, authorizationService.createAuthorizationQuery().resourceType(resource1).count());
assertEquals(0, authorizationService.createAuthorizationQuery().resourceType(nonExisting).count());
assertEquals(4, authorizationService.createAuthorizationQuery().resourceType(resource1.resourceType()).count());
assertEquals(0, authorizationService.createAuthorizationQuery().resourceType(nonExisting.resourceType()).count());
// query by resource id
assertEquals(2, authorizationService.createAuthorizationQuery().resourceId("resource1-2").count());
assertEquals(0, authorizationService.createAuthorizationQuery().resourceId("non-existing").count());
// query by permission
assertEquals(1, authorizationService.createAuthorizationQuery().hasPermission(Permissions.ACCESS).count());
assertEquals(2, authorizationService.createAuthorizationQuery().hasPermission(Permissions.DELETE).count());
assertEquals(2, authorizationService.createAuthorizationQuery().hasPermission(Permissions.READ).count());
assertEquals(3, authorizationService.createAuthorizationQuery().hasPermission(Permissions.UPDATE).count());
// multiple permissions at the same time
assertEquals(2, authorizationService.createAuthorizationQuery().hasPermission(Permissions.READ).hasPermission(Permissions.UPDATE).count());
assertEquals(2, authorizationService.createAuthorizationQuery().hasPermission(Permissions.UPDATE).hasPermission(Permissions.READ).count());
assertEquals(0, authorizationService.createAuthorizationQuery().hasPermission(Permissions.READ).hasPermission(Permissions.ACCESS).count());
// user id & resource type
assertEquals(1, authorizationService.createAuthorizationQuery().userIdIn("user1").resourceType(resource1).count());
assertEquals(0, authorizationService.createAuthorizationQuery().userIdIn("user1").resourceType(nonExisting).count());
// group id & resource type
assertEquals(1, authorizationService.createAuthorizationQuery().groupIdIn("group2").resourceType(resource2).count());
assertEquals(0, authorizationService.createAuthorizationQuery().groupIdIn("group1").resourceType(nonExisting).count());
}
use of org.camunda.bpm.engine.authorization.Resource in project camunda-bpm-platform by camunda.
the class AuthorizationQueryTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
Resource resource1 = new TestResource("resource1", 100);
Resource resource2 = new TestResource("resource2", 101);
createAuthorization("user1", null, resource1, "resource1-1", Permissions.ACCESS);
createAuthorization("user1", null, resource2, "resource2-1", Permissions.DELETE);
createAuthorization("user2", null, resource1, "resource1-2");
createAuthorization("user3", null, resource2, "resource2-1", Permissions.READ, Permissions.UPDATE);
createAuthorization(null, "group1", resource1, "resource1-1");
createAuthorization(null, "group1", resource1, "resource1-2", Permissions.UPDATE);
createAuthorization(null, "group2", resource2, "resource2-2", Permissions.READ, Permissions.UPDATE);
createAuthorization(null, "group3", resource2, "resource2-3", Permissions.DELETE);
}
Aggregations