Show More
@@ -1,218 +1,225 b'' | |||
|
1 | 1 | # coding=utf-8 |
|
2 | 2 | from django.utils.unittest import TestCase |
|
3 | 3 | from django.test.client import Client |
|
4 | 4 | |
|
5 | 5 | import boards |
|
6 | 6 | |
|
7 | 7 | from boards.models import Post, Admin, Tag |
|
8 | 8 | from neboard import settings |
|
9 | 9 | |
|
10 | 10 | TEST_TEXT = 'test text' |
|
11 | 11 | |
|
12 | 12 | NEW_THREAD_PAGE = '/' |
|
13 | 13 | THREAD_PAGE_ONE = '/thread/1/' |
|
14 | 14 | THREAD_PAGE = '/thread/' |
|
15 | 15 | TAG_PAGE = '/tag/' |
|
16 | 16 | HTTP_CODE_REDIRECT = 302 |
|
17 | 17 | HTTP_CODE_OK = 200 |
|
18 | 18 | HTTP_CODE_NOT_FOUND = 404 |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | class BoardTests(TestCase): |
|
22 | 22 | def _create_post(self): |
|
23 | 23 | return Post.objects.create_post(title='title', |
|
24 | 24 | text='text') |
|
25 | 25 | |
|
26 | 26 | def test_post_add(self): |
|
27 | 27 | post = self._create_post() |
|
28 | 28 | |
|
29 | 29 | self.assertIsNotNone(post) |
|
30 | 30 | self.assertEqual(boards.models.NO_PARENT, post.parent) |
|
31 | 31 | |
|
32 | 32 | def test_delete_post(self): |
|
33 | 33 | post = self._create_post() |
|
34 | 34 | post_id = post.id |
|
35 | 35 | |
|
36 | 36 | Post.objects.delete_post(post) |
|
37 | 37 | |
|
38 | 38 | self.assertFalse(Post.objects.exists(post_id)) |
|
39 | 39 | |
|
40 | 40 | def test_delete_posts_by_ip(self): |
|
41 | 41 | post = self._create_post() |
|
42 | 42 | post_id = post.id |
|
43 | 43 | |
|
44 | 44 | Post.objects.delete_posts_by_ip('0.0.0.0') |
|
45 | 45 | |
|
46 | 46 | self.assertFalse(Post.objects.exists(post_id)) |
|
47 | 47 | |
|
48 | 48 | # Authentication tests |
|
49 | 49 | |
|
50 | 50 | def _create_test_user(self): |
|
51 | 51 | admin = Admin(name='test_username12313584353165', |
|
52 | 52 | password='test_userpassword135135512') |
|
53 | 53 | |
|
54 | 54 | admin.save() |
|
55 | 55 | return admin |
|
56 | 56 | |
|
57 | 57 | def test_admin_login(self): |
|
58 | 58 | client = Client() |
|
59 | 59 | |
|
60 | 60 | self.assertFalse('admin' in client.session) |
|
61 | 61 | |
|
62 | 62 | admin = self._create_test_user() |
|
63 | 63 | |
|
64 | 64 | response = client.post('/login', |
|
65 | 65 | {'name': admin.name, 'password': admin.password}) |
|
66 | 66 | |
|
67 | 67 | # it means that login passed and user are redirected to another page |
|
68 | 68 | self.assertEqual(302, response.status_code) |
|
69 | 69 | |
|
70 | 70 | self.assertTrue('admin' in client.session) |
|
71 | 71 | self.assertTrue(client.session['admin']) |
|
72 | 72 | |
|
73 | 73 | admin.delete() |
|
74 | 74 | |
|
75 | 75 | wrong_name = 'sd2f1s3d21fs3d21f' |
|
76 | 76 | wrong_password = 'sd2f1s3d21fs3d21fsdfsd' |
|
77 | 77 | |
|
78 | 78 | client.post('/login', {'name': wrong_name, 'password': wrong_password}) |
|
79 | 79 | self.assertFalse(client.session['admin']) |
|
80 | 80 | |
|
81 | 81 | def test_admin_logout(self): |
|
82 | 82 | client = Client() |
|
83 | 83 | |
|
84 | 84 | self.assertFalse('admin' in client.session) |
|
85 | 85 | |
|
86 | 86 | admin = self._create_test_user() |
|
87 | 87 | |
|
88 | 88 | client.post('/login', |
|
89 | 89 | {'name': admin.name, 'password': admin.password}) |
|
90 | 90 | |
|
91 | 91 | self.assertTrue(client.session['admin']) |
|
92 | 92 | |
|
93 | 93 | client.get('/logout') |
|
94 | 94 | |
|
95 | 95 | self.assertFalse(client.session['admin']) |
|
96 | 96 | |
|
97 | 97 | admin.delete() |
|
98 | 98 | |
|
99 | 99 | def test_get_thread(self): |
|
100 | 100 | opening_post = self._create_post() |
|
101 | 101 | op_id = opening_post.id |
|
102 | 102 | |
|
103 | 103 | for i in range(0, 2): |
|
104 | 104 | Post.objects.create_post('title', 'text', |
|
105 | 105 | parent_id=op_id) |
|
106 | 106 | |
|
107 | 107 | thread = Post.objects.get_thread(op_id) |
|
108 | 108 | |
|
109 | 109 | self.assertEqual(3, len(thread)) |
|
110 | 110 | |
|
111 | 111 | def test_create_post_with_tag(self): |
|
112 | 112 | tag = Tag.objects.create(name='test_tag') |
|
113 | 113 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) |
|
114 | 114 | self.assertIsNotNone(post) |
|
115 | 115 | |
|
116 | 116 | def test_thread_max_count(self): |
|
117 | 117 | for i in range(settings.MAX_THREAD_COUNT + 1): |
|
118 | 118 | self._create_post() |
|
119 | 119 | |
|
120 | 120 | self.assertEqual(settings.MAX_THREAD_COUNT, |
|
121 | 121 | len(Post.objects.get_threads())) |
|
122 | 122 | |
|
123 | 123 | def test_get(self): |
|
124 | 124 | """Test if the get computes properly""" |
|
125 | 125 | |
|
126 | 126 | post = self._create_post() |
|
127 | 127 | |
|
128 | 128 | self.assertTrue(post.is_get()) |
|
129 | 129 | |
|
130 | 130 | def test_pages(self): |
|
131 | 131 | """Test that the thread list is properly split into pages""" |
|
132 | 132 | |
|
133 | 133 | for i in range(settings.MAX_THREAD_COUNT): |
|
134 | 134 | self._create_post() |
|
135 | 135 | |
|
136 | 136 | all_threads = Post.objects.get_threads() |
|
137 | 137 | |
|
138 | 138 | posts_in_second_page = Post.objects.get_threads(page=1) |
|
139 | 139 | first_post = posts_in_second_page[0] |
|
140 | 140 | |
|
141 | 141 | self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, |
|
142 | 142 | first_post.id) |
|
143 | 143 | |
|
144 | 144 | def test_post_validation(self): |
|
145 | 145 | """Test the validation of the post form""" |
|
146 | 146 | |
|
147 | # Disable captcha for the test | |
|
148 | captcha_enabled = settings.ENABLE_CAPTCHA | |
|
149 | settings.ENABLE_CAPTCHA = False | |
|
150 | ||
|
147 | 151 | Post.objects.all().delete() |
|
148 | 152 | |
|
149 | 153 | client = Client() |
|
150 | 154 | |
|
151 | 155 | valid_tags = u'tag1 tag_2 ΡΠ΅Π³_3' |
|
152 | 156 | invalid_tags = u'$%_356 ---' |
|
153 | 157 | |
|
154 | 158 | response = client.post(NEW_THREAD_PAGE, {'title': 'test title', |
|
155 | 159 | 'text': TEST_TEXT, |
|
156 | 160 | 'tags': valid_tags}) |
|
157 | 161 | self.assertEqual(response.status_code, HTTP_CODE_REDIRECT, |
|
158 | 162 | msg='Posting new message failed: got code ' + |
|
159 | 163 | str(response.status_code)) |
|
160 | 164 | |
|
161 | 165 | self.assertEqual(1, Post.objects.count(), |
|
162 | 166 | msg='No posts were created') |
|
163 | 167 | |
|
164 | 168 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, |
|
165 | 169 | 'tags': invalid_tags}) |
|
166 | 170 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' |
|
167 | 171 | 'where it should fail') |
|
168 | 172 | |
|
169 | 173 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, |
|
170 | 174 | 'tags': valid_tags}) |
|
171 | 175 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, |
|
172 | 176 | msg=u'Posting new message failed: got code ' + |
|
173 | 177 | str(response.status_code)) |
|
174 | 178 | |
|
175 | 179 | self.assertEqual(2, Post.objects.count(), |
|
176 | 180 | msg=u'No posts were created') |
|
177 | 181 | |
|
182 | # Restore captcha setting | |
|
183 | settings.ENABLE_CAPTCHA = captcha_enabled | |
|
184 | ||
|
178 | 185 | def test_404(self): |
|
179 | 186 | """Test receiving error 404 when opening a non-existent page""" |
|
180 | 187 | |
|
181 | 188 | Post.objects.all().delete() |
|
182 | 189 | Tag.objects.all().delete() |
|
183 | 190 | |
|
184 | 191 | tag_name = u'test_tag' |
|
185 | 192 | tags, = [Tag.objects.get_or_create(name=tag_name)] |
|
186 | 193 | client = Client() |
|
187 | 194 | |
|
188 | 195 | Post.objects.create_post('title', TEST_TEXT, tags=tags) |
|
189 | 196 | |
|
190 | 197 | existing_post_id = Post.objects.all()[0].id |
|
191 | 198 | response_existing = client.get(THREAD_PAGE + str(existing_post_id) + |
|
192 | 199 | '/') |
|
193 | 200 | self.assertEqual(HTTP_CODE_OK, response_existing.status_code, |
|
194 | 201 | u'Cannot open existing thread') |
|
195 | 202 | |
|
196 | 203 | response_not_existing = client.get(THREAD_PAGE + str( |
|
197 | 204 | existing_post_id + 1) + '/') |
|
198 | 205 | self.assertEqual(HTTP_CODE_NOT_FOUND, |
|
199 | 206 | response_not_existing.status_code, |
|
200 | 207 | u'Not existing thread is opened') |
|
201 | 208 | |
|
202 | 209 | response_existing = client.get(TAG_PAGE + tag_name + '/') |
|
203 | 210 | self.assertEqual(HTTP_CODE_OK, |
|
204 | 211 | response_existing.status_code, |
|
205 | 212 | u'Cannot open existing tag') |
|
206 | 213 | |
|
207 | 214 | response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') |
|
208 | 215 | self.assertEqual(HTTP_CODE_NOT_FOUND, |
|
209 | 216 | response_not_existing.status_code, |
|
210 | 217 | u'Not existing tag is opened') |
|
211 | 218 | |
|
212 | 219 | reply_id = Post.objects.create_post('', TEST_TEXT, |
|
213 | 220 | parent_id=existing_post_id) |
|
214 | 221 | response_not_existing = client.get(THREAD_PAGE + str( |
|
215 | 222 | reply_id) + '/') |
|
216 | 223 | self.assertEqual(HTTP_CODE_NOT_FOUND, |
|
217 | 224 | response_not_existing.status_code, |
|
218 | 225 | u'Not existing thread is opened') |
General Comments 0
You need to be logged in to leave comments.
Login now