[12286] | 1 | ## $Id: test_browser.py 14313 2016-12-06 07:46:48Z henrik $ |
---|
[12285] | 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 |
---|
[12291] | 28 | from zope.testbrowser.testing import Browser |
---|
| 29 | from hurry.workflow.interfaces import IWorkflowState |
---|
[12285] | 30 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
[12355] | 31 | from waeup.ikoba.customers.tests.test_browser import CustomersFullSetup |
---|
[14310] | 32 | from ikobacustom.aaue.customers.export import ( |
---|
| 33 | AAUECustomerExporter, |
---|
| 34 | AAUECustomerDocumentExporter, |
---|
| 35 | AAUEContractExporter) |
---|
| 36 | from ikobacustom.aaue.customers.batching import ( |
---|
| 37 | AAUECustomerProcessor, |
---|
| 38 | AAUECustomerDocumentProcessor, |
---|
| 39 | AAUEContractProcessor) |
---|
| 40 | from ikobacustom.aaue.testing import FunctionalLayer, samples_dir |
---|
[12285] | 41 | |
---|
[12355] | 42 | SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
| 43 | SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp') |
---|
| 44 | SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf') |
---|
[12285] | 45 | |
---|
| 46 | class CustomerImportExportTest(CustomerImportExportSetup): |
---|
| 47 | |
---|
| 48 | layer = FunctionalLayer |
---|
| 49 | |
---|
[12389] | 50 | def setup_customizable_params(self): |
---|
| 51 | self._contract_category = u'sample' |
---|
| 52 | return |
---|
| 53 | |
---|
[12285] | 54 | def setup_for_export(self): |
---|
| 55 | customer = createObject(u'waeup.Customer') |
---|
[12291] | 56 | customer.firstname = u'Beate' |
---|
| 57 | customer.lastname = u'Mueller' |
---|
| 58 | customer.reg_number = u'123' |
---|
| 59 | customer.sex = u'f' |
---|
[12539] | 60 | customer.email = u'aa@aa.aa' |
---|
[12291] | 61 | IWorkflowState(customer).setState('started') |
---|
[12285] | 62 | self.app['customers'].addCustomer(customer) |
---|
[14310] | 63 | document = createObject(u'waeup.AAUECustomerDocument') |
---|
[12285] | 64 | document.title = u'My first document' |
---|
| 65 | customer['documents'].addDocument(document) |
---|
[14310] | 66 | contract = createObject(u'waeup.AAUEContract') |
---|
[12367] | 67 | contract.tc_dict = {'en':u'Hello World'} |
---|
[12291] | 68 | customer['contracts'].addContract(contract) |
---|
[12285] | 69 | self.customer = customer |
---|
| 70 | self.document = document |
---|
[12291] | 71 | self.contract = contract |
---|
[12285] | 72 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
| 73 | return |
---|
| 74 | |
---|
[12291] | 75 | def test_export_reimport_customers(self): |
---|
| 76 | # we can export all customers in a portal |
---|
| 77 | # set values we can expect in export file |
---|
| 78 | self.setup_for_export() |
---|
[14310] | 79 | exporter = AAUECustomerExporter() |
---|
[12291] | 80 | exporter.export_all(self.app, self.outfile) |
---|
| 81 | result = open(self.outfile, 'rb').read() |
---|
| 82 | self.assertEqual(result, |
---|
[14313] | 83 | 'customer_id,email,firstname,inst,lastname,middlename,phone,postal_address,' |
---|
[12291] | 84 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
[14313] | 85 | 'K1000000,aa@aa.aa,Beate,,Mueller,,,,123,f,0,,,started,[]\r\n') |
---|
[12291] | 86 | # We can reimport the file ... |
---|
[14310] | 87 | processor = AAUECustomerProcessor() |
---|
[12291] | 88 | result = processor.doImport( |
---|
| 89 | self.outfile, |
---|
| 90 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
| 91 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
| 92 | mode='create') |
---|
| 93 | num, num_fail, finished_path, failed_path = result |
---|
| 94 | self.assertEqual(num_fail,1) |
---|
| 95 | # ... if we remove the original customer. |
---|
| 96 | del self.app['customers']['K1000000'] |
---|
| 97 | result = processor.doImport( |
---|
| 98 | self.outfile, |
---|
| 99 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
| 100 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
| 101 | mode='create') |
---|
| 102 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 103 | self.assertEqual(num_fail,0) |
---|
[12539] | 104 | # We can import the same file in update mode if we ignore |
---|
| 105 | # the reg_number and email address |
---|
[12291] | 106 | result = processor.doImport( |
---|
| 107 | self.outfile, |
---|
[12539] | 108 | ['customer_id','xx_email','firstname','lastname','middlename','phone', |
---|
[12291] | 109 | 'xx_reg_number','sex','suspended','suspended_comment','password','state'], |
---|
| 110 | mode='update') |
---|
| 111 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 112 | self.assertEqual(num_succ,1) |
---|
| 113 | self.assertEqual(num_fail,0) |
---|
| 114 | return |
---|
| 115 | |
---|
| 116 | def test_export_reimport_documents(self): |
---|
[12285] | 117 | # we can export all documents in a portal |
---|
| 118 | # set values we can expect in export file |
---|
| 119 | self.setup_for_export() |
---|
[14310] | 120 | exporter = AAUECustomerDocumentExporter() |
---|
[12285] | 121 | exporter.export_all(self.app, self.outfile) |
---|
| 122 | result = open(self.outfile, 'rb').read() |
---|
| 123 | self.assertMatches(result, |
---|
| 124 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
[14310] | 125 | 'AAUECustomerDocument,%s,' |
---|
[12285] | 126 | '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' |
---|
| 127 | 'created,My first document,K1000000\r\n' |
---|
| 128 | % self.document.document_id) |
---|
[12291] | 129 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
[14310] | 130 | processor = AAUECustomerDocumentProcessor() |
---|
[12285] | 131 | open(self.outfile, 'wb').write( |
---|
| 132 | 'customer_id,class_name,document_id,state,title\r\n' |
---|
[14310] | 133 | 'K1000000,AAUECustomerDocument,%s,started,My first title\r\n' |
---|
[12285] | 134 | % self.document.document_id) |
---|
| 135 | result = processor.doImport( |
---|
| 136 | self.outfile, |
---|
| 137 | ['customer_id','class_name','document_id','state','title'], |
---|
| 138 | mode='create') |
---|
| 139 | num, num_fail, finished_path, failed_path = result |
---|
| 140 | # The object exists. |
---|
| 141 | self.assertEqual(num_fail,1) |
---|
| 142 | # We remove the original document. |
---|
| 143 | del self.customer['documents'][self.document.document_id] |
---|
| 144 | result = processor.doImport( |
---|
| 145 | self.outfile, |
---|
| 146 | ['customer_id','class_name','document_id','state','title'], |
---|
| 147 | mode='create') |
---|
| 148 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 149 | self.assertEqual(num_fail,0) |
---|
| 150 | # We can import the same file in update mode. |
---|
| 151 | result = processor.doImport( |
---|
| 152 | self.outfile, |
---|
| 153 | ['customer_id','class_name','document_id','state','title'], |
---|
| 154 | mode='update') |
---|
| 155 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 156 | self.assertEqual(num_succ,1) |
---|
| 157 | self.assertEqual(num_fail,0) |
---|
[12291] | 158 | return |
---|
| 159 | |
---|
| 160 | def test_export_reimport_contracts(self): |
---|
| 161 | # we can export all contracts in a portal |
---|
| 162 | # set values we can expect in export file |
---|
| 163 | self.setup_for_export() |
---|
[14310] | 164 | exporter = AAUEContractExporter() |
---|
[12291] | 165 | exporter.export_all(self.app, self.outfile) |
---|
| 166 | result = open(self.outfile, 'rb').read() |
---|
| 167 | self.assertMatches(result, |
---|
[12502] | 168 | 'class_name,comment,contract_category,contract_id,document_object,' |
---|
[12665] | 169 | 'fee_based,history,last_product_id,product_object,product_options,' |
---|
| 170 | 'state,tc_dict,title,user_id,valid_from,valid_to\r\n' |
---|
[14310] | 171 | 'AAUEContract,,sample,%s,,0,' |
---|
[12291] | 172 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
[12665] | 173 | ',,,[],created,{\'en\': u\'Hello World\'},,K1000000,,\r\n' |
---|
[12291] | 174 | % self.contract.contract_id) |
---|
| 175 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
[14310] | 176 | processor = AAUEContractProcessor() |
---|
[12291] | 177 | open(self.outfile, 'wb').write( |
---|
| 178 | 'class_name,contract_category,contract_id,document_object,' |
---|
[12335] | 179 | 'history,last_product_id,product_object,product_options,' |
---|
[12367] | 180 | 'state,tc_dict,title,user_id\r\n' |
---|
[14310] | 181 | 'AAUEContract,sample,%s,,' |
---|
[12291] | 182 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
[12367] | 183 | ',,,[],created,{\'en\': u\'Hello World\'},,K1000000\r\n' |
---|
[12291] | 184 | % self.contract.contract_id) |
---|
| 185 | result = processor.doImport( |
---|
| 186 | self.outfile, |
---|
| 187 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 188 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 189 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 190 | mode='create') |
---|
| 191 | num, num_fail, finished_path, failed_path = result |
---|
| 192 | # The object exists. |
---|
| 193 | self.assertEqual(num_fail,1) |
---|
| 194 | # We remove the original contract. |
---|
| 195 | del self.customer['contracts'][self.contract.contract_id] |
---|
| 196 | result = processor.doImport( |
---|
| 197 | self.outfile, |
---|
| 198 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 199 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 200 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 201 | mode='create') |
---|
| 202 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 203 | self.assertEqual(num_fail,0) |
---|
| 204 | # We can import the same file in update mode. |
---|
| 205 | result = processor.doImport( |
---|
| 206 | self.outfile, |
---|
| 207 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 208 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 209 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 210 | mode='update') |
---|
| 211 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 212 | self.assertEqual(num_succ,1) |
---|
| 213 | self.assertEqual(num_fail,0) |
---|
| 214 | return |
---|
[12355] | 215 | |
---|
| 216 | class DocumentUITests(CustomersFullSetup): |
---|
| 217 | # Tests for customer document related views and pages |
---|
| 218 | |
---|
| 219 | layer = FunctionalLayer |
---|
| 220 | |
---|
[12389] | 221 | def setup_customizable_params(self): |
---|
| 222 | self._contract_category = u'sample' |
---|
[14310] | 223 | self._document_factory = 'waeup.AAUECustomerDocument' |
---|
| 224 | self._contract_factory = 'waeup.AAUEContract' |
---|
[12389] | 225 | return |
---|
| 226 | |
---|
[12382] | 227 | def test_manage_upload_sample_file(self): |
---|
[12355] | 228 | # Managers can upload a file via the DocumentManageFormPage |
---|
| 229 | # The image is stored even if form has errors |
---|
| 230 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
[12382] | 231 | self.browser.open(self.customer_path + '/documents/DOC1/manage') |
---|
[12355] | 232 | # Create a pseudo image file and select it to be uploaded |
---|
| 233 | image = open(SAMPLE_IMAGE, 'rb') |
---|
[12382] | 234 | ctrl = self.browser.getControl(name='samplescanmanageupload') |
---|
[12355] | 235 | file_ctrl = ctrl.mech_control |
---|
| 236 | file_ctrl.add_file(image, filename='my_sample_scan.jpg') |
---|
[12382] | 237 | # The Save action does not upload files |
---|
| 238 | self.browser.getControl("Save").click() # submit form |
---|
| 239 | self.assertFalse( |
---|
| 240 | 'href="http://localhost/app/customers/K1000000/documents/DOC1/sample"' |
---|
| 241 | in self.browser.contents) |
---|
| 242 | # ... but the correct upload submit button does |
---|
| 243 | image = open(SAMPLE_IMAGE) |
---|
| 244 | ctrl = self.browser.getControl(name='samplescanmanageupload') |
---|
| 245 | file_ctrl = ctrl.mech_control |
---|
| 246 | file_ctrl.add_file(image, filename='my_sample_scan.jpg') |
---|
[12355] | 247 | self.browser.getControl( |
---|
[12382] | 248 | name='upload_samplescanmanageupload').click() |
---|
[12355] | 249 | self.assertTrue( |
---|
[12382] | 250 | 'href="http://localhost/app/customers/K1000000/documents/DOC1/sample"' |
---|
| 251 | in self.browser.contents) |
---|
| 252 | # Browsing the link shows a real image |
---|
| 253 | self.browser.open('sample') |
---|
| 254 | self.assertEqual( |
---|
| 255 | self.browser.headers['content-type'], 'image/jpeg') |
---|
| 256 | self.assertEqual(len(self.browser.contents), 2787) |
---|
| 257 | # We can't reupload a file. The existing file must be deleted first. |
---|
| 258 | self.browser.open(self.customer_path + '/documents/DOC1/manage') |
---|
| 259 | self.assertFalse( |
---|
| 260 | 'upload_samplescanmanageupload' in self.browser.contents) |
---|
| 261 | # File must be deleted first |
---|
| 262 | self.browser.getControl(name='delete_samplescanmanageupload').click() |
---|
| 263 | self.assertTrue( |
---|
| 264 | 'sample deleted' in self.browser.contents) |
---|
| 265 | # Uploading a file which is bigger than 150k will raise an error |
---|
| 266 | big_image = StringIO(open(SAMPLE_IMAGE, 'rb').read() * 75) |
---|
| 267 | ctrl = self.browser.getControl(name='samplescanmanageupload') |
---|
[12355] | 268 | file_ctrl = ctrl.mech_control |
---|
[12382] | 269 | file_ctrl.add_file(big_image, filename='my_sample_scan.jpg') |
---|
[12355] | 270 | self.browser.getControl( |
---|
[12382] | 271 | name='upload_samplescanmanageupload').click() |
---|
[12355] | 272 | self.assertTrue( |
---|
[12382] | 273 | 'Uploaded file is too big' in self.browser.contents) |
---|
| 274 | # We do not rely on filename extensions given by uploaders |
---|
| 275 | image = open(SAMPLE_IMAGE, 'rb') # a jpg-file |
---|
| 276 | ctrl = self.browser.getControl(name='samplescanmanageupload') |
---|
[12355] | 277 | file_ctrl = ctrl.mech_control |
---|
[12382] | 278 | # Tell uploaded file is bmp |
---|
| 279 | file_ctrl.add_file(image, filename='my_sample_scan.bmp') |
---|
[12355] | 280 | self.browser.getControl( |
---|
[12382] | 281 | name='upload_samplescanmanageupload').click() |
---|
[12355] | 282 | self.assertTrue( |
---|
[12382] | 283 | # jpg file was recognized |
---|
| 284 | 'File sample.jpg uploaded.' in self.browser.contents) |
---|
| 285 | # Delete file again |
---|
| 286 | self.browser.getControl(name='delete_samplescanmanageupload').click() |
---|
| 287 | self.assertTrue( |
---|
| 288 | 'sample deleted' in self.browser.contents) |
---|
| 289 | # File names must meet several conditions |
---|
| 290 | bmp_image = open(SAMPLE_IMAGE_BMP, 'rb') |
---|
| 291 | ctrl = self.browser.getControl(name='samplescanmanageupload') |
---|
| 292 | file_ctrl = ctrl.mech_control |
---|
| 293 | file_ctrl.add_file(bmp_image, filename='my_sample_scan.bmp') |
---|
| 294 | self.browser.getControl( |
---|
| 295 | name='upload_samplescanmanageupload').click() |
---|
| 296 | self.assertTrue('Only the following extensions are allowed' |
---|
| 297 | in self.browser.contents) |
---|
[12369] | 298 | |
---|
| 299 | class ContractUITests(CustomersFullSetup): |
---|
| 300 | # Tests for contract related views and pages |
---|
| 301 | |
---|
| 302 | layer = FunctionalLayer |
---|
| 303 | |
---|
[12389] | 304 | def setup_customizable_params(self): |
---|
| 305 | self._contract_category = u'sample' |
---|
[14310] | 306 | self._document_factory = 'waeup.AAUECustomerDocument' |
---|
| 307 | self._contract_factory = 'waeup.AAUEContract' |
---|
[12389] | 308 | return |
---|
| 309 | |
---|
[12369] | 310 | def test_view_slips(self): |
---|
| 311 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 312 | # Officers can open contract slips. |
---|
| 313 | # First we add a submitted document and a product. |
---|
| 314 | IWorkflowState(self.document).setState('submitted') |
---|
| 315 | self.contract.document_object = self.document |
---|
| 316 | self.contract.product_object = self.product |
---|
| 317 | self.contract.tc_dict = {'en': u'<strong>Hello world</strong>'} |
---|
[12608] | 318 | self.contract.title = u'Contract Title' |
---|
[14313] | 319 | self.browser.open( |
---|
| 320 | self.customer_path + '/contracts/%s' % self.contract.contract_id) |
---|
[12369] | 321 | self.browser.getLink("Download contract slip").click() |
---|
| 322 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
| 323 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
| 324 | path = os.path.join(samples_dir(), 'contract_slip.pdf') |
---|
| 325 | open(path, 'wb').write(self.browser.contents) |
---|
| 326 | print "Sample contract_slip.pdf written to %s" % path |
---|