tests.py
176 lines
| 6.0 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 | ||||
|
r180 | import time | ||
|
r13 | |||
|
r141 | from boards.models import Post, Tag | ||
|
r28 | from neboard import settings | ||
|
r0 | |||
|
r203 | PAGE_404 = 'boards/404.html' | ||
|
r71 | TEST_TEXT = 'test text' | ||
NEW_THREAD_PAGE = '/' | ||||
|
r79 | THREAD_PAGE_ONE = '/thread/1/' | ||
THREAD_PAGE = '/thread/' | ||||
TAG_PAGE = '/tag/' | ||||
|
r71 | HTTP_CODE_REDIRECT = 302 | ||
|
r79 | HTTP_CODE_OK = 200 | ||
HTTP_CODE_NOT_FOUND = 404 | ||||
|
r71 | |||
|
r8 | |||
|
r0 | class BoardTests(TestCase): | ||
|
r203 | |||
|
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) | ||
|
r202 | self.assertIsNone(post.thread, 'Opening post has a thread') | ||
|
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 | ||||
|
r17 | def test_get_thread(self): | ||
opening_post = self._create_post() | ||||
for i in range(0, 2): | ||||
|
r202 | Post.objects.create_post('title', 'text', thread=opening_post) | ||
|
r17 | |||
|
r174 | thread = Post.objects.get_thread(opening_post.id) | ||
|
r17 | |||
|
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_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""" | ||||
|
r102 | # Disable captcha for the test | ||
captcha_enabled = settings.ENABLE_CAPTCHA | ||||
settings.ENABLE_CAPTCHA = False | ||||
|
r71 | 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') | ||||
|
r76 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, | ||
'tags': invalid_tags}) | ||||
|
r71 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' | ||
|
r77 | 'where it should fail') | ||
|
r181 | # Change posting delay so we don't have to wait for 30 seconds or more | ||
old_posting_delay = settings.POSTING_DELAY | ||||
|
r180 | # Wait fot the posting delay or we won't be able to post | ||
|
r181 | settings.POSTING_DELAY = 1 | ||
|
r180 | time.sleep(settings.POSTING_DELAY + 1) | ||
|
r79 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, | ||
|
r89 | 'tags': valid_tags}) | ||
|
r79 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, | ||
msg=u'Posting new message failed: got code ' + | ||||
|
r77 | str(response.status_code)) | ||
|
r181 | # Restore posting delay | ||
settings.POSTING_DELAY = old_posting_delay | ||||
|
r77 | |||
self.assertEqual(2, Post.objects.count(), | ||||
|
r79 | msg=u'No posts were created') | ||
|
r102 | # Restore captcha setting | ||
settings.ENABLE_CAPTCHA = captcha_enabled | ||||
|
r79 | def test_404(self): | ||
"""Test receiving error 404 when opening a non-existent page""" | ||||
Post.objects.all().delete() | ||||
Tag.objects.all().delete() | ||||
tag_name = u'test_tag' | ||||
tags, = [Tag.objects.get_or_create(name=tag_name)] | ||||
client = Client() | ||||
Post.objects.create_post('title', TEST_TEXT, tags=tags) | ||||
existing_post_id = Post.objects.all()[0].id | ||||
response_existing = client.get(THREAD_PAGE + str(existing_post_id) + | ||||
'/') | ||||
self.assertEqual(HTTP_CODE_OK, response_existing.status_code, | ||||
u'Cannot open existing thread') | ||||
response_not_existing = client.get(THREAD_PAGE + str( | ||||
existing_post_id + 1) + '/') | ||||
|
r203 | self.assertEqual(PAGE_404, | ||
|
r181 | response_not_existing.templates[0].name, | ||
|
r79 | u'Not existing thread is opened') | ||
response_existing = client.get(TAG_PAGE + tag_name + '/') | ||||
self.assertEqual(HTTP_CODE_OK, | ||||
response_existing.status_code, | ||||
u'Cannot open existing tag') | ||||
response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') | ||||
|
r203 | self.assertEqual(PAGE_404, | ||
|
r181 | response_not_existing.templates[0].name, | ||
|
r86 | u'Not existing tag is opened') | ||
reply_id = Post.objects.create_post('', TEST_TEXT, | ||||
|
r181 | thread=Post.objects.all()[0]) | ||
|
r86 | response_not_existing = client.get(THREAD_PAGE + str( | ||
reply_id) + '/') | ||||
|
r203 | self.assertEqual(PAGE_404, | ||
|
r181 | response_not_existing.templates[0].name, | ||
u'Reply is opened as a thread') | ||||