Search in sources :

Example 11 with Membership

use of io.gravitee.repository.management.model.Membership in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_AddOrUpdateMemberTest method shouldUpdateApiGroupMembership.

@Test
public void shouldUpdateApiGroupMembership() throws Exception {
    UserEntity userEntity = new UserEntity();
    userEntity.setId("my name");
    userEntity.setUsername("my name");
    userEntity.setEmail("me@mail.com");
    Membership membership = new Membership();
    membership.setUserId(userEntity.getUsername());
    membership.setReferenceType(MembershipReferenceType.GROUP);
    membership.setReferenceId(GROUP_ID);
    Map<Integer, String> roles = new HashMap<>();
    roles.put(RoleScope.API.getId(), "USER");
    membership.setRoles(roles);
    Membership newMembership = new Membership();
    newMembership.setUserId(userEntity.getUsername());
    newMembership.setReferenceType(MembershipReferenceType.GROUP);
    newMembership.setReferenceId(GROUP_ID);
    GroupEntity groupEntityMock = mock(GroupEntity.class);
    when(groupEntityMock.getName()).thenReturn("foo");
    RoleEntity role = mock(RoleEntity.class);
    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(of(membership));
    when(membershipRepository.update(any())).thenReturn(newMembership);
    MemberEntity updateMember = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getUsername(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
    verify(userService, times(2)).findById(userEntity.getId());
    verify(membershipRepository, times(2)).findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID);
    verify(membershipRepository, never()).create(any());
    verify(membershipRepository, times(1)).update(any());
    verify(emailService, never()).sendAsyncEmailNotification(any());
}
Also used : HashMap(java.util.HashMap) UserEntity(io.gravitee.management.model.UserEntity) RoleEntity(io.gravitee.management.model.RoleEntity) GroupEntity(io.gravitee.management.model.GroupEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Test(org.junit.Test)

Example 12 with Membership

use of io.gravitee.repository.management.model.Membership in project gravitee-management-rest-api by gravitee-io.

the class MembershipServiceImpl method removeRole.

@Override
public boolean removeRole(MembershipReferenceType referenceType, String referenceId, String userId, RoleScope roleScope) {
    try {
        Optional<Membership> optionalMembership = membershipRepository.findById(userId, referenceType, referenceId);
        if (optionalMembership.isPresent()) {
            Membership membership = optionalMembership.get();
            Membership previousMembership = new Membership(membership);
            membership.getRoles().remove(roleScope.getId());
            membership.setUpdatedAt(new Date());
            if (membership.getRoles().isEmpty()) {
                throw new MemberWithoutRoleException(membership.getUserId());
            } else {
                membershipRepository.update(membership);
                createAuditLog(MEMBERSHIP_UPDATED, membership.getUpdatedAt(), previousMembership, membership);
                return true;
            }
        }
        return false;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to get membership for {} {} and user", referenceType, referenceId, userId, ex);
        throw new TechnicalManagementException("An error occurs while trying to get members for " + referenceType + " " + referenceId + " and user " + userId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Membership(io.gravitee.repository.management.model.Membership)

Example 13 with Membership

use of io.gravitee.repository.management.model.Membership in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_FindByUserTest method shouldFindByUser.

@Test
public void shouldFindByUser() throws Exception {
    when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
    when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.emptySet());
    when(applicationRepository.findByGroups(Collections.emptyList(), ApplicationStatus.ACTIVE)).thenReturn(Collections.emptySet());
    Membership po = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
    Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
    Assert.assertNotNull(apps);
    Assert.assertFalse("should find app", apps.isEmpty());
    Assert.assertEquals(APPLICATION_ID, apps.iterator().next().getId());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 14 with Membership

use of io.gravitee.repository.management.model.Membership in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_FindByUserTest method shouldNotFindByUserBecauseOfArchived.

@Test
public void shouldNotFindByUserBecauseOfArchived() throws Exception {
    when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getStatus()).thenReturn(ApplicationStatus.ARCHIVED);
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
    when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.emptySet());
    when(applicationRepository.findByGroups(Collections.emptyList(), ApplicationStatus.ACTIVE)).thenReturn(Collections.emptySet());
    Membership po = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
    Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
    Assert.assertNotNull(apps);
    Assert.assertTrue("should not find app", apps.isEmpty());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 15 with Membership

use of io.gravitee.repository.management.model.Membership in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_UpdateTest method shouldUpdateBecauseSameApplication.

@Test
public void shouldUpdateBecauseSameApplication() throws TechnicalException {
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(applicationRepository.findByClientId(CLIENT_ID)).thenReturn(Optional.of(application));
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getClientId()).thenReturn(CLIENT_ID);
    when(existingApplication.getClientId()).thenReturn(CLIENT_ID);
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    when(applicationRepository.update(any())).thenReturn(application);
    Membership po = new Membership(USER_NAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    final ApplicationEntity applicationEntity = applicationService.update(APPLICATION_ID, existingApplication);
    verify(applicationRepository).update(argThat(new ArgumentMatcher<Application>() {

        public boolean matches(Object argument) {
            final Application application = (Application) argument;
            return APPLICATION_NAME.equals(application.getName()) && application.getUpdatedAt() != null;
        }
    }));
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) UpdateApplicationEntity(io.gravitee.management.model.UpdateApplicationEntity) ArgumentMatcher(org.mockito.ArgumentMatcher) Membership(io.gravitee.repository.management.model.Membership) Application(io.gravitee.repository.management.model.Application) Test(org.junit.Test)

Aggregations

Membership (io.gravitee.repository.management.model.Membership)20 Test (org.junit.Test)15 UserEntity (io.gravitee.management.model.UserEntity)8 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)6 RoleEntity (io.gravitee.management.model.RoleEntity)4 MemberEntity (io.gravitee.management.model.MemberEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 Proxy (io.gravitee.definition.model.Proxy)2 io.gravitee.management.model (io.gravitee.management.model)2 ApiEntity (io.gravitee.management.model.ApiEntity)2 GroupEntity (io.gravitee.management.model.GroupEntity)2 UpdateApplicationEntity (io.gravitee.management.model.UpdateApplicationEntity)2 Application (io.gravitee.repository.management.model.Application)2 Date (java.util.Date)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 Path (io.gravitee.definition.model.Path)1 Policy (io.gravitee.definition.model.Policy)1 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)1