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 | """ |
---|
17 | Tests for hostels. and their UI components |
---|
18 | """ |
---|
19 | import shutil |
---|
20 | import tempfile |
---|
21 | from zope.interface.verify import verifyClass, verifyObject |
---|
22 | from zope.component.hooks import setSite, clearSite |
---|
23 | from zope.testbrowser.testing import Browser |
---|
24 | from zope.security.interfaces import Unauthorized |
---|
25 | from zope.catalog.interfaces import ICatalog |
---|
26 | from zope.component import queryUtility |
---|
27 | from waeup.sirp.app import University |
---|
28 | from waeup.sirp.hostels.interfaces import ( |
---|
29 | IHostelsContainer, IHostel, IBed) |
---|
30 | from waeup.sirp.hostels.container import HostelsContainer |
---|
31 | from waeup.sirp.hostels.hostel import Hostel, Bed |
---|
32 | from waeup.sirp.testing import (FunctionalLayer, FunctionalTestCase) |
---|
33 | |
---|
34 | class 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 | ) |
---|
56 | self.assertTrue( |
---|
57 | verifyClass( |
---|
58 | IBed, Bed) |
---|
59 | ) |
---|
60 | self.assertTrue( |
---|
61 | verifyObject( |
---|
62 | IBed, Bed()) |
---|
63 | ) |
---|
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) |
---|
73 | |
---|
74 | class 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 | |
---|
94 | # Create a hostel |
---|
95 | hostel = Hostel() |
---|
96 | hostel.hostel_id = u'hall-x' |
---|
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 | |
---|
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 | |
---|
120 | class 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 |
---|
135 | assert results[0] is self.app['hostels']['hall-x']['xyz'] |
---|
136 | |
---|
137 | class 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 | |
---|
147 | def test_add_search_edit_delete_hostels(self): |
---|
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() |
---|
160 | hall = self.app['hostels']['hall-1'] |
---|
161 | hall.blocks_for_female = ['A','B'] |
---|
162 | self.browser.open(self.container_path + '/hall-1') |
---|
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) |
---|
166 | self.browser.open(self.container_path + '/hall-1/manage') |
---|
167 | self.browser.getControl(name="form.floors_per_block").value = '4' |
---|
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) |
---|