@@ -25,4 +25,53 @@ def test_synchronize_paid_subscriptions(db):
2525 baker .make (PagarmeNotification , payment = payment , status = pagarme_facade .PAID )
2626 baker .make (PagarmeNotification , payment = payment , status = pagarme_facade .REFUNDED )
2727 management .call_command ('synchronize_paid_subscriptions' )
28- assert 2 == Subscription .objects .count ()
28+ assert 2 == Subscription .objects .filter (status = Subscription .Status .INACTIVE ).count ()
29+
30+
31+ def test_fix_inactive_subscriptions (db ):
32+ payment_config = baker .make (PagarmeItemConfig )
33+ inactive_with_paid_payment = _make_subscriptions_with_payment (payment_config , pagarme_facade .PAID )
34+ unpaid_statuses = set (pagarme_facade ._impossible_states )
35+ unpaid_statuses .discard (pagarme_facade .PAID )
36+ for status in unpaid_statuses :
37+ # only one will have PAID status
38+ _make_subscriptions_with_payment (payment_config , status )
39+
40+ # Make one refunded Subscription
41+ payment = baker .make (PagarmePayment , user = baker .make (User ))
42+ payment .items .set ([payment_config ])
43+ baker .make (PagarmeNotification , payment = payment , status = pagarme_facade .PAID )
44+ baker .make (PagarmeNotification , payment = payment , status = pagarme_facade .REFUNDED )
45+ refunded_subscription = baker .make (Subscription , payment = payment , status = Subscription .Status .INACTIVE )
46+
47+ # Make one subscriptions with no payment at all
48+ baker .make (Subscription , status = Subscription .Status .INACTIVE )
49+ active_without_payment = baker .make (Subscription , status = Subscription .Status .ACTIVE )
50+
51+ active_with_paid_payment = _make_subscriptions_with_payment (payment_config , pagarme_facade .PAID )
52+ active_with_paid_payment .status = Subscription .Status .ACTIVE
53+ active_with_paid_payment .save ()
54+
55+ previous_subscriptions = Subscription .objects .count ()
56+ assert 2 == Subscription .objects .filter (status = Subscription .Status .ACTIVE ).count ()
57+
58+ management .call_command ('fix_inactive_subscriptions' )
59+
60+ assert previous_subscriptions == Subscription .objects .count ()
61+ assert 3 == Subscription .objects .filter (status = Subscription .Status .ACTIVE ).count ()
62+ assert not Subscription .objects .filter (status = Subscription .Status .ACTIVE , id = refunded_subscription .id ).exists ()
63+
64+ appended_observation_message = '\n \n Ativada via comando automático do servidor.'
65+ active_without_payment .refresh_from_db ()
66+ assert not active_without_payment .observation .endswith (appended_observation_message )
67+ active_with_paid_payment .refresh_from_db ()
68+ assert not active_with_paid_payment .observation .endswith (appended_observation_message )
69+ inactive_with_paid_payment .refresh_from_db ()
70+ assert inactive_with_paid_payment .observation .endswith (appended_observation_message )
71+
72+
73+ def _make_subscriptions_with_payment (payment_config , status ):
74+ payment = baker .make (PagarmePayment , user = baker .make (User ))
75+ payment .items .set ([payment_config ])
76+ baker .make (PagarmeNotification , payment = payment , status = status )
77+ return baker .make (Subscription , payment = payment , status = Subscription .Status .INACTIVE )
0 commit comments