Search in sources :

Example 1 with UserEntity

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());
}
Also used : UserEntity(io.gravitee.management.model.UserEntity) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) Test(org.junit.Test)

Example 2 with UserEntity

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());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) UserRoleEntity(io.gravitee.management.model.UserRoleEntity) User(io.gravitee.repository.management.model.User) ArgumentMatcher(org.mockito.ArgumentMatcher) UserEntity(io.gravitee.management.model.UserEntity) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) Test(org.junit.Test)

Example 3 with UserEntity

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());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 4 with UserEntity

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());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 5 with UserEntity

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());
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) UserEntity(io.gravitee.management.model.UserEntity)

Aggregations

UserEntity (io.gravitee.management.model.UserEntity)21 Test (org.junit.Test)14 Membership (io.gravitee.repository.management.model.Membership)8 NewExternalUserEntity (io.gravitee.management.model.NewExternalUserEntity)6 RoleEntity (io.gravitee.management.model.RoleEntity)6 UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)4 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)4 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)4 MemberEntity (io.gravitee.management.model.MemberEntity)3 HashMap (java.util.HashMap)3 ApiEntity (io.gravitee.management.model.ApiEntity)2 GroupEntity (io.gravitee.management.model.GroupEntity)2 NewApiEntity (io.gravitee.management.model.NewApiEntity)2 UpdateUserEntity (io.gravitee.management.model.UpdateUserEntity)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 JWTSigner (com.auth0.jwt.JWTSigner)1 ApiModelEntity (io.gravitee.management.model.ApiModelEntity)1 EventEntity (io.gravitee.management.model.EventEntity)1