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