source: main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py @ 8846

Last change on this file since 8846 was 8846, checked in by Henrik Bettermann, 12 years ago

Add mandates module.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1## $Id: tests.py 8846 2012-06-29 08:19:47Z 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
23from datetime import datetime, timedelta
24from zope.interface.verify import verifyClass, verifyObject
25from zope.component import createObject
26from zope.component.hooks import setSite, clearSite
27from waeup.kofa.app import University
28from waeup.kofa.interfaces import IUserAccount
29from waeup.kofa.mandates.interfaces import (
30    IMandatesContainer, IMandate)
31from waeup.kofa.mandates.container import MandatesContainer
32from waeup.kofa.mandates.mandate import Mandate
33from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
34
35class 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.mandate_id = '12345'
88        mandate.category = 'student_password'
89        self.app['mandates'].addMandate(mandate)
90        msg = mandate.execute()
91        self.assertEqual(msg, u'Wrong mandate parameters.')
92        # Add and execute an expired mandate.
93        mandate = Mandate(days=0)
94        mandate.mandate_id = '12345'
95        mandate.category = 'student_password'
96        self.app['mandates'].addMandate(mandate)
97        msg = mandate.execute()
98        self.assertEqual(msg, u'Mandate expired.')
99        # Ad and execute a perfect mandate
100        mandate = Mandate(days=0)
101        mandate.mandate_id = '12345'
102        mandate.category = 'student_password'
103        mandate.params['student_id'] = student.student_id
104        mandate.params['password'] = 'mypwd1'
105        self.app['mandates'].addMandate(mandate)
106        msg = mandate.execute()
107        # Password has been set.
108        self.assertEqual(msg, u'Password has been successfully set.')
109        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
110        # All mandates have been removed.
111        self.assertEqual(len(self.app['mandates'].keys()), 0)
112
113    def test_remove_expired(self):
114        # mandate1 is an old mandate which just expired.
115        mandate1 = Mandate(days=0)
116        mandate1.mandate_id = '12345'
117        self.app['mandates'].addMandate(mandate1)
118        # mandate2 is a new mandate with default time delta.
119        mandate2 = Mandate()
120        mandate2.mandate_id = '23456'
121        self.app['mandates'].addMandate(mandate2)
122        self.assertEqual(len(self.app['mandates'].keys()), 2)
123        self.app['mandates'].removeExpired()
124        # Only the new mandate remains in the container.
125        self.assertEqual(len(self.app['mandates'].keys()), 1)
126        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
Note: See TracBrowser for help on using the repository browser.