Mercurial > public > sg101
diff 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 |
line wrap: on
line diff
--- a/gpp/donations/tests.py Sat Jan 29 20:39:55 2011 +0000 +++ b/gpp/donations/tests.py Sun Jan 30 20:02:32 2011 +0000 @@ -1,23 +1,110 @@ """ -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". +Tests for the donations application. +""" +import urlparse +from decimal import Decimal +import datetime -Replace these with more appropriate tests for your application. -""" +from django.contrib.auth.models import User +from django.test import TestCase +from django.core.urlresolvers import reverse -from django.test import TestCase +from donations.models import Donation +import bio.badges -class SimpleTest(TestCase): - def test_basic_addition(self): + +# This data was copy/pasted from my actual Paypal IPN history. Some alterations +# were made since this file is getting committed to version control and I +# didn't want to store "real" data that could be used to trace a transaction or +# real payer. + +# This data is for a non-anonymous donation: +TEST_POST_DATA_1 = """\ +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""" + +# Data from a user that wanted to remain anonymous +TEST_POST_DATA_2 = """\ +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""" + + +class DonationsTest(TestCase): + fixtures = ['badges'] + + def test_ipn_post_1(self): """ - Tests that 1 + 1 always equals 2. + Test a simulated IPN post """ - self.failUnlessEqual(1 + 1, 2) + user = User.objects.create_user('test_user', 'test_user@example.com', + 'password') + user.save() -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. + args = urlparse.parse_qs(TEST_POST_DATA_1) + response = self.client.post(reverse('donations-ipn'), args) ->>> 1 + 1 == 2 -True -"""} + self.assertEqual(response.status_code, 200) + try: + d = Donation.objects.get(pk=1) + except Donation.DoesNotExist: + self.fail("Donation object was not created") + else: + self.assertEqual(d.user, user) + self.assertFalse(d.is_anonymous) + self.assertFalse(d.test_ipn) + self.assertEqual(d.txn_id, 'TESTTXNID5815921V') + self.assertEqual(d.txn_type, 'web_accept') + self.assertEqual(d.first_name, 'John') + self.assertEqual(d.last_name, 'Doe') + self.assertEqual(d.payer_email, 'test_user@example.com') + self.assertEqual(d.payer_id, 'FAKEPAYERID01') + self.assertEqual(d.mc_fee, Decimal('0.50')) + self.assertEqual(d.mc_gross, Decimal('5.00')) + self.assertEqual(d.memo, '') + self.assertEqual(d.payer_status, 'verified') + self.assertEqual(d.payment_date, + datetime.datetime(2011, 1, 21, 4, 14, 8)) + + # user should have got a badge for donating + p = user.get_profile() + badges = list(p.badges.all()) + self.assertEqual(len(badges), 1) + if len(badges) == 1: + self.assertEqual(badges[0].numeric_id, bio.badges.CONTRIBUTOR_PIN) + + def test_ipn_post_2(self): + """ + Test a simulated IPN post + """ + user = User.objects.create_user('test_user', 'test_user@example.com', + 'password') + user.save() + + args = urlparse.parse_qs(TEST_POST_DATA_2) + response = self.client.post(reverse('donations-ipn'), args) + + self.assertEqual(response.status_code, 200) + + try: + d = Donation.objects.get(pk=1) + except Donation.DoesNotExist: + self.fail("Donation object was not created") + else: + self.assertEqual(d.user, user) + self.assertTrue(d.is_anonymous) + self.assertFalse(d.test_ipn) + self.assertEqual(d.txn_id, 'TESTTXNIDK548343A') + self.assertEqual(d.txn_type, 'web_accept') + self.assertEqual(d.first_name, 'John') + self.assertEqual(d.last_name, 'Doe') + self.assertEqual(d.payer_email, 'test_user@example.com') + self.assertEqual(d.payer_id, 'FAKEPAYERID02') + self.assertEqual(d.mc_fee, Decimal('3.20')) + self.assertEqual(d.mc_gross, Decimal('100.00')) + self.assertEqual(d.memo, '') + self.assertEqual(d.payer_status, 'verified') + self.assertEqual(d.payment_date, + datetime.datetime(2011, 1, 16, 5, 40, 33)) + + # user should not have got a badge for donating + p = user.get_profile() + self.assertEqual(p.badges.count(), 0)