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

Last change on this file since 8852 was 8850, checked in by Henrik Bettermann, 13 years ago

mandate.category is deprecated.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: tests.py 8850 2012-06-29 14:49:01Z 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 zope.testbrowser.testing import Browser
24from datetime import datetime, timedelta
25from zope.interface.verify import verifyClass, verifyObject
26from zope.component import createObject
27from zope.component.hooks import setSite, clearSite
28from waeup.kofa.app import University
29from waeup.kofa.interfaces import IUserAccount
30from waeup.kofa.mandates.interfaces import (
31    IMandatesContainer, IMandate)
32from waeup.kofa.mandates.container import MandatesContainer
33from waeup.kofa.mandates.mandate import StudentPasswordMandate
34from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
35
36class 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, StudentPasswordMandate)
53            )
54        self.assertTrue(
55            verifyObject(
56                IMandate, StudentPasswordMandate())
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 = StudentPasswordMandate()
91        self.app['mandates'].addMandate(mandate)
92        msg = mandate.execute()
93        self.assertEqual(msg, u'Wrong mandate parameters.')
94        # Add and execute an expired mandate.
95        mandate = StudentPasswordMandate(days=0)
96        self.app['mandates'].addMandate(mandate)
97        msg = mandate.execute()
98        self.assertEqual(msg, u'Mandate expired.')
99        # Add and execute a perfect mandate
100        mandate = StudentPasswordMandate()
101        mandate.params['student_id'] = student.student_id
102        mandate.params['password'] = 'mypwd1'
103        self.app['mandates'].addMandate(mandate)
104        msg = mandate.execute()
105        # Password has been set.
106        self.assertEqual(msg, u'Password has been successfully set.')
107        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
108        # All mandates have been removed.
109        self.assertEqual(len(self.app['mandates'].keys()), 0)
110
111    def test_remove_expired(self):
112        # mandate1 is an old mandate which just expired.
113        mandate1 = StudentPasswordMandate(days=0)
114        self.app['mandates'].addMandate(mandate1)
115        # mandate2 is a new mandate with default time delta.
116        mandate2 = StudentPasswordMandate(mandate_id='23456')
117        self.app['mandates'].addMandate(mandate2)
118        self.assertEqual(len(self.app['mandates'].keys()), 2)
119        self.app['mandates'].removeExpired()
120        # Only the new mandate remains in the container.
121        self.assertEqual(len(self.app['mandates'].keys()), 1)
122        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
123
124    def test_browser(self):
125        student = createObject('waeup.Student')
126        self.app['students'].addStudent(student)
127        mandate = StudentPasswordMandate()
128        mandate.params['student_id'] = student.student_id
129        mandate.params['password'] = 'mypwd1'
130        self.app['mandates'].addMandate(mandate)
131        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
132            % mandate.mandate_id)
133        # Password has been set.
134        self.assertTrue('Password has been successfully set.'
135            in self.browser.contents)
136        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
137        # All mandates have been removed.
138        self.assertEqual(len(self.app['mandates'].keys()), 0)
Note: See TracBrowser for help on using the repository browser.