"""
Tests suite for the views of the private messages app.
"""

from django.test import TestCase, Client
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth import get_user_model
from django.utils import timezone

from ..models import (PrivateMessage,
                      BlockedUser)


class NotificationsViewsTestCase(TestCase):
    """
    Tests suite for the views.
    """

    def setUp(self):
        """
        Create a new user named "johndoe" with password "illpassword".
        """
        self.user1 = get_user_model().objects.create_user(username='johndoe1',
                                                          password='johndoe1',
                                                          email='john.doe@example.com')
        self.user2 = get_user_model().objects.create_user(username='johndoe2',
                                                          password='johndoe2',
                                                          email='john.doe@example.com')
        self.user3 = get_user_model().objects.create_user(username='johndoe3',
                                                          password='johndoe3',
                                                          email='john.doe@example.com')
        self.msg1 = PrivateMessage.objects.create(sender=self.user1,
                                                  recipient=self.user2,
                                                  subject='Test message 1',
                                                  body='Test message')
        self.msg2 = PrivateMessage.objects.create(sender=self.user1,
                                                  recipient=self.user2,
                                                  read_at=timezone.now(),
                                                  subject='Test message 2',
                                                  body='Test message')
        self.msg3 = PrivateMessage.objects.create(sender=self.user1,
                                                  recipient=self.user2,
                                                  recipient_deleted_at=timezone.now(),
                                                  subject='Test message 3',
                                                  body='Test message')
        self.msg4 = PrivateMessage.objects.create(sender=self.user1,
                                                  recipient=self.user2,
                                                  recipient_permanently_deleted=True,
                                                  subject='Test message 4',
                                                  body='Test message')
        self.msg5 = PrivateMessage.objects.create(sender=self.user2,
                                                  recipient=self.user3,
                                          
        subject='Test message 5',
                                                  body='Test message')
        self.block1 = BlockedUser.objects.create(user=self.user1, blocked_user=self.user2)

    def test_private_msg_list_view_available(self):
        """
        Test the availability of the "private messages list" view.
        """
        client = Client()
 
       client.login(username='johndoe2', password='johndoe2')
        response = client.get(reverse('privatemsg:inbox'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/inbox.html')
        self.assertIn('private_messages', response.context)
        self.assertQuerysetEqual(response.context['private_messages'], ['<PrivateMessage: Test message 2>',
                                                                        '<PrivateMessage: Test message 1>'])

    def test_read_private_msg_list_view_available(self):
        """
        Test the availability of the "read private messages list" view.
        """
        client = Client()
        client.login(username='johndoe2', password='johndoe2')
        response = client.get(reverse('privatemsg:inbox_read'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/inbox.html')
        self.assertIn('private_messages', response.context)
        self.assertQuerysetEqual(response.context['private_messages'], ['<PrivateMessage: Test message 2>'])

    def test_unread_private_msg_list_view_available(self):
        """
        Test the availability of the "unread private messages list" view.
        """
        client = Client()
        client.login(username='johndoe2', password='johndoe2')
        response = client.get(reverse('privatemsg:inbox_unread'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/inbox.html')
        self.assertIn('private_messages', response.context)
        self.assertQuerysetEqual(response.context['private_messages'], ['<PrivateMessage: Test message 1>'])

    def test_private_msg_list_view_redirect_not_login(self):
        """
        Test the redirection of the "private messages list" view when not logged-in.
        """
        client = Client()
        privatemsg_list_url = reverse('privatemsg:inbox')
        response = client.get(privatemsg_list_url)
        self.assertRedirects(response, '%s?next=%s' % (settings.LOGIN_URL, privatemsg_list_url))

    def test_mark_all_private_msg_as_read_view_available(self):
        """
        Test the availability of the "mark all private messages as read" view.
        """
        client = Client()
        client.login(username='johndoe2', password='johndoe2')
        response = client.get(reverse('privatemsg:inbox_mark_all_as_read'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/mark_all_as_read.html')

    def test_mark_all_private_msg_as_read_view_redirect_not_login(self):
        """
        Test the redirection of the "mark all private messages as read" view when not logged-in.
        """
        client = Client()
        mark_all_as_read_url = reverse('privatemsg:inbox_mark_all_as_read')
        response = client.get(mark_all_as_read_url)
        self.assertRedirects(response, '%s?next=%s' % (settings.LOGIN_URL, mark_all_as_read_url))

    def test_outbox_list_view_available(self):
        """
        Test the availability of the "sent private messages list" view.
        """
        client = Client()
        client.login(username='johndoe1', password='johndoe1')
        response = client.get(reverse('privatemsg:outbox'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/outbox.html')
        self.assertIn('private_messages', response.context)
        self.assertQuerysetEqual(response.context['private_messages'], ['<PrivateMessage: Test message 4>',
                                                                        '<PrivateMessage: Test message 3>',
                                                                        '<PrivateMessage: Test message 2>',
                                                                        '<PrivateMessage: Test message 1>'])

    def test_outbox_list_view_redirect_not_login(self):
        """
        Test the redirection of the "sent private messages list" view when not logged-in.
        """
        client = Client()
        outbox_url = reverse('privatemsg:outbox')
        response = client.get(outbox_url)
        self.assertRedirects(response, '%s?next=%s' % (settings.LOGIN_URL, outbox_url))

    def test_trashbox_list_view_available(self):
        """
        Test the availability of the "deleted private messages list" view.
        """
        client = Client()
        client.login(username='johndoe2', password='johndoe2')
        response = client.get(reverse('privatemsg:trash'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'privatemsg/trashbox.html')
        self.assertIn('private_messages', response.context)
        self.assertQuerysetEqual(response.context['private_messages'], ['<PrivateMessage: Test message 3>'])

    def test_trashbox_list_view_redirect_not_lo