Search in sources :

Example 11 with AuthRequestBO

use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.

the class AuthRouteTest method authenticate.

@Test
void authenticate() {
    final AuthRequestDTO requestDTO = randomObject(AuthRequestDTO.class);
    final AuthRequestBO requestBO = restMapper.toBO(requestDTO);
    final AuthResponseBO tokensBO = AuthResponseBO.builder().token("token").build();
    final AuthResponseDTO tokensDTO = mapper().toDTO(tokensBO);
    Mockito.when(authenticationService.authenticate(Mockito.eq(requestBO), Mockito.any())).thenReturn(Optional.of(tokensBO));
    final ValidatableResponse httpResponse = given().body(requestDTO).post(url("authenticate")).then().statusCode(200).contentType(ContentType.JSON);
    final AuthResponseDTO responseBody = httpResponse.extract().response().body().as(AuthResponseDTO.class);
    assertThat(responseBody).isEqualTo(tokensDTO);
}
Also used : ValidatableResponse(io.restassured.response.ValidatableResponse) AuthResponseDTO(com.nexblocks.authguard.api.dto.entities.AuthResponseDTO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 12 with AuthRequestBO

use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImplTest method generateFromBasicAuth.

@Test
void generateFromBasicAuth() {
    final AuthRequestBO authRequest = AuthRequestBO.builder().identifier("username").password("password").build();
    final AccountBO account = AccountBO.builder().id("account").build();
    Mockito.when(basicAuthProvider.getAccount(authRequest)).thenReturn(Either.right(account));
    Mockito.when(accountTokensRepository.save(Mockito.any())).thenReturn(CompletableFuture.completedFuture(null));
    final Try<ActionTokenBO> actual = actionTokenService.generateFromBasicAuth(authRequest, "something");
    final ActionTokenBO expected = ActionTokenBO.builder().accountId(account.getId()).validFor(Duration.ofMinutes(5).toSeconds()).build();
    assertThat(actual.isSuccess());
    assertThat(actual.get()).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(actual.get().getToken()).isNotNull();
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) Test(org.junit.jupiter.api.Test)

Example 13 with AuthRequestBO

use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.

the class AuthenticationServiceImplTest method authenticateLockedAccount.

@Test
void authenticateLockedAccount() {
    final String username = "username";
    final String password = "password";
    final AuthRequestBO authRequest = AuthRequestBO.builder().identifier(username).password(password).build();
    final AuthResponseBO tokens = RANDOM.nextObject(AuthResponseBO.class);
    final RequestContextBO requestContext = RequestContextBO.builder().build();
    Mockito.when(exchangeService.exchange(authRequest, "basic", "accessToken", requestContext)).thenReturn(tokens);
    Mockito.when(accountLocksService.getActiveLocksByAccountId(tokens.getEntityId())).thenReturn(Collections.singleton(AccountLockBO.builder().build()));
    assertThatThrownBy(() -> authenticationService.authenticate(authRequest, requestContext)).isInstanceOf(ServiceAuthorizationException.class).hasFieldOrPropertyWithValue("errorCode", ErrorCode.ACCOUNT_IS_LOCKED.getCode());
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 14 with AuthRequestBO

use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.

the class AuthenticationServiceImplTest method authenticate.

@Test
void authenticate() {
    final String username = "username";
    final String password = "password";
    final AuthRequestBO authRequest = AuthRequestBO.builder().identifier(username).password(password).build();
    final AuthResponseBO tokens = RANDOM.nextObject(AuthResponseBO.class);
    final RequestContextBO requestContext = RequestContextBO.builder().build();
    Mockito.when(exchangeService.exchange(authRequest, "basic", "accessToken", requestContext)).thenReturn(tokens);
    Mockito.when(accountLocksService.getActiveLocksByAccountId(tokens.getEntityId())).thenReturn(Collections.emptyList());
    final Optional<AuthResponseBO> result = authenticationService.authenticate(authRequest, requestContext);
    assertThat(result).isPresent().contains(tokens);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Aggregations

AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)14 Test (org.junit.jupiter.api.Test)13 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)11 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)6 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)6 AccountBO (com.nexblocks.authguard.service.model.AccountBO)5 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)4 AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)2 ActionTokenBO (com.nexblocks.authguard.service.model.ActionTokenBO)2 AuthResponseDTO (com.nexblocks.authguard.api.dto.entities.AuthResponseDTO)1 ActionTokenRequestDTO (com.nexblocks.authguard.api.dto.requests.ActionTokenRequestDTO)1 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)1 OneTimePasswordDO (com.nexblocks.authguard.dal.model.OneTimePasswordDO)1 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)1 ValidatableResponse (io.restassured.response.ValidatableResponse)1 HashSet (java.util.HashSet)1