source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py @ 6381

Last change on this file since 6381 was 6112, checked in by Henrik Bettermann, 14 years ago

Fix tests.

File size: 7.3 KB
Line 
1##
2## test_browser.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Mar 29 11:31:11 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""
23Test the applicant-related UI components.
24"""
25import shutil
26import tempfile
27import unittest
28from datetime import datetime
29from zope.app.testing.functional import FunctionalTestCase
30from zope.app.testing.functional import HTTPCaller as http
31from zope.component import createObject
32from zope.security.interfaces import Unauthorized
33from zope.testbrowser.testing import Browser
34from waeup.sirp.testing import FunctionalLayer
35from waeup.sirp.app import University
36from waeup.sirp.applicants.container import ApplicantsContainer
37
38class LoginTest(FunctionalTestCase):
39    # Here we check login view of applicants containers.
40    #
41    # Tests in here do only cover login attempts without any PINs
42    # created before.
43
44    layer = FunctionalLayer
45
46    def setUp(self):
47        super(LoginTest, self).setUp()
48
49        # Setup a sample site for each test
50        app = University()
51        self.dc_root = tempfile.mkdtemp()
52        app['datacenter'].setStoragePath(self.dc_root)
53        self.login_path = 'http://localhost/app/applicants/testapplicants/login'
54
55        # Add an applicants container where we can login (or not)
56        applicantscontainer = ApplicantsContainer()
57        applicantscontainer.ac_prefix = 'APP'
58        app['applicants']['testapplicants'] = applicantscontainer
59
60        # Put the prepopulated site into test ZODB and prepare test
61        # browser
62        self.getRootFolder()['app'] = app
63        self.browser = Browser()
64        self.browser.handleErrors = False
65
66    def tearDown(self):
67        super(LoginTest, self).tearDown()
68        shutil.rmtree(self.dc_root)
69
70    def test_anonymous_access(self):
71        # Anonymous users can access a login page
72        self.browser.open(self.login_path)
73        self.assertEqual(self.browser.headers['Status'], '200 Ok')
74        return
75
76    def test_anonymous_invalid_creds(self):
77        # Anonymous users giving invalid credentials stay at the page
78        self.browser.open(self.login_path)
79        # We do not give credentials but send the form as-is
80        submit = self.browser.getControl(name='SUBMIT')
81        submit.click()
82        # We are still at the same page...
83        self.assertEqual(self.browser.url, self.login_path)
84        self.assertEqual(self.browser.headers['Status'], '200 Ok')
85        return
86
87    def test_anonymous_invalid_creds_warning(self):
88        # Entering wrong credentials will yield a warning
89        self.browser.open(self.login_path)
90        # We do not give credentials but send the form as-is
91        submit = self.browser.getControl(name='SUBMIT')
92        submit.click()
93        self.assertTrue(
94            'Entered credentials are invalid' in self.browser.contents)
95        return
96
97    def test_manager_no_warnings(self):
98        # Browsing the login screen as a manager, won't raise warnings
99        # Authenticate ourself as manager
100        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
101        self.browser.open(self.login_path)
102        # Submit the form w/o any credentials
103        self.browser.getControl(name="SUBMIT").click()
104        self.assertTrue(
105            'Entered credentials are invalid' not in self.browser.contents)
106        return
107
108    def test_manager_no_redirect(self):
109        # Browsing the login screen as a manager won't trigger a redirect
110        # Authenticate ourself as manager
111        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
112        self.browser.open(self.login_path)
113        # Submit the form w/o any credentials
114        self.browser.getControl(name="SUBMIT").click()
115        self.assertEqual(self.browser.url, self.login_path)
116        return
117
118    def test_display_entered_values(self):
119        # After submit the entered values are displayed in the form
120        self.browser.open(self.login_path)
121        # Enter some value we can look for after submit
122        ac_series = self.browser.getControl(name="form.ac_series")
123        ac_series.value = '666'
124        self.browser.getControl(name="SUBMIT").click()
125        self.assertTrue('666' in self.browser.contents)
126        return
127
128class LoginTestWithPINs(LoginTest):
129    # Here we check login view of applicants containers with PINs provided.
130
131    # As setting up pins is time-consuming we only set them up when
132    # really needed (i.e. in this separate TestCase).
133
134    layer = FunctionalLayer
135
136    def setUp(self):
137        super(LoginTestWithPINs, self).setUp()
138
139        # Create 5 access codes with prefix'FOO' and cost 9.99 each
140        pin_container = self.getRootFolder()['app']['accesscodes']
141        pin_container.createBatch(
142            datetime.now(), 'some_userid', 'APP', 9.99, 5)
143        pins = pin_container[pin_container.keys()[0]].entries()
144        self.pins = [x.representation for x in pins]
145        self.existing_pin = self.pins[0]
146        parts = self.existing_pin.split('-')[1:]
147        self.existing_series, self.existing_number = parts
148        self.browser.handleErrors = False
149
150    def tearDown(self):
151        super(LoginTestWithPINs, self).tearDown()
152
153    def test_anonymous_valid_login(self):
154        # If we enter valid credentials, we get to the applicants form
155        self.browser.open(self.login_path)
156        # Enter some value we can look for after submit
157        ac_series = self.browser.getControl(name="form.ac_series")
158        ac_series.value = self.existing_series
159        ac_number = self.browser.getControl(name="form.ac_number")
160        ac_number.value = self.existing_number
161        self.browser.getControl(name="SUBMIT").click()
162        # We should be redirected to applicants form.
163        self.assertTrue(self.browser.url != self.login_path)
164        return
165
166    def test_anonymous_invalid_login(self):
167        # If we enter wrong credentials we won't get far
168        self.browser.open(self.login_path)
169        # Enter some value we can look for after submit
170        ac_series = self.browser.getControl(name="form.ac_series")
171        ac_series.value = 'illegal series'
172        ac_number = self.browser.getControl(name="form.ac_number")
173        ac_number.value = 'invalid number'
174        self.browser.getControl(name="SUBMIT").click()
175        # We get a warning message
176        self.assertTrue(
177            'Entered credentials are invalid' in self.browser.contents)
178        # We stay at the login page (no redirect)
179        self.assertTrue(self.browser.url == self.login_path)
180        return
181
182
183def suite():
184    suite = unittest.TestSuite()
185    for testcase in [
186        LoginTest,
187        LoginTestWithPINs,
188            ]:
189        suite.addTests(unittest.makeSuite(testcase))
190    return suite
191
192test_suite = suite
Note: See TracBrowser for help on using the repository browser.