]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/test_expiry.py
844ed4b8c87a837f7095ba593ebb492058e5a945
[patchwork] / apps / 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
30     def register(self, date):
31         user = create_user()
32         user.is_active = False
33         user.date_joined = user.last_login = date
34         user.save()
35
36         conf = EmailConfirmation(type='registration', user=user,
37                                     email=user.email)
38         conf.date = date
39         conf.save()
40
41         return (user, conf)
42
43     def testOldRegistrationExpiry(self):
44         date = ((datetime.datetime.now() - EmailConfirmation.validity) -
45                 datetime.timedelta(hours = 1))
46         (user, conf) = self.register(date)
47
48         do_expiry()
49
50         self.assertFalse(User.objects.filter(pk = user.pk).exists())
51         self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
52                             .exists())
53
54
55     def testRecentRegistrationExpiry(self):
56         date = ((datetime.datetime.now() - EmailConfirmation.validity) +
57                 datetime.timedelta(hours = 1))
58         (user, conf) = self.register(date)
59
60         do_expiry()
61
62         self.assertTrue(User.objects.filter(pk = user.pk).exists())
63         self.assertTrue(EmailConfirmation.objects.filter(pk = conf.pk)
64                             .exists())
65
66     def testInactiveRegistrationExpiry(self):
67         (user, conf) = self.register(datetime.datetime.now())
68
69         # confirm registration
70         conf.user.is_active = True
71         conf.user.save()
72         conf.deactivate()
73
74         do_expiry()
75
76         self.assertTrue(User.objects.filter(pk = user.pk).exists())
77         self.assertFalse(EmailConfirmation.objects.filter(pk = conf.pk)
78                             .exists())
79
80     def testPatchSubmitterExpiry(self):
81         defaults.project.save()
82         defaults.patch_author_person.save()
83
84         # someone submits a patch...
85         patch = Patch(project = defaults.project,
86                     msgid = 'test@example.com', name = 'test patch',
87                     submitter = defaults.patch_author_person,
88                     content = defaults.patch)
89         patch.save()
90
91         # ... then starts registration...
92         date = ((datetime.datetime.now() - EmailConfirmation.validity) -
93                 datetime.timedelta(hours = 1))
94         userid = 'test-user'
95         user = User.objects.create_user(userid,
96                 defaults.patch_author_person.email, userid)
97         user.is_active = False
98         user.date_joined = user.last_login = date
99         user.save()
100
101         self.assertEqual(user.email, patch.submitter.email)
102
103         conf = EmailConfirmation(type='registration', user=user,
104                                     email=user.email)
105         conf.date = date
106         conf.save()
107
108         # ... which expires
109         do_expiry()
110
111         # we should see no matching user
112         self.assertFalse(User.objects.filter(email = patch.submitter.email)
113                             .exists())
114         # but the patch and person should still be present
115         self.assertTrue(Person.objects.filter(
116                             pk = defaults.patch_author_person.pk).exists())
117         self.assertTrue(Patch.objects.filter(pk = patch.pk).exists())
118
119         # and there should be no user associated with the person
120         self.assertEqual(Person.objects.get(pk =
121                     defaults.patch_author_person.pk).user, None)