# Path: userprofiles/contrib/emailverification/models.py
# class EmailVerification(models.Model):
#     user = models.ForeignKey(User, verbose_name=_('User'), blank=False)
#     old_email = models.EmailField(_('Old e-mail address'))
#     new_email = models.EmailField(_('New e-mail address'))
# 
#     token = models.CharField(_('Token'), max_length=40, default=generate_token)
#     code = models.CharField(_('Code'), max_length=40, default=generate_token)
# 
#     is_approved = models.BooleanField(_('Approved'), default=False)
#     is_expired = models.BooleanField(_('Expired'), default=False)
# 
#     expiration_date = models.DateTimeField(_('Expiration date'),
#         default=generate_confirm_expire_date)
# 
#     def __unicode__(self):
#         return '%s - %s/%s' % (self.user, self.old_email, self.new_email)
# 
#     def save(self, *args, **kwargs):
#         if self.is_approved:
#             EmailVerification.objects.filter(
#                 user=self.user, is_approved=False).update(is_expired=True)
# 
#             self.is_expired = True
# 
#             if self.user.email == self.old_email:
#                 self.user.email = self.new_email
#                 self.user.save()
#         return super(EmailVerification, self).save(*args, **kwargs)
# 
#     class Meta:
#         app_label = 'userprofiles'
#         verbose_name = _('E-mail verification')
#         verbose_name_plural = _('E-mail verifications')
# 
# Path: userprofiles/settings.py
# class Settings(object):
#     def __init__(self, **kwargs):
#     def __getattr__(self, key):
# def validate_settings():


from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.settings import up_settings
    def test_email_change_email_in_use(self):
        EmailVerification.objects.create(
            user=self.user, old_email=self.user.email, new_email='test@example.com')

        self.client.login(username=self.data['username'], password=self.data['password'])

        response = self.client.post(reverse('userprofiles_email_change'),
            {'new_email': 'test@example.com'}, follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'new_email',
            [u'This email address is already in use. Please supply a different email address.'])

    def test_email_change_approve(self):
        verification = EmailVerification.objects.create(
            user=self.user, old_email=self.user.email, new_email='test@example.com')

        self.client.login(username=self.data['username'], password=self.data['password'])

        url = reverse('userprofiles_email_change_approve',
            kwargs={'token': verification.token, 'code': verification.code})
        response = self.client.get(url)

        self.assertEqual(response.status_code, 302)
        self.assertEqual(User.objects.get(pk=self.user.pk).email, 'test@example.com')

        self.assertFalse(EmailVerification.objects.filter(user=self.user,
            is_approved=False).exists())

    def test_email_change_approve_not_exists(self):

# Path: userprofiles/contrib/emailverification/models.py
# class EmailVerification(models.Model):
#     user = models.ForeignKey(User, verbose_name=_('User'), blank=False)
#     old_email = models.EmailField(_('Old e-mail address'))
#     new_email = models.EmailField(_('New e-mail address'))
# 
#     token = models.CharField(_('Token'), max_length=40, default=generate_token)
#     code = models.CharField(_('Code'), max_length=40, default=generate_token)
# 
#     is_approved = models.BooleanField(_('Approved'), default=False)
#     is_expired = models.BooleanField(_('Expired'), default=False)
# 
#     expiration_date = models.DateTimeField(_('Expiration date'),
#         default=generate_confirm_expire_date)
# 
#     def __unicode__(self):
#         return '%s - %s/%s' % (self.user, self.old_email, self.new_email)
# 
#     def save(self, *args, **kwargs):
#         if self.is_approved:
#             EmailVerification.objects.filter(
#                 user=self.user, is_approved=False).update(is_expired=True)
# 
#             self.is_expired = True
# 
#             if self.user.email == self.old_email:
#                 self.user.email = self.new_email
#                 self.user.save()
#         return super(EmailVerification, self).save(*args, **kwargs)
# 
#     class Meta:
#         app_label = 'userprofiles'
#         verbose_name = _('E-mail verification')
#         verbose_name_plural = _('E-mail verifications')
# 
# Path: userprofiles/settings.py
# class Settings(object):
#     def __init__(self, **kwargs):
#     def __getattr__(self, key):
# def validate_settings():


# Path: userprofiles/contrib/emailverification/tests/test_views.py
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.settings import up_settings

    def test_email_change_email_in_use(self):
        EmailVerification.objects.create(
            user=self.user, old_email=self.user.email, new_email='test@example.com')

        self.client.login(username=self.data['username'], password=self.data['password'])

        response = self.client.post(reverse('userprofiles_email_change'),
            {'new_email': 'test@example.com'}, follow=True)

        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'new_email',
            [u'This email address is already in use. Please supply a different email address.'])

    def test_email_change_approve(self):
        verification = EmailVerification.objects.create(
            user=self.user, old_email=self.user.email, new_email='test@example.com')

        self.client.login(username=self.data['username'], password=self.data['password'])

        url = reverse('userprofiles_email_change_approve',
            kwargs={'token': verification.token, 'code': verification.code})
        response = self.client.get(url)

        self.assertEqual(response.status_code, 302)
        self.assertEqual(User.objects.get(pk=self.user.pk).email, 'test@example.com')

        self.assertFalse(EmailVerification.objects.filter(user=self.user,
            is_approved=False).exists())

    def test_email_change_approve_not_exists(self):

        verification = EmailVerification.objects.create(