use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class AuthenticationSuccessListenerTest method shouldCreateUserWithCustomSpecificRoleAndGlobal.
@Test
public void shouldCreateUserWithCustomSpecificRoleAndGlobal() {
when(eventMock.getAuthentication()).thenReturn(authenticationMock);
when(authenticationMock.getPrincipal()).thenReturn(userDetailsMock);
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("ROLE"), new SimpleGrantedAuthority("PORTAL:ROLE2"));
when(authenticationMock.getAuthorities()).thenReturn(authorities);
when(userServiceMock.findByUsername(userDetailsMock.getUsername(), false)).thenThrow(UserNotFoundException.class);
RoleEntity roleEntity1 = mock(RoleEntity.class);
when(roleEntity1.getName()).thenReturn("ROLE");
RoleEntity roleEntity2 = mock(RoleEntity.class);
when(roleEntity2.getName()).thenReturn("ROLE2");
when(roleServiceMock.findById(RoleScope.MANAGEMENT, "ROLE")).thenReturn(roleEntity1);
when(roleServiceMock.findById(RoleScope.PORTAL, "ROLE2")).thenReturn(roleEntity2);
when(userServiceMock.create(any(NewExternalUserEntity.class), eq(false))).thenReturn(userEntity);
listener.onApplicationEvent(eventMock);
verify(userServiceMock, times(1)).findByUsername(userDetailsMock.getUsername(), false);
verify(userServiceMock, times(1)).create(any(NewExternalUserEntity.class), eq(false));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.MANAGEMENT, "ROLE"));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.PORTAL, "ROLE2"));
verify(userServiceMock, times(1)).connect(userDetailsMock.getUsername());
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleServiceImpl method findDefaultRoleByScopes.
@Override
public List<RoleEntity> findDefaultRoleByScopes(RoleScope... scopes) {
try {
LOGGER.debug("Find default Roles by scope");
List<RoleEntity> roles = new ArrayList<>();
for (RoleScope scope : scopes) {
roles.addAll(roleRepository.findByScope(scope).stream().filter(Role::isDefaultRole).map(this::convert).collect(Collectors.toList()));
}
return roles;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find default roles by scope", ex);
throw new TechnicalManagementException("An error occurs while trying to find default roles by scope", ex);
}
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_AddOrUpdateMemberTest method shouldDisallowAddManagementRoleOnGroup.
@Test(expected = NotAuthorizedMembershipException.class)
public void shouldDisallowAddManagementRoleOnGroup() throws Exception {
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.MANAGEMENT);
when(roleService.findById(any(), any())).thenReturn(role);
membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser("xxxxx", null), new MembershipService.MembershipRole(RoleScope.MANAGEMENT, "PRIMAY_OWNER"));
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_AddOrUpdateMemberTest method shouldDisallowAddPortalRoleOnGroup.
@Test(expected = NotAuthorizedMembershipException.class)
public void shouldDisallowAddPortalRoleOnGroup() throws Exception {
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.PORTAL);
when(roleService.findById(any(), any())).thenReturn(role);
membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser("xxxxx", null), new MembershipService.MembershipRole(RoleScope.PORTAL, "PRIMAY_OWNER"));
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_AddOrUpdateMemberTest method shouldAddApiGroupMembership.
@Test
public void shouldAddApiGroupMembership() throws Exception {
UserEntity userEntity = new UserEntity();
userEntity.setId("my name");
userEntity.setUsername("my name");
userEntity.setEmail("me@mail.com");
RoleEntity role = mock(RoleEntity.class);
Membership newMembership = new Membership();
newMembership.setReferenceType(MembershipReferenceType.GROUP);
newMembership.setRoles(Collections.singletonMap(RoleScope.API.getId(), "OWNER"));
newMembership.setReferenceId(GROUP_ID);
newMembership.setUserId(userEntity.getUsername());
GroupEntity groupEntityMock = mock(GroupEntity.class);
when(groupEntityMock.getName()).thenReturn("foo");
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
when(roleService.findById(any(), any())).thenReturn(role);
when(userService.findById(userEntity.getId())).thenReturn(userEntity);
when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(empty(), of(newMembership));
when(membershipRepository.create(any())).thenReturn(newMembership);
membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getId(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
verify(userService, times(2)).findById(userEntity.getId());
verify(membershipRepository, times(2)).findById(userEntity.getUsername(), MembershipReferenceType.GROUP, GROUP_ID);
verify(membershipRepository, times(1)).create(any());
verify(membershipRepository, never()).update(any());
verify(emailService, times(1)).sendAsyncEmailNotification(any());
}
Aggregations