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