bgneal@935: """Tests for the bio app's signal handlers.""" bgneal@935: bgneal@935: from collections import namedtuple bgneal@935: bgneal@935: from django.contrib.auth.models import User bgneal@935: from django.db.models.signals import post_save bgneal@935: from django.test import TestCase bgneal@935: bgneal@935: import bio.badges bgneal@935: from bio.models import Badge bgneal@935: from bio.models import BadgeOwnership bgneal@935: from bio.models import UserProfile bgneal@935: import custom_search.receivers bgneal@935: from donations.models import Donation bgneal@935: from downloads.models import Download bgneal@935: import downloads.receivers bgneal@935: from news.models import Story bgneal@935: from potd.models import Photo bgneal@935: import potd.receivers bgneal@935: from weblinks.models import Link bgneal@935: import weblinks.receivers bgneal@935: bgneal@935: bgneal@935: FakeDonation = namedtuple('FakeDonation', ['is_anonymous', 'user']) bgneal@935: FakeUserObject = namedtuple('FakeUserObject', ['user']) bgneal@935: FakeStory = namedtuple('FakeStory', ['submitter']) bgneal@935: bgneal@935: bgneal@935: class ReceiverTestCase(TestCase): bgneal@935: bgneal@935: fixtures = ['badges.json'] bgneal@935: bgneal@935: def setUp(self): bgneal@935: self.user = User.objects.create_user('user', 'user@example.com', 'pw') bgneal@935: bgneal@935: # Don't let our custom search signal handler class catch any of the bgneal@935: # signals we are throwing here. bgneal@935: custom_search.receivers.signal_processor.teardown() bgneal@935: bgneal@935: # Don't let these signal handlers fire either bgneal@935: post_save.disconnect(sender=Link, dispatch_uid='weblinks.receivers') bgneal@935: post_save.disconnect(sender=Download, dispatch_uid='downloads.receivers') bgneal@935: post_save.disconnect(sender=Photo, dispatch_uid='potd.receivers') bgneal@935: bgneal@935: def tearDown(self): bgneal@935: custom_search.receivers.signal_processor.setup() bgneal@935: post_save.connect(weblinks.receivers.on_link_save, sender=Link, bgneal@935: dispatch_uid='weblinks.receivers') bgneal@935: post_save.connect(downloads.receivers.on_download_save, sender=Download, bgneal@935: dispatch_uid='downloads.receivers') bgneal@935: post_save.connect(potd.receivers.on_photo_save, sender=Photo, bgneal@935: dispatch_uid='potd.receivers') bgneal@935: bgneal@935: def test_profile_creation(self): bgneal@935: profile = UserProfile.objects.get(user=self.user) bgneal@935: self.assertEqual(self.user.profile, profile) bgneal@935: bgneal@935: def test_donation_created(self): bgneal@935: donation = FakeDonation(False, self.user) bgneal@935: post_save.send(sender=Donation, created=True, instance=donation) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) bgneal@935: ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) bgneal@935: self.assertEqual(ownership.count, 1) bgneal@935: bgneal@935: def test_donation_updated(self): bgneal@935: donation = FakeDonation(False, self.user) bgneal@935: post_save.send(sender=Donation, created=False, instance=donation) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_donation_anonymous(self): bgneal@935: donation = FakeDonation(True, self.user) bgneal@935: post_save.send(sender=Donation, created=False, instance=donation) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_donation_no_user(self): bgneal@935: donation = FakeDonation(False, None) bgneal@935: post_save.send(sender=Donation, created=False, instance=donation) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_donation_anon_and_no_user(self): bgneal@935: donation = FakeDonation(True, None) bgneal@935: post_save.send(sender=Donation, created=False, instance=donation) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_link_created(self): bgneal@935: link = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Link, created=True, instance=link) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN) bgneal@935: ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) bgneal@935: self.assertEqual(ownership.count, 1) bgneal@935: bgneal@935: def test_link_updated(self): bgneal@935: link = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Link, created=False, instance=link) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_download_created(self): bgneal@935: dl = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Download, created=True, instance=dl) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN) bgneal@935: ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) bgneal@935: self.assertEqual(ownership.count, 1) bgneal@935: bgneal@935: def test_download_updated(self): bgneal@935: dl = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Download, created=False, instance=dl) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_story_created(self): bgneal@935: story = FakeStory(self.user) bgneal@935: post_save.send(sender=Story, created=True, instance=story) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN) bgneal@935: ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) bgneal@935: self.assertEqual(ownership.count, 1) bgneal@935: bgneal@935: def test_story_updated(self): bgneal@935: story = FakeStory(self.user) bgneal@935: post_save.send(sender=Story, created=False, instance=story) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile) bgneal@935: bgneal@935: def test_photo_created(self): bgneal@935: photo = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Photo, created=True, instance=photo) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN) bgneal@935: ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) bgneal@935: self.assertEqual(ownership.count, 1) bgneal@935: bgneal@935: def test_photo_updated(self): bgneal@935: photo = FakeUserObject(self.user) bgneal@935: post_save.send(sender=Photo, created=False, instance=photo) bgneal@935: bgneal@935: badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN) bgneal@935: self.assertRaises(BadgeOwnership.DoesNotExist, bgneal@935: BadgeOwnership.objects.get, bgneal@935: badge=badge, profile=self.user.profile)