]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/test_expiry.py
login: Focus the username field on load
[patchwork] / patchwork / tests / test_expiry.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2014 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 import unittest
21 import datetime
22 from django.test import TestCase
23 from django.contrib.auth.models import User
24 from patchwork.models import EmailConfirmation, Person, Patch
25 from patchwork.tests.utils import create_user, defaults
26 from patchwork.utils import do_expiry
27
28 class TestRegistrationExpiry(TestCase):
29     fixtures = ['default_states']
30
31     def register(self, date):
32         user = create_user()
33         user.is_active = False
34         user.date_joined = user.last_login = date
35         user.save()
36
37         conf = EmailConfirmation(type='registration', user=user,
38                                     email=user.email)
39         conf.date = date
40         conf.save()
41
42         return (user, conf)
43
44     def testOldRegistrationExpiry(self):
45         date = ((datetime.datetime.now() - EmailConfirmation.validity) -
46                 datetime.timedelta(hours = 1))
47         (user, conf) = self.register(date)
48
49         do_expiry()
50
51         self.assertFalse(User.objects.filter(pk = user.pk).exists())
52         self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
53                             .exists())
54
55
56     def testRecentRegistrationExpiry(self):
57         date = ((datetime.datetime.now() - EmailConfirmation.validity) +
58                 datetime.timedelta(hours = 1))
59         (user, conf) = self.register(date)
60
61         do_expiry()
62
63         self.assertTrue(User.objects.filter(pk = user.pk).exists())
64         self.assertTrue(EmailConfirmation.objects.filter(pk = conf.pk)
65                             .exists())
66
67     def testInactiveRegistrationExpiry(self):
68         (user, conf) = self.register(datetime.datetime.now())
69
70         # confirm registration
71         conf.user.is_active = True
72         conf.user.save()
73         conf.deactivate()
74
75         do_expiry()
76
77         self.assertTrue(User.objects.filter(pk = user.pk).exists())
78         self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
79                             .exists())
80
81     def testPatchSubmitterExpiry(self):
82         defaults.project.save()
83         defaults.patch_author_person.save()
84
85         # someone submits a patch...
86         patch = Patch(project = defaults.project,
87                     msgid = 'test@example.com', name = 'test patch',
88                     submitter = defaults.patch_author_person,
89                     content = defaults.patch)
90         patch.save()
91
92         # ... then starts registration...
93         date = ((datetime.datetime.now() - EmailConfirmation.validity) -
94                 datetime.timedelta(hours = 1))
95         userid = 'test-user'
96         user = User.objects.create_user(userid,
97                 defaults.patch_author_person.email, userid)
98         user.is_active = False
99         user.date_joined = user.last_login = date
100         user.save()
101
102         self.assertEqual(user.email, patch.submitter.email)
103
104         conf = EmailConfirmation(type='registration', user=user,
105                                     email=user.email)
106         conf.date = date
107         conf.save()
108
109         # ... which expires
110         do_expiry()
111
112         # we should see no matching user
113         self.assertFalse(User.objects.filter(email = patch.submitter.email)
114                             .exists())
115         # but the patch and person should still be present
116         self.assertTrue(Person.objects.filter(
117                             pk = defaults.patch_author_person.pk).exists())
118         self.assertTrue(Patch.objects.filter(pk = patch.pk).exists())
119
120         # and there should be no user associated with the person
121         self.assertEqual(Person.objects.get(pk =
122                     defaults.patch_author_person.pk).user, None)