## $Id: test_browser.py 14219 2016-10-20 12:48:21Z henrik $ ## ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Document browser and functional tests. """ import os import grok import datetime from cStringIO import StringIO from zope.component import queryUtility, getUtility, createObject from zope.event import notify from zope.interface.verify import verifyObject, verifyClass from zope.testbrowser.testing import Browser from hurry.workflow.interfaces import IWorkflowState from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup from waeup.ikoba.customers.tests.test_browser import CustomersFullSetup from ikobacustom.uniben.customers.export import ( UnibenCustomerExporter, UnibenCustomerPDFDocumentExporter, RIAAContractExporter) from ikobacustom.uniben.customers.batching import ( UnibenCustomerProcessor, UnibenCustomerPDFDocumentProcessor, RIAAContractProcessor) from ikobacustom.uniben.testing import FunctionalLayer, samples_dir SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp') SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf') class CustomerImportExportTest(CustomerImportExportSetup): layer = FunctionalLayer def setup_customizable_params(self): self._contract_category = u'riaa' return def setup_for_export(self): customer = createObject(u'waeup.Customer') customer.firstname = u'Beate' customer.lastname = u'Mueller' customer.reg_number = u'123' customer.sex = u'f' customer.email = u'aa@aa.aa' IWorkflowState(customer).setState('started') self.app['customers'].addCustomer(customer) document = createObject(u'waeup.UnibenCustomerPDFDocument') document.title = u'My first document' customer['documents'].addDocument(document) contract = createObject(u'waeup.RIAAContract') contract.tc_dict = {'en':u'Hello World'} customer['contracts'].addContract(contract) self.customer = customer self.document = document self.contract = contract self.outfile = os.path.join(self.workdir, 'myoutput.csv') return def test_export_reimport_customers(self): # we can export all customers in a portal # set values we can expect in export file self.setup_for_export() exporter = UnibenCustomerExporter() exporter.export_all(self.app, self.outfile) result = open(self.outfile, 'rb').read() self.assertEqual(result, 'customer_id,email,firstname,lastname,middlename,phone,' 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' 'B1000000,aa@aa.aa,Beate,Mueller,,,123,f,0,,,started,[]\r\n') # We can reimport the file ... processor = UnibenCustomerProcessor() result = processor.doImport( self.outfile, ['customer_id','email','firstname','lastname','middlename','phone', 'reg_number','sex','suspended','suspended_comment','password','state'], mode='create') num, num_fail, finished_path, failed_path = result self.assertEqual(num_fail,1) # ... if we remove the original customer. del self.app['customers']['B1000000'] result = processor.doImport( self.outfile, ['customer_id','email','firstname','lastname','middlename','phone', 'reg_number','sex','suspended','suspended_comment','password','state'], mode='create') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_fail,0) # We can import the same file in update mode if we ignore # the reg_number and email address result = processor.doImport( self.outfile, ['customer_id','xx_email','firstname','lastname','middlename','phone', 'xx_reg_number','sex','suspended','suspended_comment','password','state'], mode='update') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_succ,1) self.assertEqual(num_fail,0) return def test_export_reimport_documents(self): # we can export all documents in a portal # set values we can expect in export file self.setup_for_export() exporter = UnibenCustomerPDFDocumentExporter() exporter.export_all(self.app, self.outfile) result = open(self.outfile, 'rb').read() self.assertMatches(result, 'class_name,document_id,history,state,title,user_id\r\n' 'UnibenCustomerPDFDocument,%s,' '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' 'created,My first document,B1000000\r\n' % self.document.document_id) # We can reimport the file if we change the header (user_id -> customer_id) processor = UnibenCustomerPDFDocumentProcessor() open(self.outfile, 'wb').write( 'customer_id,class_name,document_id,state,title\r\n' 'B1000000,UnibenCustomerPDFDocument,%s,started,My first title\r\n' % self.document.document_id) result = processor.doImport( self.outfile, ['customer_id','class_name','document_id','state','title'], mode='create') num, num_fail, finished_path, failed_path = result # The object exists. self.assertEqual(num_fail,1) # We remove the original document. del self.customer['documents'][self.document.document_id] result = processor.doImport( self.outfile, ['customer_id','class_name','document_id','state','title'], mode='create') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_fail,0) # We can import the same file in update mode. result = processor.doImport( self.outfile, ['customer_id','class_name','document_id','state','title'], mode='update') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_succ,1) self.assertEqual(num_fail,0) return def disabled_test_export_reimport_contracts(self): # we can export all contracts in a portal # set values we can expect in export file self.setup_for_export() exporter = RIAAContractExporter() exporter.export_all(self.app, self.outfile) result = open(self.outfile, 'rb').read() self.assertMatches(result, 'class_name,comment,contract_category,contract_id,' 'doc1,doc10,doc11,doc2,doc3,doc4,doc5,doc6,doc7,doc8,doc9,' 'fee_based,history,last_product_id,product_object,product_options,' 'state,tc_dict,title,user_id,valid_from,valid_to\r\n' 'RIAAContract,,riaa,%s,,,,,,,,,,,,0,' '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' ',,,[],created,{\'en\': u\'Hello World\'},,B1000000,,\r\n' % self.contract.contract_id) # We can reimport the file if we change the header (user_id -> customer_id) processor = RIAAContractProcessor() open(self.outfile, 'wb').write( 'class_name,contract_category,contract_id,document_object,' 'history,last_product_id,product_object,product_options,' 'state,tc_dict,title,user_id\r\n' 'RIAAContract,riaa,%s,,' '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' ',,,[],created,{\'en\': u\'Hello World\'},,B1000000\r\n' % self.contract.contract_id) result = processor.doImport( self.outfile, ['class_name','contract_category','contract_id','document_object', 'history','last_product_id','product_object','product_options', 'state','tc_dict','title','customer_id'], mode='create') num, num_fail, finished_path, failed_path = result # The object exists. self.assertEqual(num_fail,1) # We remove the original contract. del self.customer['contracts'][self.contract.contract_id] result = processor.doImport( self.outfile, ['class_name','contract_category','contract_id','document_object', 'history','last_product_id','product_object','product_options', 'state','tc_dict','title','customer_id'], mode='create') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_fail,0) # We can import the same file in update mode. result = processor.doImport( self.outfile, ['class_name','contract_category','contract_id','document_object', 'history','last_product_id','product_object','product_options', 'state','tc_dict','title','customer_id'], mode='update') num_succ, num_fail, finished_path, failed_path = result self.assertEqual(num_succ,1) self.assertEqual(num_fail,0) return class DocumentUITests(CustomersFullSetup): # Tests for customer document related views and pages layer = FunctionalLayer def setup_customizable_params(self): self._contract_category = u'riaa' self._document_factory = 'waeup.UnibenCustomerPDFDocument' self._contract_factory = 'waeup.RIAAContract' return def test_manage_upload_pdf_file(self): # Managers can upload a file via the DocumentManageFormPage # The image is stored even if form has errors self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') self.browser.open(self.customer_path + '/documents/DOC1/manage') # Create a pseudo image file and select it to be uploaded image = open(SAMPLE_PDF, 'rb') ctrl = self.browser.getControl(name='pdfscanmanageupload') file_ctrl = ctrl.mech_control file_ctrl.add_file(image, filename='my_sample_scan.pdf') # The Save action does not upload files self.browser.getControl("Save").click() # submit form self.assertFalse( 'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"' in self.browser.contents) # ... but the correct upload submit button does image = open(SAMPLE_PDF) ctrl = self.browser.getControl(name='pdfscanmanageupload') file_ctrl = ctrl.mech_control file_ctrl.add_file(image, filename='my_sample_scan.pdf') self.browser.getControl( name='upload_pdfscanmanageupload').click() self.assertTrue( 'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"' in self.browser.contents) # Browsing the link shows a real pdf file self.browser.open('scan.pdf') self.assertEqual( self.browser.headers['content-type'], 'application/pdf') self.assertEqual(len(self.browser.contents), 24241) # We can't reupload a file. The existing file must be deleted first. self.browser.open(self.customer_path + '/documents/DOC1/manage') self.assertFalse( 'upload_pdfscanmanageupload' in self.browser.contents) # File must be deleted first self.browser.getControl(name='delete_pdfscanmanageupload').click() self.assertTrue( 'scan.pdf deleted' in self.browser.contents) class ContractUITests(CustomersFullSetup): # Tests for contract related views and pages layer = FunctionalLayer def setup_customizable_params(self): self._contract_category = u'riaa' self._document_factory = 'waeup.UnibenCustomerPDFDocument' self._contract_factory = 'waeup.RIAAContract' return def test_view_slips(self): self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') # Officers can open contract slips. # First we add a submitted document and a product. IWorkflowState(self.document).setState('submitted') self.contract.document_object = self.document self.contract.product_object = self.product self.contract.tc_dict = {'en': u'Hello world'} self.contract.title = u'Contract Title' self.browser.open(self.customer_path + '/contracts/11111222223333344444555556666677') self.browser.getLink("Download contract slip").click() self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') path = os.path.join(samples_dir(), 'contract_slip.pdf') open(path, 'wb').write(self.browser.contents) print "Sample contract_slip.pdf written to %s" % path