Search in sources :

Example 11 with AccountDO

use of com.nexblocks.authguard.dal.model.AccountDO in project AuthGuard by AuthGuard.

the class AccountsServiceImplTest method revokePermissions.

@Test
void revokePermissions() {
    final AccountDO account = createAccountDO();
    final List<PermissionBO> currentPermissions = account.getPermissions().stream().map(permissionDO -> PermissionBO.builder().group(permissionDO.getGroup()).name(permissionDO.getName()).build()).collect(Collectors.toList());
    Mockito.when(accountsRepository.getById(account.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(account)));
    final List<PermissionBO> permissionsToRevoke = Arrays.asList(currentPermissions.get(0), currentPermissions.get(1));
    final AccountBO updated = accountService.revokePermissions(account.getId(), permissionsToRevoke);
    assertThat(updated).isNotEqualTo(account);
    assertThat(updated.getPermissions()).doesNotContain(permissionsToRevoke.toArray(new PermissionBO[0]));
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) MessageBus(com.nexblocks.authguard.emb.MessageBus) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Message(com.nexblocks.authguard.emb.model.Message) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) RolesService(com.nexblocks.authguard.service.RolesService) EasyRandomParameters(org.jeasy.random.EasyRandomParameters) ImmutableMap(com.google.common.collect.ImmutableMap) EasyRandom(org.jeasy.random.EasyRandom) EventType(com.nexblocks.authguard.emb.model.EventType) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) PermissionsService(com.nexblocks.authguard.service.PermissionsService) Collectors(java.util.stream.Collectors) AccountConfig(com.nexblocks.authguard.service.config.AccountConfig) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) OffsetDateTime(java.time.OffsetDateTime) AccountsRepository(com.nexblocks.authguard.dal.persistence.AccountsRepository) AccountDO(com.nexblocks.authguard.dal.model.AccountDO) ConfigContext(com.nexblocks.authguard.config.ConfigContext) AccountDO(com.nexblocks.authguard.dal.model.AccountDO) Test(org.junit.jupiter.api.Test)

Example 12 with AccountDO

use of com.nexblocks.authguard.dal.model.AccountDO in project AuthGuard by AuthGuard.

the class AccountsServiceImplTest method getByEmail.

@Test
void getByEmail() {
    final AccountBO accountBO = createAccountBO();
    final AccountDO accountDO = serviceMapper.toDO(accountBO);
    Mockito.when(accountsRepository.getByEmail(accountBO.getEmail().getEmail(), accountBO.getDomain())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountDO)));
    final Optional<AccountBO> retrieved = accountService.getByEmail(accountBO.getEmail().getEmail(), accountBO.getDomain());
    final List<PermissionBO> expectedPermissions = accountBO.getPermissions().stream().map(permission -> permission.withEntityType(null)).collect(Collectors.toList());
    assertThat(retrieved).isPresent();
    assertThat(retrieved.get()).isEqualTo(accountBO.withPermissions(expectedPermissions));
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) MessageBus(com.nexblocks.authguard.emb.MessageBus) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Message(com.nexblocks.authguard.emb.model.Message) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) RolesService(com.nexblocks.authguard.service.RolesService) EasyRandomParameters(org.jeasy.random.EasyRandomParameters) ImmutableMap(com.google.common.collect.ImmutableMap) EasyRandom(org.jeasy.random.EasyRandom) EventType(com.nexblocks.authguard.emb.model.EventType) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) PermissionsService(com.nexblocks.authguard.service.PermissionsService) Collectors(java.util.stream.Collectors) AccountConfig(com.nexblocks.authguard.service.config.AccountConfig) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) OffsetDateTime(java.time.OffsetDateTime) AccountsRepository(com.nexblocks.authguard.dal.persistence.AccountsRepository) AccountDO(com.nexblocks.authguard.dal.model.AccountDO) ConfigContext(com.nexblocks.authguard.config.ConfigContext) AccountDO(com.nexblocks.authguard.dal.model.AccountDO) Test(org.junit.jupiter.api.Test)

Example 13 with AccountDO

use of com.nexblocks.authguard.dal.model.AccountDO in project AuthGuard by AuthGuard.

the class AccountsServiceImplTest method grantRolesInvalidRoles.

@Test
void grantRolesInvalidRoles() {
    final AccountDO account = createAccountDO();
    Mockito.when(accountsRepository.getById(account.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(account)));
    Mockito.when(accountsRepository.update(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(Optional.of(invocation.getArgument(0, AccountDO.class))));
    final List<String> roles = Arrays.asList(RANDOM.nextObject(String.class), RANDOM.nextObject(String.class));
    final List<String> validRoles = Collections.singletonList(roles.get(0));
    Mockito.when(rolesService.verifyRoles(roles, "main")).thenReturn(validRoles);
    assertThatThrownBy(() -> accountService.grantRoles(account.getId(), roles)).isInstanceOf(ServiceException.class);
}
Also used : AccountDO(com.nexblocks.authguard.dal.model.AccountDO) Test(org.junit.jupiter.api.Test)

Example 14 with AccountDO

use of com.nexblocks.authguard.dal.model.AccountDO in project AuthGuard by AuthGuard.

the class AccountsServiceImplTest method grantRolesFromDifferentDomain.

@Test
void grantRolesFromDifferentDomain() {
    final AccountDO account = createAccountDO();
    Mockito.when(accountsRepository.getById(account.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(account)));
    Mockito.when(accountsRepository.update(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(Optional.of(invocation.getArgument(0, AccountDO.class))));
    final List<String> roles = Arrays.asList(RANDOM.nextObject(String.class), RANDOM.nextObject(String.class));
    Mockito.when(rolesService.verifyRoles(roles, "other")).thenReturn(roles);
    assertThatThrownBy(() -> accountService.grantRoles(account.getId(), roles)).isInstanceOf(ServiceException.class);
}
Also used : AccountDO(com.nexblocks.authguard.dal.model.AccountDO) Test(org.junit.jupiter.api.Test)

Aggregations

AccountDO (com.nexblocks.authguard.dal.model.AccountDO)14 Test (org.junit.jupiter.api.Test)13 ImmutableMap (com.google.common.collect.ImmutableMap)6 ConfigContext (com.nexblocks.authguard.config.ConfigContext)6 AccountsRepository (com.nexblocks.authguard.dal.persistence.AccountsRepository)6 MessageBus (com.nexblocks.authguard.emb.MessageBus)6 EventType (com.nexblocks.authguard.emb.model.EventType)6 Message (com.nexblocks.authguard.emb.model.Message)6 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)6 PermissionsService (com.nexblocks.authguard.service.PermissionsService)6 RolesService (com.nexblocks.authguard.service.RolesService)6 AccountConfig (com.nexblocks.authguard.service.config.AccountConfig)6 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)6 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)6 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)6 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)6 OffsetDateTime (java.time.OffsetDateTime)6 java.util (java.util)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Supplier (java.util.function.Supplier)6