# Path: patchwork/models.py
# class EmailConfirmation(models.Model):
#     validity = datetime.timedelta(days=settings.CONFIRMATION_VALIDITY_DAYS)
#     type = models.CharField(max_length=20, choices=[
#         ('userperson', 'User-Person association'),
#         ('registration', 'Registration'),
#         ('optout', 'Email opt-out'),
#     ])
#     email = models.CharField(max_length=200)
#     user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
#     key = HashField()
#     date = models.DateTimeField(default=datetime.datetime.now)
#     active = models.BooleanField(default=True)
# 
#     def deactivate(self):
#         self.active = False
#         self.save()
# 
#     def is_valid(self):
#         return self.date + self.validity > datetime.datetime.now()
# 
#     def save(self):
#         max = 1 << 32
#         if self.key == '':
#             str = '%s%s%d' % (self.user, self.email, random.randint(0, max))
#             self.key = self._meta.get_field('key').construct(str).hexdigest()
#         super(EmailConfirmation, self).save()
# 
# class Person(models.Model):
#     email = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     user = models.ForeignKey(User, null=True, blank=True,
#                              on_delete=models.SET_NULL)
# 
#     def display_name(self):
#         if self.name:
#             return self.name
#         else:
#             return self.email
# 
#     def email_name(self):
#         if (self.name):
#             return "\"%s\" <%s>" % (self.name, self.email)
#         else:
#             return self.email
# 
#     def link_to_user(self, user):
#         self.name = user.profile.name()
#         self.user = user
# 
#     def __str__(self):
#         return self.display_name()
# 
#     class Meta:
#         verbose_name_plural = 'People'


from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from patchwork.models import EmailConfirmation, Person
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




def _confirmation_url(conf):
    return reverse('confirm', kwargs={'key': conf.key})


class TestUser(object):
    username = 'testuser'
    email = 'test@example.com'
    secondary_email = 'test2@example.com'
    password = None

    def __init__(self):
        self.password = User.objects.make_random_password()
        self.user = User.objects.create_user(self.username,
                                             self.email, self.password)


class InvalidConfirmationTest(TestCase):

    def setUp(self):
        EmailConfirmation.objects.all().delete()
        Person.objects.all().delete()
        self.user = TestUser()

# Path: patchwork/models.py
# class EmailConfirmation(models.Model):
#     validity = datetime.timedelta(days=settings.CONFIRMATION_VALIDITY_DAYS)
#     type = models.CharField(max_length=20, choices=[
#         ('userperson', 'User-Person association'),
#         ('registration', 'Registration'),
#         ('optout', 'Email opt-out'),
#     ])
#     email = models.CharField(max_length=200)
#     user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
#     key = HashField()
#     date = models.DateTimeField(default=datetime.datetime.now)
#     active = models.BooleanField(default=True)
# 
#     def deactivate(self):
#         self.active = False
#         self.save()
# 
#     def is_valid(self):
#         return self.date + self.validity > datetime.datetime.now()
# 
#     def save(self):
#         max = 1 << 32
#         if self.key == '':
#             str = '%s%s%d' % (self.user, self.email, random.randint(0, max))
#             self.key = self._meta.get_field('key').construct(str).hexdigest()
#         super(EmailConfirmation, self).save()
# 
# class Person(models.Model):
#     email = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     user = models.ForeignKey(User, null=True, blank=True,
#                              on_delete=models.SET_NULL)
# 
#     def display_name(self):
#         if self.name:
#             return self.name
#         else:
#             return self.email
# 
#     def email_name(self):
#         if (self.name):
#             return "\"%s\" <%s>" % (self.name, self.email)
#         else:
#             return self.email
# 
#     def link_to_user(self, user):
#         self.name = user.profile.name()
#         self.user = user
# 
#     def __str__(self):
#         return self.display_name()
# 
#     class Meta:
#         verbose_name_plural = 'People'


# Path: patchwork/tests/test_confirm.py
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from patchwork.models import EmailConfirmation, Person

#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




def _confirmation_url(conf):
    return reverse('confirm', kwargs={'key': conf.key})


class TestUser(object):
    username = 'testuser'
    email = 'test@example.com'
    secondary_email = 'test2@example.com'
    password = None

    def __init__(self):
        self.password = User.objects.make_random_password()
        self.user = User.objects.create_user(self.username,
                                             self.email, self.password)


class InvalidConfirmationTest(TestCase):

    def setUp(self):
        EmailConfirmation.objects.all().delete()
        Person.objects.all().delete()
        self.user = TestUser()

        self.conf = EmailConfirmation(type='userperson',