bgneal@468: """ bgneal@468: Tests for the views in the forums application. bgneal@468: bgneal@468: """ bgneal@468: from django.test import TestCase bgneal@468: from django.contrib.auth.models import User bgneal@468: from django.core.urlresolvers import reverse bgneal@468: bgneal@744: from forums.models import Forum, Topic bgneal@468: bgneal@468: bgneal@468: class ForumPostTestCase(TestCase): bgneal@468: fixtures = ['forums.json'] bgneal@468: bgneal@468: def setUp(self): bgneal@468: self.username = 'test_user' bgneal@468: self.pw = 'password' bgneal@468: self.user = User.objects.create_user(self.username, '', self.pw) bgneal@468: self.user.save() bgneal@468: self.assertTrue(self.client.login(username=self.username, bgneal@468: password=self.pw)) bgneal@468: bgneal@468: def tearDown(self): bgneal@468: self.client.logout() bgneal@468: bgneal@468: def testBasicForumsTest(self): bgneal@468: bgneal@468: forum_slug = 'shallow-end' bgneal@468: topic_name = 'A test topic' bgneal@468: topic_body = 'testing 1, 2, 3...' bgneal@468: bgneal@468: response = self.client.post( bgneal@468: reverse('forums-new_topic', kwargs={'slug': forum_slug}), bgneal@468: {'name': topic_name, 'body': topic_body}, bgneal@468: follow=True) bgneal@468: bgneal@468: self.assertEqual(len(response.redirect_chain), 1) bgneal@468: bgneal@468: if response.redirect_chain: bgneal@468: self.assertEqual(response.redirect_chain[0][0], bgneal@468: 'http://testserver' + reverse('forums-new_topic_thanks', bgneal@468: kwargs={'tid': '1'})) bgneal@468: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@468: bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: bgneal@468: forum = Forum.objects.get(slug=forum_slug) bgneal@468: try: bgneal@468: topic = Topic.objects.get(pk=1) bgneal@468: except Topic.DoesNotExist: bgneal@468: self.fail("topic doesn't exist") bgneal@468: bgneal@468: self.assertEqual(topic.forum.pk, forum.pk) bgneal@468: self.assertEqual(topic.user.pk, self.user.pk) bgneal@468: self.assertEqual(topic.name, topic_name) bgneal@468: self.assertEqual(topic.post_count, 1) bgneal@468: bgneal@468: post = topic.last_post bgneal@744: self.assertFalse(post is None) bgneal@468: bgneal@468: if post: bgneal@468: self.assertEqual(post.body, topic_body) bgneal@468: self.assertEqual(post.user.pk, self.user.pk) bgneal@468: bgneal@468: # post to the thread bgneal@468: response = self.client.get( bgneal@468: reverse('forums-topic_index', kwargs={'id': '1'})) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: bgneal@468: post2_body = 'test quick post' bgneal@468: response = self.client.post( bgneal@468: reverse('forums-quick_reply'), bgneal@468: {'body': post2_body, 'topic_id': 1}) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: try: bgneal@468: topic = Topic.objects.get(pk=1) bgneal@468: except Topic.DoesNotExist: bgneal@468: self.fail("topic doesn't exist") bgneal@468: bgneal@468: post = topic.last_post bgneal@744: self.assertFalse(post is None) bgneal@468: if post: bgneal@468: self.assertEqual(post.body, post2_body) bgneal@468: self.assertEqual(post.user.pk, self.user.pk) bgneal@468: self.assertEqual(topic.post_count, 2) bgneal@468: bgneal@468: # quote last post bgneal@468: response = self.client.get( bgneal@468: reverse('forums-new_post', kwargs={'topic_id': 1}), bgneal@468: {'quote_id': 2}) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: bgneal@468: post3_body = 'new post 3 content' bgneal@468: response = self.client.post( bgneal@468: reverse('forums-new_post', kwargs={'topic_id': 1}), bgneal@468: {'body': post3_body, 'post_id': 2}, bgneal@468: follow=True) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: try: bgneal@468: topic = Topic.objects.get(pk=1) bgneal@468: except Topic.DoesNotExist: bgneal@468: self.fail("topic doesn't exist") bgneal@468: bgneal@468: post = topic.last_post bgneal@744: self.assertFalse(post is None) bgneal@468: if post: bgneal@468: self.assertEqual(post.body, post3_body) bgneal@468: self.assertEqual(post.user.pk, self.user.pk) bgneal@468: self.assertEqual(topic.post_count, 3) bgneal@468: bgneal@468: # edit last post bgneal@468: response = self.client.get( bgneal@468: reverse('forums-edit_post', kwargs={'id': 3})) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: bgneal@468: post3_body = 'edited post 3 content' bgneal@468: response = self.client.post( bgneal@468: reverse('forums-edit_post', kwargs={'id': 3}), bgneal@468: {'body': post3_body}, bgneal@468: follow=True) bgneal@468: self.assertEqual(response.status_code, 200) bgneal@468: try: bgneal@468: topic = Topic.objects.get(pk=1) bgneal@468: except Topic.DoesNotExist: bgneal@468: self.fail("topic doesn't exist") bgneal@468: bgneal@468: post = topic.last_post bgneal@744: self.assertFalse(post is None) bgneal@468: if post: bgneal@468: self.assertEqual(post.body, post3_body) bgneal@468: self.assertEqual(post.user.pk, self.user.pk) bgneal@468: self.assertEqual(topic.post_count, 3) bgneal@468: bgneal@468: profile = self.user.get_profile() bgneal@468: self.assertEqual(profile.forum_post_count, 3)