from django.test import Client
from django.core.urlresolvers import reverse
from django.contrib.auth import get_user_model
import datetime

from core.tests.base import BaseTestCase
from profiles.utils import set_expiration_date


class ProfileViewTestCase(BaseTestCase):

    def setUp(self):
        super(ProfileViewTestCase, self).setUp()
        self.client = Client()

    def test_verify_user_verification_key(self):
        response = self.client.get(
            reverse(
                "profiles:verify_email",
                kwargs={
                    "verification_key": self.user.profile.verification_key,
                }
            ),
        )

        self.assertEqual(
           response.status_code,
           302,
        )

        self.assertTrue(
            get_user_model().objects.last().is_verified,
        )

    def test_user_verification_key_expires(self):

        # Make verification_key expire
        self.user.profile.key_expires = set_expiration_date(-1)
        self.user.profile.save()

        self.assertTrue(
            get_user_model().objects.last().profile.is_expired_key,
        )

        response = self.client.get(
            reverse(
                "profiles:verify_email",
                kwargs={
                    "verification_key": self.user.profile.verification_key,
                }
            ),
        )

        self.assertEqual(
           response.status_code,
           302,
        )

        self.assertRedirects(
            response,
            reverse(
                "profiles:key_expires",
                kwargs={
                    "verification_key": self.user.profile.verification_key,
                }
            ),
        )

    def test_renew_user_verification_key(self):

        # Make verification_key expire
        self.user.profile.key_expires = set_expiration_date(-1)
        self.user.profile.save()

        self.assertTrue(
            get_user_model().objects.last().profile.is_expired_key,
        )

        # Save previous user verification key
        prev_verification_key = self.user.profile.verification_key

        response = self.client.post(
            reverse(
                "profiles:renew_key",
            ),
            data={
                "verification_key": self.user.profile.verification_key,
            },
        )

        self.assertEqual(
           response.status_code,
           302,
        )

        self.assertFalse(
            get_user_model().objects.last().profile.is_expired_key,
        )

        self.assertNotEqual(
            prev_verification_key,
            get_user_model().objects.last().profile.verification_key,
        )

@@ -1482,17 +1482,13 @@
 sert
-Redirects
+False
 (%0A  
@@ -1501,211 +1501,51 @@
     
-response,%0A            reverse(%0A                %22profiles:key_expires%22,%0A                kwargs=%7B%0A                    %22verification_key%22: self.user.profile.verification_key,%0A                %7D%0A            )
+get_user_model().objects.last().is_verified
 ,%0A  

update ProfileViewTestCase: check that user is unverified when user verification key expires