Search in sources :

Example 1 with InviteCompleteResponse

use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.

the class UserInviteCompleter method complete.

@Override
@Transactional
public Optional<InviteCompleteResponse> complete(String inviteCode) {
    return inviteDao.findByCode(inviteCode).map(inviteEntity -> {
        if (inviteEntity.isExpired() || inviteEntity.isDisabled()) {
            throw inviteLockedException(inviteEntity.getCode());
        }
        return userDao.findByEmail(inviteEntity.getEmail()).map(userEntity -> {
            if (inviteEntity.getService() != null && inviteEntity.isUserType()) {
                ServiceRoleEntity serviceRole = new ServiceRoleEntity(inviteEntity.getService(), inviteEntity.getRole());
                userEntity.addServiceRole(serviceRole);
                userDao.merge(userEntity);
                inviteEntity.setDisabled(true);
                inviteDao.merge(inviteEntity);
                InviteCompleteResponse response = new InviteCompleteResponse(inviteEntity.toInvite());
                response.setUserExternalId(userEntity.getExternalId());
                response.setServiceExternalId(inviteEntity.getService().getExternalId());
                return Optional.of(response);
            } else {
                throw internalServerError(format("Attempting to complete user subscription to a service for a non existent service. invite-code = %s", inviteEntity.getCode()));
            }
        }).orElseGet(() -> {
            throw internalServerError(format("Attempting to complete user subscription to a service for a non existent user. invite-code = %s", inviteEntity.getCode()));
        });
    }).orElseGet(Optional::empty);
}
Also used : AdminUsersExceptions.inviteLockedException(uk.gov.pay.adminusers.service.AdminUsersExceptions.inviteLockedException) InviteDao(uk.gov.pay.adminusers.persistence.dao.InviteDao) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Inject(com.google.inject.Inject) AdminUsersExceptions.internalServerError(uk.gov.pay.adminusers.service.AdminUsersExceptions.internalServerError) Optional(java.util.Optional) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) String.format(java.lang.String.format) Transactional(com.google.inject.persist.Transactional) UserDao(uk.gov.pay.adminusers.persistence.dao.UserDao) Optional(java.util.Optional) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Transactional(com.google.inject.persist.Transactional)

Example 2 with InviteCompleteResponse

use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.

the class ServiceInviteCompleter method complete.

/**
 * Completes a service invite.
 * ie. it creates and persists a user from an invite or/and subscribe a user to an existing service
 * and if it is a service invite also creates a default service.
 * It then disables the invite.
 */
@Override
@Transactional
public Optional<InviteCompleteResponse> complete(String inviteCode) {
    return inviteDao.findByCode(inviteCode).map(inviteEntity -> {
        if (inviteEntity.isExpired() || inviteEntity.isDisabled()) {
            throw inviteLockedException(inviteEntity.getCode());
        }
        if (userDao.findByEmail(inviteEntity.getEmail()).isPresent()) {
            throw conflictingEmail(inviteEntity.getEmail());
        }
        if (inviteEntity.isServiceType()) {
            UserEntity userEntity = inviteEntity.mapToUserEntity();
            ServiceEntity serviceEntity = ServiceEntity.from(Service.from());
            if (!data.getGatewayAccountIds().isEmpty()) {
                serviceEntity.addGatewayAccountIds(data.getGatewayAccountIds().toArray(new String[0]));
            }
            serviceDao.persist(serviceEntity);
            ServiceRoleEntity serviceRoleEntity = new ServiceRoleEntity(serviceEntity, inviteEntity.getRole());
            userEntity.addServiceRole(serviceRoleEntity);
            userDao.merge(userEntity);
            inviteEntity.setService(serviceEntity);
            inviteEntity.setDisabled(true);
            inviteDao.merge(inviteEntity);
            Invite invite = linksBuilder.addUserLink(userEntity.toUser(), inviteEntity.toInvite());
            InviteCompleteResponse response = new InviteCompleteResponse(invite);
            response.setServiceExternalId(serviceEntity.getExternalId());
            response.setUserExternalId(userEntity.getExternalId());
            return response;
        } else {
            throw internalServerError(format("Attempting to complete a service invite for a non service invite of type. invite-code = %s", inviteEntity.getCode()));
        }
    });
}
Also used : ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Invite(uk.gov.pay.adminusers.model.Invite) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Transactional(com.google.inject.persist.Transactional)

Example 3 with InviteCompleteResponse

use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.

the class ServiceInviteCompleterTest method shouldCreateServiceAndUser_withGatewayAccounts_whenPassedValidServiceInviteCode.

@Test
public void shouldCreateServiceAndUser_withGatewayAccounts_whenPassedValidServiceInviteCode() {
    ServiceEntity service = new ServiceEntity();
    service.setId(serviceId);
    InviteEntity anInvite = createInvite();
    anInvite.setType(InviteType.SERVICE);
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
    when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
    InviteCompleteRequest data = new InviteCompleteRequest();
    data.setGatewayAccountIds(asList("1", "2"));
    InviteCompleteResponse inviteResponse = serviceInviteCompleter.withData(data).complete(anInvite.getCode()).get();
    verify(mockServiceDao).persist(expectedService.capture());
    verify(mockUserDao).merge(expectedInvitedUser.capture());
    verify(mockInviteDao).merge(expectedInvite.capture());
    ServiceEntity serviceEntity = expectedService.getValue();
    assertThat(serviceEntity.getGatewayAccountIds().stream().map(GatewayAccountIdEntity::getGatewayAccountId).collect(toUnmodifiableList()), hasItems("2", "1"));
    assertThat(serviceEntity.getServiceNames().get(SupportedLanguage.ENGLISH).getName(), is(Service.DEFAULT_NAME_VALUE));
    assertThat(serviceEntity.isRedirectToServiceImmediatelyOnTerminalState(), is(false));
    assertThat(serviceEntity.isCollectBillingAddress(), is(true));
    assertThat(serviceEntity.getDefaultBillingAddressCountry(), is("GB"));
    assertThat(inviteResponse.getInvite().isDisabled(), is(true));
    assertThat(inviteResponse.getInvite().getLinks().size(), is(1));
    assertThat(inviteResponse.getInvite().getLinks().get(0).getRel(), is(Link.Rel.USER));
    assertThat(inviteResponse.getInvite().getLinks().get(0).getHref(), matchesPattern("^" + baseUrl + "/v1/api/users/[0-9a-z]{32}$"));
}
Also used : GatewayAccountIdEntity(uk.gov.pay.adminusers.persistence.entity.GatewayAccountIdEntity) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) InviteCompleteRequest(uk.gov.pay.adminusers.model.InviteCompleteRequest) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Test(org.junit.jupiter.api.Test)

Example 4 with InviteCompleteResponse

use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.

the class ServiceInviteCompleterTest method shouldCreateServiceAndUser_withoutGatewayAccounts_whenPassedValidServiceInviteCode.

@Test
public void shouldCreateServiceAndUser_withoutGatewayAccounts_whenPassedValidServiceInviteCode() {
    ServiceEntity service = new ServiceEntity();
    service.addOrUpdateServiceName(ServiceNameEntity.from(SupportedLanguage.ENGLISH, Service.DEFAULT_NAME_VALUE));
    service.setId(serviceId);
    InviteEntity anInvite = createInvite();
    anInvite.setType(InviteType.SERVICE);
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
    when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
    InviteCompleteResponse inviteResponse = serviceInviteCompleter.withData(new InviteCompleteRequest()).complete(anInvite.getCode()).get();
    verify(mockServiceDao).persist(expectedService.capture());
    verify(mockUserDao).merge(expectedInvitedUser.capture());
    verify(mockInviteDao).merge(expectedInvite.capture());
    assertThat(expectedService.getValue().getGatewayAccountIds().isEmpty(), is(true));
    ServiceEntity serviceEntity = expectedService.getValue();
    assertThat(serviceEntity.getGatewayAccountIds().isEmpty(), is(true));
    assertThat(serviceEntity.getServiceNames().get(SupportedLanguage.ENGLISH).getName(), is(Service.DEFAULT_NAME_VALUE));
    assertThat(serviceEntity.isRedirectToServiceImmediatelyOnTerminalState(), is(false));
    assertThat(serviceEntity.isCollectBillingAddress(), is(true));
    assertThat(serviceEntity.getDefaultBillingAddressCountry(), is("GB"));
    assertThat(inviteResponse.getInvite().isDisabled(), is(true));
    assertThat(inviteResponse.getInvite().getLinks().size(), is(1));
    assertThat(inviteResponse.getInvite().getLinks().get(0).getRel(), is(Link.Rel.USER));
    assertThat(inviteResponse.getInvite().getLinks().get(0).getHref(), matchesPattern("^" + baseUrl + "/v1/api/users/[0-9a-z]{32}$"));
}
Also used : InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) InviteCompleteRequest(uk.gov.pay.adminusers.model.InviteCompleteRequest) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Test(org.junit.jupiter.api.Test)

Example 5 with InviteCompleteResponse

use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.

the class UserInviteCompleterTest method shouldSuccess_whenSubscribingAServiceToAnExistingUser_forValidInvite.

@Test
public void shouldSuccess_whenSubscribingAServiceToAnExistingUser_forValidInvite() {
    ServiceEntity service = new ServiceEntity();
    service.setId(serviceId);
    service.setExternalId(serviceExternalId);
    InviteEntity anInvite = createInvite();
    anInvite.setType(InviteType.USER);
    anInvite.setService(service);
    UserEntity user = UserEntity.from(aUser(anInvite.getEmail()));
    when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.of(user));
    Optional<InviteCompleteResponse> completedInvite = userInviteCompleter.complete(inviteCode);
    ArgumentCaptor<UserEntity> persistedUser = ArgumentCaptor.forClass(UserEntity.class);
    verify(mockUserDao).merge(persistedUser.capture());
    assertThat(completedInvite.isPresent(), is(true));
    assertThat(completedInvite.get().getInvite().isDisabled(), is(true));
    assertThat(persistedUser.getValue().getServicesRole(service.getExternalId()).isPresent(), is(true));
}
Also used : InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Test(org.junit.jupiter.api.Test)

Aggregations

InviteCompleteResponse (uk.gov.pay.adminusers.model.InviteCompleteResponse)5 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)4 Test (org.junit.jupiter.api.Test)3 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)3 Transactional (com.google.inject.persist.Transactional)2 InviteCompleteRequest (uk.gov.pay.adminusers.model.InviteCompleteRequest)2 ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)2 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)2 Inject (com.google.inject.Inject)1 String.format (java.lang.String.format)1 Optional (java.util.Optional)1 Invite (uk.gov.pay.adminusers.model.Invite)1 InviteDao (uk.gov.pay.adminusers.persistence.dao.InviteDao)1 UserDao (uk.gov.pay.adminusers.persistence.dao.UserDao)1 GatewayAccountIdEntity (uk.gov.pay.adminusers.persistence.entity.GatewayAccountIdEntity)1 AdminUsersExceptions.internalServerError (uk.gov.pay.adminusers.service.AdminUsersExceptions.internalServerError)1 AdminUsersExceptions.inviteLockedException (uk.gov.pay.adminusers.service.AdminUsersExceptions.inviteLockedException)1