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

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

First version of bed creator (work in progress).

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
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"""
17Tests for hostels. and their UI components
18"""
19import shutil
20import tempfile
21from zope.interface.verify import verifyClass, verifyObject
22from zope.component.hooks import setSite, clearSite
23from zope.testbrowser.testing import Browser
24from zope.security.interfaces import Unauthorized
25from waeup.sirp.app import University
26from waeup.sirp.hostels.interfaces import (
27    IHostelsContainer, IHostel, IBed)
28from waeup.sirp.hostels.container import HostelsContainer
29from waeup.sirp.hostels.hostel import Hostel, Bed
30from waeup.sirp.testing import (FunctionalLayer, FunctionalTestCase)
31
32class HostelsContainerTestCase(FunctionalTestCase):
33
34    layer = FunctionalLayer
35
36    def test_interfaces(self):
37        # Make sure the correct interfaces are implemented.
38        self.assertTrue(
39            verifyClass(
40                IHostelsContainer, HostelsContainer)
41            )
42        self.assertTrue(
43            verifyObject(
44                IHostelsContainer, HostelsContainer())
45            )
46        self.assertTrue(
47            verifyClass(
48                IHostel, Hostel)
49            )
50        self.assertTrue(
51            verifyObject(
52                IHostel, Hostel())
53            )
54        self.assertTrue(
55            verifyClass(
56                IBed, Bed)
57            )
58        self.assertTrue(
59            verifyObject(
60                IBed, Bed())
61            )
62        return
63
64    def test_base(self):
65        # We cannot call the fundamental methods of a base in that case
66        container = HostelsContainer()
67        self.assertRaises(
68            NotImplementedError, container.archive)
69        self.assertRaises(
70            NotImplementedError, container.clear)
71
72class HostelsFullSetup(FunctionalTestCase):
73
74    def setUp(self):
75        super(HostelsFullSetup, self).setUp()
76
77        # Setup a sample site for each test
78        app = University()
79        self.dc_root = tempfile.mkdtemp()
80        app['datacenter'].setStoragePath(self.dc_root)
81
82        # Prepopulate the ZODB...
83        self.getRootFolder()['app'] = app
84        # we add the site immediately after creation to the
85        # ZODB. Catalogs and other local utilities are not setup
86        # before that step.
87        self.app = self.getRootFolder()['app']
88        # Set site here. Some of the following setup code might need
89        # to access grok.getSite() and should get our new app then
90        setSite(app)
91
92        self.container_path = 'http://localhost/app/hostels'
93        self.manage_container_path = self.container_path + '/@@manage'
94        self.add_hostel_path = self.container_path + '/addhostel'
95
96        # Put the prepopulated site into test ZODB and prepare test
97        # browser
98        self.browser = Browser()
99        self.browser.handleErrors = False
100
101    def tearDown(self):
102        super(HostelsFullSetup, self).tearDown()
103        clearSite()
104        shutil.rmtree(self.dc_root)
105
106class HostelsUITests(HostelsFullSetup):
107
108    layer = FunctionalLayer
109
110    def test_anonymous_access(self):
111        # Anonymous users can't access hostels containers
112        self.assertRaises(
113            Unauthorized, self.browser.open, self.manage_container_path)
114        return
115
116    def test_add_search_edit_delete_hostels(self):
117        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
118        self.browser.open(self.container_path)
119        self.browser.getLink("Manage accommodation").click()
120        self.assertEqual(self.browser.headers['Status'], '200 Ok')
121        self.assertEqual(self.browser.url, self.manage_container_path)
122        self.browser.getControl("Add hostel").click()
123        self.assertEqual(self.browser.headers['Status'], '200 Ok')
124        self.assertEqual(self.browser.url, self.add_hostel_path)
125        self.browser.getControl("Create hostel").click()
126        self.assertEqual(self.browser.headers['Status'], '200 Ok')
127        self.assertTrue('Hostel created' in self.browser.contents)
128        #import pdb; pdb.set_trace()
129        hall = self.app['hostels']['hall_1']
130        hall.blocks_for_female = ['A','B']
131        self.browser.open(self.container_path + '/hall_1')
132        expected = '''...<ul id="form.blocks_for_female" ><li>Block A</li>
133<li>Block B</li></ul>...'''
134        self.assertMatches(expected,self.browser.contents)
135        self.browser.open(self.container_path + '/hall_1/manage')
136        self.browser.getControl(name="form.floors_per_block").value = '4'
137        self.browser.getControl("Save").click()
138        self.assertTrue('Form has been saved' in self.browser.contents)
139        self.browser.open(self.manage_container_path)
140        ctrl = self.browser.getControl(name='val_id')
141        value = ctrl.options[0]
142        ctrl.getControl(value=value).selected = True
143        self.browser.getControl("Remove selected", index=0).click()
144        self.assertTrue('Successfully removed' in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.