Show More
@@ -1,233 +1,233 b'' | |||
|
1 | 1 | # coding=utf-8 |
|
2 | 2 | from django.test import TestCase |
|
3 | 3 | from django.test.client import Client |
|
4 | 4 | import time |
|
5 | 5 | |
|
6 | 6 | from boards.models import Post, Tag |
|
7 | 7 | from neboard import settings |
|
8 | 8 | |
|
9 | 9 | PAGE_404 = 'boards/404.html' |
|
10 | 10 | |
|
11 | 11 | TEST_TEXT = 'test text' |
|
12 | 12 | |
|
13 | 13 | NEW_THREAD_PAGE = '/' |
|
14 | 14 | THREAD_PAGE_ONE = '/thread/1/' |
|
15 | 15 | THREAD_PAGE = '/thread/' |
|
16 | 16 | TAG_PAGE = '/tag/' |
|
17 | 17 | HTTP_CODE_REDIRECT = 302 |
|
18 | 18 | HTTP_CODE_OK = 200 |
|
19 | 19 | HTTP_CODE_NOT_FOUND = 404 |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | class PostTests(TestCase): |
|
23 | 23 | |
|
24 | 24 | def _create_post(self): |
|
25 | 25 | return Post.objects.create_post(title='title', |
|
26 | 26 | text='text') |
|
27 | 27 | |
|
28 | 28 | def test_post_add(self): |
|
29 | 29 | """Test adding post""" |
|
30 | 30 | |
|
31 | 31 | post = self._create_post() |
|
32 | 32 | |
|
33 | 33 | self.assertIsNotNone(post, 'No post was created') |
|
34 | 34 | |
|
35 | 35 | def test_delete_post(self): |
|
36 | 36 | """Test post deletion""" |
|
37 | 37 | |
|
38 | 38 | post = self._create_post() |
|
39 | 39 | post_id = post.id |
|
40 | 40 | |
|
41 | 41 | Post.objects.delete_post(post) |
|
42 | 42 | |
|
43 | 43 | self.assertFalse(Post.objects.filter(id=post_id).exists()) |
|
44 | 44 | |
|
45 | 45 | def test_delete_thread(self): |
|
46 | 46 | """Test thread deletion""" |
|
47 | 47 | |
|
48 | 48 | opening_post = self._create_post() |
|
49 | 49 | thread = opening_post.thread_new |
|
50 | 50 | reply = Post.objects.create_post("", "", thread=thread) |
|
51 | 51 | |
|
52 | 52 | thread.delete_with_posts() |
|
53 | 53 | |
|
54 | 54 | self.assertFalse(Post.objects.filter(id=reply.id).exists()) |
|
55 | 55 | |
|
56 | 56 | def test_post_to_thread(self): |
|
57 | 57 | """Test adding post to a thread""" |
|
58 | 58 | |
|
59 | 59 | op = self._create_post() |
|
60 | 60 | post = Post.objects.create_post("", "", thread=op.thread_new) |
|
61 | 61 | |
|
62 | 62 | self.assertIsNotNone(post, 'Reply to thread wasn\'t created') |
|
63 | 63 | self.assertEqual(op.thread_new.last_edit_time, post.pub_time, |
|
64 | 64 | 'Post\'s create time doesn\'t match thread last edit' |
|
65 | 65 | ' time') |
|
66 | 66 | |
|
67 | 67 | def test_delete_posts_by_ip(self): |
|
68 | 68 | """Test deleting posts with the given ip""" |
|
69 | 69 | |
|
70 | 70 | post = self._create_post() |
|
71 | 71 | post_id = post.id |
|
72 | 72 | |
|
73 | 73 | Post.objects.delete_posts_by_ip('0.0.0.0') |
|
74 | 74 | |
|
75 | 75 | self.assertFalse(Post.objects.filter(id=post_id).exists()) |
|
76 | 76 | |
|
77 | 77 | def test_get_thread(self): |
|
78 | 78 | """Test getting all posts of a thread""" |
|
79 | 79 | |
|
80 | 80 | opening_post = self._create_post() |
|
81 | 81 | |
|
82 | 82 | for i in range(0, 2): |
|
83 | 83 | Post.objects.create_post('title', 'text', |
|
84 | 84 | thread=opening_post.thread_new) |
|
85 | 85 | |
|
86 | 86 | thread = opening_post.thread_new |
|
87 | 87 | |
|
88 | 88 | self.assertEqual(3, thread.replies.count()) |
|
89 | 89 | |
|
90 | 90 | def test_create_post_with_tag(self): |
|
91 | 91 | """Test adding tag to post""" |
|
92 | 92 | |
|
93 | 93 | tag = Tag.objects.create(name='test_tag') |
|
94 | 94 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) |
|
95 | 95 | |
|
96 | 96 | thread = post.thread_new |
|
97 | 97 | self.assertIsNotNone(post, 'Post not created') |
|
98 | 98 | self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread') |
|
99 | 99 | self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag') |
|
100 | 100 | |
|
101 | 101 | def test_thread_max_count(self): |
|
102 | 102 | """Test deletion of old posts when the max thread count is reached""" |
|
103 | 103 | |
|
104 | 104 | for i in range(settings.MAX_THREAD_COUNT + 1): |
|
105 | 105 | self._create_post() |
|
106 | 106 | |
|
107 | 107 | self.assertEqual(settings.MAX_THREAD_COUNT, |
|
108 | 108 | len(Post.objects.get_threads())) |
|
109 | 109 | |
|
110 | 110 | def test_pages(self): |
|
111 | 111 | """Test that the thread list is properly split into pages""" |
|
112 | 112 | |
|
113 | 113 | for i in range(settings.MAX_THREAD_COUNT): |
|
114 | 114 | self._create_post() |
|
115 | 115 | |
|
116 | 116 | all_threads = Post.objects.get_threads() |
|
117 | 117 | |
|
118 |
posts_in_second_page = Post.objects.get_threads(page= |
|
|
118 | posts_in_second_page = Post.objects.get_threads(page=2) | |
|
119 | 119 | first_post = posts_in_second_page[0] |
|
120 | 120 | |
|
121 | 121 | self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, |
|
122 | 122 | first_post.id) |
|
123 | 123 | |
|
124 | 124 | def test_linked_tag(self): |
|
125 | 125 | """Test adding a linked tag""" |
|
126 | 126 | |
|
127 | 127 | linked_tag = Tag.objects.create(name=u'tag1') |
|
128 | 128 | tag = Tag.objects.create(name=u'tag2', linked=linked_tag) |
|
129 | 129 | |
|
130 | 130 | post = Post.objects.create_post("", "", tags=[tag]) |
|
131 | 131 | |
|
132 | 132 | self.assertTrue(linked_tag in post.thread_new.tags.all(), |
|
133 | 133 | 'Linked tag was not added') |
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | class PagesTest(TestCase): |
|
137 | 137 | |
|
138 | 138 | def test_404(self): |
|
139 | 139 | """Test receiving error 404 when opening a non-existent page""" |
|
140 | 140 | |
|
141 | 141 | tag_name = u'test_tag' |
|
142 | 142 | tag = Tag.objects.create(name=tag_name) |
|
143 | 143 | client = Client() |
|
144 | 144 | |
|
145 | 145 | Post.objects.create_post('title', TEST_TEXT, tags=[tag]) |
|
146 | 146 | |
|
147 | 147 | existing_post_id = Post.objects.all()[0].id |
|
148 | 148 | response_existing = client.get(THREAD_PAGE + str(existing_post_id) + |
|
149 | 149 | '/') |
|
150 | 150 | self.assertEqual(HTTP_CODE_OK, response_existing.status_code, |
|
151 | 151 | u'Cannot open existing thread') |
|
152 | 152 | |
|
153 | 153 | response_not_existing = client.get(THREAD_PAGE + str( |
|
154 | 154 | existing_post_id + 1) + '/') |
|
155 | 155 | self.assertEqual(PAGE_404, |
|
156 | 156 | response_not_existing.templates[0].name, |
|
157 | 157 | u'Not existing thread is opened') |
|
158 | 158 | |
|
159 | 159 | response_existing = client.get(TAG_PAGE + tag_name + '/') |
|
160 | 160 | self.assertEqual(HTTP_CODE_OK, |
|
161 | 161 | response_existing.status_code, |
|
162 | 162 | u'Cannot open existing tag') |
|
163 | 163 | |
|
164 | 164 | response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') |
|
165 | 165 | self.assertEqual(PAGE_404, |
|
166 | 166 | response_not_existing.templates[0].name, |
|
167 | 167 | u'Not existing tag is opened') |
|
168 | 168 | |
|
169 | 169 | reply_id = Post.objects.create_post('', TEST_TEXT, |
|
170 | 170 | thread=Post.objects.all()[0] |
|
171 | 171 | .thread) |
|
172 | 172 | response_not_existing = client.get(THREAD_PAGE + str( |
|
173 | 173 | reply_id) + '/') |
|
174 | 174 | self.assertEqual(PAGE_404, |
|
175 | 175 | response_not_existing.templates[0].name, |
|
176 | 176 | u'Reply is opened as a thread') |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | class FormTest(TestCase): |
|
180 | 180 | def test_post_validation(self): |
|
181 | 181 | # Disable captcha for the test |
|
182 | 182 | captcha_enabled = settings.ENABLE_CAPTCHA |
|
183 | 183 | settings.ENABLE_CAPTCHA = False |
|
184 | 184 | |
|
185 | 185 | client = Client() |
|
186 | 186 | |
|
187 | 187 | valid_tags = u'tag1 tag_2 ΡΠ΅Π³_3' |
|
188 | 188 | invalid_tags = u'$%_356 ---' |
|
189 | 189 | |
|
190 | 190 | response = client.post(NEW_THREAD_PAGE, {'title': 'test title', |
|
191 | 191 | 'text': TEST_TEXT, |
|
192 | 192 | 'tags': valid_tags}) |
|
193 | 193 | self.assertEqual(response.status_code, HTTP_CODE_REDIRECT, |
|
194 | 194 | msg='Posting new message failed: got code ' + |
|
195 | 195 | str(response.status_code)) |
|
196 | 196 | |
|
197 | 197 | self.assertEqual(1, Post.objects.count(), |
|
198 | 198 | msg='No posts were created') |
|
199 | 199 | |
|
200 | 200 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, |
|
201 | 201 | 'tags': invalid_tags}) |
|
202 | 202 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' |
|
203 | 203 | 'where it should fail') |
|
204 | 204 | |
|
205 | 205 | # Change posting delay so we don't have to wait for 30 seconds or more |
|
206 | 206 | old_posting_delay = settings.POSTING_DELAY |
|
207 | 207 | # Wait fot the posting delay or we won't be able to post |
|
208 | 208 | settings.POSTING_DELAY = 1 |
|
209 | 209 | time.sleep(settings.POSTING_DELAY + 1) |
|
210 | 210 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, |
|
211 | 211 | 'tags': valid_tags}) |
|
212 | 212 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, |
|
213 | 213 | msg=u'Posting new message failed: got code ' + |
|
214 | 214 | str(response.status_code)) |
|
215 | 215 | # Restore posting delay |
|
216 | 216 | settings.POSTING_DELAY = old_posting_delay |
|
217 | 217 | |
|
218 | 218 | self.assertEqual(2, Post.objects.count(), |
|
219 | 219 | msg=u'No posts were created') |
|
220 | 220 | |
|
221 | 221 | # Restore captcha setting |
|
222 | 222 | settings.ENABLE_CAPTCHA = captcha_enabled |
|
223 | 223 | |
|
224 | 224 | |
|
225 | 225 | class ViewTest(TestCase): |
|
226 | 226 | def test_index(self): |
|
227 | 227 | client = Client() |
|
228 | 228 | |
|
229 | 229 | response = client.get('/') |
|
230 | 230 | self.assertEqual(HTTP_CODE_OK, response.status_code, 'Index page not ' |
|
231 | 231 | 'opened') |
|
232 | 232 | self.assertEqual('boards/posting_general.html', response.templates[0] |
|
233 | .name, 'Index page should open posting_general template') No newline at end of file | |
|
233 | .name, 'Index page should open posting_general template') |
General Comments 0
You need to be logged in to leave comments.
Login now