use of com.nexblocks.authguard.service.model.RequestContextBO 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());
}
use of com.nexblocks.authguard.service.model.RequestContextBO 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());
}
use of com.nexblocks.authguard.service.model.RequestContextBO in project AuthGuard by AuthGuard.
the class AuthRouteTest method authenticateUnsuccessful.
@Test
void authenticateUnsuccessful() {
final AuthRequestDTO requestDTO = randomObject(AuthRequestDTO.class);
final AuthRequestBO requestBO = restMapper.toBO(requestDTO);
final RequestContextBO requestContext = RequestContextBO.builder().build();
Mockito.when(authenticationService.authenticate(requestBO, requestContext)).thenReturn(Optional.empty());
given().body(requestDTO).post(url("authenticate")).then().statusCode(400);
}
use of com.nexblocks.authguard.service.model.RequestContextBO in project AuthGuard by AuthGuard.
the class ApplicationsRoute method create.
public void create(final Context context) {
final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
final CreateAppRequestDTO request = appRequestRequestBodyHandler.getValidated(context);
final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
final Optional<Object> created = Optional.of(restMapper.toBO(request)).map(appBO -> applicationsService.create(appBO, requestContext)).map(restMapper::toDTO);
if (created.isPresent()) {
context.status(201).json(created.get());
} else {
context.status(400).json(new Error("400", "Failed to create application"));
}
}
use of com.nexblocks.authguard.service.model.RequestContextBO in project AuthGuard by AuthGuard.
the class AuthRoute method logout.
@Override
public void logout(final Context context) {
final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
final RequestContextBO requestContext = RequestContextExtractor.extractWithoutIdempotentKey(context);
authenticationService.logout(restMapper.toBO(authenticationRequest), requestContext).ifPresentOrElse(tokens -> context.json(restMapper.toDTO(tokens)), () -> context.status(400).json(new Error("400", "Failed to log user out")));
}
Aggregations