annotate donations/tests.py @ 1201:fe10aea76cbd tip

Add 2023 MP3 compilation links
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Mar 2024 14:50:23 -0500
parents 9e803323a0d0
children
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@619 11 from django.conf import settings
bgneal@33 12
bgneal@316 13 from donations.models import Donation
bgneal@316 14 import bio.badges
bgneal@33 15
bgneal@316 16
bgneal@316 17 # This data was copy/pasted from my actual Paypal IPN history. Some alterations
bgneal@316 18 # were made since this file is getting committed to version control and I
bgneal@316 19 # didn't want to store "real" data that could be used to trace a transaction or
bgneal@316 20 # real payer.
bgneal@316 21
bgneal@316 22 # This data is for a non-anonymous donation:
bgneal@316 23 TEST_POST_DATA_1 = """\
bgneal@316 24 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 25
bgneal@316 26 # Data from a user that wanted to remain anonymous
bgneal@316 27 TEST_POST_DATA_2 = """\
bgneal@316 28 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 29
bgneal@316 30
bgneal@619 31 class DonationsIpnTestCase(TestCase):
bgneal@316 32 fixtures = ['badges']
bgneal@316 33
bgneal@316 34 def test_ipn_post_1(self):
bgneal@33 35 """
bgneal@316 36 Test a simulated IPN post
bgneal@33 37 """
bgneal@316 38 user = User.objects.create_user('test_user', 'test_user@example.com',
bgneal@316 39 'password')
bgneal@316 40 user.save()
bgneal@33 41
bgneal@316 42 args = urlparse.parse_qs(TEST_POST_DATA_1)
bgneal@316 43 response = self.client.post(reverse('donations-ipn'), args)
bgneal@33 44
bgneal@316 45 self.assertEqual(response.status_code, 200)
bgneal@33 46
bgneal@316 47 try:
bgneal@316 48 d = Donation.objects.get(pk=1)
bgneal@316 49 except Donation.DoesNotExist:
bgneal@316 50 self.fail("Donation object was not created")
bgneal@316 51 else:
bgneal@316 52 self.assertEqual(d.user, user)
bgneal@316 53 self.assertFalse(d.is_anonymous)
bgneal@316 54 self.assertFalse(d.test_ipn)
bgneal@316 55 self.assertEqual(d.txn_id, 'TESTTXNID5815921V')
bgneal@316 56 self.assertEqual(d.txn_type, 'web_accept')
bgneal@316 57 self.assertEqual(d.first_name, 'John')
bgneal@316 58 self.assertEqual(d.last_name, 'Doe')
bgneal@316 59 self.assertEqual(d.payer_email, 'test_user@example.com')
bgneal@316 60 self.assertEqual(d.payer_id, 'FAKEPAYERID01')
bgneal@316 61 self.assertEqual(d.mc_fee, Decimal('0.50'))
bgneal@316 62 self.assertEqual(d.mc_gross, Decimal('5.00'))
bgneal@316 63 self.assertEqual(d.memo, '')
bgneal@316 64 self.assertEqual(d.payer_status, 'verified')
bgneal@316 65 self.assertEqual(d.payment_date,
bgneal@316 66 datetime.datetime(2011, 1, 21, 4, 14, 8))
bgneal@316 67
bgneal@316 68 # user should have got a badge for donating
bgneal@789 69 p = user.profile
bgneal@316 70 badges = list(p.badges.all())
bgneal@316 71 self.assertEqual(len(badges), 1)
bgneal@316 72 if len(badges) == 1:
bgneal@316 73 self.assertEqual(badges[0].numeric_id, bio.badges.CONTRIBUTOR_PIN)
bgneal@316 74
bgneal@316 75 def test_ipn_post_2(self):
bgneal@316 76 """
bgneal@316 77 Test a simulated IPN post
bgneal@316 78 """
bgneal@316 79 user = User.objects.create_user('test_user', 'test_user@example.com',
bgneal@316 80 'password')
bgneal@316 81 user.save()
bgneal@316 82
bgneal@316 83 args = urlparse.parse_qs(TEST_POST_DATA_2)
bgneal@316 84 response = self.client.post(reverse('donations-ipn'), args)
bgneal@316 85
bgneal@316 86 self.assertEqual(response.status_code, 200)
bgneal@316 87
bgneal@316 88 try:
bgneal@316 89 d = Donation.objects.get(pk=1)
bgneal@316 90 except Donation.DoesNotExist:
bgneal@316 91 self.fail("Donation object was not created")
bgneal@316 92 else:
bgneal@316 93 self.assertEqual(d.user, user)
bgneal@316 94 self.assertTrue(d.is_anonymous)
bgneal@316 95 self.assertFalse(d.test_ipn)
bgneal@316 96 self.assertEqual(d.txn_id, 'TESTTXNIDK548343A')
bgneal@316 97 self.assertEqual(d.txn_type, 'web_accept')
bgneal@316 98 self.assertEqual(d.first_name, 'John')
bgneal@316 99 self.assertEqual(d.last_name, 'Doe')
bgneal@316 100 self.assertEqual(d.payer_email, 'test_user@example.com')
bgneal@316 101 self.assertEqual(d.payer_id, 'FAKEPAYERID02')
bgneal@316 102 self.assertEqual(d.mc_fee, Decimal('3.20'))
bgneal@316 103 self.assertEqual(d.mc_gross, Decimal('100.00'))
bgneal@316 104 self.assertEqual(d.memo, '')
bgneal@577 105 self.assertEqual(d.payer_status, 'unverified')
bgneal@316 106 self.assertEqual(d.payment_date,
bgneal@316 107 datetime.datetime(2011, 1, 16, 5, 40, 33))
bgneal@316 108
bgneal@316 109 # user should not have got a badge for donating
bgneal@789 110 p = user.profile
bgneal@316 111 self.assertEqual(p.badges.count(), 0)
bgneal@619 112
bgneal@619 113
bgneal@619 114 class DonationsManagerTestCase(TestCase):
bgneal@619 115 fixtures = ['donations_test']
bgneal@619 116
bgneal@619 117 def test_monthly_goal_pct(self):
bgneal@619 118
bgneal@785 119 self.assertEqual(settings.DONATIONS_GOAL, Decimal('120.00'))
bgneal@619 120 pct = Donation.objects.monthly_goal_pct(year=2012, month=9)
bgneal@785 121 self.assertEqual(pct, 19)
bgneal@619 122
bgneal@619 123 def test_monthly_goal_pct2(self):
bgneal@619 124
bgneal@619 125 # Ensure we get 0 when there are no records
bgneal@619 126
bgneal@619 127 Donation.objects.all().delete()
bgneal@619 128 pct = Donation.objects.monthly_goal_pct(year=2012, month=9)
bgneal@619 129 self.assertEqual(pct, 0)
bgneal@619 130
bgneal@619 131 def test_monthly_stats(self):
bgneal@619 132
bgneal@619 133 gross, net, donations = Donation.objects.monthly_stats(year=2012, month=9)
bgneal@619 134 self.assertEqual(gross, Decimal('25.00'))
bgneal@619 135 self.assertEqual(net, Decimal('23.47'))
bgneal@619 136 self.assertEqual(len(donations), 2)
bgneal@619 137
bgneal@619 138 def test_monthly_stats2(self):
bgneal@619 139
bgneal@619 140 # Ensure we get 0 when there are no records
bgneal@619 141
bgneal@619 142 Donation.objects.all().delete()
bgneal@619 143
bgneal@619 144 gross, net, donations = Donation.objects.monthly_stats(year=2012, month=9)
bgneal@619 145 self.assertEqual(gross, Decimal('0.00'))
bgneal@619 146 self.assertEqual(net, Decimal('0.00'))
bgneal@619 147 self.assertEqual(len(donations), 0)