Skip to content

Commit ef9789d

Browse files
renzonrenzon
authored andcommitted
Fixed existing user creation when accessing Pytools LP
close #1332
1 parent 9ae66a9 commit ef9789d

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

Pipfile.lock

Lines changed: 36 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pythonpro/payments/tests/test_pagarme_boleto.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
import responses
33
from django.urls import reverse
4+
from model_mommy import mommy
45
from rolepermissions.checkers import has_role
56

67
from pythonpro.django_assertions import dj_assert_contains
@@ -219,3 +220,14 @@ def test_client_update_on_mail_chimp(resp_token_with_no_user, django_user_model,
219220

220221
def test_client_lead_not_created_on_mailchimp(resp_token_with_no_user, django_user_model, create_or_update_lead):
221222
create_or_update_lead.assert_called_once_with(CUSTOMER_FIRST_NAME, CUSTOMER_EMAIL)
223+
224+
225+
@pytest.fixture
226+
def resp_existing_user_not_logged(db, client, create_or_update_client, create_or_update_lead, resps_success,
227+
django_user_model):
228+
mommy.make(django_user_model, email=CUSTOMER_EMAIL)
229+
return client.post(reverse('payments:pytools_capture'), transaction_data, secure=True)
230+
231+
232+
def test_existing_user_not_created(resp_existing_user_not_logged, django_user_model):
233+
assert 1 == django_user_model.objects.all().count()

pythonpro/payments/views.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ def pytools_capture(request):
3636
user = request.user
3737
if not user.is_authenticated:
3838
customer = pagarme_resp['customer']
39-
customer_first_name = customer['name'].split()[0]
4039
customer_email = customer['email']
41-
form = UserSignupForm({'first_name': customer_first_name, 'email': customer_email})
42-
source = request.GET.get('utm_source', default='unknown')
43-
user = form.save(source=source)
44-
if not pagarme_resp['payment_method'] == 'credit_card':
45-
assign_role(user, 'lead')
46-
try:
47-
mailchimp_facade.create_or_update_lead(customer_first_name, customer_email)
48-
except MailChimpError:
49-
pass
40+
if not get_user_model().objects.filter(email=customer_email).exists():
41+
customer_first_name = customer['name'].split()[0]
42+
form = UserSignupForm({'first_name': customer_first_name, 'email': customer_email})
43+
source = request.GET.get('utm_source', default='unknown')
44+
user = form.save(source=source)
45+
if not pagarme_resp['payment_method'] == 'credit_card':
46+
assign_role(user, 'lead')
47+
try:
48+
mailchimp_facade.create_or_update_lead(customer_first_name, customer_email)
49+
except MailChimpError:
50+
pass
5051

5152
if pagarme_resp['payment_method'] == 'credit_card':
5253
_promote_client(user, request)

0 commit comments

Comments
 (0)