Search in sources :

Example 6 with PermissionDto

use of org.motechproject.security.model.PermissionDto in project motech by motech.

the class WebSecurityBundleIT method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    PermissionDto permission = new PermissionDto(PERMISSION_NAME, BUNDLE_NAME);
    RoleDto role = new RoleDto(ROLE_NAME, Collections.singletonList(PERMISSION_NAME));
    if (!userService.hasActiveMotechAdmin()) {
        userService.registerMotechAdmin(ADMIN, ADMIN, "aaa@admin.com", Locale.ENGLISH);
    }
    setUpSecurityContext(ADMIN, ADMIN, getPermissions());
    permissionService.addPermission(permission);
    roleService.createRole(role);
    userService.register(USER_NAME, USER_PASSWORD, USER_EMAIL, USER_EXTERNAL_ID, Arrays.asList(ROLE_NAME, MOTECH_ADMIN), USER_LOCALE);
    originalSecurityProxy = getFromContext(MotechProxyManager.class).getFilterChainProxy();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) PermissionDto(org.motechproject.security.model.PermissionDto) Before(org.junit.Before)

Example 7 with PermissionDto

use of org.motechproject.security.model.PermissionDto in project motech by motech.

the class WebSecurityBundleIT method testWebSecurityServices.

@Test
public void testWebSecurityServices() throws Exception {
    // given
    PermissionDto permission = new PermissionDto(PERMISSION_NAME, BUNDLE_NAME);
    RoleDto role = new RoleDto(ROLE_NAME, Arrays.asList(PERMISSION_NAME));
    // when
    permissionService.addPermission(permission);
    roleService.createRole(role);
    userService.register(USER_NAME, USER_PASSWORD, USER_EMAIL, USER_EXTERNAL_ID, Arrays.asList(ROLE_NAME), USER_LOCALE);
    // then
    assertTrue(String.format("Permission %s has not been saved", PERMISSION_NAME), permissionService.getPermissions().contains(permission));
    assertEquals(String.format("Role %s has not been saved properly", ROLE_NAME), role, roleService.getRole(ROLE_NAME));
    assertNotNull(String.format("User %s has not been registered", USER_NAME), userService.hasUser(USER_NAME));
    assertTrue(String.format("User doesn't have role %s", ROLE_NAME), userService.getRoles(USER_NAME).contains(ROLE_NAME));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) PermissionDto(org.motechproject.security.model.PermissionDto) Test(org.junit.Test)

Example 8 with PermissionDto

use of org.motechproject.security.model.PermissionDto in project motech by motech.

the class RolesBundleIT method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    PermissionDto someOtherPermission = new PermissionDto(PERMISSION_NAME, BUNDLE_NAME);
    RoleDto someOtherRole = new RoleDto(SOME_ROLE, Arrays.asList(PERMISSION_NAME));
    // when
    permissionService.addPermission(someOtherPermission);
    roleService.createRole(someOtherRole);
    if (!userService.hasActiveMotechAdmin()) {
        userService.registerMotechAdmin("motech", "motech", "motech@motech.com", USER_LOCALE);
    }
    setUpSecurityContext("motech", "motech", getPermissions());
    if (!userService.hasUser(USER_AUTHORISED_TO_MANAGE_ROLES)) {
        userService.register(USER_AUTHORISED_TO_MANAGE_ROLES, USER_PASSWORD, "test-user-can-manage-roles@mail.com", USER_EXTERNAL_ID, Arrays.asList(MOTECH_ADMIN), USER_LOCALE);
    }
    if (!userService.hasUser(USER_NOT_AUTHORISED_TO_MANAGE_ROLES)) {
        userService.register(USER_NOT_AUTHORISED_TO_MANAGE_ROLES, USER_PASSWORD, "test-user-cannot-manage-roles@mail.com", USER_EXTERNAL_ID, Arrays.asList(SOME_ROLE), USER_LOCALE);
    }
    clearSecurityContext();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) PermissionDto(org.motechproject.security.model.PermissionDto) Before(org.junit.Before)

Example 9 with PermissionDto

use of org.motechproject.security.model.PermissionDto in project motech by motech.

the class MotechPermissionServiceTest method shouldReturnPermissions.

@Test
public void shouldReturnPermissions() {
    MotechPermission perm1 = new MotechPermission("perm1", "bundle1");
    MotechPermission perm2 = new MotechPermission("perm2", "bundle2");
    when(motechPermissionsDataService.retrieveAll()).thenReturn(asList(perm1, perm2));
    List<PermissionDto> result = permissionService.getPermissions();
    assertNotNull(result);
    assertEquals(asList("perm1", "perm2"), extract(result, on(PermissionDto.class).getPermissionName()));
    assertEquals(asList("bundle1", "bundle2"), extract(result, on(PermissionDto.class).getBundleName()));
}
Also used : MotechPermission(org.motechproject.security.domain.MotechPermission) PermissionDto(org.motechproject.security.model.PermissionDto) Test(org.junit.Test)

Example 10 with PermissionDto

use of org.motechproject.security.model.PermissionDto in project motech by motech.

the class SecurityRoleLoaderTest method shouldCreateNewRoles.

@Test
public void shouldCreateNewRoles() throws IOException {
    when(roleService.getRole("Test Role")).thenReturn(null);
    when(applicationContext.getResource("roles.json")).thenReturn(resource);
    when(resource.exists()).thenReturn(true);
    try (InputStream in = getClass().getClassLoader().getResourceAsStream("roles.json")) {
        when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(IOUtils.toByteArray(in)));
    }
    securityRoleLoader.loadRoles(applicationContext);
    verify(roleService).getRole("Test Role");
    ArgumentCaptor<RoleDto> captor = ArgumentCaptor.forClass(RoleDto.class);
    verify(roleService).createRole(captor.capture());
    assertEquals("Test Role", captor.getValue().getRoleName());
    assertEquals(asList("perm1", "perm2"), captor.getValue().getPermissionNames());
    ArgumentCaptor<PermissionDto> permissionCaptor = ArgumentCaptor.forClass(PermissionDto.class);
    verify(permissionService, times(2)).addPermission(permissionCaptor.capture());
    verifyPermission("perm1", null, permissionCaptor.getAllValues().get(0));
    verifyPermission("perm2", null, permissionCaptor.getAllValues().get(1));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PermissionDto(org.motechproject.security.model.PermissionDto) Test(org.junit.Test)

Aggregations

PermissionDto (org.motechproject.security.model.PermissionDto)10 Test (org.junit.Test)7 RoleDto (org.motechproject.security.model.RoleDto)5 MotechPermission (org.motechproject.security.domain.MotechPermission)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Before (org.junit.Before)2