Skip to content

Commit

Permalink
Remove more Profile.objects.get(user=user)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhitlock committed Jun 20, 2024
1 parent 2b6cf89 commit 1fd6d6e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
3 changes: 1 addition & 2 deletions emails/tests/models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,8 @@ def test_make_domain_address_valid_premium_user_with_no_subdomain(self) -> None:
provider="fxa",
extra_data={"subscriptions": [premium_subscription()]},
)
user_profile = Profile.objects.get(user=user)
with pytest.raises(CannotMakeAddressException) as exc_info:
DomainAddress.make_domain_address(user_profile, "test-nosubdomain")
DomainAddress.make_domain_address(user.profile, "test-nosubdomain")
assert exc_info.value.get_codes() == "need_subdomain"

def test_make_domain_address_dupe_of_existing_raises(self):
Expand Down
7 changes: 3 additions & 4 deletions emails/tests/views_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,9 @@ def setUp(self) -> None:
RelayAddress, user=self.user, address="ebsbdsan7", domain=2
)
self.premium_user = make_premium_test_user()
self.premium_profile = Profile.objects.get(user=self.premium_user)
self.premium_profile.subdomain = "subdomain"
self.premium_profile.last_engagement = datetime.now(UTC)
self.premium_profile.save()
self.premium_user.profile.subdomain = "subdomain"
self.premium_user.profile.last_engagement = datetime.now(UTC)
self.premium_user.profile.save()

def test_single_recipient_sns_notification(self) -> None:
pre_sns_notification_last_engagement = self.ra.user.profile.last_engagement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from model_bakery import baker
from waffle.models import Flag

from emails.models import Profile
from privaterelay.management.commands.sync_phone_related_dates_on_profile import (
sync_phone_related_dates_on_profile,
)
Expand Down Expand Up @@ -69,10 +68,10 @@ def test_phone_subscription_user_with_no_phone_subscription_data_does_not_get_up
phone_user: User,
) -> None:
mocked_dates.return_value = (None, None, None)
profile = Profile.objects.get(user=phone_user)

num_profiles_updated = sync_phone_related_dates_on_profile("subscription")

profile = phone_user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset is None
assert profile.date_subscribed_phone is None
Expand Down Expand Up @@ -108,11 +107,11 @@ def test_free_phone_user_gets_only_date_phone_subscription_reset_field_updated(
expected_now = patch_datetime_now
mocked_dates.return_value = (None, None, None)
account: SocialAccount = baker.make(SocialAccount, provider="fxa")
profile = Profile.objects.get(user=account.user)
create_free_phones_flag_for_user(profile.user)
create_free_phones_flag_for_user(account.user)

num_profiles_updated = sync_phone_related_dates_on_profile("free")

profile = account.user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset == expected_now.replace(day=1)
assert profile.date_subscribed_phone is None
Expand All @@ -128,13 +127,13 @@ def test_free_phone_user_with_existing_date_phone_subscription_reset_field_does_
expected_now = patch_datetime_now
mocked_dates.return_value = (None, None, None)
account: SocialAccount = baker.make(SocialAccount, provider="fxa")
profile = Profile.objects.get(user=account.user)
profile.date_phone_subscription_reset = expected_now
profile.save()
create_free_phones_flag_for_user(profile.user)
account.user.profile.date_phone_subscription_reset = expected_now
account.user.profile.save()
create_free_phones_flag_for_user(account.user)

num_profiles_updated = sync_phone_related_dates_on_profile("free")

profile = account.user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset == expected_now
assert profile.date_subscribed_phone is None
Expand All @@ -147,10 +146,9 @@ def test_free_phone_user_with_existing_date_phone_subscription_reset_field_does_
def test_monthly_phone_subscriber_profile_date_fields_all_updated(
mocked_dates, patch_datetime_now, phone_user
):
profile = Profile.objects.get(user=phone_user)
date_subscribed_phone = datetime.now(UTC) - timedelta(3)
profile.date_subscribed_phone = date_subscribed_phone
profile.save()
phone_user.profile.date_subscribed_phone = date_subscribed_phone
phone_user.profile.save()
mocked_dates.return_value = (
date_subscribed_phone,
date_subscribed_phone,
Expand All @@ -159,6 +157,7 @@ def test_monthly_phone_subscriber_profile_date_fields_all_updated(

num_profiles_updated = sync_phone_related_dates_on_profile("subscription")

profile = phone_user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset == date_subscribed_phone
assert profile.date_subscribed_phone == date_subscribed_phone
Expand All @@ -171,7 +170,7 @@ def test_monthly_phone_subscriber_profile_date_fields_all_updated(
def test_monthly_phone_subscriber_renewed_subscription_profile_date_phone_subscription_start_and_end_updated( # noqa: E501
mocked_dates, patch_datetime_now, phone_user
):
profile = Profile.objects.get(user=phone_user)
profile = phone_user.profile
first_day_of_current_month = datetime.now(UTC).replace(day=1)
# get first of the last month
first_day_of_last_month = (first_day_of_current_month - timedelta(1)).replace(day=1)
Expand Down Expand Up @@ -207,7 +206,6 @@ def test_monthly_phone_subscriber_renewed_subscription_profile_date_phone_subscr
def test_yearly_phone_subscriber_profile_date_fields_all_updated(
mocked_dates, patch_datetime_now, phone_user
):
profile = Profile.objects.get(user=phone_user)
date_subscribed_phone = datetime.now(UTC) - timedelta(3)
mocked_dates.return_value = (
date_subscribed_phone,
Expand All @@ -217,6 +215,7 @@ def test_yearly_phone_subscriber_profile_date_fields_all_updated(

num_profiles_updated = sync_phone_related_dates_on_profile("subscription")

profile = phone_user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset == date_subscribed_phone
assert profile.date_subscribed_phone == date_subscribed_phone
Expand All @@ -229,17 +228,17 @@ def test_yearly_phone_subscriber_profile_date_fields_all_updated(
def test_yearly_phone_subscriber_with_subscription_date_older_than_31_days_profile_date_fields_all_updated( # noqa: E501
mocked_dates, patch_datetime_now, phone_user
):
profile = Profile.objects.get(user=phone_user)
date_subscribed_phone = datetime.now(UTC) - timedelta(90)
profile.date_subscribed_phone = date_subscribed_phone
profile.save()
phone_user.profile.date_subscribed_phone = date_subscribed_phone
phone_user.profile.save()
mocked_dates.return_value = (
date_subscribed_phone,
date_subscribed_phone,
date_subscribed_phone + timedelta(365),
)
num_profiles_updated = sync_phone_related_dates_on_profile("subscription")

profile = phone_user.profile
profile.refresh_from_db()
sixty_two_days_from_subsciprion_date = date_subscribed_phone + timedelta(62)
assert profile.date_phone_subscription_reset == sixty_two_days_from_subsciprion_date
Expand All @@ -257,10 +256,9 @@ def test_command_with_one_update(
phone_user,
capsys,
):
profile = Profile.objects.get(user=phone_user)
date_subscribed_phone = datetime.now(UTC)
profile.date_subscribed_phone = date_subscribed_phone
profile.save()
phone_user.profile.date_subscribed_phone = date_subscribed_phone
phone_user.profile.save()
mocked_dates.return_value = (
date_subscribed_phone,
date_subscribed_phone,
Expand All @@ -270,6 +268,7 @@ def test_command_with_one_update(
out, err = capsys.readouterr()
num_profiles_updated = int(out.split(" ")[0])

profile = phone_user.profile
profile.refresh_from_db()
assert profile.date_phone_subscription_reset == date_subscribed_phone
assert profile.date_subscribed_phone == date_subscribed_phone
Expand Down

0 comments on commit 1fd6d6e

Please sign in to comment.