1 | ## $Id: test_browser.py 12239 2014-12-15 06:48:21Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Test the customer-related UI components. |
---|
20 | """ |
---|
21 | import shutil |
---|
22 | import tempfile |
---|
23 | from datetime import datetime, timedelta, date |
---|
24 | from StringIO import StringIO |
---|
25 | import os |
---|
26 | from zope.event import notify |
---|
27 | from zope.component import createObject, queryUtility, getUtility |
---|
28 | from zope.component.hooks import setSite, clearSite |
---|
29 | from zope.schema.interfaces import ConstraintNotSatisfied |
---|
30 | from zope.catalog.interfaces import ICatalog |
---|
31 | from zope.security.interfaces import Unauthorized |
---|
32 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
33 | from zope.testbrowser.testing import Browser |
---|
34 | from hurry.workflow.interfaces import ( |
---|
35 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
36 | from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase |
---|
37 | from waeup.ikoba.app import Company |
---|
38 | from waeup.ikoba.interfaces import ( |
---|
39 | IUserAccount, IJobManager, APPROVED, SUBMITTED, |
---|
40 | IFileStoreNameChooser, IExtFileStore, IFileStoreHandler) |
---|
41 | from waeup.ikoba.imagestorage import ( |
---|
42 | FileStoreNameChooser, ExtFileStore, DefaultFileStoreHandler, |
---|
43 | DefaultStorage) |
---|
44 | from waeup.ikoba.authentication import LocalRoleSetEvent |
---|
45 | from waeup.ikoba.tests.test_async import FunctionalAsyncTestCase |
---|
46 | from waeup.ikoba.interfaces import VERIFIED |
---|
47 | from waeup.ikoba.browser.tests.test_pdf import samples_dir |
---|
48 | |
---|
49 | PH_LEN = 15911 # Length of placeholder file |
---|
50 | |
---|
51 | SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
52 | #SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp') |
---|
53 | SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf') |
---|
54 | |
---|
55 | class FullSetup(FunctionalTestCase): |
---|
56 | # A test case that only contains a setup and teardown |
---|
57 | # |
---|
58 | # Complete setup for customers handlings is rather complex and |
---|
59 | # requires lots of things created before we can start. This is a |
---|
60 | # setup that does all this, creates a company etc. |
---|
61 | # so that we do not have to bother with that in different |
---|
62 | # test cases. |
---|
63 | |
---|
64 | layer = FunctionalLayer |
---|
65 | |
---|
66 | def setUp(self): |
---|
67 | super(FullSetup, self).setUp() |
---|
68 | |
---|
69 | # Setup a sample site for each test |
---|
70 | app = Company() |
---|
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.login_path = 'http://localhost/app/login' |
---|
85 | self.container_path = 'http://localhost/app/documents' |
---|
86 | |
---|
87 | # Put the prepopulated site into test ZODB and prepare test |
---|
88 | # browser |
---|
89 | self.browser = Browser() |
---|
90 | self.browser.handleErrors = False |
---|
91 | |
---|
92 | def tearDown(self): |
---|
93 | super(FullSetup, self).tearDown() |
---|
94 | clearSite() |
---|
95 | shutil.rmtree(self.dc_root) |
---|
96 | |
---|
97 | |
---|
98 | class DocumentUITests(FullSetup): |
---|
99 | # Tests for document related views and pages |
---|
100 | |
---|
101 | def test_manage_file_document(self): |
---|
102 | # Managers can access the pages of documentsconter |
---|
103 | # and can perform actions |
---|
104 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
105 | self.browser.open('http://localhost/app') |
---|
106 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
107 | self.browser.getLink("Documents").click() |
---|
108 | self.assertEqual(self.browser.url, self.container_path) |
---|
109 | self.browser.getLink("Manage").click() |
---|
110 | self.browser.getControl("Add document").click() |
---|
111 | self.browser.getControl(name="doctype").value = ['PDFDocument'] |
---|
112 | self.browser.getControl(name="form.title").value = 'My PDF Document' |
---|
113 | self.browser.getControl("Add document").click() |
---|
114 | self.assertTrue('PDF Document added.' in self.browser.contents) |
---|
115 | document = self.app['documents']['d101'] |
---|
116 | |
---|
117 | # Document can be edited |
---|
118 | self.browser.getLink("d101").click() |
---|
119 | self.browser.getLink("Manage").click() |
---|
120 | self.browser.getControl(name="form.title").value = 'My first doc' |
---|
121 | self.browser.getControl("Save").click() |
---|
122 | self.assertTrue('Form has been saved.' in self.browser.contents) |
---|
123 | self.browser.getLink("View").click() |
---|
124 | self.assertEqual(self.browser.url, self.container_path + '/d101/index') |
---|
125 | |
---|
126 | # File can be uploaded |
---|
127 | self.browser.getLink("Manage").click() |
---|
128 | # Create a pseudo image file and select it to be uploaded |
---|
129 | image = open(SAMPLE_IMAGE, 'rb') |
---|
130 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
131 | file_ctrl = ctrl.mech_control |
---|
132 | file_ctrl.add_file(image, filename='my_sample_scan.jpg') |
---|
133 | self.browser.getControl( |
---|
134 | name='upload_pdfscanmanageupload').click() |
---|
135 | self.assertTrue( |
---|
136 | 'pdf file extension expected' in self.browser.contents) |
---|
137 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
138 | file_ctrl = ctrl.mech_control |
---|
139 | file_ctrl.add_file(image, filename='my_sample_scan.pdf') |
---|
140 | self.browser.getControl( |
---|
141 | name='upload_pdfscanmanageupload').click() |
---|
142 | self.assertTrue( |
---|
143 | 'Could not determine file type' in self.browser.contents) |
---|
144 | pdf = open(SAMPLE_PDF, 'rb') |
---|
145 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
146 | file_ctrl = ctrl.mech_control |
---|
147 | file_ctrl.add_file(pdf, filename='my_sample_scan.pdf') |
---|
148 | self.browser.getControl( |
---|
149 | name='upload_pdfscanmanageupload').click() |
---|
150 | self.assertTrue( |
---|
151 | 'href="http://localhost/app/documents/d101/sample.pdf">PDF File</a>' |
---|
152 | in self.browser.contents) |
---|
153 | # Browsing the link shows a real pdf |
---|
154 | self.browser.open('sample.pdf') |
---|
155 | self.assertEqual( |
---|
156 | self.browser.headers['content-type'], 'application/pdf') |
---|
157 | |
---|
158 | # Transitions can be performed |
---|
159 | self.browser.open(self.container_path + '/d101') |
---|
160 | self.browser.getLink("Transition").click() |
---|
161 | self.browser.getControl(name="transition").value = ['publish'] |
---|
162 | self.browser.getControl("Save").click() |
---|
163 | self.assertEqual(document.state, 'published') |
---|
164 | |
---|
165 | # Documents can be removed |
---|
166 | self.browser.getLink("Documents").click() |
---|
167 | self.browser.getLink("Manage").click() |
---|
168 | ctrl = self.browser.getControl(name='val_id') |
---|
169 | ctrl.getControl(value=document.document_id).selected = True |
---|
170 | self.browser.getControl("Remove selected", index=0).click() |
---|
171 | self.assertTrue('Successfully removed' in self.browser.contents) |
---|
172 | |
---|
173 | # All actions are being logged |
---|
174 | logfile = os.path.join( |
---|
175 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
176 | logcontent = open(logfile).read() |
---|
177 | |
---|
178 | self.assertTrue( |
---|
179 | 'INFO - zope.mgr - %s - Document created' % document.document_id |
---|
180 | in logcontent) |
---|
181 | self.assertTrue( |
---|
182 | 'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: PDF Document %s' |
---|
183 | % document.document_id in logcontent) |
---|
184 | self.assertTrue( |
---|
185 | 'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - saved: title' |
---|
186 | % document.document_id in logcontent) |
---|
187 | self.assertTrue( |
---|
188 | 'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - uploaded: sample.pdf (my_sample_scan.pdf)' |
---|
189 | % document.document_id in logcontent) |
---|
190 | self.assertTrue( |
---|
191 | 'INFO - zope.mgr - %s - Document published' % document.document_id |
---|
192 | in logcontent) |
---|
193 | self.assertTrue( |
---|
194 | 'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s' |
---|
195 | % document.document_id in logcontent) |
---|
196 | |
---|
197 | def test_manage_html_document(self): |
---|
198 | # Managers can access the pages of documentsconter |
---|
199 | # and can perform actions |
---|
200 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
201 | self.browser.open('http://localhost/app') |
---|
202 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
203 | self.browser.getLink("Documents").click() |
---|
204 | self.assertEqual(self.browser.url, self.container_path) |
---|
205 | self.browser.getLink("Manage").click() |
---|
206 | self.browser.getControl("Add document").click() |
---|
207 | self.browser.getControl(name="doctype").value = ['HTMLDocument'] |
---|
208 | self.browser.getControl(name="form.title").value = 'My HTML Document' |
---|
209 | self.browser.getControl("Add document").click() |
---|
210 | self.assertTrue('HTML Document added.' in self.browser.contents) |
---|
211 | document = self.app['documents']['d101'] |
---|
212 | |
---|
213 | # Document can be edited |
---|
214 | self.browser.getLink("d101").click() |
---|
215 | self.browser.getLink("Manage").click() |
---|
216 | self.browser.getControl(name="form.title").value = 'My second doc' |
---|
217 | self.browser.getControl(name="form.html_multilingual").value = """ |
---|
218 | <h1>Hello World</h1> |
---|
219 | >>de<< |
---|
220 | <h1>Hallo Welt</h1> |
---|
221 | """ |
---|
222 | self.browser.getControl("Save").click() |
---|
223 | self.assertTrue('Form has been saved.' in self.browser.contents) |
---|
224 | self.browser.getLink("View").click() |
---|
225 | self.assertEqual(self.browser.url, self.container_path + '/d101/index') |
---|
226 | self.assertTrue( |
---|
227 | '<h1>Hello World</h1>' in self.browser.contents) |
---|
228 | self.assertFalse( |
---|
229 | '<h1>Hallo Welt</h1>' in self.browser.contents) |
---|
230 | self.browser.getLink("de").click() |
---|
231 | self.assertFalse( |
---|
232 | '<h1>Hello World</h1>' in self.browser.contents) |
---|
233 | self.assertTrue( |
---|
234 | '<h1>Hallo Welt</h1>' in self.browser.contents) |
---|
235 | |
---|
236 | # Transitions can be performed |
---|
237 | self.browser.open(self.container_path + '/d101') |
---|
238 | self.browser.getLink("Transition").click() |
---|
239 | self.browser.getControl(name="transition").value = ['publish'] |
---|
240 | self.browser.getControl("Speichern").click() |
---|
241 | self.assertEqual(document.state, 'published') |
---|
242 | |
---|
243 | # Documents can be removed |
---|
244 | self.browser.getLink("en", index=2).click() |
---|
245 | self.browser.getLink("Documents").click() |
---|
246 | self.browser.getLink("Manage").click() |
---|
247 | ctrl = self.browser.getControl(name='val_id') |
---|
248 | ctrl.getControl(value=document.document_id).selected = True |
---|
249 | self.browser.getControl("Remove selected", index=0).click() |
---|
250 | self.assertTrue('Successfully removed' in self.browser.contents) |
---|
251 | |
---|
252 | # All actions are being logged |
---|
253 | logfile = os.path.join( |
---|
254 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
255 | logcontent = open(logfile).read() |
---|
256 | |
---|
257 | self.assertTrue( |
---|
258 | 'INFO - zope.mgr - %s - Document created' % document.document_id |
---|
259 | in logcontent) |
---|
260 | self.assertTrue( |
---|
261 | 'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: HTML Document %s' |
---|
262 | % document.document_id in logcontent) |
---|
263 | self.assertTrue( |
---|
264 | 'INFO - zope.mgr - documents.browser.HTMLDocumentManageFormPage - %s - saved: title + html_multilingual' |
---|
265 | % document.document_id in logcontent) |
---|
266 | self.assertTrue( |
---|
267 | 'INFO - zope.mgr - %s - Document published' % document.document_id |
---|
268 | in logcontent) |
---|
269 | self.assertTrue( |
---|
270 | 'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s' |
---|
271 | % document.document_id in logcontent) |
---|
272 | |
---|