Search in sources :

Example 21 with UserEntity

use of io.gravitee.management.model.UserEntity 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());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) GroupEntity(io.gravitee.management.model.GroupEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

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