def test_doesnt_raise_if_no_exact_duplicate(app):
    # Given
    first_name = "John"
    last_name = "Doe"
    date_of_birth = datetime(1993, 2, 2)
    users_factories.UserFactory(firstName="Joe",
                                lastName=last_name,
                                dateOfBirth=date_of_birth,
                                email="*****@*****.**")
    users_factories.UserFactory(firstName=first_name,
                                lastName="Dane",
                                dateOfBirth=date_of_birth,
                                email="*****@*****.**")
    users_factories.UserFactory(firstName=first_name,
                                lastName=last_name,
                                dateOfBirth=datetime(1992, 2, 2),
                                email="*****@*****.**")

    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        first_name=first_name,
        last_name=last_name,
        date_of_birth=date_of_birth)

    try:
        # When
        validate(beneficiary_pre_subcription)
    except CantRegisterBeneficiary:
        # Then
        assert pytest.fail(
            "Should not raise an exception when email not given")
 def test_beneficiary_is_duplicate(self):
     first_name = "John"
     last_name = "Doe"
     users_factories.BeneficiaryGrant18Factory.create_batch(
         4, firstName=first_name, lastName=last_name)
     beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
         first_name=first_name, last_name=last_name)
     with pytest.raises(BeneficiaryIsADuplicate):
         validate(beneficiary_pre_subcription)
def test_validates_for_non_beneficiary_with_same_mail(app):
    email = "*****@*****.**"
    existing_user = users_factories.UserFactory(email=email,
                                                isEmailValidated=True)

    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        email=email)

    # Should not raise an exception
    validate(beneficiary_pre_subcription, preexisting_account=existing_user)
def test_should_not_raise_if_eligible(postal_code):
    # Given
    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        postal_code=postal_code)

    try:
        # When
        validate(beneficiary_pre_subcription)
    except CantRegisterBeneficiary:
        # Then
        assert pytest.fail("Should not raise when postal code is eligible")
def test_raises_if_not_eligible(postal_code):
    # Given
    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        postal_code=postal_code)

    # When
    with pytest.raises(BeneficiaryIsNotEligible) as error:
        validate(beneficiary_pre_subcription)

    # Then
    assert str(error.value) == f"Postal code {postal_code} is not eligible."
def test_should_not_raise_exception_for_valid_beneficiary(app):
    # Given
    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory()

    try:
        # When
        validate(beneficiary_pre_subcription)
    except CantRegisterBeneficiary:
        # Then
        assert pytest.fail(
            "Should not raise an exception when email not given")
    def test_duplicate_current_rule_still_exist(self):
        first_name = "John"
        last_name = "Doe"
        existing_user = users_factories.BeneficiaryGrant18Factory(
            firstName=first_name, lastName=last_name)

        beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
            first_name=first_name,
            last_name=last_name,
            date_of_birth=existing_user.dateOfBirth)
        with pytest.raises(BeneficiaryIsADuplicate):
            validate(beneficiary_pre_subcription)
    def when_beneficiary_is_not_eligible_sends_correct_template_sendinblue(
            self, mocked_make_data, app):
        # given
        beneficiary_pre_subscription = BeneficiaryPreSubscriptionFactory()

        # when
        send_rejection_email_to_beneficiary_pre_subscription(
            beneficiary_pre_subscription, beneficiary_is_eligible=False)

        # then
        mocked_make_data.assert_called_once()
        assert len(mails_testing.outbox) == 1  # test number of emails sent
        assert mails_testing.outbox[0].sent_data["MJ-TemplateID"] == 1619528
def test_doesnt_raise_if_email_not_taken(app):
    # Given
    users_factories.UserFactory(email="*****@*****.**")

    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        email="*****@*****.**")

    try:
        # When
        validate(beneficiary_pre_subcription)
    except CantRegisterBeneficiary:
        # Then
        assert pytest.fail(
            "Should not raise an exception when email not given")
    def when_beneficiary_is_a_duplicate_sends_correct_template_sendinblue(
            self, app):
        # given
        beneficiary_pre_subscription = BeneficiaryPreSubscriptionFactory()

        # when
        send_rejection_email_to_beneficiary_pre_subscription(
            beneficiary_pre_subscription, beneficiary_is_eligible=True)

        # then
        assert len(mails_testing.outbox) == 1  # test number of emails sent
        assert mails_testing.outbox[0].sent_data["template"] == asdict(
            TransactionalEmail.
            EMAIL_DUPLICATE_BENEFICIARY_PRE_SUBCRIPTION_REJECTED.value)
def test_raises_if_email_already_taken_by_beneficiary(app):
    # Given
    email = "*****@*****.**"
    existing_user = users_factories.BeneficiaryGrant18Factory(email=email)

    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        email=email)

    # When
    with pytest.raises(BeneficiaryIsADuplicate) as error:
        validate(beneficiary_pre_subcription,
                 preexisting_account=existing_user)

    # Then
    assert str(error.value) == f"Email {email} is already taken."
 def test_duplicate_jouve_new_rule_date_range(self):
     first_name = "John"
     last_name = "Doe"
     users_factories.BeneficiaryGrant18Factory.create_batch(
         2,
         firstName=first_name,
         lastName=last_name,
         dateCreated=datetime.now() - timedelta(days=100))
     users_factories.BeneficiaryGrant18Factory.create_batch(
         2,
         firstName=first_name,
         lastName=last_name,
         dateCreated=datetime.now() - timedelta(days=100))
     beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
         first_name=first_name, last_name=last_name)
     validate(beneficiary_pre_subcription)
def test_raises_if_duplicate(app):
    # Given
    first_name = "John"
    last_name = "Doe"
    existing_user = users_factories.BeneficiaryGrant18Factory(
        firstName=first_name, lastName=last_name)

    beneficiary_pre_subcription = BeneficiaryPreSubscriptionFactory(
        first_name=first_name,
        last_name=last_name,
        date_of_birth=existing_user.dateOfBirth)

    # When
    with pytest.raises(BeneficiaryIsADuplicate) as error:
        validate(beneficiary_pre_subcription)

    # Then
    assert str(
        error.value) == f"User with id {existing_user.id} is a duplicate."