Mercurial > public > sg101
comparison donations/tests.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/donations/tests.py@4fb264b671d5 |
children | 00c14431e911 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Tests for the donations application. | |
3 """ | |
4 import urlparse | |
5 from decimal import Decimal | |
6 import datetime | |
7 | |
8 from django.contrib.auth.models import User | |
9 from django.test import TestCase | |
10 from django.core.urlresolvers import reverse | |
11 | |
12 from donations.models import Donation | |
13 import bio.badges | |
14 | |
15 | |
16 # This data was copy/pasted from my actual Paypal IPN history. Some alterations | |
17 # were made since this file is getting committed to version control and I | |
18 # didn't want to store "real" data that could be used to trace a transaction or | |
19 # real payer. | |
20 | |
21 # This data is for a non-anonymous donation: | |
22 TEST_POST_DATA_1 = """\ | |
23 mc_gross=5.00&protection_eligibility=Ineligible&payer_id=FAKEPAYERID01&tax=0.00&payment_date=04:14:08 Jan 21, 2011 PST&payment_status=Completed&charset=windows-1252&first_name=John&option_selection1=No&mc_fee=0.50¬ify_version=3.0&custom=test_user&payer_status=verified&business=brian@surfguitar101.com&quantity=1&verify_sign=Ai1PaTHIS-IS-FAKE-DATA-jB264AOjpiTa4vcsPCEavq-83oyIclHKI&payer_email=test_user@example.com&option_name1=List your name?&txn_id=TESTTXNID5815921V&payment_type=instant&last_name=Doe&receiver_email=brian@surfguitar101.com&payment_fee=0.50&receiver_id=FAKERECEIVERU&txn_type=web_accept&item_name=Donation for www.surfguitar101.com&mc_currency=USD&item_number=500&residence_country=AU&handling_amount=0.00&transaction_subject=test_user&payment_gross=5.00&shipping=0.00""" | |
24 | |
25 # Data from a user that wanted to remain anonymous | |
26 TEST_POST_DATA_2 = """\ | |
27 mc_gross=100.00&protection_eligibility=Ineligible&payer_id=FAKEPAYERID02&tax=0.00&payment_date=05:40:33 Jan 16, 2011 PST&payment_status=Completed&charset=windows-1252&first_name=John&option_selection1=No&mc_fee=3.20¬ify_version=3.0&custom=test_user&payer_status=unverified&business=brian@surfguitar101.com&quantity=1&verify_sign=AIkKNFAKE-DATA-NOT-REALpqCSxA-E7Tm4rMGlUpNy6ym0.exBzfiyI&payer_email=test_user@example.com&option_name1=List your name?&txn_id=TESTTXNIDK548343A&payment_type=instant&last_name=Doe&receiver_email=brian@surfguitar101.com&payment_fee=3.20&receiver_id=FAKERECEIVERU&txn_type=web_accept&item_name=Donation for www.surfguitar101.com&mc_currency=USD&item_number=501&residence_country=US&handling_amount=0.00&transaction_subject=test_user&payment_gross=100.00&shipping=0.00""" | |
28 | |
29 | |
30 class DonationsTest(TestCase): | |
31 fixtures = ['badges'] | |
32 | |
33 def test_ipn_post_1(self): | |
34 """ | |
35 Test a simulated IPN post | |
36 """ | |
37 user = User.objects.create_user('test_user', 'test_user@example.com', | |
38 'password') | |
39 user.save() | |
40 | |
41 args = urlparse.parse_qs(TEST_POST_DATA_1) | |
42 response = self.client.post(reverse('donations-ipn'), args) | |
43 | |
44 self.assertEqual(response.status_code, 200) | |
45 | |
46 try: | |
47 d = Donation.objects.get(pk=1) | |
48 except Donation.DoesNotExist: | |
49 self.fail("Donation object was not created") | |
50 else: | |
51 self.assertEqual(d.user, user) | |
52 self.assertFalse(d.is_anonymous) | |
53 self.assertFalse(d.test_ipn) | |
54 self.assertEqual(d.txn_id, 'TESTTXNID5815921V') | |
55 self.assertEqual(d.txn_type, 'web_accept') | |
56 self.assertEqual(d.first_name, 'John') | |
57 self.assertEqual(d.last_name, 'Doe') | |
58 self.assertEqual(d.payer_email, 'test_user@example.com') | |
59 self.assertEqual(d.payer_id, 'FAKEPAYERID01') | |
60 self.assertEqual(d.mc_fee, Decimal('0.50')) | |
61 self.assertEqual(d.mc_gross, Decimal('5.00')) | |
62 self.assertEqual(d.memo, '') | |
63 self.assertEqual(d.payer_status, 'verified') | |
64 self.assertEqual(d.payment_date, | |
65 datetime.datetime(2011, 1, 21, 4, 14, 8)) | |
66 | |
67 # user should have got a badge for donating | |
68 p = user.get_profile() | |
69 badges = list(p.badges.all()) | |
70 self.assertEqual(len(badges), 1) | |
71 if len(badges) == 1: | |
72 self.assertEqual(badges[0].numeric_id, bio.badges.CONTRIBUTOR_PIN) | |
73 | |
74 def test_ipn_post_2(self): | |
75 """ | |
76 Test a simulated IPN post | |
77 """ | |
78 user = User.objects.create_user('test_user', 'test_user@example.com', | |
79 'password') | |
80 user.save() | |
81 | |
82 args = urlparse.parse_qs(TEST_POST_DATA_2) | |
83 response = self.client.post(reverse('donations-ipn'), args) | |
84 | |
85 self.assertEqual(response.status_code, 200) | |
86 | |
87 try: | |
88 d = Donation.objects.get(pk=1) | |
89 except Donation.DoesNotExist: | |
90 self.fail("Donation object was not created") | |
91 else: | |
92 self.assertEqual(d.user, user) | |
93 self.assertTrue(d.is_anonymous) | |
94 self.assertFalse(d.test_ipn) | |
95 self.assertEqual(d.txn_id, 'TESTTXNIDK548343A') | |
96 self.assertEqual(d.txn_type, 'web_accept') | |
97 self.assertEqual(d.first_name, 'John') | |
98 self.assertEqual(d.last_name, 'Doe') | |
99 self.assertEqual(d.payer_email, 'test_user@example.com') | |
100 self.assertEqual(d.payer_id, 'FAKEPAYERID02') | |
101 self.assertEqual(d.mc_fee, Decimal('3.20')) | |
102 self.assertEqual(d.mc_gross, Decimal('100.00')) | |
103 self.assertEqual(d.memo, '') | |
104 self.assertEqual(d.payer_status, 'unverified') | |
105 self.assertEqual(d.payment_date, | |
106 datetime.datetime(2011, 1, 16, 5, 40, 33)) | |
107 | |
108 # user should not have got a badge for donating | |
109 p = user.get_profile() | |
110 self.assertEqual(p.badges.count(), 0) |