source: main/waeup.sirp/trunk/src/waeup/sirp/hostels/tests.py @ 6987

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

Add more tests for hostel management.

  • Property svn:keywords set to Id
File size: 8.9 KB
RevLine 
[6951]1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""
[6961]17Tests for hostels. and their UI components
[6951]18"""
[6961]19import shutil
20import tempfile
[6951]21from zope.interface.verify import verifyClass, verifyObject
[6961]22from zope.component.hooks import setSite, clearSite
23from zope.testbrowser.testing import Browser
24from zope.security.interfaces import Unauthorized
[6972]25from zope.catalog.interfaces import ICatalog
26from zope.component import queryUtility
[6961]27from waeup.sirp.app import University
[6951]28from waeup.sirp.hostels.interfaces import (
[6963]29    IHostelsContainer, IHostel, IBed)
[6951]30from waeup.sirp.hostels.container import HostelsContainer
[6963]31from waeup.sirp.hostels.hostel import Hostel, Bed
[6951]32from waeup.sirp.testing import (FunctionalLayer, FunctionalTestCase)
33
34class HostelsContainerTestCase(FunctionalTestCase):
35
36    layer = FunctionalLayer
37
38    def test_interfaces(self):
39        # Make sure the correct interfaces are implemented.
40        self.assertTrue(
41            verifyClass(
42                IHostelsContainer, HostelsContainer)
43            )
44        self.assertTrue(
45            verifyObject(
46                IHostelsContainer, HostelsContainer())
47            )
48        self.assertTrue(
49            verifyClass(
50                IHostel, Hostel)
51            )
52        self.assertTrue(
53            verifyObject(
54                IHostel, Hostel())
55            )
[6963]56        self.assertTrue(
57            verifyClass(
58                IBed, Bed)
59            )
60        self.assertTrue(
61            verifyObject(
62                IBed, Bed())
63            )
[6951]64        return
65
66    def test_base(self):
67        # We cannot call the fundamental methods of a base in that case
68        container = HostelsContainer()
69        self.assertRaises(
70            NotImplementedError, container.archive)
71        self.assertRaises(
72            NotImplementedError, container.clear)
[6961]73
74class HostelsFullSetup(FunctionalTestCase):
75
76    def setUp(self):
77        super(HostelsFullSetup, self).setUp()
78
79        # Setup a sample site for each test
80        app = University()
81        self.dc_root = tempfile.mkdtemp()
82        app['datacenter'].setStoragePath(self.dc_root)
83
84        # Prepopulate the ZODB...
85        self.getRootFolder()['app'] = app
86        # we add the site immediately after creation to the
87        # ZODB. Catalogs and other local utilities are not setup
88        # before that step.
89        self.app = self.getRootFolder()['app']
90        # Set site here. Some of the following setup code might need
91        # to access grok.getSite() and should get our new app then
92        setSite(app)
93
[6972]94        # Create a hostel
95        hostel = Hostel()
[6973]96        hostel.hostel_id = u'hall-x'
[6972]97        self.app['hostels'][hostel.hostel_id] = hostel
98
99        # Create a bed
100        bed = Bed()
101        bed.bed_id = u'xyz'
102        bed.bed_number = 1
103        bed.bed_type = u'abc'
104        self.app['hostels'][hostel.hostel_id][bed.bed_id] = bed
105
[6961]106        self.container_path = 'http://localhost/app/hostels'
107        self.manage_container_path = self.container_path + '/@@manage'
108        self.add_hostel_path = self.container_path + '/addhostel'
109
110        # Put the prepopulated site into test ZODB and prepare test
111        # browser
112        self.browser = Browser()
113        self.browser.handleErrors = False
114
115    def tearDown(self):
116        super(HostelsFullSetup, self).tearDown()
117        clearSite()
118        shutil.rmtree(self.dc_root)
119
[6972]120class BedCatalogTests(HostelsFullSetup):
121
122    layer = FunctionalLayer
123
124    def test_get_catalog(self):
125        # We can get an students catalog if we wish
126        cat = queryUtility(ICatalog, name='beds_catalog')
127        assert cat is not None
128
129    def test_search_by_type(self):
130        # We can find a certain bed
131        cat = queryUtility(ICatalog, name='beds_catalog')
132        results = cat.searchResults(bed_type=('abc', 'abc'))
133        results = [x for x in results] # Turn results generator into list
134        assert len(results) == 1
[6973]135        assert results[0] is self.app['hostels']['hall-x']['xyz']
[6972]136
[6961]137class HostelsUITests(HostelsFullSetup):
138
139    layer = FunctionalLayer
140
141    def test_anonymous_access(self):
142        # Anonymous users can't access hostels containers
143        self.assertRaises(
144            Unauthorized, self.browser.open, self.manage_container_path)
145        return
146
[6985]147    def test_add_search_edit_delete_manage_hostels(self):
[6961]148        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
149        self.browser.open(self.container_path)
150        self.browser.getLink("Manage accommodation").click()
151        self.assertEqual(self.browser.headers['Status'], '200 Ok')
152        self.assertEqual(self.browser.url, self.manage_container_path)
153        self.browser.getControl("Add hostel").click()
154        self.assertEqual(self.browser.headers['Status'], '200 Ok')
155        self.assertEqual(self.browser.url, self.add_hostel_path)
156        self.browser.getControl("Create hostel").click()
157        self.assertEqual(self.browser.headers['Status'], '200 Ok')
158        self.assertTrue('Hostel created' in self.browser.contents)
[6973]159        hall = self.app['hostels']['hall-1']
[6961]160        hall.blocks_for_female = ['A','B']
[6973]161        self.browser.open(self.container_path + '/hall-1')
[6961]162        expected = '''...<ul id="form.blocks_for_female" ><li>Block A</li>
163<li>Block B</li></ul>...'''
164        self.assertMatches(expected,self.browser.contents)
[6973]165        self.browser.open(self.container_path + '/hall-1/manage')
[6985]166        self.browser.getControl(name="form.rooms_per_floor").value = '1'
[6962]167        self.browser.getControl("Save").click()
168        self.assertTrue('Form has been saved' in self.browser.contents)
[6985]169        # Since the testbrowser does not support Javascrip the
170        # save action cleared the settings above and we have to set them
171        # again
172        self.assertTrue(len(hall.blocks_for_female) == 0)
173        hall.blocks_for_female = ['A','B']
174        hall.beds_for_fresh = ['A','B']
175        hall.beds_for_all = ['D','E']
176        self.browser.getControl("Update all beds").click()
177        expected = '...0 empty beds removed, 8 beds added, 0 occupied beds modified...'
178        self.assertMatches(expected,self.browser.contents)
179        cat = queryUtility(ICatalog, name='beds_catalog')
180        results = cat.searchResults(
181            bed_type=('regular_female_all', 'regular_female_all'))
182        #import pdb; pdb.set_trace()
183        results = [x for x in results]
184        assert len(results) == 4
185        # Set bed reserved
186        ctrl = self.browser.getControl(name='val_id')
187        ctrl.getControl(value='hall-1_A_101_D').selected = True
188        self.browser.getControl("Switch reservation", index=0).click()
189        self.assertTrue('Successfully switched beds: hall-1_A_101_D'
190            in self.browser.contents)
191        assert self.app[
192            'hostels'][
193            'hall-1'][
194            'hall-1_A_101_D'].bed_type == 'regular_female_reserved'
195        expected = 'name="form.beds_reserved.0." size="20" type="text" value="A_101_D"  />'
196        self.assertTrue(expected in self.browser.contents)
197        # Release bed
198        ctrl = self.browser.getControl(name='val_id')
199        ctrl.getControl(value='hall-1_A_101_D').selected = True
200        self.browser.getControl("Switch reservation", index=0).click()
201        assert self.app[
202            'hostels'][
203            'hall-1'][
204            'hall-1_A_101_D'].bed_type == 'regular_female_all'
205        self.assertFalse(expected in self.browser.contents)
206        # Change hostel configuration
207        hall.beds_for_all = ['D']
208        self.browser.getControl("Update all beds").click()
209        expected = '...8 empty beds removed, 6 beds added, 0 occupied beds modified...'
210        self.assertMatches(expected,self.browser.contents)
211        results = cat.searchResults(
212            bed_type=('regular_female_all', 'regular_female_all'))
213        results = [x for x in results]
214        assert len(results) == 2
215        # Remove entire hostel
[6962]216        self.browser.open(self.manage_container_path)
217        ctrl = self.browser.getControl(name='val_id')
218        value = ctrl.options[0]
219        ctrl.getControl(value=value).selected = True
220        self.browser.getControl("Remove selected", index=0).click()
221        self.assertTrue('Successfully removed' in self.browser.contents)
[6985]222        # Catalog is empty
223        results = cat.searchResults(
224            bed_type=('regular_female_all', 'regular_female_all'))
225        results = [x for x in results]
226        assert len(results) == 0
Note: See TracBrowser for help on using the repository browser.