Search in sources :

Example 1 with CreateCredentialsRequestDTO

use of com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO in project AuthGuard by AuthGuard.

the class CreateCredentialsRequestValidatorTest method validateInvalidIdentifierFields.

@Test
void validateInvalidIdentifierFields() {
    final CreateCredentialsRequestDTO request = CreateCredentialsRequestDTO.builder().accountId("account").domain("main").addIdentifiers(UserIdentifierDTO.builder().build()).plainPassword("password").build();
    final Validator<CreateCredentialsRequestDTO> validator = Validators.getForClass(CreateCredentialsRequestDTO.class);
    final List<Violation> violations = validator.validate(request);
    assertThat(violations).containsExactlyInAnyOrder(new Violation("type", ViolationType.MISSING_REQUIRED_VALUE), new Violation("identifier", ViolationType.MISSING_REQUIRED_VALUE));
}
Also used : Violation(com.nexblocks.authguard.api.dto.validation.violations.Violation) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Example 2 with CreateCredentialsRequestDTO

use of com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO in project AuthGuard by AuthGuard.

the class AccountsApiTest method createWithCredentials.

@Test
void createWithCredentials() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenReturn(accountResponse);
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenReturn(credentialsResponse);
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Example 3 with CreateCredentialsRequestDTO

use of com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO in project AuthGuard by AuthGuard.

the class AccountsApiTest method createWithCredentialsAccountExists.

@Test
void createWithCredentialsAccountExists() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(accountResponse.getId()).build())));
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenReturn(credentialsResponse);
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CompletionException(java.util.concurrent.CompletionException) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Example 4 with CreateCredentialsRequestDTO

use of com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO in project AuthGuard by AuthGuard.

the class CredentialsRoute method create.

public void create(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateCredentialsRequestDTO request = credentialsRequestBodyHandler.getValidated(context);
    if (!ActorDomainVerifier.verifyActorDomain(context, request.getDomain())) {
        return;
    }
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final CredentialsBO credentials = restMapper.toBO(request);
    final List<UserIdentifierBO> identifiers = credentials.getIdentifiers().stream().map(identifier -> identifier.withDomain(request.getDomain())).collect(Collectors.toList());
    final Optional<CredentialsDTO> created = Optional.of(credentials.withIdentifiers(identifiers)).map(credentialsBO -> credentialsService.create(credentialsBO, requestContext)).map(restMapper::toDTO);
    if (created.isPresent()) {
        context.status(201).json(created.get());
    } else {
        context.status(400).json(new Error("400", "Failed to create credentials"));
    }
}
Also used : CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) Inject(com.google.inject.Inject) UserIdentifiersRequestDTO(com.nexblocks.authguard.api.dto.requests.UserIdentifiersRequestDTO) Violation(com.nexblocks.authguard.api.dto.validation.violations.Violation) CredentialsService(com.nexblocks.authguard.service.CredentialsService) CredentialsApi(com.nexblocks.authguard.api.routes.CredentialsApi) UserIdentifierDTO(com.nexblocks.authguard.api.dto.entities.UserIdentifierDTO) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) ActorDomainVerifier(com.nexblocks.authguard.rest.access.ActorDomainVerifier) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) PasswordResetRequestDTO(com.nexblocks.authguard.api.dto.requests.PasswordResetRequestDTO) RequestValidationException(com.nexblocks.authguard.rest.exceptions.RequestValidationException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AuthGuardRoles(com.nexblocks.authguard.api.access.AuthGuardRoles) PasswordResetTokenRequestDTO(com.nexblocks.authguard.api.dto.requests.PasswordResetTokenRequestDTO) Collectors(java.util.stream.Collectors) List(java.util.List) BodyHandler(com.nexblocks.authguard.rest.util.BodyHandler) Error(com.nexblocks.authguard.api.dto.entities.Error) RestJsonMapper(com.nexblocks.authguard.rest.mappers.RestJsonMapper) Optional(java.util.Optional) Collections(java.util.Collections) ViolationType(com.nexblocks.authguard.api.dto.validation.violations.ViolationType) CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) Error(com.nexblocks.authguard.api.dto.entities.Error) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO)

Example 5 with CreateCredentialsRequestDTO

use of com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO in project AuthGuard by AuthGuard.

the class AccountsApiTest method createWithCredentialsAllExist.

@Test
void createWithCredentialsAllExist() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(accountResponse.getId()).build())));
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(credentialsResponse.getId()).build())));
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CompletionException(java.util.concurrent.CompletionException) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Aggregations

CreateCredentialsRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO)8 Test (org.junit.jupiter.api.Test)7 Violation (com.nexblocks.authguard.api.dto.validation.violations.Violation)4 CredentialsBO (com.nexblocks.authguard.service.model.CredentialsBO)4 ValidatableResponse (io.restassured.response.ValidatableResponse)4 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)3 CreateCompleteAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO)3 CreateCompleteAccountResponseDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO)3 AccountBO (com.nexblocks.authguard.service.model.AccountBO)3 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)3 CredentialsDTO (com.nexblocks.authguard.api.dto.entities.CredentialsDTO)2 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)2 CompletionException (java.util.concurrent.CompletionException)2 Inject (com.google.inject.Inject)1 AuthGuardRoles (com.nexblocks.authguard.api.access.AuthGuardRoles)1 Error (com.nexblocks.authguard.api.dto.entities.Error)1 UserIdentifierDTO (com.nexblocks.authguard.api.dto.entities.UserIdentifierDTO)1 PasswordResetRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordResetRequestDTO)1 PasswordResetTokenRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordResetTokenRequestDTO)1 UserIdentifiersRequestDTO (com.nexblocks.authguard.api.dto.requests.UserIdentifiersRequestDTO)1