33from django_pagarme .models import PagarmePayment , PagarmeNotification , PagarmeItemConfig
44from model_bakery import baker
55
6+ from pythonpro .cohorts .models import Cohort
67from pythonpro .domain import checkout_domain
8+ from pythonpro .memberkit import api
79from pythonpro .memberkit .models import SubscriptionType , Subscription , PaymentItemConfigToSubscriptionType
810
911
@@ -28,7 +30,7 @@ def send_purchase_notification_mock(mocker):
2830
2931
3032def test_pagarme_payment_paid_boleto (db , tag_as_mock , remove_tags_mock , promote_user_mock , logged_user ,
31- send_purchase_notification_mock ):
33+ send_purchase_notification_mock , mock_subscription_creation ):
3234 payment = baker .make (PagarmePayment , payment_method = facade .BOLETO , user = logged_user )
3335 baker .make (PagarmeNotification , status = facade .PAID , payment = payment )
3436 config = baker .make (PagarmeItemConfig , payments = [payment ])
@@ -44,25 +46,80 @@ def test_pagarme_payment_paid_boleto(db, tag_as_mock, remove_tags_mock, promote_
4446 send_purchase_notification_mock .asser_called_once_with (payment .id )
4547
4648
49+ @pytest .fixture
50+ def api_key ():
51+ key = 'some_key'
52+ api .set_default_api_key (key )
53+ return key
54+
55+
56+ @pytest .fixture (autouse = True )
57+ def enable_memberkit (settings ):
58+ settings .MEMBERKIT_ON = True
59+
60+
61+ @pytest .fixture
62+ def mock_email_marketing_facade (mocker ):
63+ return mocker .patch (
64+ 'pythonpro.domain.subscription_domain.email_marketing_facade.create_or_update_user.delay'
65+ )
66+
67+
4768def test_subscription_creation (db , tag_as_mock , remove_tags_mock , promote_user_mock , logged_user ,
48- send_purchase_notification_mock ):
69+ send_purchase_notification_mock , responses , api_key , mock_email_marketing_facade ):
4970 payment = baker .make (PagarmePayment , payment_method = facade .BOLETO , user = logged_user )
71+ cohort = baker .make (Cohort )
5072 baker .make (PagarmeNotification , status = facade .PAID , payment = payment )
5173 config = baker .make (PagarmeItemConfig , payments = [payment ])
52- subscription_type = baker .make (SubscriptionType )
74+ subscription_type = baker .make (SubscriptionType , include_on_cohort = True )
5375 PaymentItemConfigToSubscriptionType .objects .create (payment_item = config , subscription_type = subscription_type )
76+ user_response = {
77+ 'bio' : None ,
78+ 'blocked' : False ,
79+ 'email' : logged_user .email ,
80+ 'enrollments' : [],
81+ 'full_name' : logged_user .get_full_name (),
82+ 'id' : 1 ,
83+ 'memberships' : [
84+ {
85+ 'expire_date' : '2200-01-01' ,
86+ 'membership_level_id' : 4424 ,
87+ 'status' : 'active'
88+ }
89+ ],
90+ 'profile_image_url' : None ,
91+ 'unlimited' : True
92+ }
93+ responses .add (
94+ responses .POST ,
95+ f'https://memberkit.com.br/api/v1/users?api_key={ api_key } ' ,
96+ )
97+ responses .add (
98+ responses .GET ,
99+ f'https://memberkit.com.br/api/v1/users/{ logged_user .email } ?api_key={ api_key } ' ,
100+ json = user_response
101+ )
54102 checkout_domain .payment_handler_task (payment .id )
55103 subscription = Subscription .objects .first ()
56104 assert subscription is not None
57105 assert subscription .subscriber_id == payment .user_id
58- assert subscription .status == Subscription .Status .INACTIVE
106+ assert subscription .status == Subscription .Status .ACTIVE
59107 assert subscription .payment_id == payment .id
60108 assert list (subscription .subscription_types .all ()) == [subscription_type ]
61109 assert subscription .responsible is None
62- assert subscription .observation == 'Criação como resposta de pagamento no Pagarme'
110+ assert 'Criação como resposta de pagamento no Pagarme' in subscription .observation
111+ assert cohort .students .filter (id = logged_user .id ).exists ()
112+ assert mock_email_marketing_facade .call_count == 1
113+
114+
115+ @pytest .fixture
116+ def mock_subscription_creation (mocker ):
117+ return mocker .patch (
118+ 'pythonpro.domain.checkout_domain.subscription_domain.create_subscription_and_activate_services' )
63119
64120
65- def test_pagarme_payment_paid_credit_card (db , tag_as_mock , remove_tags_mock , promote_user_mock , logged_user ,
121+ def test_pagarme_payment_paid_credit_card (mock_subscription_creation , db , tag_as_mock , remove_tags_mock ,
122+ promote_user_mock , logged_user ,
66123 send_purchase_notification_mock ):
67124 payment = baker .make (PagarmePayment , payment_method = facade .CREDIT_CARD , user = logged_user )
68125 baker .make (PagarmeNotification , status = facade .PAID , payment = payment )
@@ -78,8 +135,8 @@ def test_pagarme_payment_paid_credit_card(db, tag_as_mock, remove_tags_mock, pro
78135 send_purchase_notification_mock .asser_called_once_with (payment .id )
79136
80137
81- def test_pagarme_payment_waiting_payment_boleto (db , tag_as_mock , remove_tags_mock , promote_user_mock , logged_user ,
82- send_purchase_notification_mock ):
138+ def test_pagarme_payment_waiting_payment_boleto (mock_subscription_creation , db , tag_as_mock , remove_tags_mock ,
139+ promote_user_mock , logged_user , send_purchase_notification_mock ):
83140 payment = baker .make (PagarmePayment , payment_method = facade .BOLETO , user = logged_user )
84141 baker .make (PagarmeNotification , status = facade .WAITING_PAYMENT , payment = payment )
85142 config = baker .make (PagarmeItemConfig , payments = [payment ])
0 commit comments