use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserServiceTest method shouldFindByUsername.
@Test
public void shouldFindByUsername() throws TechnicalException {
when(user.getUsername()).thenReturn(USER_NAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getFirstname()).thenReturn(FIRST_NAME);
when(user.getLastname()).thenReturn(LAST_NAME);
when(user.getPassword()).thenReturn(PASSWORD);
when(userRepository.findByUsername(USER_NAME)).thenReturn(of(user));
final UserEntity userEntity = userService.findByUsername(USER_NAME, false);
assertEquals(USER_NAME, userEntity.getUsername());
assertEquals(FIRST_NAME, userEntity.getFirstname());
assertEquals(LAST_NAME, userEntity.getLastname());
assertEquals(EMAIL, userEntity.getEmail());
assertEquals(PASSWORD, userEntity.getPassword());
assertEquals(null, userEntity.getRoles());
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserServiceTest method shouldCreate.
@Test
public void shouldCreate() throws TechnicalException {
when(newUser.getUsername()).thenReturn(USER_NAME);
when(newUser.getEmail()).thenReturn(EMAIL);
when(newUser.getFirstname()).thenReturn(FIRST_NAME);
when(newUser.getLastname()).thenReturn(LAST_NAME);
when(userRepository.findById(USER_NAME)).thenReturn(Optional.empty());
when(user.getId()).thenReturn(USER_NAME);
when(user.getUsername()).thenReturn(USER_NAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getFirstname()).thenReturn(FIRST_NAME);
when(user.getLastname()).thenReturn(LAST_NAME);
when(user.getPassword()).thenReturn(PASSWORD);
when(user.getCreatedAt()).thenReturn(date);
when(user.getUpdatedAt()).thenReturn(date);
when(userRepository.create(any(User.class))).thenReturn(user);
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.PORTAL);
when(role.getName()).thenReturn("USER");
when(roleService.findDefaultRoleByScopes(RoleScope.MANAGEMENT, RoleScope.PORTAL)).thenReturn(Collections.singletonList(role));
when(membershipService.getRole(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name(), user.getId(), RoleScope.PORTAL)).thenReturn(role);
final UserEntity createdUserEntity = userService.create(newUser, false);
verify(userRepository).create(argThat(new ArgumentMatcher<User>() {
public boolean matches(final Object argument) {
final User userToCreate = (User) argument;
return USER_NAME.equals(userToCreate.getUsername()) && EMAIL.equals(userToCreate.getEmail()) && FIRST_NAME.equals(userToCreate.getFirstname()) && LAST_NAME.equals(userToCreate.getLastname()) && userToCreate.getCreatedAt() != null && userToCreate.getUpdatedAt() != null && userToCreate.getCreatedAt().equals(userToCreate.getUpdatedAt());
}
}));
assertEquals(USER_NAME, createdUserEntity.getUsername());
assertEquals(FIRST_NAME, createdUserEntity.getFirstname());
assertEquals(LAST_NAME, createdUserEntity.getLastname());
assertEquals(EMAIL, createdUserEntity.getEmail());
assertEquals(PASSWORD, createdUserEntity.getPassword());
assertEquals(ROLES, createdUserEntity.getRoles());
assertEquals(date, createdUserEntity.getCreatedAt());
assertEquals(date, createdUserEntity.getUpdatedAt());
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMembersTest method shouldGetMembersWithMembership.
@Test
public void shouldGetMembersWithMembership() throws Exception {
Membership membership = new Membership();
membership.setReferenceId(API_ID);
membership.setCreatedAt(new Date());
membership.setUpdatedAt(membership.getCreatedAt());
membership.setReferenceType(MembershipReferenceType.API);
membership.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
membership.setUserId("user-id");
UserEntity userEntity = new UserEntity();
userEntity.setUsername(membership.getUserId());
userEntity.setFirstname("John");
userEntity.setLastname("Doe");
RoleEntity po = mock(RoleEntity.class);
po.setScope(io.gravitee.management.model.permissions.RoleScope.API);
po.setName(SystemRole.PRIMARY_OWNER.name());
when(membershipRepository.findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(Collections.singleton(membership));
when(userService.findById(membership.getUserId())).thenReturn(userEntity);
when(membershipRepository.findById(userEntity.getUsername(), MembershipReferenceType.API, API_ID)).thenReturn(of(membership));
when(roleService.findById(RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(po);
Set<MemberEntity> members = membershipService.getMembers(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name());
Assert.assertNotNull(members);
Assert.assertFalse("members must not be empty", members.isEmpty());
verify(membershipRepository, times(1)).findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name());
verify(userService, times(1)).findById(membership.getUserId());
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMembersTest method shouldGetMembersWithoutMembership.
@Test
public void shouldGetMembersWithoutMembership() throws Exception {
Membership membership = new Membership();
membership.setReferenceId(API_ID);
membership.setCreatedAt(new Date());
membership.setUpdatedAt(membership.getCreatedAt());
membership.setReferenceType(MembershipReferenceType.API);
membership.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
membership.setUserId("user-id");
UserEntity userEntity = new UserEntity();
userEntity.setUsername(membership.getUserId());
userEntity.setFirstname("John");
userEntity.setLastname("Doe");
RoleEntity po = mock(RoleEntity.class);
po.setScope(io.gravitee.management.model.permissions.RoleScope.API);
po.setName(SystemRole.PRIMARY_OWNER.name());
when(membershipRepository.findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, null)).thenReturn(Collections.singleton(membership));
when(userService.findById(membership.getUserId())).thenReturn(userEntity);
when(membershipRepository.findById(userEntity.getUsername(), MembershipReferenceType.API, API_ID)).thenReturn(of(membership));
when(roleService.findById(RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(po);
Set<MemberEntity> members = membershipService.getMembers(MembershipReferenceType.API, API_ID, RoleScope.API);
Assert.assertNotNull(members);
Assert.assertFalse("members must not be empty", members.isEmpty());
verify(membershipRepository, times(1)).findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, null);
verify(userService, times(1)).findById(membership.getUserId());
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class AuthenticationSuccessListener method onApplicationEvent.
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal();
try {
UserEntity registeredUser = userService.findByUsername(details.getUsername(), false);
// Principal username is the technical identifier of the user
details.setUsername(registeredUser.getId());
} catch (UserNotFoundException unfe) {
final NewExternalUserEntity newUser = new NewExternalUserEntity();
newUser.setUsername(details.getUsername());
newUser.setSource(details.getSource());
newUser.setSourceId(details.getSourceId());
newUser.setFirstname(details.getFirstname());
newUser.setLastname(details.getLastname());
newUser.setEmail(details.getEmail());
boolean addDefaultRole = false;
if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) {
addDefaultRole = true;
}
UserEntity createdUser = userService.create(newUser, addDefaultRole);
// Principal username is the technical identifier of the user
details.setUsername(createdUser.getId());
if (!addDefaultRole) {
addRole(RoleScope.MANAGEMENT, createdUser.getId(), event.getAuthentication().getAuthorities());
addRole(RoleScope.PORTAL, createdUser.getId(), event.getAuthentication().getAuthorities());
}
}
userService.connect(details.getUsername());
}
Aggregations