Search in sources :

Example 1 with MotechRole

use of org.motechproject.security.domain.MotechRole in project motech by motech.

the class MotechURLSecurityServiceBundleIT method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    securityService = getFromContext(MotechURLSecurityService.class, "motechURLSecurityService");
    authenticationManager = getFromContext(AuthenticationManager.class, "authenticationManager");
    motechUserService.registerMotechAdmin("motech", "motech", "aaa@admin.com", Locale.ENGLISH);
    setUpSecurityContext("motech", "motech", getPermissions());
    getSecurityRuleDataService().deleteAll();
    usersDataService.deleteAll();
    rolesDataService.deleteAll();
    rolesDataService.create(new MotechRole(SECURITY_MANAGE_ADMIN, asList(PermissionNames.MANAGE_URL_PERMISSION), false));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MotechURLSecurityService(org.motechproject.security.service.MotechURLSecurityService) MotechRole(org.motechproject.security.domain.MotechRole) Before(org.junit.Before)

Example 2 with MotechRole

use of org.motechproject.security.domain.MotechRole in project motech by motech.

the class MotechAuthenticationProviderTest method shouldRetrieveUserFromDatabase.

@Test
public void shouldRetrieveUserFromDatabase() {
    MotechUser motechUser = new MotechUser("bob", "encodedPassword", "entity_1", "", asList("some_role"), "", Locale.ENGLISH);
    MotechRole motechRole = new MotechRole("some_role", asList("some_permission"), false);
    when(motechUsersDao.findByUserName("bob")).thenReturn(motechUser);
    // when(allMotechRoles.findByRoleName("some_role")).thenReturn(motechRole);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "password");
    UserDetails userDetails = authenticationProvider.retrieveUser("bob", authentication);
    assertEquals("encodedPassword", userDetails.getPassword());
    assertEquals(motechUser.getUserName(), ((MotechUserProfile) authentication.getDetails()).getUserName());
    assertEquals(motechUser.getUserName(), userDetails.getUsername());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) MotechRole(org.motechproject.security.domain.MotechRole) Test(org.junit.Test)

Example 3 with MotechRole

use of org.motechproject.security.domain.MotechRole in project motech by motech.

the class MotechRoleServiceTest method shouldRefreshUserContextWhenRoleIsDeleted.

@Test
public void shouldRefreshUserContextWhenRoleIsDeleted() {
    RoleDto role = new RoleDto("role1", asList("permission1"));
    MotechRole motechRole = mock(MotechRole.class);
    when(motechRole.isDeletable()).thenReturn(true);
    when(rolesDataService.findByRoleName("role1")).thenReturn(motechRole);
    motechRoleService.deleteRole(role);
    verify(userContextsService).refreshAllUsersContextIfActive();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechRole(org.motechproject.security.domain.MotechRole) Test(org.junit.Test)

Example 4 with MotechRole

use of org.motechproject.security.domain.MotechRole in project motech by motech.

the class MotechRoleServiceTest method shouldRefreshUserContextWhenRoleIsUpdated.

@Test
public void shouldRefreshUserContextWhenRoleIsUpdated() {
    RoleDto role = new RoleDto("role1", asList("permission1"));
    MotechRole motechRole = mock(MotechRole.class);
    when(rolesDataService.findByRoleName("role1")).thenReturn(motechRole);
    motechRoleService.updateRole(role);
    verify(userContextsService).refreshAllUsersContextIfActive();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechRole(org.motechproject.security.domain.MotechRole) Test(org.junit.Test)

Example 5 with MotechRole

use of org.motechproject.security.domain.MotechRole in project motech by motech.

the class MotechRoleServiceBundleIT method shouldNotCreateNewRoleIfRoleOfTheSameNameAlreadyExists.

@Test
public void shouldNotCreateNewRoleIfRoleOfTheSameNameAlreadyExists() {
    String roleName = "sameRole";
    motechRoleService.createRole(new RoleDto(roleName, asList("per1", "per2"), false));
    motechRoleService.createRole(new RoleDto(roleName, asList("per3", "per4"), false));
    MotechRole motechRole = rolesDataService.findByRoleName(roleName);
    List<MotechRole> allRoles = rolesDataService.retrieveAll();
    int numberOfRoles = 0;
    for (MotechRole role : allRoles) {
        if (roleName.equalsIgnoreCase(role.getRoleName())) {
            ++numberOfRoles;
        }
    }
    assertEquals(1, numberOfRoles);
    assertEquals("per1", motechRole.getPermissionNames().get(0));
    assertEquals("per2", motechRole.getPermissionNames().get(1));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechRole(org.motechproject.security.domain.MotechRole) Test(org.junit.Test)

Aggregations

MotechRole (org.motechproject.security.domain.MotechRole)8 Test (org.junit.Test)4 MotechUser (org.motechproject.security.domain.MotechUser)3 RoleDto (org.motechproject.security.model.RoleDto)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Before (org.junit.Before)1 RoleHasUserException (org.motechproject.security.exception.RoleHasUserException)1 MotechURLSecurityService (org.motechproject.security.service.MotechURLSecurityService)1 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1