summaryrefslogtreecommitdiffstats
path: root/patchwork/tests/test_expiry.py
blob: ca22970ccddf9fd5fadece47b4cf9dcc8255ab29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Patchwork - automated patch tracking system
# Copyright (C) 2014 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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

import unittest
import datetime
from django.test import TestCase
from django.contrib.auth.models import User
from patchwork.models import EmailConfirmation, Person, Patch
from patchwork.tests.utils import create_user, defaults
from patchwork.utils import do_expiry

class TestRegistrationExpiry(TestCase):
    fixtures = ['default_states']

    def register(self, date):
        user = create_user()
        user.is_active = False
        user.date_joined = user.last_login = date
        user.save()

        conf = EmailConfirmation(type='registration', user=user,
                                    email=user.email)
        conf.date = date
        conf.save()

        return (user, conf)

    def testOldRegistrationExpiry(self):
        date = ((datetime.datetime.now() - EmailConfirmation.validity) -
                datetime.timedelta(hours = 1))
        (user, conf) = self.register(date)

        do_expiry()

        self.assertFalse(User.objects.filter(pk = user.pk).exists())
        self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
                            .exists())


    def testRecentRegistrationExpiry(self):
        date = ((datetime.datetime.now() - EmailConfirmation.validity) +
                datetime.timedelta(hours = 1))
        (user, conf) = self.register(date)

        do_expiry()

        self.assertTrue(User.objects.filter(pk = user.pk).exists())
        self.assertTrue(EmailConfirmation.objects.filter(pk = conf.pk)
                            .exists())

    def testInactiveRegistrationExpiry(self):
        (user, conf) = self.register(datetime.datetime.now())

        # confirm registration
        conf.user.is_active = True
        conf.user.save()
        conf.deactivate()

        do_expiry()

        self.assertTrue(User.objects.filter(pk = user.pk).exists())
        self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
                            .exists())

    def testPatchSubmitterExpiry(self):
        defaults.project.save()
        defaults.patch_author_person.save()

        # someone submits a patch...
        patch = Patch(project = defaults.project,
                    msgid = 'test@example.com', name = 'test patch',
                    submitter = defaults.patch_author_person,
                    content = defaults.patch)
        patch.save()

        # ... then starts registration...
        date = ((datetime.datetime.now() - EmailConfirmation.validity) -
                datetime.timedelta(hours = 1))
        userid = 'test-user'
        user = User.objects.create_user(userid,
                defaults.patch_author_person.email, userid)
        user.is_active = False
        user.date_joined = user.last_login = date
        user.save()

        self.assertEqual(user.email, patch.submitter.email)

        conf = EmailConfirmation(type='registration', user=user,
                                    email=user.email)
        conf.date = date
        conf.save()

        # ... which expires
        do_expiry()

        # we should see no matching user
        self.assertFalse(User.objects.filter(email = patch.submitter.email)
                            .exists())
        # but the patch and person should still be present
        self.assertTrue(Person.objects.filter(
                            pk = defaults.patch_author_person.pk).exists())
        self.assertTrue(Patch.objects.filter(pk = patch.pk).exists())

        # and there should be no user associated with the person
        self.assertEqual(Person.objects.get(pk =
                    defaults.patch_author_person.pk).user, None)