11from datetime import datetime , timedelta
22
3+ from django .core import mail
4+
35from nose .tools import eq_
46
57import amo
68import amo .tests
7- from addons .models import Addon
8- from market .models import AddonPremium
9- from market .cron import clean_out_addonpremium
9+ from addons .models import Addon , AddonUser
10+ from market .cron import clean_out_addonpremium , mail_pending_refunds
11+ from market .models import AddonPremium , Refund
12+ from stats .models import Contribution
13+ from users .models import UserProfile
1014
1115
1216class TestCronDeletes (amo .tests .TestCase ):
@@ -30,3 +34,67 @@ def test_doesnt_delete(self):
3034 clean_out_addonpremium (days = 1 )
3135 eq_ (AddonPremium .objects .count (), 3 )
3236
37+
38+ class TestPendingRefunds (amo .tests .TestCase ):
39+ fixtures = ['webapps/337141-steamcube' , 'base/users' ]
40+
41+ def create_refund (self , webapp = None ):
42+ webapp = webapp if webapp else self .webapp
43+ contribution = Contribution .objects .create (addon = webapp )
44+ return Refund .objects .create (contribution = contribution )
45+
46+ def setUp (self ):
47+ self .webapp = Addon .objects .get (pk = 337141 )
48+ self .author = self .webapp .authors .all ()[0 ]
49+ self .refund = self .create_refund ()
50+
51+ def test_none (self ):
52+ self .refund .delete ()
53+ mail_pending_refunds ()
54+ eq_ (len (mail .outbox ), 0 )
55+
56+ def test_not_pending (self ):
57+ for status in [amo .REFUND_APPROVED , amo .REFUND_APPROVED_INSTANT ,
58+ amo .REFUND_DECLINED ]:
59+ self .refund .update (status = status )
60+ mail_pending_refunds ()
61+ eq_ (len (mail .outbox ), 0 )
62+
63+ def test_single (self ):
64+ mail_pending_refunds ()
65+ eq_ (len (mail .outbox ), 1 )
66+ assert str (self .webapp .name ) in mail .outbox [0 ].body
67+ assert '1 request' in mail .outbox [0 ].body
68+ assert mail .outbox [0 ].to == [self .author .email ]
69+
70+ def test_plural (self ):
71+ self .create_refund ()
72+ mail_pending_refunds ()
73+ eq_ (len (mail .outbox ), 1 )
74+ assert '2 requests' in mail .outbox [0 ].body
75+
76+ def test_two_owners (self ):
77+ user = UserProfile .objects .exclude (pk = self .author .pk )[0 ]
78+ AddonUser .objects .create (user = user , addon = self .webapp )
79+ mail_pending_refunds ()
80+ eq_ (len (mail .outbox ), 2 )
81+ emails = set ([m .to [0 ] for m in mail .outbox ])
82+ eq_ (set ([self .author .email , user .email ]), emails )
83+
84+ def test_one_owner_one_other (self ):
85+ user = UserProfile .objects .exclude (pk = self .author .pk )[0 ]
86+ AddonUser .objects .create (user = user , addon = self .webapp ,
87+ role = amo .AUTHOR_ROLE_VIEWER )
88+ mail_pending_refunds ()
89+ eq_ (len (mail .outbox ), 1 )
90+ assert mail .outbox [0 ].to == [self .author .email ]
91+
92+ def test_two_addons (self ):
93+ other = Addon .objects .create (app_slug = 'something-else' ,
94+ name = 'cthulhu' , type = amo .ADDON_WEBAPP )
95+ AddonUser .objects .create (user = self .author , addon = other ,
96+ role = amo .AUTHOR_ROLE_OWNER )
97+ self .create_refund (other )
98+ mail_pending_refunds ()
99+ eq_ (len (mail .outbox ), 1 )
100+ eq_ (mail .outbox [0 ].body .count ('1 request' ), 2 )
0 commit comments