annotate forums/tests/view_tests.py @ 629:f4c043cf55ac

Wiki integration. Requests don't always have sessions. In particular this occurs when a request is made without a trailing slash. The Common middleware redirects when this happens, and the middleware process_request() processing stops before a session can get added. So just set an attribute on the request object for each operation. This seemed weird to me at first, but there are plenty of examples of this in the Django code base already.
author Brian Neal <bgneal@gmail.com>
date Tue, 13 Nov 2012 13:50:06 -0600
parents ee87ea74d46b
children
rev   line source
bgneal@468 1 """
bgneal@468 2 Tests for the views in the forums application.
bgneal@468 3
bgneal@468 4 """
bgneal@468 5 from django.test import TestCase
bgneal@468 6 from django.contrib.auth.models import User
bgneal@468 7 from django.core.urlresolvers import reverse
bgneal@468 8
bgneal@468 9 from forums.models import Forum, Topic, Post
bgneal@468 10
bgneal@468 11
bgneal@468 12 class ForumPostTestCase(TestCase):
bgneal@468 13 fixtures = ['forums.json']
bgneal@468 14
bgneal@468 15 def setUp(self):
bgneal@468 16 self.username = 'test_user'
bgneal@468 17 self.pw = 'password'
bgneal@468 18 self.user = User.objects.create_user(self.username, '', self.pw)
bgneal@468 19 self.user.save()
bgneal@468 20 self.assertTrue(self.client.login(username=self.username,
bgneal@468 21 password=self.pw))
bgneal@468 22
bgneal@468 23 def tearDown(self):
bgneal@468 24 self.client.logout()
bgneal@468 25
bgneal@468 26 def testBasicForumsTest(self):
bgneal@468 27
bgneal@468 28 forum_slug = 'shallow-end'
bgneal@468 29 topic_name = 'A test topic'
bgneal@468 30 topic_body = 'testing 1, 2, 3...'
bgneal@468 31
bgneal@468 32 response = self.client.post(
bgneal@468 33 reverse('forums-new_topic', kwargs={'slug': forum_slug}),
bgneal@468 34 {'name': topic_name, 'body': topic_body},
bgneal@468 35 follow=True)
bgneal@468 36
bgneal@468 37 self.assertEqual(len(response.redirect_chain), 1)
bgneal@468 38
bgneal@468 39 if response.redirect_chain:
bgneal@468 40 self.assertEqual(response.redirect_chain[0][0],
bgneal@468 41 'http://testserver' + reverse('forums-new_topic_thanks',
bgneal@468 42 kwargs={'tid': '1'}))
bgneal@468 43 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@468 44
bgneal@468 45 self.assertEqual(response.status_code, 200)
bgneal@468 46
bgneal@468 47 forum = Forum.objects.get(slug=forum_slug)
bgneal@468 48 try:
bgneal@468 49 topic = Topic.objects.get(pk=1)
bgneal@468 50 except Topic.DoesNotExist:
bgneal@468 51 self.fail("topic doesn't exist")
bgneal@468 52
bgneal@468 53 self.assertEqual(topic.forum.pk, forum.pk)
bgneal@468 54 self.assertEqual(topic.user.pk, self.user.pk)
bgneal@468 55 self.assertEqual(topic.name, topic_name)
bgneal@468 56 self.assertEqual(topic.post_count, 1)
bgneal@468 57
bgneal@468 58 post = topic.last_post
bgneal@468 59 self.failIf(post is None)
bgneal@468 60
bgneal@468 61 if post:
bgneal@468 62 self.assertEqual(post.body, topic_body)
bgneal@468 63 self.assertEqual(post.user.pk, self.user.pk)
bgneal@468 64
bgneal@468 65 # post to the thread
bgneal@468 66 response = self.client.get(
bgneal@468 67 reverse('forums-topic_index', kwargs={'id': '1'}))
bgneal@468 68 self.assertEqual(response.status_code, 200)
bgneal@468 69
bgneal@468 70 post2_body = 'test quick post'
bgneal@468 71 response = self.client.post(
bgneal@468 72 reverse('forums-quick_reply'),
bgneal@468 73 {'body': post2_body, 'topic_id': 1})
bgneal@468 74 self.assertEqual(response.status_code, 200)
bgneal@468 75 try:
bgneal@468 76 topic = Topic.objects.get(pk=1)
bgneal@468 77 except Topic.DoesNotExist:
bgneal@468 78 self.fail("topic doesn't exist")
bgneal@468 79
bgneal@468 80 post = topic.last_post
bgneal@468 81 self.failIf(post is None)
bgneal@468 82 if post:
bgneal@468 83 self.assertEqual(post.body, post2_body)
bgneal@468 84 self.assertEqual(post.user.pk, self.user.pk)
bgneal@468 85 self.assertEqual(topic.post_count, 2)
bgneal@468 86
bgneal@468 87 # quote last post
bgneal@468 88 response = self.client.get(
bgneal@468 89 reverse('forums-new_post', kwargs={'topic_id': 1}),
bgneal@468 90 {'quote_id': 2})
bgneal@468 91 self.assertEqual(response.status_code, 200)
bgneal@468 92
bgneal@468 93 post3_body = 'new post 3 content'
bgneal@468 94 response = self.client.post(
bgneal@468 95 reverse('forums-new_post', kwargs={'topic_id': 1}),
bgneal@468 96 {'body': post3_body, 'post_id': 2},
bgneal@468 97 follow=True)
bgneal@468 98 self.assertEqual(response.status_code, 200)
bgneal@468 99 try:
bgneal@468 100 topic = Topic.objects.get(pk=1)
bgneal@468 101 except Topic.DoesNotExist:
bgneal@468 102 self.fail("topic doesn't exist")
bgneal@468 103
bgneal@468 104 post = topic.last_post
bgneal@468 105 self.failIf(post is None)
bgneal@468 106 if post:
bgneal@468 107 self.assertEqual(post.body, post3_body)
bgneal@468 108 self.assertEqual(post.user.pk, self.user.pk)
bgneal@468 109 self.assertEqual(topic.post_count, 3)
bgneal@468 110
bgneal@468 111 # edit last post
bgneal@468 112 response = self.client.get(
bgneal@468 113 reverse('forums-edit_post', kwargs={'id': 3}))
bgneal@468 114 self.assertEqual(response.status_code, 200)
bgneal@468 115
bgneal@468 116 post3_body = 'edited post 3 content'
bgneal@468 117 response = self.client.post(
bgneal@468 118 reverse('forums-edit_post', kwargs={'id': 3}),
bgneal@468 119 {'body': post3_body},
bgneal@468 120 follow=True)
bgneal@468 121 self.assertEqual(response.status_code, 200)
bgneal@468 122 try:
bgneal@468 123 topic = Topic.objects.get(pk=1)
bgneal@468 124 except Topic.DoesNotExist:
bgneal@468 125 self.fail("topic doesn't exist")
bgneal@468 126
bgneal@468 127 post = topic.last_post
bgneal@468 128 self.failIf(post is None)
bgneal@468 129 if post:
bgneal@468 130 self.assertEqual(post.body, post3_body)
bgneal@468 131 self.assertEqual(post.user.pk, self.user.pk)
bgneal@468 132 self.assertEqual(topic.post_count, 3)
bgneal@468 133
bgneal@468 134 profile = self.user.get_profile()
bgneal@468 135 self.assertEqual(profile.forum_post_count, 3)