from django.test import TestCase, Client
from boards.models import Tag, Post

TEST_TEXT = 'test'

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 PagesTest(TestCase):

    def test_404(self):
        """Test receiving error 404 when opening a non-existent page"""

        tag_name = '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,
                         '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,
                         'Not existing thread is opened')

        response_existing = client.get(TAG_PAGE + tag_name + '/')
        self.assertEqual(HTTP_CODE_OK,
                         response_existing.status_code,
                         'Cannot open existing tag')

        response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/')
        self.assertEqual(HTTP_CODE_NOT_FOUND, response_not_existing.status_code,
                         'Not existing tag is opened')

        reply_id = Post.objects.create_post('', TEST_TEXT,
                                            thread=Post.objects.all()[0]
                                            .get_thread()).id
        response_not_existing = client.get(THREAD_PAGE + str(
            reply_id) + '/')
        self.assertEqual(HTTP_CODE_REDIRECT, response_not_existing.status_code,
                         'Reply is opened as a thread')