source: main/ikobacustom.uniben/trunk/src/ikobacustom/uniben/customers/tests/test_browser.py @ 16281

Last change on this file since 16281 was 14219, checked in by Henrik Bettermann, 8 years ago

Customize RIAAContractExporter.mangle_value.

  • Property svn:keywords set to Id
File size: 13.2 KB
Line 
1## $Id: test_browser.py 14219 2016-10-20 12: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"""
19Document browser and functional tests.
20"""
21import os
22import grok
23import datetime
24from cStringIO import StringIO
25from zope.component import queryUtility, getUtility, createObject
26from zope.event import notify
27from zope.interface.verify import verifyObject, verifyClass
28from zope.testbrowser.testing import Browser
29from hurry.workflow.interfaces import IWorkflowState
30from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
31from waeup.ikoba.customers.tests.test_browser import CustomersFullSetup
32from ikobacustom.uniben.customers.export import (
33    UnibenCustomerExporter,
34    UnibenCustomerPDFDocumentExporter,
35    RIAAContractExporter)
36from ikobacustom.uniben.customers.batching import (
37    UnibenCustomerProcessor,
38    UnibenCustomerPDFDocumentProcessor,
39    RIAAContractProcessor)
40from ikobacustom.uniben.testing import FunctionalLayer, samples_dir
41
42SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
43SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp')
44SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf')
45
46class CustomerImportExportTest(CustomerImportExportSetup):
47
48    layer = FunctionalLayer
49
50    def setup_customizable_params(self):
51        self._contract_category = u'riaa'
52        return
53
54    def setup_for_export(self):
55        customer = createObject(u'waeup.Customer')
56        customer.firstname = u'Beate'
57        customer.lastname = u'Mueller'
58        customer.reg_number = u'123'
59        customer.sex = u'f'
60        customer.email = u'aa@aa.aa'
61        IWorkflowState(customer).setState('started')
62        self.app['customers'].addCustomer(customer)
63        document = createObject(u'waeup.UnibenCustomerPDFDocument')
64        document.title = u'My first document'
65        customer['documents'].addDocument(document)
66        contract = createObject(u'waeup.RIAAContract')
67        contract.tc_dict = {'en':u'Hello World'}
68        customer['contracts'].addContract(contract)
69        self.customer = customer
70        self.document = document
71        self.contract = contract
72        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
73        return
74
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()
79        exporter = UnibenCustomerExporter()
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'
85            'B1000000,aa@aa.aa,Beate,Mueller,,,123,f,0,,,started,[]\r\n')
86        # We can reimport the file ...
87        processor = UnibenCustomerProcessor()
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']['B1000000']
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)
104        # We can import the same file in update mode if we ignore
105        # the reg_number and email address
106        result = processor.doImport(
107            self.outfile,
108            ['customer_id','xx_email','firstname','lastname','middlename','phone',
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):
117        # we can export all documents in a portal
118        # set values we can expect in export file
119        self.setup_for_export()
120        exporter = UnibenCustomerPDFDocumentExporter()
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'
125            'UnibenCustomerPDFDocument,%s,'
126            '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],'
127            'created,My first document,B1000000\r\n'
128            % self.document.document_id)
129        # We can reimport the file if we change the header (user_id -> customer_id)
130        processor = UnibenCustomerPDFDocumentProcessor()
131        open(self.outfile, 'wb').write(
132            'customer_id,class_name,document_id,state,title\r\n'
133            'B1000000,UnibenCustomerPDFDocument,%s,started,My first title\r\n'
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)
158        return
159
160    def disabled_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()
164        exporter = RIAAContractExporter()
165        exporter.export_all(self.app, self.outfile)
166        result = open(self.outfile, 'rb').read()
167        self.assertMatches(result,
168            'class_name,comment,contract_category,contract_id,'
169            'doc1,doc10,doc11,doc2,doc3,doc4,doc5,doc6,doc7,doc8,doc9,'
170            'fee_based,history,last_product_id,product_object,product_options,'
171            'state,tc_dict,title,user_id,valid_from,valid_to\r\n'
172            'RIAAContract,,riaa,%s,,,,,,,,,,,,0,'
173            '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']'
174            ',,,[],created,{\'en\': u\'Hello World\'},,B1000000,,\r\n'
175            % self.contract.contract_id)
176        # We can reimport the file if we change the header (user_id -> customer_id)
177        processor = RIAAContractProcessor()
178        open(self.outfile, 'wb').write(
179            'class_name,contract_category,contract_id,document_object,'
180            'history,last_product_id,product_object,product_options,'
181            'state,tc_dict,title,user_id\r\n'
182            'RIAAContract,riaa,%s,,'
183            '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']'
184            ',,,[],created,{\'en\': u\'Hello World\'},,B1000000\r\n'
185            % self.contract.contract_id)
186        result = processor.doImport(
187            self.outfile,
188            ['class_name','contract_category','contract_id','document_object',
189            'history','last_product_id','product_object','product_options',
190            'state','tc_dict','title','customer_id'],
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',
200            'history','last_product_id','product_object','product_options',
201            'state','tc_dict','title','customer_id'],
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',
209            'history','last_product_id','product_object','product_options',
210            'state','tc_dict','title','customer_id'],
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
216
217class DocumentUITests(CustomersFullSetup):
218    # Tests for customer document related views and pages
219
220    layer = FunctionalLayer
221
222    def setup_customizable_params(self):
223        self._contract_category = u'riaa'
224        self._document_factory = 'waeup.UnibenCustomerPDFDocument'
225        self._contract_factory = 'waeup.RIAAContract'
226        return
227
228    def test_manage_upload_pdf_file(self):
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')
232        self.browser.open(self.customer_path + '/documents/DOC1/manage')
233        # Create a pseudo image file and select it to be uploaded
234        image = open(SAMPLE_PDF, 'rb')
235        ctrl = self.browser.getControl(name='pdfscanmanageupload')
236        file_ctrl = ctrl.mech_control
237        file_ctrl.add_file(image, filename='my_sample_scan.pdf')
238        # The Save action does not upload files
239        self.browser.getControl("Save").click() # submit form
240        self.assertFalse(
241            'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"'
242            in self.browser.contents)
243        # ... but the correct upload submit button does
244        image = open(SAMPLE_PDF)
245        ctrl = self.browser.getControl(name='pdfscanmanageupload')
246        file_ctrl = ctrl.mech_control
247        file_ctrl.add_file(image, filename='my_sample_scan.pdf')
248        self.browser.getControl(
249            name='upload_pdfscanmanageupload').click()
250        self.assertTrue(
251            'href="http://localhost/app/customers/B1000000/documents/DOC1/scan.pdf"'
252            in self.browser.contents)
253        # Browsing the link shows a real pdf file
254        self.browser.open('scan.pdf')
255        self.assertEqual(
256            self.browser.headers['content-type'], 'application/pdf')
257        self.assertEqual(len(self.browser.contents), 24241)
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(
261            'upload_pdfscanmanageupload' in self.browser.contents)
262        # File must be deleted first
263        self.browser.getControl(name='delete_pdfscanmanageupload').click()
264        self.assertTrue(
265            'scan.pdf deleted' in self.browser.contents)
266
267
268class ContractUITests(CustomersFullSetup):
269    # Tests for contract related views and pages
270
271    layer = FunctionalLayer
272
273    def setup_customizable_params(self):
274        self._contract_category = u'riaa'
275        self._document_factory = 'waeup.UnibenCustomerPDFDocument'
276        self._contract_factory = 'waeup.RIAAContract'
277        return
278
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>'}
287        self.contract.title = u'Contract Title'
288        self.browser.open(self.customer_path + '/contracts/11111222223333344444555556666677')
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
Note: See TracBrowser for help on using the repository browser.