# coding=utf-8 from django.utils.unittest import TestCase from django.test.client import Client import boards from boards.models import Post, Admin, Tag from neboard import settings TEST_TEXT = 'test text' NEW_THREAD_PAGE = '/' THREAD_PAGE_ONE = '/thread/1/' THREAD_PAGE = '/thread/' TAG_PAGE = '/tag/' HTTP_CODE_REDIRECT = 302 HTTP_CODE_OK = 200 HTTP_CODE_NOT_FOUND = 404 class BoardTests(TestCase): def _create_post(self): return Post.objects.create_post(title='title', text='text') def test_post_add(self): post = self._create_post() self.assertIsNotNone(post) self.assertEqual(boards.models.NO_PARENT, post.parent) def test_delete_post(self): post = self._create_post() post_id = post.id Post.objects.delete_post(post) self.assertFalse(Post.objects.exists(post_id)) def test_delete_posts_by_ip(self): post = self._create_post() post_id = post.id Post.objects.delete_posts_by_ip('0.0.0.0') self.assertFalse(Post.objects.exists(post_id)) # Authentication tests def _create_test_user(self): admin = Admin(name='test_username12313584353165', password='test_userpassword135135512') admin.save() return admin def test_admin_login(self): client = Client() self.assertFalse('admin' in client.session) admin = self._create_test_user() response = client.post('/login', {'name': admin.name, 'password': admin.password}) # 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' client.post('/login', {'name': wrong_name, 'password': wrong_password}) self.assertFalse(client.session['admin']) def test_admin_logout(self): client = Client() self.assertFalse('admin' in client.session) admin = self._create_test_user() client.post('/login', {'name': admin.name, 'password': admin.password}) self.assertTrue(client.session['admin']) client.get('/logout') self.assertFalse(client.session['admin']) admin.delete() 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', parent_id=op_id) thread = Post.objects.get_thread(op_id) self.assertEqual(3, len(thread)) 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]) 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, 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, first_post.id) def test_post_validation(self): """Test the validation of the post form""" # Disable captcha for the test captcha_enabled = settings.ENABLE_CAPTCHA settings.ENABLE_CAPTCHA = False 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') 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') response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, 'tags': valid_tags}) self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, msg=u'Posting new message failed: got code ' + str(response.status_code)) self.assertEqual(2, Post.objects.count(), msg=u'No posts were created') # Restore captcha setting settings.ENABLE_CAPTCHA = captcha_enabled 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) + '/') self.assertEqual(HTTP_CODE_NOT_FOUND, response_not_existing.status_code, 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(HTTP_CODE_NOT_FOUND, response_not_existing.status_code, u'Not existing tag is opened') reply_id = Post.objects.create_post('', TEST_TEXT, parent_id=existing_post_id) response_not_existing = client.get(THREAD_PAGE + str( reply_id) + '/') self.assertEqual(HTTP_CODE_NOT_FOUND, response_not_existing.status_code, u'Not existing thread is opened')