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:email_verification",
                kwargs={
                    "verification_key": self.user.profile.verification_key,
                }
            ),
            follow=True,
        )

        self.assertEqual(
           response.status_code,
           200,
        )

        self.assertRedirects(
            response,
            "/signin/?next=/",
        )

        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()

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

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

        self.assertEqual(
           response.status_code,
           200,
        )

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

@@ -1147,24 +1147,128 @@
 ile.save()%0A%0A
+        self.assertTrue(%0A            get_user_model().objects.last().profile.is_expired_key,%0A        )%0A%0A
         resp
@@ -1283,32 +1283,32 @@
 elf.client.get(%0A
-
             reve
@@ -1533,112 +1533,8 @@
  )%0A%0A
-        self.assertTrue(%0A            get_user_model().objects.last().profile.is_expired_key,%0A        )%0A%0A
     
@@ -1839,32 +1839,32 @@
               %7D%0A
-
             ),%0A 
@@ -1860,20 +1860,1107 @@
        ),%0A        )%0A
+%0A    def test_renew_user_verification_key(self):%0A%0A        # Make verification_key expire%0A        self.user.profile.key_expires = set_expiration_date(-1)%0A        self.user.profile.save()%0A%0A        self.assertTrue(%0A            get_user_model().objects.last().profile.is_expired_key,%0A        )%0A%0A        # Save previous user verification key%0A        prev_verification_key = self.user.profile.verification_key%0A%0A        response = self.client.post(%0A            reverse(%0A                %22profiles:renew_key%22,%0A            ),%0A            data=%7B%0A                %22verification_key%22: self.user.profile.verification_key,%0A            %7D,%0A            follow=True,%0A        )%0A%0A        self.assertEqual(%0A           response.status_code,%0A           200,%0A        )%0A%0A        self.assertRedirects(%0A            response,%0A            %22/signin/?next=/%22,%0A        )%0A%0A        self.assertFalse(%0A            get_user_model().objects.last().profile.is_expired_key,%0A        )%0A%0A        self.assertNotEqual(%0A            prev_verification_key,%0A            get_user_model().objects.last().profile.verification_key,%0A        )%0A

update ProfileViewTestCase: test to renew user verification key