use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.
the class CopyValuesControllerTest method testQuery.
private GetDataResponse testQuery(String path, ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> pipeline, Map<String, String[]> params) {
N2oInvocationFactory invocationFactory = Mockito.mock(N2oInvocationFactory.class);
TestDataProviderEngine testDataProviderEngine = new TestDataProviderEngine();
testDataProviderEngine.setResourceLoader(new DefaultResourceLoader());
Mockito.when(invocationFactory.produce(Mockito.any(Class.class))).thenReturn(testDataProviderEngine);
ContextEngine contextEngine = Mockito.mock(ContextEngine.class);
UserContext userContext = new UserContext(contextEngine);
ContextProcessor contextProcessor = new ContextProcessor(userContext);
N2oQueryProcessor queryProcessor = new N2oQueryProcessor(invocationFactory, new N2oQueryExceptionHandler());
N2oEnvironment env = new N2oEnvironment();
env.setContextProcessor(contextProcessor);
queryProcessor.setEnvironment(env);
N2oSubModelsProcessor subModelsProcessor = Mockito.mock(N2oSubModelsProcessor.class);
Mockito.doNothing().when(subModelsProcessor);
DataProcessingStack dataProcessingStack = Mockito.mock(SpringDataProcessingStack.class);
CopyValuesController copyValuesController = new CopyValuesController(dataProcessingStack, queryProcessor, subModelsProcessor, null, null);
Map<String, Object> map = new HashMap<>();
map.put("CopyValuesController", copyValuesController);
N2oRouter router = new N2oRouter(builder.getEnvironment(), pipeline);
N2oControllerFactory factory = new N2oControllerFactory(map);
factory.setEnvironment(builder.getEnvironment());
DataController controller = new DataController(factory, builder.getEnvironment(), router);
return controller.getData(path, params, userContext);
}
use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.
the class DefaultValuesControllerTest method testQuery.
private GetDataResponse testQuery(String path, ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> pipeline, Map<String, String[]> params) {
N2oInvocationFactory invocationFactory = Mockito.mock(N2oInvocationFactory.class);
TestDataProviderEngine testDataProviderEngine = new TestDataProviderEngine();
testDataProviderEngine.setResourceLoader(new DefaultResourceLoader());
Mockito.when(invocationFactory.produce(Mockito.any(Class.class))).thenReturn(testDataProviderEngine);
ContextEngine contextEngine = Mockito.mock(ContextEngine.class);
UserContext userContext = new UserContext(contextEngine);
ContextProcessor contextProcessor = new ContextProcessor(userContext);
N2oQueryProcessor queryProcessor = new N2oQueryProcessor(invocationFactory, new N2oQueryExceptionHandler());
N2oEnvironment env = new N2oEnvironment();
env.setContextProcessor(contextProcessor);
queryProcessor.setEnvironment(env);
N2oSubModelsProcessor subModelsProcessor = Mockito.mock(N2oSubModelsProcessor.class);
Mockito.doNothing().when(subModelsProcessor);
DataProcessingStack dataProcessingStack = Mockito.mock(SpringDataProcessingStack.class);
SimpleDefaultValuesController valuesController = new SimpleDefaultValuesController(dataProcessingStack, queryProcessor, subModelsProcessor, null, null);
Map<String, Object> map = new HashMap<>();
map.put("SimpleDefaultValuesController", valuesController);
N2oRouter router = new N2oRouter(builder.getEnvironment(), pipeline);
N2oControllerFactory factory = new N2oControllerFactory(map);
factory.setEnvironment(builder.getEnvironment());
DataController controller = new DataController(factory, builder.getEnvironment(), router);
return controller.getData(path, params, userContext);
}
use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.
the class SecurityProviderTest method collectRestrictionsRemovePermitAllFilters.
@Test
public void collectRestrictionsRemovePermitAllFilters() {
SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
UserContext userContext = new UserContext(new TestContextEngine());
SecurityFilters securityFilters = new SecurityFilters();
ArrayList<N2oObjectFilter> authFilters = new ArrayList<>();
authFilters.add(new N2oObjectFilter("authGender", "man", FilterType.eq, "authGenderFilter"));
authFilters.add(new N2oObjectFilter("authPosition", "developer", FilterType.eq, "authPositionFilter"));
securityFilters.setAuthenticatedFilters(authFilters);
// проверка удаления фильтров по всем пользователям
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
Set<String> permitAllRemoveFilters = new HashSet<>();
permitAllRemoveFilters.add("authPositionFilter");
securityFilters.setRemovePermitAllFilters(permitAllRemoveFilters);
List<Restriction> restrictions = securityProvider.collectRestrictions(securityFilters, userContext);
assertThat(restrictions.contains(new Restriction("authPosition", "developer", FilterType.eq)), is(false));
assertThat(restrictions.contains(new Restriction("authGender", "man", FilterType.eq)), is(true));
}
use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.
the class SecurityProviderTest method checkRestrictions.
@Test
public void checkRestrictions() {
SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
SecurityProvider notStrictSecurityProvider = new SecurityProvider(permissionApi, false);
UserContext userContext = new UserContext(new TestContextEngine());
SecurityFilters securityFilters = new SecurityFilters();
securityFilters.setAuthenticatedFilters(Arrays.asList(new N2oObjectFilter("foo", "1", FilterType.eq, "filter1"), new N2oObjectFilter("name", FilterType.isNotNull, "filter7"), new N2oObjectFilter("surname", "1", FilterType.eqOrIsNull, "filter6")));
securityFilters.setAnonymousFilters(Arrays.asList(new N2oObjectFilter("age", FilterType.isNull, "filter8"), new N2oObjectFilter("foo", "1", FilterType.notEq, "filter2")));
securityFilters.setRoleFilters(Collections.singletonMap("role1", Collections.singletonList(new N2oObjectFilter("bar", new String[] { "1", "2", "3" }, FilterType.in, "filter3"))));
securityFilters.setPermissionFilters(Collections.singletonMap("permission1", Collections.singletonList(new N2oObjectFilter("list", new String[] { "1", "2", "#{three}" }, FilterType.contains, "filter4"))));
securityFilters.setUserFilters(Collections.singletonMap("username1", Collections.singletonList(new N2oObjectFilter("name", "#{username}", FilterType.eq, "filter5"))));
// аутентифицирован
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
// foo == 1 and name != null and surname == null
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("name", "Ivan"), securityFilters, userContext);
// foo != 1
notStrictSecurityProvider.checkRestrictions(new DataSet().add("foo", 1).add("name", "Ivan"), securityFilters, userContext);
try {
securityProvider.checkRestrictions(new DataSet().add("name", "Ivan").add("foo", 2), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("foo"));
}
// foo == null
notStrictSecurityProvider.checkRestrictions(new DataSet(), securityFilters, userContext);
try {
securityProvider.checkRestrictions(new DataSet().add("name", "Ivan"), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("foo"));
}
// анонимный доступ
when(permissionApi.hasAuthentication(userContext)).thenReturn(false);
// foo != 1
securityProvider.checkRestrictions(new DataSet().add("foo", 2), securityFilters, userContext);
// foo == 1
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("foo"));
}
// age != null
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 3).add("age", 10), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("age"));
}
// доступ аутентифицированным и по ролям
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
// bar in (1, 2, 3)
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan"), securityFilters, userContext);
} catch (AccessDeniedException e) {
Assert.fail();
}
// bar not in (1, 2, 3)
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 4).add("name", "Ivan"), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("bar"));
}
// доступ аутентифицированным, по ролям и по полномочиям
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
when(permissionApi.hasPermission(userContext, "permission1")).thenReturn(true);
userContext.set("three", 3);
// list contains (1, 2, 3)
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan").add("list", Arrays.asList(3, 2, 1, 4)), securityFilters, userContext);
} catch (AccessDeniedException e) {
Assert.fail();
}
// list not contains (1, 2, 3)
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan").add("list", Arrays.asList(1, 2)), securityFilters, userContext);
Assert.fail();
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("list"));
}
// доступ аутентифицированным, по ролям, по полномочиям, по имени пользователя
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
when(permissionApi.hasPermission(userContext, "permission1")).thenReturn(true);
when(permissionApi.hasUsername(userContext, "username1")).thenReturn(true);
userContext.set("username", "Joe");
// name == #{username}
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("list", Arrays.asList(3, 2, 1, 4)).add("name", "Joe"), securityFilters, userContext);
} catch (AccessDeniedException e) {
Assert.fail();
}
// name != #{username}
try {
securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("list", Arrays.asList(3, 2, 1, 4)).add("name", "Doe"), securityFilters, userContext);
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), endsWith("name"));
}
}
use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.
the class SecurityProviderTest method checkAccessHasRole.
@Test
public void checkAccessHasRole() {
SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
UserContext userContext = new UserContext(new TestContextEngine());
when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
when(permissionApi.hasRole(userContext, "admin")).thenReturn(true);
when(permissionApi.hasRole(userContext, "role1")).thenReturn(false);
when(permissionApi.hasRole(userContext, "role2")).thenReturn(false);
Security.SecurityObject securityObject = new Security.SecurityObject();
securityObject.setDenied(false);
securityObject.setPermitAll(false);
securityObject.setAnonymous(true);
securityObject.setAuthenticated(false);
securityObject.setRoles(new HashSet<>(Arrays.asList("role2", "role1", "admin")));
Map<String, Security.SecurityObject> securityObjectMap = new HashMap<>();
securityObjectMap.put("custom", securityObject);
Security security = new Security();
security.setSecurityMap(securityObjectMap);
try {
securityProvider.checkAccess(security, userContext);
Assert.fail("Expected exception to be thrown");
} catch (Exception e) {
assertThat(e, instanceOf(AccessDeniedException.class));
}
when(permissionApi.hasRole(userContext, "admin")).thenReturn(false);
try {
securityProvider.checkAccess(security, userContext);
Assert.fail("Expected exception to be thrown");
} catch (Exception e) {
assertThat(e, instanceOf(AccessDeniedException.class));
}
}
Aggregations