Show More
@@ -1,182 +1,175 b'' | |||
|
1 | 1 | # coding=utf-8 |
|
2 |
from django. |
|
|
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 BoardTests(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 | post = self._create_post() |
|
30 | 30 | |
|
31 | 31 | self.assertIsNotNone(post) |
|
32 | 32 | self.assertIsNone(post.thread, 'Opening post has a thread') |
|
33 | 33 | |
|
34 | 34 | def test_delete_post(self): |
|
35 | 35 | post = self._create_post() |
|
36 | 36 | post_id = post.id |
|
37 | 37 | |
|
38 | 38 | Post.objects.delete_post(post) |
|
39 | 39 | |
|
40 | 40 | self.assertFalse(Post.objects.exists(post_id)) |
|
41 | 41 | |
|
42 | 42 | def test_delete_posts_by_ip(self): |
|
43 | 43 | post = self._create_post() |
|
44 | 44 | post_id = post.id |
|
45 | 45 | |
|
46 | 46 | Post.objects.delete_posts_by_ip('0.0.0.0') |
|
47 | 47 | |
|
48 | 48 | self.assertFalse(Post.objects.exists(post_id)) |
|
49 | 49 | |
|
50 | # Authentication tests | |
|
51 | ||
|
52 | 50 | def test_get_thread(self): |
|
53 | 51 | opening_post = self._create_post() |
|
54 | 52 | |
|
55 | 53 | for i in range(0, 2): |
|
56 | 54 | Post.objects.create_post('title', 'text', thread=opening_post) |
|
57 | 55 | |
|
58 | 56 | thread = Post.objects.get_thread(opening_post.id) |
|
59 | 57 | |
|
60 | 58 | self.assertEqual(3, len(thread)) |
|
61 | 59 | |
|
62 | 60 | def test_create_post_with_tag(self): |
|
63 | 61 | tag = Tag.objects.create(name='test_tag') |
|
64 | 62 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) |
|
65 | 63 | self.assertIsNotNone(post) |
|
66 | 64 | |
|
67 | 65 | def test_thread_max_count(self): |
|
68 | 66 | for i in range(settings.MAX_THREAD_COUNT + 1): |
|
69 | 67 | self._create_post() |
|
70 | 68 | |
|
71 | 69 | self.assertEqual(settings.MAX_THREAD_COUNT, |
|
72 | 70 | len(Post.objects.get_threads())) |
|
73 | 71 | |
|
74 | 72 | def test_pages(self): |
|
75 | 73 | """Test that the thread list is properly split into pages""" |
|
76 | 74 | |
|
77 | 75 | for i in range(settings.MAX_THREAD_COUNT): |
|
78 | 76 | self._create_post() |
|
79 | 77 | |
|
80 | 78 | all_threads = Post.objects.get_threads() |
|
81 | 79 | |
|
82 | 80 | posts_in_second_page = Post.objects.get_threads(page=1) |
|
83 | 81 | first_post = posts_in_second_page[0] |
|
84 | 82 | |
|
85 | 83 | self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, |
|
86 | 84 | first_post.id) |
|
87 | 85 | |
|
88 | 86 | def test_post_validation(self): |
|
89 | 87 | """Test the validation of the post form""" |
|
90 | 88 | |
|
91 | 89 | # Disable captcha for the test |
|
92 | 90 | captcha_enabled = settings.ENABLE_CAPTCHA |
|
93 | 91 | settings.ENABLE_CAPTCHA = False |
|
94 | 92 | |
|
95 | Post.objects.all().delete() | |
|
96 | ||
|
97 | 93 | client = Client() |
|
98 | 94 | |
|
99 | 95 | valid_tags = u'tag1 tag_2 ΡΠ΅Π³_3' |
|
100 | 96 | invalid_tags = u'$%_356 ---' |
|
101 | 97 | |
|
102 | 98 | response = client.post(NEW_THREAD_PAGE, {'title': 'test title', |
|
103 | 99 | 'text': TEST_TEXT, |
|
104 | 100 | 'tags': valid_tags}) |
|
105 | 101 | self.assertEqual(response.status_code, HTTP_CODE_REDIRECT, |
|
106 | 102 | msg='Posting new message failed: got code ' + |
|
107 | 103 | str(response.status_code)) |
|
108 | 104 | |
|
109 | 105 | self.assertEqual(1, Post.objects.count(), |
|
110 | 106 | msg='No posts were created') |
|
111 | 107 | |
|
112 | 108 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, |
|
113 | 109 | 'tags': invalid_tags}) |
|
114 | 110 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' |
|
115 | 111 | 'where it should fail') |
|
116 | 112 | |
|
117 | 113 | # Change posting delay so we don't have to wait for 30 seconds or more |
|
118 | 114 | old_posting_delay = settings.POSTING_DELAY |
|
119 | 115 | # Wait fot the posting delay or we won't be able to post |
|
120 | 116 | settings.POSTING_DELAY = 1 |
|
121 | 117 | time.sleep(settings.POSTING_DELAY + 1) |
|
122 | 118 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, |
|
123 | 119 | 'tags': valid_tags}) |
|
124 | 120 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, |
|
125 | 121 | msg=u'Posting new message failed: got code ' + |
|
126 | 122 | str(response.status_code)) |
|
127 | 123 | # Restore posting delay |
|
128 | 124 | settings.POSTING_DELAY = old_posting_delay |
|
129 | 125 | |
|
130 | 126 | self.assertEqual(2, Post.objects.count(), |
|
131 | 127 | msg=u'No posts were created') |
|
132 | 128 | |
|
133 | 129 | # Restore captcha setting |
|
134 | 130 | settings.ENABLE_CAPTCHA = captcha_enabled |
|
135 | 131 | |
|
136 | 132 | def test_404(self): |
|
137 | 133 | """Test receiving error 404 when opening a non-existent page""" |
|
138 | 134 | |
|
139 | Post.objects.all().delete() | |
|
140 | Tag.objects.all().delete() | |
|
141 | ||
|
142 | 135 | tag_name = u'test_tag' |
|
143 | 136 | tag = Tag.objects.create(name=tag_name) |
|
144 | 137 | client = Client() |
|
145 | 138 | |
|
146 | 139 | Post.objects.create_post('title', TEST_TEXT, tags=[tag]) |
|
147 | 140 | |
|
148 | 141 | existing_post_id = Post.objects.all()[0].id |
|
149 | 142 | response_existing = client.get(THREAD_PAGE + str(existing_post_id) + |
|
150 | 143 | '/') |
|
151 | 144 | self.assertEqual(HTTP_CODE_OK, response_existing.status_code, |
|
152 | 145 | u'Cannot open existing thread') |
|
153 | 146 | |
|
154 | 147 | response_not_existing = client.get(THREAD_PAGE + str( |
|
155 | 148 | existing_post_id + 1) + '/') |
|
156 | 149 | self.assertEqual(PAGE_404, |
|
157 | 150 | response_not_existing.templates[0].name, |
|
158 | 151 | u'Not existing thread is opened') |
|
159 | 152 | |
|
160 | 153 | response_existing = client.get(TAG_PAGE + tag_name + '/') |
|
161 | 154 | self.assertEqual(HTTP_CODE_OK, |
|
162 | 155 | response_existing.status_code, |
|
163 | 156 | u'Cannot open existing tag') |
|
164 | 157 | |
|
165 | 158 | response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') |
|
166 | 159 | self.assertEqual(PAGE_404, |
|
167 | 160 | response_not_existing.templates[0].name, |
|
168 | 161 | u'Not existing tag is opened') |
|
169 | 162 | |
|
170 | 163 | reply_id = Post.objects.create_post('', TEST_TEXT, |
|
171 | 164 | thread=Post.objects.all()[0]) |
|
172 | 165 | response_not_existing = client.get(THREAD_PAGE + str( |
|
173 | 166 | reply_id) + '/') |
|
174 | 167 | self.assertEqual(PAGE_404, |
|
175 | 168 | response_not_existing.templates[0].name, |
|
176 | 169 | u'Reply is opened as a thread') |
|
177 | 170 | |
|
178 | 171 | def test_linked_tag(self): |
|
179 | 172 | tag = Tag.objects.create(name=u'tag1') |
|
180 | 173 | linked_tag = Tag.objects.create(name=u'tag2', linked=tag) |
|
181 | 174 | |
|
182 | 175 | # TODO run add post view and check the tag is added No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now