comparison gpp/donations/tests.py @ 316:767cedc7d12a

Fixing #144; integrate with new Django logging support. Also added unit tests for Donations app.
author Brian Neal <bgneal@gmail.com>
date Sun, 30 Jan 2011 20:02:32 +0000
parents c018872385ea
children 4fb264b671d5
comparison
equal deleted inserted replaced
315:36373d995611 316:767cedc7d12a
1 """ 1 """
2 This file demonstrates two different styles of tests (one doctest and one 2 Tests for the donations application.
3 unittest). These will both pass when you run "manage.py test". 3 """
4 import urlparse
5 from decimal import Decimal
6 import datetime
4 7
5 Replace these with more appropriate tests for your application. 8 from django.contrib.auth.models import User
6 """ 9 from django.test import TestCase
10 from django.core.urlresolvers import reverse
7 11
8 from django.test import TestCase 12 from donations.models import Donation
13 import bio.badges
9 14
10 class SimpleTest(TestCase): 15
11 def test_basic_addition(self): 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&notify_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&notify_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):
12 """ 34 """
13 Tests that 1 + 1 always equals 2. 35 Test a simulated IPN post
14 """ 36 """
15 self.failUnlessEqual(1 + 1, 2) 37 user = User.objects.create_user('test_user', 'test_user@example.com',
38 'password')
39 user.save()
16 40
17 __test__ = {"doctest": """ 41 args = urlparse.parse_qs(TEST_POST_DATA_1)
18 Another way to test that 1 + 1 is equal to 2. 42 response = self.client.post(reverse('donations-ipn'), args)
19 43
20 >>> 1 + 1 == 2 44 self.assertEqual(response.status_code, 200)
21 True
22 """}
23 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, 'verified')
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)