[12286] | 1 | ## $Id: test_browser.py 14219 2016-10-20 12:48:21Z 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 |
---|
[14178] | 32 | from ikobacustom.uniben.customers.export import ( |
---|
[14181] | 33 | UnibenCustomerExporter, |
---|
[14188] | 34 | UnibenCustomerPDFDocumentExporter, |
---|
[14184] | 35 | RIAAContractExporter) |
---|
[14178] | 36 | from ikobacustom.uniben.customers.batching import ( |
---|
[14181] | 37 | UnibenCustomerProcessor, |
---|
[14188] | 38 | UnibenCustomerPDFDocumentProcessor, |
---|
[14184] | 39 | RIAAContractProcessor) |
---|
[14178] | 40 | from ikobacustom.uniben.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): |
---|
[14184] | 51 | self._contract_category = u'riaa' |
---|
[12389] | 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) |
---|
[14188] | 63 | document = createObject(u'waeup.UnibenCustomerPDFDocument') |
---|
[12285] | 64 | document.title = u'My first document' |
---|
| 65 | customer['documents'].addDocument(document) |
---|
[14184] | 66 | contract = createObject(u'waeup.RIAAContract') |
---|
[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() |
---|
[14181] | 79 | exporter = UnibenCustomerExporter() |
---|
[12291] | 80 | exporter.export_all(self.app, self.outfile) |
---|
| 81 | result = open(self.outfile, 'rb').read() |
---|
| 82 | self.assertEqual(result, |
---|
| 83 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
| 84 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
[14188] | 85 | 'B1000000,aa@aa.aa,Beate,Mueller,,,123,f,0,,,started,[]\r\n') |
---|
[12291] | 86 | # We can reimport the file ... |
---|
[14181] | 87 | processor = UnibenCustomerProcessor() |
---|
[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. |
---|
[14188] | 96 | del self.app['customers']['B1000000'] |
---|
[12291] | 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() |
---|
[14188] | 120 | exporter = UnibenCustomerPDFDocumentExporter() |
---|
[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' |
---|
[14188] | 125 | 'UnibenCustomerPDFDocument,%s,' |
---|
[12285] | 126 | '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' |
---|
[14188] | 127 | 'created,My first document,B1000000\r\n' |
---|
[12285] | 128 | % self.document.document_id) |
---|
[12291] | 129 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
[14188] | 130 | processor = UnibenCustomerPDFDocumentProcessor() |
---|
[12285] | 131 | open(self.outfile, 'wb').write( |
---|
| 132 | 'customer_id,class_name,document_id,state,title\r\n' |
---|
[14188] | 133 | 'B1000000,UnibenCustomerPDFDocument,%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 | |
---|
[14190] | 160 | def disabled_test_export_reimport_contracts(self): |
---|
[12291] | 161 | # we can export all contracts in a portal |
---|
| 162 | # set values we can expect in export file |
---|
| 163 | self.setup_for_export() |
---|
[14184] | 164 | exporter = RIAAContractExporter() |
---|
[12291] | 165 | exporter.export_all(self.app, self.outfile) |
---|
| 166 | result = open(self.outfile, 'rb').read() |
---|
| 167 | self.assertMatches(result, |
---|
[14188] | 168 | 'class_name,comment,contract_category,contract_id,' |
---|
| 169 | 'doc1,doc10,doc11,doc2,doc3,doc4,doc5,doc6,doc7,doc8,doc9,' |
---|
[12665] | 170 | 'fee_based,history,last_product_id,product_object,product_options,' |
---|
| 171 | 'state,tc_dict,title,user_id,valid_from,valid_to\r\n' |
---|
[14188] | 172 | 'RIAAContract,,riaa,%s,,,,,,,,,,,,0,' |
---|
[12291] | 173 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
[14188] | 174 | ',,,[],created,{\'en\': u\'Hello World\'},,B1000000,,\r\n' |
---|
[12291] | 175 | % self.contract.contract_id) |
---|
| 176 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
[14184] | 177 | processor = RIAAContractProcessor() |
---|
[12291] | 178 | open(self.outfile, 'wb').write( |
---|
| 179 | 'class_name,contract_category,contract_id,document_object,' |
---|
[12335] | 180 | 'history,last_product_id,product_object,product_options,' |
---|
[12367] | 181 | 'state,tc_dict,title,user_id\r\n' |
---|
[14184] | 182 | 'RIAAContract,riaa,%s,,' |
---|
[12291] | 183 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
[14188] | 184 | ',,,[],created,{\'en\': u\'Hello World\'},,B1000000\r\n' |
---|
[12291] | 185 | % self.contract.contract_id) |
---|
| 186 | result = processor.doImport( |
---|
| 187 | self.outfile, |
---|
| 188 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 189 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 190 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 191 | mode='create') |
---|
| 192 | num, num_fail, finished_path, failed_path = result |
---|
| 193 | # The object exists. |
---|
| 194 | self.assertEqual(num_fail,1) |
---|
| 195 | # We remove the original contract. |
---|
| 196 | del self.customer['contracts'][self.contract.contract_id] |
---|
| 197 | result = processor.doImport( |
---|
| 198 | self.outfile, |
---|
| 199 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 200 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 201 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 202 | mode='create') |
---|
| 203 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 204 | self.assertEqual(num_fail,0) |
---|
| 205 | # We can import the same file in update mode. |
---|
| 206 | result = processor.doImport( |
---|
| 207 | self.outfile, |
---|
| 208 | ['class_name','contract_category','contract_id','document_object', |
---|
[12335] | 209 | 'history','last_product_id','product_object','product_options', |
---|
[12367] | 210 | 'state','tc_dict','title','customer_id'], |
---|
[12291] | 211 | mode='update') |
---|
| 212 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 213 | self.assertEqual(num_succ,1) |
---|
| 214 | self.assertEqual(num_fail,0) |
---|
| 215 | return |
---|
[12355] | 216 | |
---|
| 217 | class DocumentUITests(CustomersFullSetup): |
---|
| 218 | # Tests for customer document related views and pages |
---|
| 219 | |
---|
| 220 | layer = FunctionalLayer |
---|
| 221 | |
---|
[12389] | 222 | def setup_customizable_params(self): |
---|
[14184] | 223 | self._contract_category = u'riaa' |
---|
[14197] | 224 | self._document_factory = 'waeup.UnibenCustomerPDFDocument' |
---|
[14184] | 225 | self._contract_factory = 'waeup.RIAAContract' |
---|
[12389] | 226 | return |
---|
| 227 | |
---|
[14197] | 228 | def test_manage_upload_pdf_file(self): |
---|
[12355] | 229 | # Managers can upload a file via the DocumentManageFormPage |
---|
| 230 | # The image is stored even if form has errors |
---|
| 231 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
[12382] | 232 | self.browser.open(self.customer_path + '/documents/DOC1/manage') |
---|
[12355] | 233 | # Create a pseudo image file and select it to be uploaded |
---|
[14197] | 234 | image = open(SAMPLE_PDF, 'rb') |
---|
| 235 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
[12355] | 236 | file_ctrl = ctrl.mech_control |
---|
[14197] | 237 | file_ctrl.add_file(image, filename='my_sample_scan.pdf') |
---|
[12382] | 238 | # The Save action does not upload files |
---|
| 239 | self.browser.getControl("Save").click() # submit form |
---|
| 240 | self.assertFalse( |
---|
[14197] | 241 | 'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"' |
---|
[12382] | 242 | in self.browser.contents) |
---|
| 243 | # ... but the correct upload submit button does |
---|
[14197] | 244 | image = open(SAMPLE_PDF) |
---|
| 245 | ctrl = self.browser.getControl(name='pdfscanmanageupload') |
---|
[12382] | 246 | file_ctrl = ctrl.mech_control |
---|
[14197] | 247 | file_ctrl.add_file(image, filename='my_sample_scan.pdf') |
---|
[12355] | 248 | self.browser.getControl( |
---|
[14197] | 249 | name='upload_pdfscanmanageupload').click() |
---|
[12355] | 250 | self.assertTrue( |
---|
[14197] | 251 | 'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"' |
---|
[12382] | 252 | in self.browser.contents) |
---|
[14197] | 253 | # Browsing the link shows a real pdf file |
---|
| 254 | self.browser.open('scan.pdf') |
---|
[12382] | 255 | self.assertEqual( |
---|
[14197] | 256 | self.browser.headers['content-type'], 'application/pdf') |
---|
| 257 | self.assertEqual(len(self.browser.contents), 24241) |
---|
[12382] | 258 | # We can't reupload a file. The existing file must be deleted first. |
---|
| 259 | self.browser.open(self.customer_path + '/documents/DOC1/manage') |
---|
| 260 | self.assertFalse( |
---|
[14197] | 261 | 'upload_pdfscanmanageupload' in self.browser.contents) |
---|
[12382] | 262 | # File must be deleted first |
---|
[14197] | 263 | self.browser.getControl(name='delete_pdfscanmanageupload').click() |
---|
[12382] | 264 | self.assertTrue( |
---|
[14197] | 265 | 'scan.pdf deleted' in self.browser.contents) |
---|
[12369] | 266 | |
---|
[14197] | 267 | |
---|
[12369] | 268 | class ContractUITests(CustomersFullSetup): |
---|
| 269 | # Tests for contract related views and pages |
---|
| 270 | |
---|
| 271 | layer = FunctionalLayer |
---|
| 272 | |
---|
[12389] | 273 | def setup_customizable_params(self): |
---|
[14184] | 274 | self._contract_category = u'riaa' |
---|
[14197] | 275 | self._document_factory = 'waeup.UnibenCustomerPDFDocument' |
---|
[14184] | 276 | self._contract_factory = 'waeup.RIAAContract' |
---|
[12389] | 277 | return |
---|
| 278 | |
---|
[12369] | 279 | def test_view_slips(self): |
---|
| 280 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 281 | # Officers can open contract slips. |
---|
| 282 | # First we add a submitted document and a product. |
---|
| 283 | IWorkflowState(self.document).setState('submitted') |
---|
| 284 | self.contract.document_object = self.document |
---|
| 285 | self.contract.product_object = self.product |
---|
| 286 | self.contract.tc_dict = {'en': u'<strong>Hello world</strong>'} |
---|
[12608] | 287 | self.contract.title = u'Contract Title' |
---|
[14219] | 288 | self.browser.open(self.customer_path + '/contracts/11111222223333344444555556666677') |
---|
[12369] | 289 | self.browser.getLink("Download contract slip").click() |
---|
| 290 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
| 291 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
| 292 | path = os.path.join(samples_dir(), 'contract_slip.pdf') |
---|
| 293 | open(path, 'wb').write(self.browser.contents) |
---|
| 294 | print "Sample contract_slip.pdf written to %s" % path |
---|