1 | ## $Id: test_browser.py 12401 2015-01-05 07:45:40Z 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 | Document browser and functional tests. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import grok |
---|
23 | import datetime |
---|
24 | from cStringIO import StringIO |
---|
25 | from zope.component import queryUtility, getUtility, createObject |
---|
26 | from zope.event import notify |
---|
27 | from zope.interface.verify import verifyObject, verifyClass |
---|
28 | from zope.testbrowser.testing import Browser |
---|
29 | from hurry.workflow.interfaces import IWorkflowState |
---|
30 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
31 | from waeup.ikoba.customers.tests.test_browser import CustomersFullSetup |
---|
32 | from ikobacustom.pcn.customers.export import ( |
---|
33 | PCNCustomerExporter, |
---|
34 | PCNCustomerPDFDocumentExporter, |
---|
35 | PCNCustomerJPGDocumentExporter, |
---|
36 | PCNLicenseContractExporter) |
---|
37 | from ikobacustom.pcn.customers.batching import ( |
---|
38 | PCNCustomerProcessor, |
---|
39 | PCNCustomerPDFDocumentProcessor, |
---|
40 | PCNCustomerJPGDocumentProcessor, |
---|
41 | PCNLicenseContractProcessor) |
---|
42 | from ikobacustom.pcn.testing import FunctionalLayer, samples_dir |
---|
43 | |
---|
44 | SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
45 | SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp') |
---|
46 | SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf') |
---|
47 | |
---|
48 | class CustomerImportExportTest(CustomerImportExportSetup): |
---|
49 | |
---|
50 | layer = FunctionalLayer |
---|
51 | |
---|
52 | def setup_customizable_params(self): |
---|
53 | self._contract_category = u'license' |
---|
54 | return |
---|
55 | |
---|
56 | def setup_for_export(self): |
---|
57 | customer = createObject(u'waeup.Customer') |
---|
58 | customer.firstname = u'Beate' |
---|
59 | customer.lastname = u'Mueller' |
---|
60 | customer.reg_number = u'123' |
---|
61 | customer.sex = u'f' |
---|
62 | IWorkflowState(customer).setState('started') |
---|
63 | self.app['customers'].addCustomer(customer) |
---|
64 | document1 = createObject(u'waeup.PCNCustomerPDFDocument') |
---|
65 | document1.title = u'My first document' |
---|
66 | document2 = createObject(u'waeup.PCNCustomerJPGDocument') |
---|
67 | document2.title = u'My second document' |
---|
68 | customer['documents'].addDocument(document1) |
---|
69 | customer['documents'].addDocument(document2) |
---|
70 | contract = createObject(u'waeup.PCNLicenseContract') |
---|
71 | contract.tc_dict = {'en':u'Hello World'} |
---|
72 | customer['contracts'].addContract(contract) |
---|
73 | self.customer = customer |
---|
74 | self.document1 = document1 |
---|
75 | self.document2 = document2 |
---|
76 | self.contract = contract |
---|
77 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
78 | return |
---|
79 | |
---|
80 | def test_export_reimport_customers(self): |
---|
81 | # we can export all customers in a portal |
---|
82 | # set values we can expect in export file |
---|
83 | self.setup_for_export() |
---|
84 | exporter = PCNCustomerExporter() |
---|
85 | exporter.export_all(self.app, self.outfile) |
---|
86 | result = open(self.outfile, 'rb').read() |
---|
87 | self.assertEqual(result, |
---|
88 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
89 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
90 | 'K1000000,,Beate,Mueller,,,123,f,0,,,started,[]\r\n') |
---|
91 | # We can reimport the file ... |
---|
92 | processor = PCNCustomerProcessor() |
---|
93 | result = processor.doImport( |
---|
94 | self.outfile, |
---|
95 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
96 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
97 | mode='create') |
---|
98 | num, num_fail, finished_path, failed_path = result |
---|
99 | self.assertEqual(num_fail,1) |
---|
100 | # ... if we remove the original customer. |
---|
101 | del self.app['customers']['K1000000'] |
---|
102 | result = processor.doImport( |
---|
103 | self.outfile, |
---|
104 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
105 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
106 | mode='create') |
---|
107 | num_succ, num_fail, finished_path, failed_path = result |
---|
108 | self.assertEqual(num_fail,0) |
---|
109 | # We can import the same file in update mode if we ignore the reg_number. |
---|
110 | result = processor.doImport( |
---|
111 | self.outfile, |
---|
112 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
113 | 'xx_reg_number','sex','suspended','suspended_comment','password','state'], |
---|
114 | mode='update') |
---|
115 | num_succ, num_fail, finished_path, failed_path = result |
---|
116 | self.assertEqual(num_succ,1) |
---|
117 | self.assertEqual(num_fail,0) |
---|
118 | return |
---|
119 | |
---|
120 | def test_export_reimport_pdf_documents(self): |
---|
121 | # we can export all documents in a portal |
---|
122 | # set values we can expect in export file |
---|
123 | self.setup_for_export() |
---|
124 | exporter = PCNCustomerPDFDocumentExporter() |
---|
125 | exporter.export_all(self.app, self.outfile) |
---|
126 | result = open(self.outfile, 'rb').read() |
---|
127 | self.assertMatches(result, |
---|
128 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
129 | 'PCNCustomerPDFDocument,%s,' |
---|
130 | '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' |
---|
131 | 'created,My first document,K1000000\r\n' |
---|
132 | % self.document1.document_id) |
---|
133 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
134 | processor = PCNCustomerPDFDocumentProcessor() |
---|
135 | open(self.outfile, 'wb').write( |
---|
136 | 'customer_id,class_name,document_id,state,title\r\n' |
---|
137 | 'K1000000,PCNCustomerPDFDocument,%s,started,My first title\r\n' |
---|
138 | % self.document1.document_id) |
---|
139 | result = processor.doImport( |
---|
140 | self.outfile, |
---|
141 | ['customer_id','class_name','document_id','state','title'], |
---|
142 | mode='create') |
---|
143 | num, num_fail, finished_path, failed_path = result |
---|
144 | # The object exists. |
---|
145 | self.assertEqual(num_fail,1) |
---|
146 | # We remove the original document. |
---|
147 | del self.customer['documents'][self.document1.document_id] |
---|
148 | result = processor.doImport( |
---|
149 | self.outfile, |
---|
150 | ['customer_id','class_name','document_id','state','title'], |
---|
151 | mode='create') |
---|
152 | num_succ, num_fail, finished_path, failed_path = result |
---|
153 | self.assertEqual(num_fail,0) |
---|
154 | # We can import the same file in update mode. |
---|
155 | result = processor.doImport( |
---|
156 | self.outfile, |
---|
157 | ['customer_id','class_name','document_id','state','title'], |
---|
158 | mode='update') |
---|
159 | num_succ, num_fail, finished_path, failed_path = result |
---|
160 | self.assertEqual(num_succ,1) |
---|
161 | self.assertEqual(num_fail,0) |
---|
162 | return |
---|
163 | |
---|
164 | def test_export_reimport_jpg_documents(self): |
---|
165 | # we can export all documents in a portal |
---|
166 | # set values we can expect in export file |
---|
167 | self.setup_for_export() |
---|
168 | exporter = PCNCustomerJPGDocumentExporter() |
---|
169 | exporter.export_all(self.app, self.outfile) |
---|
170 | result = open(self.outfile, 'rb').read() |
---|
171 | self.assertMatches(result, |
---|
172 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
173 | 'PCNCustomerJPGDocument,%s,' |
---|
174 | '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' |
---|
175 | 'created,My second document,K1000000\r\n' |
---|
176 | % self.document2.document_id) |
---|
177 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
178 | processor = PCNCustomerJPGDocumentProcessor() |
---|
179 | open(self.outfile, 'wb').write( |
---|
180 | 'customer_id,class_name,document_id,state,title\r\n' |
---|
181 | 'K1000000,PCNCustomerJPGDocument,%s,started,My second title\r\n' |
---|
182 | % self.document2.document_id) |
---|
183 | result = processor.doImport( |
---|
184 | self.outfile, |
---|
185 | ['customer_id','class_name','document_id','state','title'], |
---|
186 | mode='create') |
---|
187 | num, num_fail, finished_path, failed_path = result |
---|
188 | # The object exists. |
---|
189 | self.assertEqual(num_fail,1) |
---|
190 | # We remove the original document. |
---|
191 | del self.customer['documents'][self.document2.document_id] |
---|
192 | result = processor.doImport( |
---|
193 | self.outfile, |
---|
194 | ['customer_id','class_name','document_id','state','title'], |
---|
195 | mode='create') |
---|
196 | num_succ, num_fail, finished_path, failed_path = result |
---|
197 | self.assertEqual(num_fail,0) |
---|
198 | # We can import the same file in update mode. |
---|
199 | result = processor.doImport( |
---|
200 | self.outfile, |
---|
201 | ['customer_id','class_name','document_id','state','title'], |
---|
202 | mode='update') |
---|
203 | num_succ, num_fail, finished_path, failed_path = result |
---|
204 | self.assertEqual(num_succ,1) |
---|
205 | self.assertEqual(num_fail,0) |
---|
206 | return |
---|
207 | |
---|
208 | def test_export_reimport_contracts(self): |
---|
209 | # we can export all contracts in a portal |
---|
210 | # set values we can expect in export file |
---|
211 | self.setup_for_export() |
---|
212 | exporter = PCNLicenseContractExporter() |
---|
213 | exporter.export_all(self.app, self.outfile) |
---|
214 | result = open(self.outfile, 'rb').read() |
---|
215 | self.assertMatches(result, |
---|
216 | 'class_name,contract_category,contract_id,document_object,' |
---|
217 | 'history,last_product_id,product_object,product_options,' |
---|
218 | 'state,tc_dict,title,user_id\r\n' |
---|
219 | 'PCNLicenseContract,license,%s,,' |
---|
220 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
221 | ',,,[],created,{\'en\': u\'Hello World\'},,K1000000\r\n' |
---|
222 | % self.contract.contract_id) |
---|
223 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
224 | processor = PCNLicenseContractProcessor() |
---|
225 | open(self.outfile, 'wb').write( |
---|
226 | 'class_name,contract_category,contract_id,document_object,' |
---|
227 | 'history,last_product_id,product_object,product_options,' |
---|
228 | 'state,tc_dict,title,user_id\r\n' |
---|
229 | 'PCNLicenseContract,license,%s,,' |
---|
230 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
231 | ',,,[],created,{\'en\': u\'Hello World\'},,K1000000\r\n' |
---|
232 | % self.contract.contract_id) |
---|
233 | result = processor.doImport( |
---|
234 | self.outfile, |
---|
235 | ['class_name','contract_category','contract_id','document_object', |
---|
236 | 'history','last_product_id','product_object','product_options', |
---|
237 | 'state','tc_dict','title','customer_id'], |
---|
238 | mode='create') |
---|
239 | num, num_fail, finished_path, failed_path = result |
---|
240 | # The object exists. |
---|
241 | self.assertEqual(num_fail,1) |
---|
242 | # We remove the original contract. |
---|
243 | del self.customer['contracts'][self.contract.contract_id] |
---|
244 | result = processor.doImport( |
---|
245 | self.outfile, |
---|
246 | ['class_name','contract_category','contract_id','document_object', |
---|
247 | 'history','last_product_id','product_object','product_options', |
---|
248 | 'state','tc_dict','title','customer_id'], |
---|
249 | mode='create') |
---|
250 | num_succ, num_fail, finished_path, failed_path = result |
---|
251 | self.assertEqual(num_fail,0) |
---|
252 | # We can import the same file in update mode. |
---|
253 | result = processor.doImport( |
---|
254 | self.outfile, |
---|
255 | ['class_name','contract_category','contract_id','document_object', |
---|
256 | 'history','last_product_id','product_object','product_options', |
---|
257 | 'state','tc_dict','title','customer_id'], |
---|
258 | mode='update') |
---|
259 | num_succ, num_fail, finished_path, failed_path = result |
---|
260 | self.assertEqual(num_succ,1) |
---|
261 | self.assertEqual(num_fail,0) |
---|
262 | return |
---|
263 | |
---|
264 | class DocumentUITests(CustomersFullSetup): |
---|
265 | # Tests for customer document related views and pages |
---|
266 | |
---|
267 | layer = FunctionalLayer |
---|
268 | |
---|
269 | def setup_customizable_params(self): |
---|
270 | self._contract_category = u'license' |
---|
271 | self._document_factory = 'waeup.PCNCustomerPDFDocument' |
---|
272 | self._contract_factory = 'waeup.PCNLicenseContract' |
---|
273 | return |
---|
274 | |
---|
275 | def test_manage_upload_pdf_file(self): |
---|
276 | # Managers can upload a file via the DocumentManageFormPage |
---|
277 | # The image is stored even if form has errors |
---|
278 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
279 | self.browser.open(self.customer_path + '/documents') |
---|
280 | self.browser.getControl("Add document").click() |
---|
281 | self.browser.getControl(name="doctype").value = ['PCNCustomerPDFDocument'] |
---|
282 | self.browser.getControl(name="form.title").value = 'My PCN Document' |
---|
283 | self.browser.getControl("Add document").click() |
---|
284 | docid = [i for i in self.customer['documents'].keys() if len(i) > 10][0] |
---|
285 | self.browser.open(self.documents_path + '/%s/manage' % docid) |
---|
286 | # Create a pseudo image file and select it to be uploaded |
---|
287 | image = open(SAMPLE_IMAGE, 'rb') |
---|
288 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
289 | file_ctrl = ctrl.mech_control |
---|
290 | file_ctrl.add_file(image, filename='my_sample_scan.jpg') |
---|
291 | self.browser.getControl( |
---|
292 | name='upload_pdfscanmanageupload').click() |
---|
293 | self.assertTrue( |
---|
294 | 'pdf file format expected' in self.browser.contents) |
---|
295 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
296 | file_ctrl = ctrl.mech_control |
---|
297 | file_ctrl.add_file(image, filename='my_sample_scan.pdf') |
---|
298 | self.browser.getControl( |
---|
299 | name='upload_pdfscanmanageupload').click() |
---|
300 | self.assertTrue( |
---|
301 | 'Could not determine file type' in self.browser.contents) |
---|
302 | pdf = open(SAMPLE_PDF, 'rb') |
---|
303 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
304 | file_ctrl = ctrl.mech_control |
---|
305 | file_ctrl.add_file(pdf, filename='my_sample_scan.pdf') |
---|
306 | self.browser.getControl( |
---|
307 | name='upload_pdfscanmanageupload').click() |
---|
308 | self.assertTrue( |
---|
309 | 'href="http://localhost/app/customers/K1000000/documents/%s/scan.pdf">PDF File</a>' |
---|
310 | % docid in self.browser.contents) |
---|
311 | # Browsing the link shows a real pdf |
---|
312 | self.browser.open('scan.pdf') |
---|
313 | self.assertEqual( |
---|
314 | self.browser.headers['content-type'], 'application/pdf') |
---|
315 | |
---|
316 | class ContractUITests(CustomersFullSetup): |
---|
317 | # Tests for contract related views and pages |
---|
318 | |
---|
319 | layer = FunctionalLayer |
---|
320 | |
---|
321 | def setup_customizable_params(self): |
---|
322 | self._contract_category = u'license' |
---|
323 | self._document_factory = 'waeup.PCNCustomerPDFDocument' |
---|
324 | self._contract_factory = 'waeup.PCNLicenseContract' |
---|
325 | return |
---|
326 | |
---|
327 | def test_view_slips(self): |
---|
328 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
329 | # Officers can open contract slips. |
---|
330 | # First we add a submitted document and a product. |
---|
331 | IWorkflowState(self.document).setState('submitted') |
---|
332 | self.contract.document_object = self.document |
---|
333 | self.contract.product_object = self.product |
---|
334 | self.contract.tc_dict = {'en': u'<strong>Hello world</strong>'} |
---|
335 | self.browser.open(self.customer_path + '/contracts/CON1') |
---|
336 | self.browser.getLink("Download contract slip").click() |
---|
337 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
338 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
339 | path = os.path.join(samples_dir(), 'contract_slip.pdf') |
---|
340 | open(path, 'wb').write(self.browser.contents) |
---|
341 | print "Sample contract_slip.pdf written to %s" % path |
---|