source: main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/tests.py

Last change on this file was 14221, checked in by Henrik Bettermann, 8 years ago

Count mandates on configuration page and provide 'Purge' button.

  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1## $Id: tests.py 14221 2016-10-20 21:13:17Z 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"""
19Tests for mandates.
20"""
21import tempfile
22import shutil
23import os
24from zope.testbrowser.testing import Browser
25from datetime import datetime, timedelta
26from zope.interface.verify import verifyClass, verifyObject
27from zope.component import createObject
28from zope.component.hooks import setSite, clearSite
29from waeup.ikoba.app import Company
30from waeup.ikoba.interfaces import IUserAccount
31from waeup.ikoba.mandates.interfaces import (
32    IMandatesContainer, IMandate)
33from waeup.ikoba.mandates.container import MandatesContainer
34from waeup.ikoba.mandates.mandate import PasswordMandate
35from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
36
37class MandatesContainerTestCase(FunctionalTestCase):
38
39    layer = FunctionalLayer
40
41    def test_interfaces(self):
42        # Make sure the correct interfaces are implemented.
43        self.assertTrue(
44            verifyClass(
45                IMandatesContainer, MandatesContainer)
46            )
47        self.assertTrue(
48            verifyObject(
49                IMandatesContainer, MandatesContainer())
50            )
51        self.assertTrue(
52            verifyClass(
53                IMandate, PasswordMandate)
54            )
55        self.assertTrue(
56            verifyObject(
57                IMandate, PasswordMandate())
58            )
59        return
60
61    def setUp(self):
62        super(MandatesContainerTestCase, self).setUp()
63
64        # Setup a sample site for each test
65        app = Company()
66        self.dc_root = tempfile.mkdtemp()
67        app['datacenter'].setStoragePath(self.dc_root)
68
69        # Prepopulate the ZODB...
70        self.getRootFolder()['app'] = app
71        # we add the site immediately after creation to the
72        # ZODB. Catalogs and other local utilities are not setup
73        # before that step.
74        self.app = self.getRootFolder()['app']
75        # Set site here. Some of the following setup code might need
76        # to access grok.getSite() and should get our new app then
77        setSite(app)
78
79        self.browser = Browser()
80        self.browser.handleErrors = False
81
82    def tearDown(self):
83        super(MandatesContainerTestCase, self).tearDown()
84        clearSite()
85        shutil.rmtree(self.dc_root)
86
87    def test_set_customer_password(self):
88        customer = createObject('waeup.Customer')
89        # Add and execute a mandate with missing parameters.
90        mandate = PasswordMandate()
91        IUserAccount(customer).setPassword('old_pw')
92        self.app['mandates'].addMandate(mandate)
93        (msg, redirect_path) = mandate.execute()
94        self.assertEqual(msg, u'Wrong mandate parameters.')
95        # Add and execute an expired mandate.
96        mandate = PasswordMandate(days=0)
97        mandate.params['user'] = customer
98        mandate.params['password'] = 'mypwd1'
99        self.app['mandates'].addMandate(mandate)
100        (msg, redirect_path) = mandate.execute()
101        self.assertEqual(msg, u'Mandate expired.')
102        self.assertEqual(redirect_path, '')
103        # Password has not been set
104        self.assertTrue(IUserAccount(customer).checkPassword('old_pw'))
105        # Add and execute a perfect mandate
106        mandate = PasswordMandate()
107        mandate.params['user'] = customer
108        mandate.params['password'] = 'mypwd1'
109        self.app['mandates'].addMandate(mandate)
110        (msg, redirect_path) = mandate.execute()
111        # Password has been set.
112        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
113        self.assertTrue(IUserAccount(customer).checkPassword('mypwd1'))
114        # All mandates have been removed.
115        self.assertEqual(len(self.app['mandates'].keys()), 0)
116        logfile = os.path.join(
117            self.app['datacenter'].storage, 'logs', 'main.log')
118        logcontent = open(logfile).read()
119        self.assertTrue('system - PasswordMandate used: K1000000' in logcontent)
120
121    def test_set_officer_password(self):
122        self.app['users'].addUser('bob', 'bobssecret')
123        officer = self.app['users']['bob']
124        mandate = PasswordMandate()
125        mandate.params['user'] = officer
126        mandate.params['password'] = 'mypwd1'
127        self.app['mandates'].addMandate(mandate)
128        (msg, redirect_path) = mandate.execute()
129        # Password has been set.
130        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
131        self.assertTrue(IUserAccount(officer).checkPassword('mypwd1'))
132        logfile = os.path.join(
133            self.app['datacenter'].storage, 'logs', 'main.log')
134        logcontent = open(logfile).read()
135        self.assertTrue('system - PasswordMandate used: bob' in logcontent)
136
137    def test_remove_expired(self):
138        # mandate1 is an old mandate which just expired.
139        mandate1 = PasswordMandate(days=0)
140        self.app['mandates'].addMandate(mandate1)
141        # mandate2 is a new mandate with default time delta.
142        mandate2 = PasswordMandate(mandate_id='23456')
143        self.app['mandates'].addMandate(mandate2)
144        self.assertEqual(len(self.app['mandates'].keys()), 2)
145        num_deleted = self.app['mandates'].removeExpired()
146        self.assertEqual(num_deleted, 1)
147        # Only the new mandate remains in the container.
148        self.assertEqual(len(self.app['mandates'].keys()), 1)
149        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
150        logfile = os.path.join(
151            self.app['datacenter'].storage, 'logs', 'main.log')
152        logcontent = open(logfile).read()
153        self.assertTrue('system - 1 mandates purged' in logcontent)
154
155    def test_purge_mandates(self):
156        # mandate1 is an old mandate which just expired.
157        mandate1 = PasswordMandate(days=0)
158        self.app['mandates'].addMandate(mandate1)
159        # mandate2 is a new mandate with default time delta.
160        mandate2 = PasswordMandate(mandate_id='23456')
161        self.app['mandates'].addMandate(mandate2)
162        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
163        self.browser.open('http://localhost/app/configuration')
164        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
165        self.assertTrue('<span>expired</span>' in self.browser.contents)
166        self.browser.getControl("Purge mandates").click()
167        self.assertTrue('1 mandate(s) were purged' in self.browser.contents)
168        self.assertEqual(self.app['mandates'].count, (1, 0, 1))
169        return
170
171    def test_browser(self):
172        customer = createObject('waeup.Customer')
173        self.app['customers'].addCustomer(customer)
174        mandate = PasswordMandate()
175        mandate.params['user'] = customer
176        mandate.params['password'] = 'mypwd1'
177        self.app['mandates'].addMandate(mandate)
178        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
179            % mandate.mandate_id)
180        # Password has been set.
181        self.assertTrue(
182            'Password has been successfully set. Login with your new password.'
183            in self.browser.contents)
184        self.assertTrue(IUserAccount(customer).checkPassword('mypwd1'))
185        # All mandates have been removed.
186        self.assertEqual(len(self.app['mandates'].keys()), 0)
187        # We redirect to login page not to the frontpage.
188        self.assertEqual(
189            self.browser.url,
190            'http://localhost/app/login?camefrom=PasswordMandate')
191        # After login we redirect to the changepassword page.
192        self.browser.getControl(name="form.login").value = customer.customer_id
193        self.browser.getControl(name="form.password").value = 'mypwd1'
194        self.browser.getControl("Login").click()
195        self.assertEqual(
196            self.browser.url,
197            'http://localhost/app/customers/%s/changepassword'
198            % customer.customer_id)
Note: See TracBrowser for help on using the repository browser.