Search in sources :

Example 1 with ApiKeyDO

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

the class ApiKeysServiceImplTest method getByAppId.

@Test
void getByAppId() {
    final String appId = "app";
    final String key = "key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().appId(appId).key(key).build();
    final ApiKeyDO apiKeyDO = serviceMapper.toDO(apiKeyBO);
    Mockito.when(apiKeysRepository.getByAppId(appId)).thenReturn(CompletableFuture.completedFuture(Collections.singletonList(apiKeyDO)));
    final List<ApiKeyBO> actual = apiKeysService.getByAppId(appId);
    assertThat(actual).isEqualTo(Collections.singletonList(apiKeyBO));
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Example 2 with ApiKeyDO

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

the class DefaultApiKeyExchangeTest method verifyAndGetAppId.

@Test
void verifyAndGetAppId() {
    final String key = "key";
    final String hashedKey = apiKeyHashProvider.getHash().hash(key);
    final String appId = "app";
    final ApiKeyDO apiKeyDO = ApiKeyDO.builder().key(hashedKey).appId(appId).build();
    Mockito.when(repository.getByKey(hashedKey)).thenReturn(CompletableFuture.completedFuture(Optional.of(apiKeyDO)));
    final Optional<String> retrieved = exchange.verifyAndGetAppId(key).join();
    assertThat(retrieved).contains(appId);
}
Also used : ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Example 3 with ApiKeyDO

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

the class ApiKeysServiceImplTest method getById.

@Test
void getById() {
    final String id = "api_key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().id(id).build();
    final ApiKeyDO apiKeyDO = ApiKeyDO.builder().id(id).build();
    Mockito.when(apiKeysRepository.getById(id)).thenReturn(CompletableFuture.completedFuture(Optional.of(apiKeyDO)));
    final Optional<ApiKeyBO> actual = apiKeysService.getById(id);
    assertThat(actual).contains(apiKeyBO);
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Example 4 with ApiKeyDO

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

the class ApiKeysServiceImplTest method generateApiKey.

@Test
void generateApiKey() {
    final String appId = "app";
    final String key = "key";
    final AppBO app = AppBO.builder().id(appId).build();
    Mockito.when(applicationsService.getById(appId)).thenReturn(Optional.of(app));
    Mockito.when(apiKeysRepository.save(Mockito.any())).thenAnswer(invocation -> CompletableFuture.completedFuture(invocation.getArgument(0, ApiKeyDO.class)));
    Mockito.when(apiKeyExchange.generateKey(app)).thenReturn(AuthResponseBO.builder().token(key).build());
    final ApiKeyBO actual = apiKeysService.generateApiKey(appId);
    assertThat(actual.getAppId()).isEqualTo(appId);
    assertThat(actual.getKey()).isEqualTo(key);
    // verify that we persisted the hashed key and not the clear key
    final ArgumentCaptor<ApiKeyDO> argumentCaptor = ArgumentCaptor.forClass(ApiKeyDO.class);
    Mockito.verify(apiKeysRepository).save(argumentCaptor.capture());
    final ApiKeyDO persisted = argumentCaptor.getValue();
    assertThat(persisted.getKey()).isEqualTo(apiKeyHash.hash(key));
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) AppBO(com.nexblocks.authguard.service.model.AppBO) Test(org.junit.jupiter.api.Test)

Example 5 with ApiKeyDO

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

the class ApiKeysServiceImplTest method delete.

@Test
void delete() {
    final String id = "api_key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().id(id).build();
    final ApiKeyDO apiKeyDO = ApiKeyDO.builder().id(id).build();
    Mockito.when(apiKeysRepository.delete(id)).thenReturn(CompletableFuture.completedFuture(Optional.of(apiKeyDO)));
    final Optional<ApiKeyBO> actual = apiKeysService.delete(id);
    assertThat(actual).contains(apiKeyBO);
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Aggregations

ApiKeyDO (com.nexblocks.authguard.dal.model.ApiKeyDO)5 Test (org.junit.jupiter.api.Test)5 ApiKeyBO (com.nexblocks.authguard.service.model.ApiKeyBO)4 AppBO (com.nexblocks.authguard.service.model.AppBO)1