1 | ## $Id: tests.py 8848 2012-06-29 11:26:59Z 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 zope.testbrowser.testing import Browser |
---|
24 | from datetime import datetime, timedelta |
---|
25 | from zope.interface.verify import verifyClass, verifyObject |
---|
26 | from zope.component import createObject |
---|
27 | from zope.component.hooks import setSite, clearSite |
---|
28 | from waeup.kofa.app import University |
---|
29 | from waeup.kofa.interfaces import IUserAccount |
---|
30 | from waeup.kofa.mandates.interfaces import ( |
---|
31 | IMandatesContainer, IMandate) |
---|
32 | from waeup.kofa.mandates.container import MandatesContainer |
---|
33 | from waeup.kofa.mandates.mandate import Mandate |
---|
34 | from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase) |
---|
35 | |
---|
36 | class MandatesContainerTestCase(FunctionalTestCase): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def test_interfaces(self): |
---|
41 | # Make sure the correct interfaces are implemented. |
---|
42 | self.assertTrue( |
---|
43 | verifyClass( |
---|
44 | IMandatesContainer, MandatesContainer) |
---|
45 | ) |
---|
46 | self.assertTrue( |
---|
47 | verifyObject( |
---|
48 | IMandatesContainer, MandatesContainer()) |
---|
49 | ) |
---|
50 | self.assertTrue( |
---|
51 | verifyClass( |
---|
52 | IMandate, Mandate) |
---|
53 | ) |
---|
54 | self.assertTrue( |
---|
55 | verifyObject( |
---|
56 | IMandate, Mandate()) |
---|
57 | ) |
---|
58 | return |
---|
59 | |
---|
60 | def setUp(self): |
---|
61 | super(MandatesContainerTestCase, self).setUp() |
---|
62 | |
---|
63 | # Setup a sample site for each test |
---|
64 | app = University() |
---|
65 | self.dc_root = tempfile.mkdtemp() |
---|
66 | app['datacenter'].setStoragePath(self.dc_root) |
---|
67 | |
---|
68 | # Prepopulate the ZODB... |
---|
69 | self.getRootFolder()['app'] = app |
---|
70 | # we add the site immediately after creation to the |
---|
71 | # ZODB. Catalogs and other local utilities are not setup |
---|
72 | # before that step. |
---|
73 | self.app = self.getRootFolder()['app'] |
---|
74 | # Set site here. Some of the following setup code might need |
---|
75 | # to access grok.getSite() and should get our new app then |
---|
76 | setSite(app) |
---|
77 | |
---|
78 | self.browser = Browser() |
---|
79 | self.browser.handleErrors = False |
---|
80 | |
---|
81 | def tearDown(self): |
---|
82 | super(MandatesContainerTestCase, self).tearDown() |
---|
83 | clearSite() |
---|
84 | shutil.rmtree(self.dc_root) |
---|
85 | |
---|
86 | def test_set_password(self): |
---|
87 | student = createObject('waeup.Student') |
---|
88 | self.app['students'].addStudent(student) |
---|
89 | # Add and execute a mandate with missing parameters. |
---|
90 | mandate = Mandate() |
---|
91 | mandate.category = 'student_password' |
---|
92 | self.app['mandates'].addMandate(mandate) |
---|
93 | msg = mandate.execute() |
---|
94 | self.assertEqual(msg, u'Wrong mandate parameters.') |
---|
95 | # Add and execute an expired mandate. |
---|
96 | mandate = Mandate(days=0) |
---|
97 | mandate.category = 'student_password' |
---|
98 | self.app['mandates'].addMandate(mandate) |
---|
99 | msg = mandate.execute() |
---|
100 | self.assertEqual(msg, u'Mandate expired.') |
---|
101 | # Add and execute a perfect mandate |
---|
102 | mandate = Mandate() |
---|
103 | mandate.category = 'student_password' |
---|
104 | mandate.params['student_id'] = student.student_id |
---|
105 | mandate.params['password'] = 'mypwd1' |
---|
106 | self.app['mandates'].addMandate(mandate) |
---|
107 | msg = mandate.execute() |
---|
108 | # Password has been set. |
---|
109 | self.assertEqual(msg, u'Password has been successfully set.') |
---|
110 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
111 | # All mandates have been removed. |
---|
112 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
113 | |
---|
114 | def test_remove_expired(self): |
---|
115 | # mandate1 is an old mandate which just expired. |
---|
116 | mandate1 = Mandate(days=0) |
---|
117 | self.app['mandates'].addMandate(mandate1) |
---|
118 | # mandate2 is a new mandate with default time delta. |
---|
119 | mandate2 = Mandate(mandate_id='23456') |
---|
120 | self.app['mandates'].addMandate(mandate2) |
---|
121 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|
122 | self.app['mandates'].removeExpired() |
---|
123 | # Only the new mandate remains in the container. |
---|
124 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
125 | self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456']) |
---|
126 | |
---|
127 | def test_browser(self): |
---|
128 | student = createObject('waeup.Student') |
---|
129 | self.app['students'].addStudent(student) |
---|
130 | mandate = Mandate() |
---|
131 | mandate.category = 'student_password' |
---|
132 | mandate.params['student_id'] = student.student_id |
---|
133 | mandate.params['password'] = 'mypwd1' |
---|
134 | self.app['mandates'].addMandate(mandate) |
---|
135 | self.browser.open('http://localhost/app/mandate?mandate_id=%s' |
---|
136 | % mandate.mandate_id) |
---|
137 | # Password has been set. |
---|
138 | self.assertTrue('Password has been successfully set.' |
---|
139 | in self.browser.contents) |
---|
140 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
141 | # All mandates have been removed. |
---|
142 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|