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());
}
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);
}
}
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());
}
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());
}
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());
}
Aggregations