__author__ = 'traviswarren'

import datetime

from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from train_track.tests.model_factory import EventFactory


class EventArchiveViewTestCases(TestCase):

    def setUp(self):
        self.user = User.objects.create_user(
            username='user',
            email='user@email.com',
            password='password')
        self.staff = User.objects.create_superuser(
            username='staff',
            email='staff@email.com',
            password='password')

    def test_event_archive_is_not_public(self):
        response = self.client.get(reverse('event:event-archive'))

        self.assertContains(response, 'Access Denied', status_code=403)

    def test_event_archive_is_not_user_accessible(self):

        self.assertTrue(self.client.login(username=self.user.username, password='password'))

        response = self.client.get(reverse('event:event-archive'))

        self.assertContains(response, 'Access Denied', status_code=403)

    def test_event_archive_is_staff_only(self):
        EventFactory(event_date=datetime.datetime.now() - datetime.timedelta(weeks=1))

        self.assertTrue(self.client.login(username=self.staff.username, password='password'))

        response = self.client.get(reverse('event:event-archive'))

        self.assertTemplateUsed('event/event_archive.html')
        self.assertContains(response, 'Event Archive')


class EventYearArchiveViewTestCases(TestCase):

    def setUp(self):
        self.staff = User.objects.create_superuser(
            username='staff',
            email='staff@email.com',
            password='password')

    def test_event_year_archive(self):

        self.assertTrue(self.client.login(username=self.staff.username, password='password'))

        event = EventFactory(event_date=datetime.datetime.now())

        response = self.client.get(reverse('event:event-year-archive',
                                           kwargs={'year': event.event_date.strftime('%Y')}))

        self.assertContains(response, event.location)
        self.assertTemplateUsed('event/event_archive_year.html')
        self.assertEqual(len(response.context['object_list']), 1)

    def test_event_month_archive(self):

        self.assertTrue(self.client.login(username=self.staff.username, password='password'))

        event = EventFactory(event_date=datetime.datetime.now())

        response = self.client.get(reverse('event:event-archive-month',
                                           kwargs={
                                               'year': event.event_date.strftime('%Y'),
                                               'month': event.event_date.strftime('%b').lower()}))

        self.assertContains(response, event.location)
        self.assertTemplateUsed('event/event_archive_month.html')
        self.assertEqual(len(response.context['object_list']), 1)