annotate forums/tests/test_receivers.py @ 1172:b957e4829a03

Add reCAPTCHA to contact form
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2018 13:53:05 -0500
parents 8b48ec450497
children
rev   line source
bgneal@936 1 """Tests for the forum app's signal handlers."""
bgneal@936 2
bgneal@936 3 from django.contrib.auth.models import User
bgneal@936 4 from django.test import TestCase
bgneal@936 5
bgneal@936 6 from mock import patch
bgneal@936 7
bgneal@936 8 import custom_search.receivers
bgneal@936 9
bgneal@936 10 from forums.models import Forum
bgneal@936 11 from forums.models import Post
bgneal@936 12 from forums.models import Topic
bgneal@936 13
bgneal@936 14
bgneal@936 15 class ReceiverTestCase(TestCase):
bgneal@936 16
bgneal@936 17 fixtures = ['forums.json']
bgneal@936 18
bgneal@936 19 def setUp(self):
bgneal@936 20 self.user = User.objects.create_user('user', 'user@example.com', 'pw')
bgneal@936 21
bgneal@936 22 # Don't let our custom search signal handler class catch any of the
bgneal@936 23 # signals we are throwing here.
bgneal@936 24 custom_search.receivers.signal_processor.teardown()
bgneal@936 25
bgneal@936 26 def tearDown(self):
bgneal@936 27 custom_search.receivers.signal_processor.setup()
bgneal@936 28
bgneal@936 29 @patch('forums.latest.notify_topic_delete')
bgneal@936 30 def test_signal_handlers(self, mock_latest):
bgneal@936 31 forum = Forum.objects.get(pk=2)
bgneal@936 32 topic = Topic(forum=forum,
bgneal@936 33 name='Test Topic',
bgneal@936 34 user=self.user)
bgneal@936 35 topic.save()
bgneal@936 36 forum = Forum.objects.get(pk=2)
bgneal@936 37 self.assertEqual(1, forum.topic_count)
bgneal@936 38
bgneal@936 39 post = Post(topic=topic, user=self.user, body='test')
bgneal@936 40 post.save()
bgneal@936 41
bgneal@936 42 topic = Topic.objects.get(pk=topic.pk)
bgneal@936 43 self.assertEqual(topic.post_count, 1)
bgneal@936 44 forum = Forum.objects.get(pk=2)
bgneal@936 45 self.assertEqual(forum.post_count, 1)
bgneal@936 46
bgneal@936 47 # To prevent cascading deletes
bgneal@936 48 topic.last_post_pre_delete()
bgneal@936 49 topic.save()
bgneal@936 50 forum.last_post_pre_delete()
bgneal@936 51 forum.save()
bgneal@936 52
bgneal@936 53 post.delete()
bgneal@936 54 topic = Topic.objects.get(pk=topic.pk)
bgneal@936 55 self.assertEqual(topic.post_count, 0)
bgneal@936 56 forum = Forum.objects.get(pk=2)
bgneal@936 57 self.assertEqual(forum.post_count, 0)
bgneal@936 58
bgneal@936 59 topic.delete()
bgneal@936 60 forum = Forum.objects.get(pk=2)
bgneal@936 61 self.assertEqual(0, forum.topic_count)
bgneal@936 62
bgneal@936 63 mock_latest.assert_called_once_with(topic)