annotate gpp/donations/tests.py @ 322:c3d3d7114749

Fix #148; Django now requires AJAX posts to present the CSRF token. Added code suggested by Django docs to shoutbox.js. Since shoutbox.js is on every page, it should cover all cases.
author Brian Neal <bgneal@gmail.com>
date Sat, 12 Feb 2011 21:37:17 +0000
parents 767cedc7d12a
children 4fb264b671d5
rev   line source
bgneal@33 1 """
bgneal@316 2 Tests for the donations application.
bgneal@316 3 """
bgneal@316 4 import urlparse
bgneal@316 5 from decimal import Decimal
bgneal@316 6 import datetime
bgneal@33 7
bgneal@316 8 from django.contrib.auth.models import User
bgneal@316 9 from django.test import TestCase
bgneal@316 10 from django.core.urlresolvers import reverse
bgneal@33 11
bgneal@316 12 from donations.models import Donation
bgneal@316 13 import bio.badges
bgneal@33 14
bgneal@316 15
bgneal@316 16 # This data was copy/pasted from my actual Paypal IPN history. Some alterations
bgneal@316 17 # were made since this file is getting committed to version control and I
bgneal@316 18 # didn't want to store "real" data that could be used to trace a transaction or
bgneal@316 19 # real payer.
bgneal@316 20
bgneal@316 21 # This data is for a non-anonymous donation:
bgneal@316 22 TEST_POST_DATA_1 = """\
bgneal@316 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"""
bgneal@316 24
bgneal@316 25 # Data from a user that wanted to remain anonymous
bgneal@316 26 TEST_POST_DATA_2 = """\
bgneal@316 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"""
bgneal@316 28
bgneal@316 29
bgneal@316 30 class DonationsTest(TestCase):
bgneal@316 31 fixtures = ['badges']
bgneal@316 32
bgneal@316 33 def test_ipn_post_1(self):
bgneal@33 34 """
bgneal@316 35 Test a simulated IPN post
bgneal@33 36 """
bgneal@316 37 user = User.objects.create_user('test_user', 'test_user@example.com',
bgneal@316 38 'password')
bgneal@316 39 user.save()
bgneal@33 40
bgneal@316 41 args = urlparse.parse_qs(TEST_POST_DATA_1)
bgneal@316 42 response = self.client.post(reverse('donations-ipn'), args)
bgneal@33 43
bgneal@316 44 self.assertEqual(response.status_code, 200)
bgneal@33 45
bgneal@316 46 try:
bgneal@316 47 d = Donation.objects.get(pk=1)
bgneal@316 48 except Donation.DoesNotExist:
bgneal@316 49 self.fail("Donation object was not created")
bgneal@316 50 else:
bgneal@316 51 self.assertEqual(d.user, user)
bgneal@316 52 self.assertFalse(d.is_anonymous)
bgneal@316 53 self.assertFalse(d.test_ipn)
bgneal@316 54 self.assertEqual(d.txn_id, 'TESTTXNID5815921V')
bgneal@316 55 self.assertEqual(d.txn_type, 'web_accept')
bgneal@316 56 self.assertEqual(d.first_name, 'John')
bgneal@316 57 self.assertEqual(d.last_name, 'Doe')
bgneal@316 58 self.assertEqual(d.payer_email, 'test_user@example.com')
bgneal@316 59 self.assertEqual(d.payer_id, 'FAKEPAYERID01')
bgneal@316 60 self.assertEqual(d.mc_fee, Decimal('0.50'))
bgneal@316 61 self.assertEqual(d.mc_gross, Decimal('5.00'))
bgneal@316 62 self.assertEqual(d.memo, '')
bgneal@316 63 self.assertEqual(d.payer_status, 'verified')
bgneal@316 64 self.assertEqual(d.payment_date,
bgneal@316 65 datetime.datetime(2011, 1, 21, 4, 14, 8))
bgneal@316 66
bgneal@316 67 # user should have got a badge for donating
bgneal@316 68 p = user.get_profile()
bgneal@316 69 badges = list(p.badges.all())
bgneal@316 70 self.assertEqual(len(badges), 1)
bgneal@316 71 if len(badges) == 1:
bgneal@316 72 self.assertEqual(badges[0].numeric_id, bio.badges.CONTRIBUTOR_PIN)
bgneal@316 73
bgneal@316 74 def test_ipn_post_2(self):
bgneal@316 75 """
bgneal@316 76 Test a simulated IPN post
bgneal@316 77 """
bgneal@316 78 user = User.objects.create_user('test_user', 'test_user@example.com',
bgneal@316 79 'password')
bgneal@316 80 user.save()
bgneal@316 81
bgneal@316 82 args = urlparse.parse_qs(TEST_POST_DATA_2)
bgneal@316 83 response = self.client.post(reverse('donations-ipn'), args)
bgneal@316 84
bgneal@316 85 self.assertEqual(response.status_code, 200)
bgneal@316 86
bgneal@316 87 try:
bgneal@316 88 d = Donation.objects.get(pk=1)
bgneal@316 89 except Donation.DoesNotExist:
bgneal@316 90 self.fail("Donation object was not created")
bgneal@316 91 else:
bgneal@316 92 self.assertEqual(d.user, user)
bgneal@316 93 self.assertTrue(d.is_anonymous)
bgneal@316 94 self.assertFalse(d.test_ipn)
bgneal@316 95 self.assertEqual(d.txn_id, 'TESTTXNIDK548343A')
bgneal@316 96 self.assertEqual(d.txn_type, 'web_accept')
bgneal@316 97 self.assertEqual(d.first_name, 'John')
bgneal@316 98 self.assertEqual(d.last_name, 'Doe')
bgneal@316 99 self.assertEqual(d.payer_email, 'test_user@example.com')
bgneal@316 100 self.assertEqual(d.payer_id, 'FAKEPAYERID02')
bgneal@316 101 self.assertEqual(d.mc_fee, Decimal('3.20'))
bgneal@316 102 self.assertEqual(d.mc_gross, Decimal('100.00'))
bgneal@316 103 self.assertEqual(d.memo, '')
bgneal@316 104 self.assertEqual(d.payer_status, 'verified')
bgneal@316 105 self.assertEqual(d.payment_date,
bgneal@316 106 datetime.datetime(2011, 1, 16, 5, 40, 33))
bgneal@316 107
bgneal@316 108 # user should not have got a badge for donating
bgneal@316 109 p = user.get_profile()
bgneal@316 110 self.assertEqual(p.badges.count(), 0)