tests.py
161 lines
| 4.7 KiB
| text/x-python
|
PythonLexer
/ boards / tests.py
|
r71 | # coding=utf-8 | |
|
r13 | from django.utils.unittest import TestCase | |
from django.test.client import Client | |||
|
r7 | import boards | |
|
r0 | ||
|
r24 | from boards.models import Post, Admin, Tag | |
|
r28 | from neboard import settings | |
|
r0 | ||
|
r71 | TEST_TEXT = 'test text' | |
NEW_THREAD_PAGE = '/' | |||
HTTP_CODE_REDIRECT = 302 | |||
|
r8 | ||
|
r0 | class BoardTests(TestCase): | |
|
r16 | def _create_post(self): | |
|
r22 | return Post.objects.create_post(title='title', | |
text='text') | |||
|
r0 | ||
def test_post_add(self): | |||
|
r16 | post = self._create_post() | |
|
r22 | ||
|
r1 | self.assertIsNotNone(post) | |
|
r7 | self.assertEqual(boards.models.NO_PARENT, post.parent) | |
|
r0 | ||
def test_delete_post(self): | |||
|
r16 | post = self._create_post() | |
|
r8 | post_id = post.id | |
|
r22 | ||
|
r1 | Post.objects.delete_post(post) | |
|
r0 | ||
|
r16 | self.assertFalse(Post.objects.exists(post_id)) | |
|
r0 | ||
def test_delete_posts_by_ip(self): | |||
|
r16 | post = self._create_post() | |
|
r8 | post_id = post.id | |
|
r0 | ||
|
r1 | Post.objects.delete_posts_by_ip('0.0.0.0') | |
|
r0 | ||
|
r16 | self.assertFalse(Post.objects.exists(post_id)) | |
|
r13 | ||
# Authentication tests | |||
def _create_test_user(self): | |||
|
r22 | admin = Admin(name='test_username12313584353165', | |
password='test_userpassword135135512') | |||
|
r13 | ||
admin.save() | |||
return admin | |||
def test_admin_login(self): | |||
client = Client() | |||
self.assertFalse('admin' in client.session) | |||
admin = self._create_test_user() | |||
|
r22 | response = client.post('/login', | |
{'name': admin.name, 'password': admin.password}) | |||
|
r13 | ||
# it means that login passed and user are redirected to another page | |||
self.assertEqual(302, response.status_code) | |||
self.assertTrue('admin' in client.session) | |||
self.assertTrue(client.session['admin']) | |||
admin.delete() | |||
wrong_name = 'sd2f1s3d21fs3d21f' | |||
wrong_password = 'sd2f1s3d21fs3d21fsdfsd' | |||
|
r22 | client.post('/login', {'name': wrong_name, 'password': wrong_password}) | |
|
r13 | self.assertFalse(client.session['admin']) | |
def test_admin_logout(self): | |||
client = Client() | |||
self.assertFalse('admin' in client.session) | |||
admin = self._create_test_user() | |||
|
r22 | client.post('/login', | |
{'name': admin.name, 'password': admin.password}) | |||
|
r13 | ||
self.assertTrue(client.session['admin']) | |||
|
r22 | client.get('/logout') | |
|
r13 | ||
self.assertFalse(client.session['admin']) | |||
admin.delete() | |||
|
r17 | def test_get_thread(self): | |
opening_post = self._create_post() | |||
op_id = opening_post.id | |||
for i in range(0, 2): | |||
Post.objects.create_post('title', 'text', | |||
|
r22 | parent_id=op_id) | |
|
r17 | ||
thread = Post.objects.get_thread(op_id) | |||
|
r19 | self.assertEqual(3, len(thread)) | |
|
r24 | ||
def test_create_post_with_tag(self): | |||
tag = Tag.objects.create(name='test_tag') | |||
post = Post.objects.create_post(title='title', text='text', tags=[tag]) | |||
|
r28 | self.assertIsNotNone(post) | |
def test_thread_max_count(self): | |||
for i in range(settings.MAX_THREAD_COUNT + 1): | |||
self._create_post() | |||
self.assertEqual(settings.MAX_THREAD_COUNT, | |||
|
r46 | len(Post.objects.get_threads())) | |
def test_get(self): | |||
"""Test if the get computes properly""" | |||
post = self._create_post() | |||
self.assertTrue(post.is_get()) | |||
def test_pages(self): | |||
"""Test that the thread list is properly split into pages""" | |||
for i in range(settings.MAX_THREAD_COUNT): | |||
self._create_post() | |||
all_threads = Post.objects.get_threads() | |||
posts_in_second_page = Post.objects.get_threads(page=1) | |||
first_post = posts_in_second_page[0] | |||
self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, | |||
|
r71 | first_post.id) | |
def test_post_validation(self): | |||
"""Test the validation of the post form""" | |||
Post.objects.all().delete() | |||
client = Client() | |||
valid_tags = u'tag1 tag_2 тег_3' | |||
invalid_tags = u'$%_356 ---' | |||
response = client.post(NEW_THREAD_PAGE, {'title': 'test title', | |||
'text': TEST_TEXT, | |||
'tags': valid_tags}) | |||
self.assertEqual(response.status_code, HTTP_CODE_REDIRECT, | |||
msg='Posting new message failed: got code ' + | |||
str(response.status_code)) | |||
self.assertEqual(1, Post.objects.count(), | |||
msg='No posts were created') | |||
response = client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, | |||
'tags': invalid_tags}) | |||
self.assertEqual(1, Post.objects.count(), msg='The validation passed ' | |||
'where it should fail') |