comparison gpp/forums/tests/view_tests.py @ 468:ad4b63fbc584

Adding a basic forums test that exercises adding a new topic, post, and editing a post.
author Brian Neal <bgneal@gmail.com>
date Sun, 07 Aug 2011 03:38:42 +0000
parents
children
comparison
equal deleted inserted replaced
467:b910cc1460c8 468:ad4b63fbc584
1 """
2 Tests for the views in the forums application.
3
4 """
5 from django.test import TestCase
6 from django.contrib.auth.models import User
7 from django.core.urlresolvers import reverse
8
9 from forums.models import Forum, Topic, Post
10
11
12 class ForumPostTestCase(TestCase):
13 fixtures = ['forums.json']
14
15 def setUp(self):
16 self.username = 'test_user'
17 self.pw = 'password'
18 self.user = User.objects.create_user(self.username, '', self.pw)
19 self.user.save()
20 self.assertTrue(self.client.login(username=self.username,
21 password=self.pw))
22
23 def tearDown(self):
24 self.client.logout()
25
26 def testBasicForumsTest(self):
27
28 forum_slug = 'shallow-end'
29 topic_name = 'A test topic'
30 topic_body = 'testing 1, 2, 3...'
31
32 response = self.client.post(
33 reverse('forums-new_topic', kwargs={'slug': forum_slug}),
34 {'name': topic_name, 'body': topic_body},
35 follow=True)
36
37 self.assertEqual(len(response.redirect_chain), 1)
38
39 if response.redirect_chain:
40 self.assertEqual(response.redirect_chain[0][0],
41 'http://testserver' + reverse('forums-new_topic_thanks',
42 kwargs={'tid': '1'}))
43 self.assertEqual(response.redirect_chain[0][1], 302)
44
45 self.assertEqual(response.status_code, 200)
46
47 forum = Forum.objects.get(slug=forum_slug)
48 try:
49 topic = Topic.objects.get(pk=1)
50 except Topic.DoesNotExist:
51 self.fail("topic doesn't exist")
52
53 self.assertEqual(topic.forum.pk, forum.pk)
54 self.assertEqual(topic.user.pk, self.user.pk)
55 self.assertEqual(topic.name, topic_name)
56 self.assertEqual(topic.post_count, 1)
57
58 post = topic.last_post
59 self.failIf(post is None)
60
61 if post:
62 self.assertEqual(post.body, topic_body)
63 self.assertEqual(post.user.pk, self.user.pk)
64
65 # post to the thread
66 response = self.client.get(
67 reverse('forums-topic_index', kwargs={'id': '1'}))
68 self.assertEqual(response.status_code, 200)
69
70 post2_body = 'test quick post'
71 response = self.client.post(
72 reverse('forums-quick_reply'),
73 {'body': post2_body, 'topic_id': 1})
74 self.assertEqual(response.status_code, 200)
75 try:
76 topic = Topic.objects.get(pk=1)
77 except Topic.DoesNotExist:
78 self.fail("topic doesn't exist")
79
80 post = topic.last_post
81 self.failIf(post is None)
82 if post:
83 self.assertEqual(post.body, post2_body)
84 self.assertEqual(post.user.pk, self.user.pk)
85 self.assertEqual(topic.post_count, 2)
86
87 # quote last post
88 response = self.client.get(
89 reverse('forums-new_post', kwargs={'topic_id': 1}),
90 {'quote_id': 2})
91 self.assertEqual(response.status_code, 200)
92
93 post3_body = 'new post 3 content'
94 response = self.client.post(
95 reverse('forums-new_post', kwargs={'topic_id': 1}),
96 {'body': post3_body, 'post_id': 2},
97 follow=True)
98 self.assertEqual(response.status_code, 200)
99 try:
100 topic = Topic.objects.get(pk=1)
101 except Topic.DoesNotExist:
102 self.fail("topic doesn't exist")
103
104 post = topic.last_post
105 self.failIf(post is None)
106 if post:
107 self.assertEqual(post.body, post3_body)
108 self.assertEqual(post.user.pk, self.user.pk)
109 self.assertEqual(topic.post_count, 3)
110
111 # edit last post
112 response = self.client.get(
113 reverse('forums-edit_post', kwargs={'id': 3}))
114 self.assertEqual(response.status_code, 200)
115
116 post3_body = 'edited post 3 content'
117 response = self.client.post(
118 reverse('forums-edit_post', kwargs={'id': 3}),
119 {'body': post3_body},
120 follow=True)
121 self.assertEqual(response.status_code, 200)
122 try:
123 topic = Topic.objects.get(pk=1)
124 except Topic.DoesNotExist:
125 self.fail("topic doesn't exist")
126
127 post = topic.last_post
128 self.failIf(post is None)
129 if post:
130 self.assertEqual(post.body, post3_body)
131 self.assertEqual(post.user.pk, self.user.pk)
132 self.assertEqual(topic.post_count, 3)
133
134 profile = self.user.get_profile()
135 self.assertEqual(profile.forum_post_count, 3)