Skip to content

Commit

Permalink
Merge pull request #1457 from mozilla/fix-ga-user_purchased_premium-e…
Browse files Browse the repository at this point in the history
…vent-mpp-1512

fix MPP-1512: send ga event for any premium user with "clicked-purchase"
  • Loading branch information
groovecoder authored Jan 10, 2022
2 parents a4008fe + 60c577b commit b283d97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
27 changes: 16 additions & 11 deletions privaterelay/tests/views_tests.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import json

from django.contrib.auth.models import User
from django.test import TestCase

from allauth.socialaccount.models import SocialAccount
from allauth.account.models import EmailAddress
from model_bakery import baker

from ..views import _update_extra_data_and_email
from ..views import _update_all_data


class UpdateExtraDataAndEmailTest(TestCase):
def test_update_extra_data_and_email(self):
def test_update_all_data(self):
user = baker.make(User)
ea = baker.make(EmailAddress, user=user)
sa = baker.make(
SocialAccount, user=user, extra_data='{"test": "test"}'
SocialAccount, user=user, provider='fxa',
extra_data=json.loads('{"test": "test"}')
)
new_extra_data = '{"test": "updated"}'
new_extra_data = json.loads('{"test": "updated"}')
new_email = '[email protected]'

response = _update_extra_data_and_email(
response = _update_all_data(
sa, new_extra_data, new_email
)

Expand All @@ -30,21 +33,23 @@ def test_update_extra_data_and_email(self):
assert sa.extra_data == new_extra_data
assert ea.email == new_email

def test_update_extra_data_and_email_conflict(self):
extra_data='{"test": "test"}'
def test_update_all_data_conflict(self):
extra_data=json.loads('{"test": "test"}')

user = baker.make(User, email='[email protected]')
baker.make(EmailAddress, user=user, email='[email protected]')
baker.make(SocialAccount, user=user, extra_data=extra_data)
baker.make(SocialAccount, user=user, provider='fxa',
extra_data=extra_data)

user2 = baker.make(User, email='[email protected]')
ea2 = baker.make(EmailAddress, user=user2, email='[email protected]')
sa2 = baker.make(SocialAccount, user=user2, extra_data=extra_data)
sa2 = baker.make(SocialAccount, user=user2, provider='fxa',
extra_data=extra_data)

new_extra_data = '{"test": "updated"}'
new_extra_data = json.loads('{"test": "updated"}')
new_email = '[email protected]'

response = _update_extra_data_and_email(
response = _update_all_data(
sa2, new_extra_data, new_email
)

Expand Down
29 changes: 14 additions & 15 deletions privaterelay/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,10 @@ def faq(request):
def profile(request):
if (not request.user or request.user.is_anonymous):
return redirect(reverse('fxa_login'))
newly_premium = False
profile = request.user.profile_set.first()
if ('fxa_refresh' in request.GET or
'clicked-purchase' in request.COOKIES
):
had_premium = profile.has_premium
if 'fxa_refresh' in request.GET:
fxa = _get_fxa(request)
_handle_fxa_profile_change(fxa)
now_has_premium = profile.has_premium
newly_premium = not had_premium and now_has_premium
if newly_premium:
profile.date_subscribed = datetime.now(timezone.utc)
profile.save()

relay_addresses = RelayAddress.objects.filter(user=request.user).order_by(
'-created_at'
Expand All @@ -110,7 +101,9 @@ def profile(request):
if datetime.now(timezone.utc) < settings.PREMIUM_RELEASE_DATE:
context.update({'show_data_notification_banner': True})
response = render(request, 'profile.html', context)
if newly_premium:
if ('clicked-purchase' in request.COOKIES and
profile.has_premium
):
event = 'user_purchased_premium'
incr_if_enabled(event, 1)
response.delete_cookie('clicked-purchase')
Expand Down Expand Up @@ -328,15 +321,21 @@ def _handle_fxa_profile_change(
'real_address': sha256(new_email.encode('utf-8')).hexdigest(),
})

return _update_extra_data_and_email(
social_account, extra_data, new_email
)
return _update_all_data(social_account, extra_data, new_email)

def _update_extra_data_and_email(social_account, extra_data, new_email):
def _update_all_data(social_account, extra_data, new_email):
try:
profile = social_account.user.profile_set.first()
had_premium = profile.has_premium
with transaction.atomic():
social_account.extra_data = extra_data
social_account.save()
profile = social_account.user.profile_set.first()
now_has_premium = profile.has_premium
newly_premium = not had_premium and now_has_premium
if newly_premium:
profile.date_subscribed = datetime.now(timezone.utc)
profile.save()
social_account.user.email = new_email
social_account.user.save()
email_address_record = social_account.user.emailaddress_set.first()
Expand Down

0 comments on commit b283d97

Please sign in to comment.