from django.test import TestCase, Client import time from boards import settings from boards.models import Post 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 ---' 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 neboard.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')