test_forms.py
56 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
neko259
|
r821 | from django.test import TestCase, Client | ||
import time | ||||
from boards import settings | ||||
neko259
|
r922 | from boards.models import Post, Tag | ||
neko259
|
r821 | import neboard | ||
TEST_TAG = 'test_tag' | ||||
PAGE_404 = 'boards/404.html' | ||||
TEST_TEXT = 'test text' | ||||
NEW_THREAD_PAGE = '/' | ||||
THREAD_PAGE_ONE = '/thread/1/' | ||||
HTTP_CODE_REDIRECT = 302 | ||||
class FormTest(TestCase): | ||||
def test_post_validation(self): | ||||
client = Client() | ||||
valid_tags = 'tag1 tag_2 тег_3' | ||||
invalid_tags = '$%_356 ---' | ||||
neko259
|
r922 | Tag.objects.create(name='tag1', required=True) | ||
neko259
|
r821 | |||
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') | ||||
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') | ||||
# Change posting delay so we don't have to wait for 30 seconds or more | ||||
old_posting_delay = neboard.settings.POSTING_DELAY | ||||
# Wait fot the posting delay or we won't be able to post | ||||
settings.POSTING_DELAY = 1 | ||||
time.sleep(neboard.settings.POSTING_DELAY + 1) | ||||
response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, | ||||
'tags': valid_tags}) | ||||
self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, | ||||
msg='Posting new message failed: got code ' + | ||||
str(response.status_code)) | ||||
# Restore posting delay | ||||
settings.POSTING_DELAY = old_posting_delay | ||||
self.assertEqual(2, Post.objects.count(), | ||||
msg='No posts were created') | ||||