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