def test_recredit_around_the_year(self):
        with freeze_time("2015-05-01"):
            user_activated_at_15 = user_factories.UnderageBeneficiaryFactory(subscription_age=15)
            user_activated_at_16 = user_factories.UnderageBeneficiaryFactory(subscription_age=16)
            user_activated_at_17 = user_factories.UnderageBeneficiaryFactory(subscription_age=17)

        assert user_activated_at_15.deposit.amount == 20
        assert user_activated_at_16.deposit.amount == 30
        assert user_activated_at_17.deposit.amount == 30

        # recredit the following year
        with freeze_time("2016-05-01"):
            recredit_underage_users()

        assert user_activated_at_15.deposit.amount == 50
        assert user_activated_at_16.deposit.amount == 60
        assert user_activated_at_17.deposit.amount == 30

        # recrediting the day after does not affect the amount
        with freeze_time("2016-05-02"):
            recredit_underage_users()

        assert user_activated_at_15.deposit.amount == 50
        assert user_activated_at_16.deposit.amount == 60
        assert user_activated_at_17.deposit.amount == 30

        # recredit the following year
        with freeze_time("2017-05-01"):
            recredit_underage_users()

        assert user_activated_at_15.deposit.amount == 80
        assert user_activated_at_16.deposit.amount == 60
        assert user_activated_at_17.deposit.amount == 30
Example #2
0
    def test_return_correct_data_for_native_age_17_user_v2(self):
        # Given
        user = users_factories.UnderageBeneficiaryFactory(firstName="Fabien", subscription_age=17)

        # When
        data = get_accepted_as_underage_beneficiary_email_data(user)

        # Then
        assert data.params["FIRSTNAME"] == "Fabien"
        assert data.params["CREDIT"] == 30
Example #3
0
    def test_deposit_created_when_another_type_already_exist_for_user(self):
        birth_date = datetime.combine(datetime.utcnow(), time(
            0, 0)) - relativedelta(years=18, months=4)
        with freeze_time(datetime.utcnow() - relativedelta(years=3)):
            beneficiary = users_factories.UnderageBeneficiaryFactory(
                dateOfBirth=birth_date)

        api.create_deposit(beneficiary, "created by test")

        assert beneficiary.deposit.type == DepositType.GRANT_18
        assert len(beneficiary.deposits) == 2
    def test_underage_user_validation_is_beneficiary(self, age):
        user = users_factories.UnderageBeneficiaryFactory()
        fraud_check = fraud_factories.BeneficiaryFraudCheckFactory(
            type=fraud_models.FraudCheckType.EDUCONNECT,
            user=user,
            resultContent=fraud_factories.EduconnectContentFactory(age=age),
        )
        fraud_result = fraud_api.on_identity_fraud_check_result(
            user, fraud_check)

        assert (
            "L’utilisateur est déjà bénéfiaire, avec un portefeuille non expiré. Il ne peut pas prétendre au pass culture 15-17 ans"
            in fraud_result.reason)
        assert fraud_result.status == fraud_models.FraudStatus.KO
Example #5
0
    def test_wallet_balance_expired_deposit(self):
        with freeze_time(datetime.utcnow() - relativedelta(years=2, days=2)):
            user = users_factories.UnderageBeneficiaryFactory(
                subscription_age=16)
            # disable trigger because deposit.expirationDate > now() is False in database time
            db.session.execute(
                "ALTER TABLE booking DISABLE TRIGGER booking_update;")
            bookings_factories.IndividualBookingFactory(
                individualBooking__user=user, amount=18)
            db.session.execute(
                "ALTER TABLE booking ENABLE TRIGGER booking_update;")

        assert db.session.query(func.get_wallet_balance(user.id,
                                                        False)).first()[0] == 0
Example #6
0
def create_industrial_app_underage_beneficiaries():
    if not FeatureToggle.ENABLE_NATIVE_EAC_INDIVIDUAL.is_active():
        logger.warning("cannot create underage beneficiaries when the flag ENABLE_NATIVE_EAC_INDIVIDUAL OFF")
        return {}

    logger.info("create_industrial_app_underage_beneficiaries")

    users_by_name = {}

    variants = itertools.product(DEPARTEMENT_CODES, UNDERAGE_BENEFICIARIES_TAGS)

    for index, (departement_code, tag) in enumerate(variants, start=0):
        short_tag = "".join([chunk[0].upper() for chunk in tag.split("-")])

        email = f"pctest.mineur{departement_code}.{tag}@example.com"

        if tag == "15-years-old-underage-beneficiary":
            age = 15
        elif tag == "16-years-old-underage-beneficiary":
            age = 16
        else:
            age = 17

        user = users_factories.UnderageBeneficiaryFactory(
            subscription_age=age,
            culturalSurveyId=None,
            departementCode=str(departement_code),
            email=email,
            phoneNumber=f"+336{index:0>8}",
            firstName="PC Test Mineur",
            hasSeenTutorials=True,
            lastName=f"{departement_code} {short_tag}",
            needsToFillCulturalSurvey=False,
            postalCode="{}100".format(departement_code),
            publicName=f"PC Test Mineur {departement_code} {short_tag}",
            deposit__source="sandbox",
        )

        user_key = f"jeune{departement_code} {tag}"
        users_by_name[user_key] = user

    logger.info("created %d underage beneficiaries", len(users_by_name))

    return users_by_name
    def test_duplicates_fraud_checks(self):
        already_existing_user = users_factories.UnderageBeneficiaryFactory(
            subscription_age=15)
        fraud_check = fraud_factories.BeneficiaryFraudCheckFactory(
            type=fraud_models.FraudCheckType.EDUCONNECT,
            resultContent=fraud_factories.EduconnectContentFactory(
                first_name=already_existing_user.firstName,
                last_name=already_existing_user.lastName,
                birth_date=already_existing_user.dateOfBirth,
            ),
        )
        result = fraud_api.educonnect_fraud_checks(fraud_check)

        duplicate_check = next(fraud_check for fraud_check in result
                               if fraud_check.reason_code ==
                               fraud_models.FraudReasonCode.DUPLICATE_USER)

        assert duplicate_check.status == fraud_models.FraudStatus.SUSPICIOUS
        assert duplicate_check.detail == f"Duplicat de l'utilisateur {already_existing_user.id}"
    def test_ine_duplicates_fraud_checks(self):
        fraud_factories.IneHashWhitelistFactory(
            ine_hash="ylwavk71o3jiwyla83fxk5pcmmu0ws01")
        same_ine_user = users_factories.UnderageBeneficiaryFactory(
            ineHash="ylwavk71o3jiwyla83fxk5pcmmu0ws01")
        fraud_check = fraud_factories.BeneficiaryFraudCheckFactory(
            type=fraud_models.FraudCheckType.EDUCONNECT,
            resultContent=fraud_factories.EduconnectContentFactory(
                ine_hash=same_ine_user.ineHash),
        )
        result = fraud_api.educonnect_fraud_checks(fraud_check)

        duplicate_ine_check = next(fraud_check for fraud_check in result
                                   if fraud_check.reason_code ==
                                   fraud_models.FraudReasonCode.DUPLICATE_INE)
        assert duplicate_ine_check.status == fraud_models.FraudStatus.SUSPICIOUS
        assert (
            duplicate_ine_check.detail ==
            f"L'INE ylwavk71o3jiwyla83fxk5pcmmu0ws01 est déjà pris par l'utilisateur {same_ine_user.id}"
        )
def create_underage_beneficiary() -> None:
    users_factories.UnderageBeneficiaryFactory(
        email="*****@*****.**",
        deposit__source="sandbox",
    )
    logger.info("created 1 underage beneficiary")
Example #10
0
def create_short_email_beneficiaries() -> dict:
    fake = Faker("fr_FR")
    users = []
    is_eac_active = FeatureToggle.ENABLE_NATIVE_EAC_INDIVIDUAL.is_active()

    if is_eac_active:
        for age in [15, 16, 17]:
            users.append(
                users_factories.UnderageBeneficiaryFactory(
                    email=f"bene_{age}@example.com",
                    subscription_age=age,
                    firstName=fake.first_name(),
                    lastName=fake.last_name(),
                    needsToFillCulturalSurvey=False,
                )
            )
    for age in [15, 16, 17, 18]:
        users.append(
            users_factories.UserFactory(
                email=f"eli_{age}@example.com",
                address=None,
                city=None,
                dateOfBirth=datetime.combine(date.today(), time(0, 0)) - relativedelta(years=age, months=5),
                departementCode=None,
                firstName=None,
                lastName=None,
                postalCode=None,
                needsToFillCulturalSurvey=False,
            )
        )

    users.append(
        users_factories.BeneficiaryGrant18Factory(
            email="*****@*****.**",
            firstName=fake.first_name(),
            lastName=fake.last_name(),
            needsToFillCulturalSurvey=False,
        )
    )
    if is_eac_active:
        with freeze_time(datetime.utcnow() - relativedelta(years=3)):
            users.append(
                users_factories.UnderageBeneficiaryFactory(
                    email="*****@*****.**",
                    dateOfBirth=datetime.combine(date.today(), time(0, 0)) - relativedelta(years=15, months=5),
                    subscription_age=15,
                    firstName=fake.first_name(),
                    lastName=fake.last_name(),
                    needsToFillCulturalSurvey=False,
                )
            )

            beneficiary_and_exunderage = users_factories.UnderageBeneficiaryFactory(
                email="*****@*****.**",
                dateOfBirth=datetime.combine(date.today(), time(0, 0)) - relativedelta(years=15, months=5),
                subscription_age=15,
                firstName=fake.first_name(),
                lastName=fake.last_name(),
                needsToFillCulturalSurvey=False,
            )
            db.session.execute("ALTER TABLE booking DISABLE TRIGGER booking_update;")
            bookings_factory.IndividualBookingFactory(individualBooking__user=beneficiary_and_exunderage)
            db.session.execute("ALTER TABLE booking ENABLE TRIGGER booking_update;")

        subscription_api.activate_beneficiary(beneficiary_and_exunderage, "sandbox")
        users.append(beneficiary_and_exunderage)

    with freeze_time(datetime.utcnow() - relativedelta(years=GRANT_18_VALIDITY_IN_YEARS, months=5)):
        users.append(
            users_factories.BeneficiaryGrant18Factory(
                email="*****@*****.**",
                dateOfBirth=datetime.combine(date.today(), time(0, 0))
                - relativedelta(years=ELIGIBILITY_AGE_18, months=5),
                firstName=fake.first_name(),
                lastName=fake.last_name(),
                needsToFillCulturalSurvey=False,
            )
        )

    user_by_email = {}
    for user in users:
        user_by_email[user.email] = user

    return user_by_email
    def test_recredit_underage_users(self):
        with freeze_time("2020-01-01"):
            user_15 = user_factories.UnderageBeneficiaryFactory()
            user_16_not_recredited = user_factories.UnderageBeneficiaryFactory(
                subscription_age=16,
            )
            user_16_already_recredited = user_factories.UnderageBeneficiaryFactory(
                subscription_age=16,
            )
            user_17_not_recredited = user_factories.UnderageBeneficiaryFactory(
                subscription_age=17,
            )
            user_17_only_recredited_at_16 = user_factories.UnderageBeneficiaryFactory(
                subscription_age=17,
            )
            user_17_already_recredited = user_factories.UnderageBeneficiaryFactory(
                subscription_age=17,
            )
            user_17_already_recredited_twice = user_factories.UnderageBeneficiaryFactory(
                subscription_age=17,
            )

            user_17_not_activated = user_factories.UserFactory(dateOfBirth=datetime.datetime(2004, 5, 1))
            user_17_not_activated.add_underage_beneficiary_role()

            payments_factories.RecreditFactory(
                deposit=user_16_already_recredited.deposit, recreditType=payments_models.RecreditType.RECREDIT_16
            )
            payments_factories.RecreditFactory(
                deposit=user_17_only_recredited_at_16.deposit,
                recreditType=payments_models.RecreditType.RECREDIT_16,
                dateCreated=datetime.datetime(2020, 1, 1),
            )
            payments_factories.RecreditFactory(
                deposit=user_17_already_recredited.deposit, recreditType=payments_models.RecreditType.RECREDIT_17
            )
            payments_factories.RecreditFactory(
                deposit=user_17_already_recredited_twice.deposit,
                recreditType=payments_models.RecreditType.RECREDIT_16,
                dateCreated=datetime.datetime(2019, 5, 1),
            )
            payments_factories.RecreditFactory(
                deposit=user_17_already_recredited_twice.deposit,
                recreditType=payments_models.RecreditType.RECREDIT_17,
                dateCreated=datetime.datetime(2020, 5, 1),
            )

        assert payments_models.Recredit.query.count() == 5

        assert user_17_not_activated.deposit is None

        assert user_15.deposit.amount == 20
        assert user_16_not_recredited.deposit.amount == 30
        assert user_16_already_recredited.deposit.amount == 30
        assert user_17_not_recredited.deposit.amount == 30
        assert user_17_only_recredited_at_16.deposit.amount == 30
        assert user_17_already_recredited.deposit.amount == 30
        assert user_17_already_recredited_twice.deposit.amount == 30

        with freeze_time("2020, 5, 2"):
            recredit_underage_users()

        # Assert we created new Recredits for user_16_not_recredited, user_17_not_recredited and user_17_only_recredited_at_16
        assert payments_models.Recredit.query.count() == 8
        assert user_15.deposit.recredits == []
        assert user_16_not_recredited.deposit.recredits[0].recreditType == payments_models.RecreditType.RECREDIT_16
        assert user_17_not_recredited.deposit.recredits[0].recreditType == payments_models.RecreditType.RECREDIT_17
        assert len(user_17_only_recredited_at_16.deposit.recredits) == 2
        assert (
            user_17_only_recredited_at_16.deposit.recredits[0].recreditType == payments_models.RecreditType.RECREDIT_17
        )

        assert user_15.deposit.amount == 20
        assert user_16_not_recredited.deposit.amount == 30 + 30
        assert user_16_already_recredited.deposit.amount == 30
        assert user_17_not_recredited.deposit.amount == 30 + 30
        assert user_17_only_recredited_at_16.deposit.amount == 30 + 30
        assert user_17_already_recredited.deposit.amount == 30
        assert user_17_already_recredited_twice.deposit.amount == 30