bgneal@935
|
1 """Tests for the bio app's signal handlers."""
|
bgneal@935
|
2
|
bgneal@935
|
3 from collections import namedtuple
|
bgneal@935
|
4
|
bgneal@935
|
5 from django.contrib.auth.models import User
|
bgneal@935
|
6 from django.db.models.signals import post_save
|
bgneal@935
|
7 from django.test import TestCase
|
bgneal@935
|
8
|
bgneal@935
|
9 import bio.badges
|
bgneal@935
|
10 from bio.models import Badge
|
bgneal@935
|
11 from bio.models import BadgeOwnership
|
bgneal@935
|
12 from bio.models import UserProfile
|
bgneal@935
|
13 import custom_search.receivers
|
bgneal@935
|
14 from donations.models import Donation
|
bgneal@935
|
15 from downloads.models import Download
|
bgneal@935
|
16 import downloads.receivers
|
bgneal@935
|
17 from news.models import Story
|
bgneal@935
|
18 from potd.models import Photo
|
bgneal@935
|
19 import potd.receivers
|
bgneal@935
|
20 from weblinks.models import Link
|
bgneal@935
|
21 import weblinks.receivers
|
bgneal@935
|
22
|
bgneal@935
|
23
|
bgneal@935
|
24 FakeDonation = namedtuple('FakeDonation', ['is_anonymous', 'user'])
|
bgneal@935
|
25 FakeUserObject = namedtuple('FakeUserObject', ['user'])
|
bgneal@935
|
26 FakeStory = namedtuple('FakeStory', ['submitter'])
|
bgneal@935
|
27
|
bgneal@935
|
28
|
bgneal@935
|
29 class ReceiverTestCase(TestCase):
|
bgneal@935
|
30
|
bgneal@935
|
31 fixtures = ['badges.json']
|
bgneal@935
|
32
|
bgneal@935
|
33 def setUp(self):
|
bgneal@935
|
34 self.user = User.objects.create_user('user', 'user@example.com', 'pw')
|
bgneal@935
|
35
|
bgneal@935
|
36 # Don't let our custom search signal handler class catch any of the
|
bgneal@935
|
37 # signals we are throwing here.
|
bgneal@935
|
38 custom_search.receivers.signal_processor.teardown()
|
bgneal@935
|
39
|
bgneal@935
|
40 # Don't let these signal handlers fire either
|
bgneal@935
|
41 post_save.disconnect(sender=Link, dispatch_uid='weblinks.receivers')
|
bgneal@935
|
42 post_save.disconnect(sender=Download, dispatch_uid='downloads.receivers')
|
bgneal@935
|
43 post_save.disconnect(sender=Photo, dispatch_uid='potd.receivers')
|
bgneal@935
|
44
|
bgneal@935
|
45 def tearDown(self):
|
bgneal@935
|
46 custom_search.receivers.signal_processor.setup()
|
bgneal@935
|
47 post_save.connect(weblinks.receivers.on_link_save, sender=Link,
|
bgneal@935
|
48 dispatch_uid='weblinks.receivers')
|
bgneal@935
|
49 post_save.connect(downloads.receivers.on_download_save, sender=Download,
|
bgneal@935
|
50 dispatch_uid='downloads.receivers')
|
bgneal@935
|
51 post_save.connect(potd.receivers.on_photo_save, sender=Photo,
|
bgneal@935
|
52 dispatch_uid='potd.receivers')
|
bgneal@935
|
53
|
bgneal@935
|
54 def test_profile_creation(self):
|
bgneal@935
|
55 profile = UserProfile.objects.get(user=self.user)
|
bgneal@935
|
56 self.assertEqual(self.user.profile, profile)
|
bgneal@935
|
57
|
bgneal@935
|
58 def test_donation_created(self):
|
bgneal@935
|
59 donation = FakeDonation(False, self.user)
|
bgneal@935
|
60 post_save.send(sender=Donation, created=True, instance=donation)
|
bgneal@935
|
61
|
bgneal@935
|
62 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
|
bgneal@935
|
63 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
|
bgneal@935
|
64 self.assertEqual(ownership.count, 1)
|
bgneal@935
|
65
|
bgneal@935
|
66 def test_donation_updated(self):
|
bgneal@935
|
67 donation = FakeDonation(False, self.user)
|
bgneal@935
|
68 post_save.send(sender=Donation, created=False, instance=donation)
|
bgneal@935
|
69
|
bgneal@935
|
70 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
|
bgneal@935
|
71 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
72 BadgeOwnership.objects.get,
|
bgneal@935
|
73 badge=badge, profile=self.user.profile)
|
bgneal@935
|
74
|
bgneal@935
|
75 def test_donation_anonymous(self):
|
bgneal@935
|
76 donation = FakeDonation(True, self.user)
|
bgneal@935
|
77 post_save.send(sender=Donation, created=False, instance=donation)
|
bgneal@935
|
78
|
bgneal@935
|
79 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
|
bgneal@935
|
80 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
81 BadgeOwnership.objects.get,
|
bgneal@935
|
82 badge=badge, profile=self.user.profile)
|
bgneal@935
|
83
|
bgneal@935
|
84 def test_donation_no_user(self):
|
bgneal@935
|
85 donation = FakeDonation(False, None)
|
bgneal@935
|
86 post_save.send(sender=Donation, created=False, instance=donation)
|
bgneal@935
|
87
|
bgneal@935
|
88 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
|
bgneal@935
|
89 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
90 BadgeOwnership.objects.get,
|
bgneal@935
|
91 badge=badge, profile=self.user.profile)
|
bgneal@935
|
92
|
bgneal@935
|
93 def test_donation_anon_and_no_user(self):
|
bgneal@935
|
94 donation = FakeDonation(True, None)
|
bgneal@935
|
95 post_save.send(sender=Donation, created=False, instance=donation)
|
bgneal@935
|
96
|
bgneal@935
|
97 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
|
bgneal@935
|
98 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
99 BadgeOwnership.objects.get,
|
bgneal@935
|
100 badge=badge, profile=self.user.profile)
|
bgneal@935
|
101
|
bgneal@935
|
102 def test_link_created(self):
|
bgneal@935
|
103 link = FakeUserObject(self.user)
|
bgneal@935
|
104 post_save.send(sender=Link, created=True, instance=link)
|
bgneal@935
|
105
|
bgneal@935
|
106 badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN)
|
bgneal@935
|
107 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
|
bgneal@935
|
108 self.assertEqual(ownership.count, 1)
|
bgneal@935
|
109
|
bgneal@935
|
110 def test_link_updated(self):
|
bgneal@935
|
111 link = FakeUserObject(self.user)
|
bgneal@935
|
112 post_save.send(sender=Link, created=False, instance=link)
|
bgneal@935
|
113
|
bgneal@935
|
114 badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN)
|
bgneal@935
|
115 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
116 BadgeOwnership.objects.get,
|
bgneal@935
|
117 badge=badge, profile=self.user.profile)
|
bgneal@935
|
118
|
bgneal@935
|
119 def test_download_created(self):
|
bgneal@935
|
120 dl = FakeUserObject(self.user)
|
bgneal@935
|
121 post_save.send(sender=Download, created=True, instance=dl)
|
bgneal@935
|
122
|
bgneal@935
|
123 badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN)
|
bgneal@935
|
124 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
|
bgneal@935
|
125 self.assertEqual(ownership.count, 1)
|
bgneal@935
|
126
|
bgneal@935
|
127 def test_download_updated(self):
|
bgneal@935
|
128 dl = FakeUserObject(self.user)
|
bgneal@935
|
129 post_save.send(sender=Download, created=False, instance=dl)
|
bgneal@935
|
130
|
bgneal@935
|
131 badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN)
|
bgneal@935
|
132 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
133 BadgeOwnership.objects.get,
|
bgneal@935
|
134 badge=badge, profile=self.user.profile)
|
bgneal@935
|
135
|
bgneal@935
|
136 def test_story_created(self):
|
bgneal@935
|
137 story = FakeStory(self.user)
|
bgneal@935
|
138 post_save.send(sender=Story, created=True, instance=story)
|
bgneal@935
|
139
|
bgneal@935
|
140 badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN)
|
bgneal@935
|
141 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
|
bgneal@935
|
142 self.assertEqual(ownership.count, 1)
|
bgneal@935
|
143
|
bgneal@935
|
144 def test_story_updated(self):
|
bgneal@935
|
145 story = FakeStory(self.user)
|
bgneal@935
|
146 post_save.send(sender=Story, created=False, instance=story)
|
bgneal@935
|
147
|
bgneal@935
|
148 badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN)
|
bgneal@935
|
149 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
150 BadgeOwnership.objects.get,
|
bgneal@935
|
151 badge=badge, profile=self.user.profile)
|
bgneal@935
|
152
|
bgneal@935
|
153 def test_photo_created(self):
|
bgneal@935
|
154 photo = FakeUserObject(self.user)
|
bgneal@935
|
155 post_save.send(sender=Photo, created=True, instance=photo)
|
bgneal@935
|
156
|
bgneal@935
|
157 badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN)
|
bgneal@935
|
158 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
|
bgneal@935
|
159 self.assertEqual(ownership.count, 1)
|
bgneal@935
|
160
|
bgneal@935
|
161 def test_photo_updated(self):
|
bgneal@935
|
162 photo = FakeUserObject(self.user)
|
bgneal@935
|
163 post_save.send(sender=Photo, created=False, instance=photo)
|
bgneal@935
|
164
|
bgneal@935
|
165 badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN)
|
bgneal@935
|
166 self.assertRaises(BadgeOwnership.DoesNotExist,
|
bgneal@935
|
167 BadgeOwnership.objects.get,
|
bgneal@935
|
168 badge=badge, profile=self.user.profile)
|