__author__ = 'traviswarren'

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

from train_track.apps.profile.models import UserProfile


class ProfileCreateViewTestCases(TestCase):

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

    def test_get_form(self):

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

        response = self.client.get(reverse('profile:profile-create'))

        self.assertTemplateUsed(response, 'profile/userprofile_form.html')
        self.assertContains(response, 'New Profile')

    def test_post_invalid_form(self):

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

        response = self.client.post(reverse('profile:profile-create'), data={'first_name': ''})

        self.assertTemplateUsed(response, 'profile/userprofile_form.html')
        self.failIf(response.context['form'].is_valid())
        self.assertFormError(response, 'form', 'first_name', 'This field is required.')

    def test_post_valid_form(self):

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

        response = self.client.post(reverse('profile:profile-create'), data={
            'first_name': 'Travis', 'last_name': 'warren', 'email': 'test@test.com',
            'phone': '0000-000-000', 'dob': '01/01/2000', 'sex': 'F', 'is_active': False})

        self.assertRedirects(response, 'http://testserver/profiles/list/all/')

        self.assertTrue(UserProfile.objects.filter(user__username='traviswarren').exists())