Example #1
0
def validate(beneficiary_pre_subscription: BeneficiaryPreSubscription, preexisting_account: User = None) -> None:
    _check_department_is_eligible(beneficiary_pre_subscription)
    if not preexisting_account:
        _check_email_is_not_taken(beneficiary_pre_subscription)
    else:
        if preexisting_account.isBeneficiary or not preexisting_account.isEmailValidated:
            raise BeneficiaryIsADuplicate(f"Email {beneficiary_pre_subscription.email} is already taken.")
    _check_not_a_duplicate(beneficiary_pre_subscription)
Example #2
0
def _check_not_a_duplicate(beneficiary_pre_subscription: BeneficiaryPreSubscription) -> None:
    duplicates = get_beneficiary_duplicates(
        first_name=beneficiary_pre_subscription.first_name,
        last_name=beneficiary_pre_subscription.last_name,
        date_of_birth=beneficiary_pre_subscription.date_of_birth,
    )

    if duplicates:
        raise BeneficiaryIsADuplicate(f"User with id {duplicates[0].id} is a duplicate.")
def test_calls_send_rejection_mail_with_validation_error(
    mocked_send_rejection_email_to_beneficiary_pre_subscription, stubed_validate, app
):
    # Given
    application_id = 35
    error = BeneficiaryIsADuplicate("Some reason")
    stubed_validate.side_effect = error
    pre_subscription = BeneficiaryPreSubscription(
        application_id=application_id,
        postal_code=f"{application_id:02d}123",
        **PRE_SUBSCRIPTION_BASE_DATA,
    )

    # When
    create_beneficiary_from_application.execute(application_id)

    # Then
    mocked_send_rejection_email_to_beneficiary_pre_subscription.assert_called_once_with(
        beneficiary_pre_subscription=pre_subscription,
        beneficiary_is_eligible=True,
    )
Example #4
0
def _check_email_is_not_taken(beneficiary_pre_subscription: BeneficiaryPreSubscription) -> None:
    email = beneficiary_pre_subscription.email

    if find_user_by_email(email):
        raise BeneficiaryIsADuplicate(f"Email {email} is already taken.")