Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
(PC-11147) User: update eligibility_start_datetime for underage users
Browse files Browse the repository at this point in the history
  • Loading branch information
anoukhello committed Oct 8, 2021
1 parent e50d6a4 commit 3fab9bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
45 changes: 16 additions & 29 deletions src/pcapi/core/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ def eligibility(self) -> Optional[EligibilityType]:
# To avoid import loops
from pcapi.domain.beneficiary_pre_subscription.validator import _is_postal_code_eligible

if self.age == constants.ELIGIBILITY_AGE_18 and _is_postal_code_eligible(self.departementCode):
if not _is_postal_code_eligible(self.departementCode):
return None

if self.age == constants.ELIGIBILITY_AGE_18:
return EligibilityType.AGE18

if self.age in constants.ELIGIBILITY_UNDERAGE_RANGE and FeatureToggle.ENABLE_NATIVE_EAC_INDIVIDUAL.is_active():
Expand Down Expand Up @@ -358,41 +361,25 @@ def allowed_eligibility_check_methods(self) -> Optional[list[EligibilityCheckMet

@property
def eligibility_start_datetime(self) -> Optional[datetime]:
# To avoid import loops
from pcapi.domain.beneficiary_pre_subscription.validator import _is_postal_code_eligible

if not self.dateOfBirth:
if not self.dateOfBirth or not self.eligibility:
return None

eligibility_start = datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_AGE_18
)
eligibility_stop = datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_AGE_18 + 1
)
now = datetime.combine(date.today(), time(0, 0))
if not _is_postal_code_eligible(self.departementCode) and eligibility_start <= now < eligibility_stop:
return None
return eligibility_start
if self.eligibility == EligibilityType.UNDERAGE:
return datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_UNDERAGE_RANGE[0]
)
return datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(years=constants.ELIGIBILITY_AGE_18)

@property
def eligibility_end_datetime(self) -> Optional[datetime]:
# To avoid import loops
from pcapi.domain.beneficiary_pre_subscription.validator import _is_postal_code_eligible

if not self.dateOfBirth:
if not self.dateOfBirth or not self.eligibility:
return None

eligibility_start = datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_AGE_18
)
eligibility_stop = datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_AGE_18 + 1
)
now = datetime.combine(date.today(), time(0, 0))
if not _is_postal_code_eligible(self.departementCode) and eligibility_start <= now < eligibility_stop:
return None
return eligibility_stop
if self.eligibility == EligibilityType.UNDERAGE:
return datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(
years=constants.ELIGIBILITY_UNDERAGE_RANGE[-1] + 1
)
return datetime.combine(self.dateOfBirth, time(0, 0)) + relativedelta(years=constants.ELIGIBILITY_AGE_18 + 1)

@hybrid_property
def is_phone_validated(self):
Expand Down
11 changes: 9 additions & 2 deletions tests/core/users/external/external_users_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def test_update_external_pro_user():

n_query_get_user = 1
n_query_is_pro = 1
n_query_check_feature_WHOLE_FRANCE_OPENING_active = 1

with assert_num_queries(n_query_get_user + n_query_is_pro):
with assert_num_queries(n_query_get_user + n_query_is_pro + n_query_check_feature_WHOLE_FRANCE_OPENING_active):
update_external_user(user)

assert len(batch_testing.requests) == 0
Expand All @@ -77,9 +78,15 @@ def test_get_user_attributes():
n_query_get_deposit = 1
n_query_is_pro = 1
n_query_get_last_favorite = 1
n_query_check_feature_WHOLE_FRANCE_OPENING_active = 1

with assert_num_queries(
n_query_get_user + n_query_get_bookings + n_query_get_deposit + n_query_is_pro + n_query_get_last_favorite
n_query_get_user
+ n_query_get_bookings
+ n_query_get_deposit
+ n_query_is_pro
+ n_query_get_last_favorite
+ n_query_check_feature_WHOLE_FRANCE_OPENING_active
):
attributes = get_user_attributes(user)

Expand Down
17 changes: 16 additions & 1 deletion tests/core/users/test_users_sql_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pcapi.core.bookings.factories as bookings_factories
import pcapi.core.offers.factories as offers_factories
import pcapi.core.payments.factories as payments_factories
from pcapi.core.testing import override_features
from pcapi.core.users import factories as users_factories
from pcapi.model_creators.generic_creators import create_offerer
from pcapi.model_creators.generic_creators import create_user_offerer
Expand Down Expand Up @@ -208,7 +209,8 @@ def test_user_age(self):
assert users_factories.UserFactory.build(dateOfBirth=datetime(2000, 7, 1)).age == 17
assert users_factories.UserFactory.build(dateOfBirth=datetime(1999, 5, 1)).age == 19

def test_eligibility_start_end_datetime(self):
@freeze_time("2018-06-01")
def test_eligibility_start_end_datetime_beneficiary(self):
assert users_factories.UserFactory.build(dateOfBirth=None).eligibility_start_datetime is None
assert users_factories.UserFactory.build(
dateOfBirth=datetime(2000, 6, 1, 5, 1)
Expand All @@ -219,6 +221,19 @@ def test_eligibility_start_end_datetime(self):
dateOfBirth=datetime(2000, 6, 1, 5, 1)
).eligibility_end_datetime == datetime(2019, 6, 1, 0, 0)

@override_features(ENABLE_NATIVE_EAC_INDIVIDUAL=True)
@freeze_time("2018-06-01")
def test_eligibility_start_end_datetime_underage_beneficiary(self):
assert users_factories.UserFactory.build(dateOfBirth=None).eligibility_start_datetime is None
assert users_factories.UserFactory.build(
dateOfBirth=datetime(2003, 6, 1, 5, 1)
).eligibility_start_datetime == datetime(2018, 6, 1, 0, 0)

assert users_factories.UserFactory.build(dateOfBirth=None).eligibility_end_datetime is None
assert users_factories.UserFactory.build(
dateOfBirth=datetime(2003, 6, 1, 5, 1)
).eligibility_end_datetime == datetime(2021, 6, 1, 0, 0)


@pytest.mark.usefixtures("db_session")
class DepositVersionTest:
Expand Down
4 changes: 2 additions & 2 deletions tests/routes/native/v1/account_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def test_get_non_eligible_user_profile_from_non_eligible_area(self, app):
"depositVersion": None,
"depositType": None,
"depositExpirationDate": None,
"eligibilityEndDatetime": "2019-01-01T00:00:00Z",
"eligibilityStartDatetime": "2018-01-01T00:00:00Z",
"eligibilityEndDatetime": None,
"eligibilityStartDatetime": None,
"hasCompletedIdCheck": None,
"isBeneficiary": False,
"roles": [],
Expand Down

0 comments on commit 3fab9bf

Please sign in to comment.