tests.py
257 lines
| 8.5 KiB
| text/x-python
|
PythonLexer
/ boards / tests.py
neko259
|
r71 | # coding=utf-8 | ||
neko259
|
r545 | import time | ||
import logging | ||||
neko259
|
r345 | from django.test import TestCase | ||
Ilyas
|
r13 | from django.test.client import Client | ||
neko259
|
r545 | from django.core.urlresolvers import reverse, NoReverseMatch | ||
Ilyas
|
r13 | |||
neko259
|
r141 | from boards.models import Post, Tag | ||
neko259
|
r545 | from boards import urls | ||
neko259
|
r28 | from neboard import settings | ||
neko259
|
r0 | |||
neko259
|
r203 | PAGE_404 = 'boards/404.html' | ||
neko259
|
r71 | TEST_TEXT = 'test text' | ||
NEW_THREAD_PAGE = '/' | ||||
neko259
|
r79 | THREAD_PAGE_ONE = '/thread/1/' | ||
THREAD_PAGE = '/thread/' | ||||
TAG_PAGE = '/tag/' | ||||
neko259
|
r71 | HTTP_CODE_REDIRECT = 302 | ||
neko259
|
r79 | HTTP_CODE_OK = 200 | ||
HTTP_CODE_NOT_FOUND = 404 | ||||
neko259
|
r71 | |||
neko259
|
r545 | logger = logging.getLogger(__name__) | ||
neko259
|
r8 | |||
neko259
|
r381 | class PostTests(TestCase): | ||
neko259
|
r203 | |||
neko259
|
r16 | def _create_post(self): | ||
neko259
|
r22 | return Post.objects.create_post(title='title', | ||
text='text') | ||||
neko259
|
r0 | |||
def test_post_add(self): | ||||
neko259
|
r381 | """Test adding post""" | ||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r22 | |||
neko259
|
r398 | self.assertIsNotNone(post, 'No post was created') | ||
neko259
|
r0 | |||
def test_delete_post(self): | ||||
neko259
|
r381 | """Test post deletion""" | ||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r8 | post_id = post.id | ||
neko259
|
r22 | |||
neko259
|
r1 | Post.objects.delete_post(post) | ||
neko259
|
r0 | |||
neko259
|
r396 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | ||
neko259
|
r0 | |||
neko259
|
r381 | def test_delete_thread(self): | ||
"""Test thread deletion""" | ||||
neko259
|
r398 | opening_post = self._create_post() | ||
thread = opening_post.thread_new | ||||
neko259
|
r381 | reply = Post.objects.create_post("", "", thread=thread) | ||
neko259
|
r398 | thread.delete_with_posts() | ||
neko259
|
r381 | |||
neko259
|
r396 | self.assertFalse(Post.objects.filter(id=reply.id).exists()) | ||
neko259
|
r381 | |||
neko259
|
r380 | def test_post_to_thread(self): | ||
neko259
|
r381 | """Test adding post to a thread""" | ||
neko259
|
r380 | op = self._create_post() | ||
neko259
|
r398 | post = Post.objects.create_post("", "", thread=op.thread_new) | ||
neko259
|
r380 | |||
self.assertIsNotNone(post, 'Reply to thread wasn\'t created') | ||||
neko259
|
r398 | self.assertEqual(op.thread_new.last_edit_time, post.pub_time, | ||
neko259
|
r380 | 'Post\'s create time doesn\'t match thread last edit' | ||
' time') | ||||
neko259
|
r0 | def test_delete_posts_by_ip(self): | ||
neko259
|
r381 | """Test deleting posts with the given ip""" | ||
neko259
|
r16 | post = self._create_post() | ||
neko259
|
r8 | post_id = post.id | ||
neko259
|
r0 | |||
neko259
|
r1 | Post.objects.delete_posts_by_ip('0.0.0.0') | ||
neko259
|
r0 | |||
neko259
|
r396 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | ||
Ilyas
|
r13 | |||
neko259
|
r17 | def test_get_thread(self): | ||
neko259
|
r381 | """Test getting all posts of a thread""" | ||
neko259
|
r17 | opening_post = self._create_post() | ||
for i in range(0, 2): | ||||
neko259
|
r398 | Post.objects.create_post('title', 'text', | ||
thread=opening_post.thread_new) | ||||
neko259
|
r17 | |||
neko259
|
r400 | thread = opening_post.thread_new | ||
neko259
|
r17 | |||
neko259
|
r398 | self.assertEqual(3, thread.replies.count()) | ||
neko259
|
r24 | |||
def test_create_post_with_tag(self): | ||||
neko259
|
r381 | """Test adding tag to post""" | ||
neko259
|
r24 | tag = Tag.objects.create(name='test_tag') | ||
post = Post.objects.create_post(title='title', text='text', tags=[tag]) | ||||
neko259
|
r398 | |||
thread = post.thread_new | ||||
self.assertIsNotNone(post, 'Post not created') | ||||
self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread') | ||||
self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag') | ||||
neko259
|
r28 | |||
def test_thread_max_count(self): | ||||
neko259
|
r381 | """Test deletion of old posts when the max thread count is reached""" | ||
neko259
|
r28 | for i in range(settings.MAX_THREAD_COUNT + 1): | ||
self._create_post() | ||||
self.assertEqual(settings.MAX_THREAD_COUNT, | ||||
neko259
|
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() | ||||
neko259
|
r494 | posts_in_second_page = Post.objects.get_threads(page=2) | ||
neko259
|
r46 | first_post = posts_in_second_page[0] | ||
self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, | ||||
neko259
|
r71 | first_post.id) | ||
neko259
|
r381 | def test_linked_tag(self): | ||
"""Test adding a linked tag""" | ||||
linked_tag = Tag.objects.create(name=u'tag1') | ||||
tag = Tag.objects.create(name=u'tag2', linked=linked_tag) | ||||
post = Post.objects.create_post("", "", tags=[tag]) | ||||
neko259
|
r398 | self.assertTrue(linked_tag in post.thread_new.tags.all(), | ||
neko259
|
r381 | 'Linked tag was not added') | ||
class PagesTest(TestCase): | ||||
def test_404(self): | ||||
"""Test receiving error 404 when opening a non-existent page""" | ||||
tag_name = u'test_tag' | ||||
tag = Tag.objects.create(name=tag_name) | ||||
client = Client() | ||||
Post.objects.create_post('title', TEST_TEXT, tags=[tag]) | ||||
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) + '/') | ||||
self.assertEqual(PAGE_404, | ||||
response_not_existing.templates[0].name, | ||||
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' + '/') | ||||
self.assertEqual(PAGE_404, | ||||
response_not_existing.templates[0].name, | ||||
u'Not existing tag is opened') | ||||
reply_id = Post.objects.create_post('', TEST_TEXT, | ||||
neko259
|
r398 | thread=Post.objects.all()[0] | ||
.thread) | ||||
neko259
|
r381 | response_not_existing = client.get(THREAD_PAGE + str( | ||
reply_id) + '/') | ||||
self.assertEqual(PAGE_404, | ||||
response_not_existing.templates[0].name, | ||||
u'Reply is opened as a thread') | ||||
class FormTest(TestCase): | ||||
neko259
|
r71 | def test_post_validation(self): | ||
neko259
|
r102 | # Disable captcha for the test | ||
captcha_enabled = settings.ENABLE_CAPTCHA | ||||
settings.ENABLE_CAPTCHA = False | ||||
neko259
|
r71 | 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') | ||||
neko259
|
r76 | client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT, | ||
'tags': invalid_tags}) | ||||
neko259
|
r71 | self.assertEqual(1, Post.objects.count(), msg='The validation passed ' | ||
neko259
|
r77 | 'where it should fail') | ||
neko259
|
r181 | # Change posting delay so we don't have to wait for 30 seconds or more | ||
old_posting_delay = settings.POSTING_DELAY | ||||
neko259
|
r180 | # Wait fot the posting delay or we won't be able to post | ||
neko259
|
r181 | settings.POSTING_DELAY = 1 | ||
neko259
|
r180 | time.sleep(settings.POSTING_DELAY + 1) | ||
neko259
|
r79 | response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, | ||
neko259
|
r89 | 'tags': valid_tags}) | ||
neko259
|
r79 | self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, | ||
msg=u'Posting new message failed: got code ' + | ||||
neko259
|
r77 | str(response.status_code)) | ||
neko259
|
r181 | # Restore posting delay | ||
settings.POSTING_DELAY = old_posting_delay | ||||
neko259
|
r77 | |||
self.assertEqual(2, Post.objects.count(), | ||||
neko259
|
r79 | msg=u'No posts were created') | ||
neko259
|
r102 | # Restore captcha setting | ||
settings.ENABLE_CAPTCHA = captcha_enabled | ||||
neko259
|
r415 | |||
class ViewTest(TestCase): | ||||
neko259
|
r545 | |||
def test_all_views(self): | ||||
''' | ||||
Try opening all views defined in ulrs.py that don't need additional | ||||
parameters | ||||
''' | ||||
neko259
|
r415 | client = Client() | ||
neko259
|
r545 | for url in urls.urlpatterns: | ||
try: | ||||
view_name = url.name | ||||
logger.debug('Testing view %s' % view_name) | ||||
neko259
|
r415 | |||
neko259
|
r545 | try: | ||
response = client.get(reverse(view_name)) | ||||
self.assertEqual(HTTP_CODE_OK, response.status_code, | ||||
'%s view not opened' % view_name) | ||||
except NoReverseMatch: | ||||
# This view just needs additional arguments | ||||
pass | ||||
except Exception, e: | ||||
self.fail('Got exception %s at %s view' % (e, view_name)) | ||||
except AttributeError: | ||||
# This is normal, some views do not have names | ||||
pass | ||||