Search in sources :

Example 1 with AccountDO

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

the class AccountsServiceImplTest method grantPermissionsFromDifferentDomain.

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

Example 2 with AccountDO

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

the class AccountsServiceImplTest method patch.

@Test
void patch() {
    final AccountBO accountBO = createAccountBO().withCreatedAt(OffsetDateTime.now()).withLastModified(OffsetDateTime.now());
    final AccountBO update = AccountBO.builder().firstName("first_name").middleName("middle_name").lastName("last_name").phoneNumber(PhoneNumberBO.builder().number("new_number").build()).email(AccountEmailBO.builder().email("new_primary").build()).backupEmail(AccountEmailBO.builder().email("new_backup").build()).build();
    final AccountDO accountDO = serviceMapper.toDO(accountBO);
    Mockito.when(accountsRepository.getById(accountDO.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountDO)));
    Mockito.when(accountsRepository.update(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(Optional.of(invocation.getArgument(0, AccountDO.class))));
    final Optional<AccountBO> updated = accountService.patch(accountDO.getId(), update);
    final AccountBO expected = accountBO.withFirstName(update.getFirstName()).withMiddleName(update.getMiddleName()).withLastName(update.getLastName()).withPhoneNumber(update.getPhoneNumber()).withEmail(update.getEmail()).withBackupEmail(update.getBackupEmail());
    final List<PermissionBO> expectedPermissions = accountBO.getPermissions().stream().map(permission -> permission.withEntityType(null)).collect(Collectors.toList());
    assertThat(updated).isPresent();
    assertThat(updated.get()).isEqualToIgnoringGivenFields(expected.withPermissions(expectedPermissions), "lastModified");
    assertThat(updated.get().getLastModified()).isAfter(expected.getLastModified());
    // need better assertion
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("accounts"), any());
    Mockito.verify(messageBus, Mockito.times(3)).publish(eq("verification"), any());
}
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 3 with AccountDO

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

the class AccountsServiceImplTest method grantRoles.

@Test
void grantRoles() {
    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, "main")).thenReturn(roles);
    final AccountBO updated = accountService.grantRoles(account.getId(), roles);
    assertThat(updated).isNotEqualTo(account);
    assertThat(updated.getRoles()).contains(roles.toArray(new String[0]));
}
Also used : AccountDO(com.nexblocks.authguard.dal.model.AccountDO) Test(org.junit.jupiter.api.Test)

Example 4 with AccountDO

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

the class AccountsServiceImplTest method activateAccount.

@Test
void activateAccount() {
    final AccountBO accountBO = createAccountBO();
    final AccountDO accountDO = serviceMapper.toDO(accountBO);
    Mockito.when(accountsRepository.getById(accountDO.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountDO)));
    Mockito.when(accountsRepository.update(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(Optional.of(invocation.getArgument(0, AccountDO.class))));
    final AccountBO updated = accountService.activate(accountDO.getId()).orElse(null);
    final List<PermissionBO> expectedPermissions = accountBO.getPermissions().stream().map(permission -> permission.withEntityType(null)).collect(Collectors.toList());
    assertThat(updated).isNotNull();
    assertThat(updated).isEqualToIgnoringGivenFields(accountBO.withPermissions(expectedPermissions), "id", "createdAt", "lastModified", "active");
    assertThat(updated.isActive()).isTrue();
}
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 5 with AccountDO

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

the class AccountsServiceImplTest method revokeRoles.

@Test
void revokeRoles() {
    final AccountDO account = createAccountDO();
    final List<String> currentRoles = new ArrayList<>(account.getRoles());
    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> rolesToRevoke = Arrays.asList(currentRoles.get(0), currentRoles.get(1));
    final AccountBO updated = accountService.revokeRoles(account.getId(), rolesToRevoke);
    assertThat(updated).isNotEqualTo(account);
    assertThat(updated.getRoles()).doesNotContain(rolesToRevoke.toArray(new String[0]));
}
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