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