1
|
|
|
from django.contrib.auth import get_user_model |
2
|
|
|
from django.test import TestCase |
3
|
|
|
|
4
|
|
|
from simple_forums import forms, models |
5
|
|
|
from simple_forums.tests.testing_utils import create_thread, create_topic |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestThreadCreationForm(TestCase): |
9
|
|
|
""" Test the form used to create new threads """ |
10
|
|
|
|
11
|
|
|
def test_empty(self): |
12
|
|
|
""" Test validation of an empty form. |
13
|
|
|
|
14
|
|
|
If the form is empty, it should return errors about all the |
15
|
|
|
required fields. |
16
|
|
|
""" |
17
|
|
|
form = forms.ThreadCreationForm({}) |
18
|
|
|
|
19
|
|
|
expected_errors = { |
20
|
|
|
'topic': ['This field is required.'], |
21
|
|
|
'title': ['This field is required.'], |
22
|
|
|
'body': ['This field is required.'], |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
self.assertFalse(form.is_valid()) |
26
|
|
|
self.assertEqual(expected_errors, form.errors) |
27
|
|
|
|
28
|
|
|
def test_save(self): |
29
|
|
|
""" Test saving the form. |
30
|
|
|
|
31
|
|
|
Saving the form should create a new thread from the title field |
32
|
|
|
in the form, and a new message on that thread containing the |
33
|
|
|
body field from the form. |
34
|
|
|
""" |
35
|
|
|
topic = create_topic() |
36
|
|
|
data = { |
37
|
|
|
'topic': '%d' % topic.pk, |
38
|
|
|
'title': 'Test thread title', |
39
|
|
|
'body': 'Test message body', |
40
|
|
|
} |
41
|
|
|
form = forms.ThreadCreationForm(data) |
42
|
|
|
user = get_user_model().objects.create_user( |
43
|
|
|
username='test', |
44
|
|
|
password='test') |
45
|
|
|
|
46
|
|
|
form.save(user) |
47
|
|
|
|
48
|
|
|
self.assertEqual(1, models.Thread.objects.count()) |
49
|
|
|
self.assertEqual(1, models.Message.objects.count()) |
50
|
|
|
|
51
|
|
|
thread = models.Thread.objects.get() |
52
|
|
|
message = models.Message.objects.get() |
53
|
|
|
|
54
|
|
|
self.assertEqual(data['title'], thread.title) |
55
|
|
|
self.assertEqual(user, message.user) |
56
|
|
|
self.assertEqual(thread, message.thread) |
57
|
|
|
self.assertEqual(data['body'], message.body) |
58
|
|
|
|
59
|
|
|
def test_save_invalid_data(self): |
60
|
|
|
""" Test attempting to save an invalid form. |
61
|
|
|
|
62
|
|
|
Trying to save a form with invalid data should not create a new |
63
|
|
|
thread. |
64
|
|
|
""" |
65
|
|
|
data = { |
66
|
|
|
'foo': 'bar', |
67
|
|
|
'bar': 'foo', |
68
|
|
|
} |
69
|
|
|
form = forms.ThreadCreationForm(data) |
70
|
|
|
|
71
|
|
|
user = get_user_model().objects.create_user( |
72
|
|
|
username='test', |
73
|
|
|
password='test') |
74
|
|
|
|
75
|
|
|
form.save(user) |
76
|
|
|
|
77
|
|
|
self.assertEqual(0, models.Thread.objects.count()) |
78
|
|
|
self.assertEqual(0, models.Message.objects.count()) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
class TestThreadReplyForm(TestCase): |
82
|
|
|
""" Tests for form used to reply to threads """ |
83
|
|
|
|
84
|
|
|
def test_empty(self): |
85
|
|
|
""" Test validating an empty form. |
86
|
|
|
|
87
|
|
|
If the form is empty, errors should be raised for each required |
88
|
|
|
field. |
89
|
|
|
""" |
90
|
|
|
form = forms.ThreadReplyForm({}) |
91
|
|
|
|
92
|
|
|
expected_errors = { |
93
|
|
|
'body': ['This field is required.'], |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
self.assertTrue(form.is_bound) |
97
|
|
|
self.assertFalse(form.is_valid()) |
98
|
|
|
self.assertEqual(expected_errors, form.errors) |
99
|
|
|
|
100
|
|
|
def test_save(self): |
101
|
|
|
""" Test saving a valid form. |
102
|
|
|
|
103
|
|
|
Saving a valid form should create a new reply on the given |
104
|
|
|
thread. |
105
|
|
|
""" |
106
|
|
|
user = get_user_model().objects.create_user( |
107
|
|
|
username='test', |
108
|
|
|
password='test') |
109
|
|
|
thread = create_thread() |
110
|
|
|
|
111
|
|
|
data = { |
112
|
|
|
'body': 'Test body text.', |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
form = forms.ThreadReplyForm(data) |
116
|
|
|
message = form.save(user, thread) |
117
|
|
|
|
118
|
|
|
self.assertEqual(1, thread.message_set.count()) |
119
|
|
|
self.assertEqual(data['body'], message.body) |
120
|
|
|
|
121
|
|
|
def test_save_invalid_data(self): |
122
|
|
|
""" Test trying to save invalid data. |
123
|
|
|
|
124
|
|
|
Trying to save a form with invalid data should not create a new |
125
|
|
|
message. |
126
|
|
|
""" |
127
|
|
|
user = get_user_model().objects.create_user( |
128
|
|
|
username='test', |
129
|
|
|
password='test') |
130
|
|
|
thread = create_thread() |
131
|
|
|
|
132
|
|
|
form = forms.ThreadReplyForm({}) |
133
|
|
|
form.save(user, thread) |
134
|
|
|
|
135
|
|
|
self.assertEqual(0, models.Message.objects.count()) |
136
|
|
|
|