1 | ## $Id: tests.py 8847 2012-06-29 10:21:03Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Tests for mandates. |
---|
20 | """ |
---|
21 | import tempfile |
---|
22 | import shutil |
---|
23 | from datetime import datetime, timedelta |
---|
24 | from zope.interface.verify import verifyClass, verifyObject |
---|
25 | from zope.component import createObject |
---|
26 | from zope.component.hooks import setSite, clearSite |
---|
27 | from waeup.kofa.app import University |
---|
28 | from waeup.kofa.interfaces import IUserAccount |
---|
29 | from waeup.kofa.mandates.interfaces import ( |
---|
30 | IMandatesContainer, IMandate) |
---|
31 | from waeup.kofa.mandates.container import MandatesContainer |
---|
32 | from waeup.kofa.mandates.mandate import Mandate |
---|
33 | from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase) |
---|
34 | |
---|
35 | class MandatesContainerTestCase(FunctionalTestCase): |
---|
36 | |
---|
37 | layer = FunctionalLayer |
---|
38 | |
---|
39 | def test_interfaces(self): |
---|
40 | # Make sure the correct interfaces are implemented. |
---|
41 | self.assertTrue( |
---|
42 | verifyClass( |
---|
43 | IMandatesContainer, MandatesContainer) |
---|
44 | ) |
---|
45 | self.assertTrue( |
---|
46 | verifyObject( |
---|
47 | IMandatesContainer, MandatesContainer()) |
---|
48 | ) |
---|
49 | self.assertTrue( |
---|
50 | verifyClass( |
---|
51 | IMandate, Mandate) |
---|
52 | ) |
---|
53 | self.assertTrue( |
---|
54 | verifyObject( |
---|
55 | IMandate, Mandate()) |
---|
56 | ) |
---|
57 | return |
---|
58 | |
---|
59 | def setUp(self): |
---|
60 | super(MandatesContainerTestCase, self).setUp() |
---|
61 | |
---|
62 | # Setup a sample site for each test |
---|
63 | app = University() |
---|
64 | self.dc_root = tempfile.mkdtemp() |
---|
65 | app['datacenter'].setStoragePath(self.dc_root) |
---|
66 | |
---|
67 | # Prepopulate the ZODB... |
---|
68 | self.getRootFolder()['app'] = app |
---|
69 | # we add the site immediately after creation to the |
---|
70 | # ZODB. Catalogs and other local utilities are not setup |
---|
71 | # before that step. |
---|
72 | self.app = self.getRootFolder()['app'] |
---|
73 | # Set site here. Some of the following setup code might need |
---|
74 | # to access grok.getSite() and should get our new app then |
---|
75 | setSite(app) |
---|
76 | |
---|
77 | def tearDown(self): |
---|
78 | super(MandatesContainerTestCase, self).tearDown() |
---|
79 | clearSite() |
---|
80 | shutil.rmtree(self.dc_root) |
---|
81 | |
---|
82 | def test_set_password(self): |
---|
83 | student = createObject('waeup.Student') |
---|
84 | self.app['students'].addStudent(student) |
---|
85 | # Add and execute a mandate with missing parameters. |
---|
86 | mandate = Mandate() |
---|
87 | mandate.category = 'student_password' |
---|
88 | self.app['mandates'].addMandate(mandate) |
---|
89 | msg = mandate.execute() |
---|
90 | self.assertEqual(msg, u'Wrong mandate parameters.') |
---|
91 | # Add and execute an expired mandate. |
---|
92 | mandate = Mandate(days=0) |
---|
93 | mandate.category = 'student_password' |
---|
94 | self.app['mandates'].addMandate(mandate) |
---|
95 | msg = mandate.execute() |
---|
96 | self.assertEqual(msg, u'Mandate expired.') |
---|
97 | # Ad and execute a perfect mandate |
---|
98 | mandate = Mandate(days=0) |
---|
99 | mandate.category = 'student_password' |
---|
100 | mandate.params['student_id'] = student.student_id |
---|
101 | mandate.params['password'] = 'mypwd1' |
---|
102 | self.app['mandates'].addMandate(mandate) |
---|
103 | msg = mandate.execute() |
---|
104 | # Password has been set. |
---|
105 | self.assertEqual(msg, u'Password has been successfully set.') |
---|
106 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
107 | # All mandates have been removed. |
---|
108 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
109 | |
---|
110 | def test_remove_expired(self): |
---|
111 | # mandate1 is an old mandate which just expired. |
---|
112 | mandate1 = Mandate(days=0) |
---|
113 | self.app['mandates'].addMandate(mandate1) |
---|
114 | # mandate2 is a new mandate with default time delta. |
---|
115 | mandate2 = Mandate(mandate_id='23456') |
---|
116 | self.app['mandates'].addMandate(mandate2) |
---|
117 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|
118 | self.app['mandates'].removeExpired() |
---|
119 | # Only the new mandate remains in the container. |
---|
120 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
121 | self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456']) |
---|