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

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

Implement reserved bed switcher.
Change bed_type notation.

  • Property svn:keywords set to Id
File size: 6.5 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
[6962]147    def test_add_search_edit_delete_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)
159        #import pdb; pdb.set_trace()
[6973]160        hall = self.app['hostels']['hall-1']
[6961]161        hall.blocks_for_female = ['A','B']
[6973]162        self.browser.open(self.container_path + '/hall-1')
[6961]163        expected = '''...<ul id="form.blocks_for_female" ><li>Block A</li>
164<li>Block B</li></ul>...'''
165        self.assertMatches(expected,self.browser.contents)
[6973]166        self.browser.open(self.container_path + '/hall-1/manage')
[6970]167        self.browser.getControl(name="form.floors_per_block").value = '4'
[6962]168        self.browser.getControl("Save").click()
169        self.assertTrue('Form has been saved' in self.browser.contents)
170        self.browser.open(self.manage_container_path)
171        ctrl = self.browser.getControl(name='val_id')
172        value = ctrl.options[0]
173        ctrl.getControl(value=value).selected = True
174        self.browser.getControl("Remove selected", index=0).click()
175        self.assertTrue('Successfully removed' in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.