source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/tests/test_browser.py @ 12386

Last change on this file since 12386 was 12386, checked in by Henrik Bettermann, 10 years ago

Tell the truth.

  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1## $Id: test_browser.py 12386 2015-01-03 18:46:36Z 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"""
19Test the customer-related UI components.
20"""
21import shutil
22import tempfile
23from datetime import datetime, timedelta, date
24from StringIO import StringIO
25import os
26from zope.event import notify
27from zope.component import createObject, queryUtility, getUtility
28from zope.component.hooks import setSite, clearSite
29from zope.schema.interfaces import ConstraintNotSatisfied
30from zope.catalog.interfaces import ICatalog
31from zope.security.interfaces import Unauthorized
32from zope.securitypolicy.interfaces import IPrincipalRoleManager
33from zope.testbrowser.testing import Browser
34from hurry.workflow.interfaces import (
35    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
36from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
37from waeup.ikoba.app import Company
38from waeup.ikoba.interfaces import (
39    IUserAccount, IJobManager, APPROVED, SUBMITTED, PUBLISHED,
40    IFileStoreNameChooser, IExtFileStore, IFileStoreHandler)
41from waeup.ikoba.imagestorage import (
42    FileStoreNameChooser, ExtFileStore, DefaultFileStoreHandler,
43    DefaultStorage)
44from waeup.ikoba.authentication import LocalRoleSetEvent
45from waeup.ikoba.tests.test_async import FunctionalAsyncTestCase
46from waeup.ikoba.interfaces import VERIFIED
47from waeup.ikoba.browser.tests.test_pdf import samples_dir
48
49PH_LEN = 15911  # Length of placeholder file
50
51SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
52#SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp')
53SAMPLE_PDF = os.path.join(os.path.dirname(__file__), 'test_pdf.pdf')
54
55class FullSetup(FunctionalTestCase):
56    # A test case that only contains a setup and teardown
57    #
58    # Complete setup for customers handlings is rather complex and
59    # requires lots of things created before we can start. This is a
60    # setup that does all this, creates a company etc.
61    # so that we do not have to bother with that in different
62    # test cases.
63
64    layer = FunctionalLayer
65
66    def setUp(self):
67        super(FullSetup, self).setUp()
68
69        # Setup a sample site for each test
70        app = Company()
71        self.dc_root = tempfile.mkdtemp()
72        app['datacenter'].setStoragePath(self.dc_root)
73
74        # Prepopulate the ZODB...
75        self.getRootFolder()['app'] = app
76        # we add the site immediately after creation to the
77        # ZODB. Catalogs and other local utilities are not setup
78        # before that step.
79        self.app = self.getRootFolder()['app']
80        # Set site here. Some of the following setup code might need
81        # to access grok.getSite() and should get our new app then
82        setSite(app)
83
84        self.login_path = 'http://localhost/app/login'
85        self.container_path = 'http://localhost/app/documents'
86
87        # Put the prepopulated site into test ZODB and prepare test
88        # browser
89        self.browser = Browser()
90        self.browser.handleErrors = False
91
92    def tearDown(self):
93        super(FullSetup, self).tearDown()
94        clearSite()
95        shutil.rmtree(self.dc_root)
96
97
98class DocumentUITests(FullSetup):
99    # Tests for document related views and pages
100
101    def test_manage_file_document(self):
102        # Managers can access the pages of documentsconter
103        # and can perform actions
104        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
105        self.browser.open('http://localhost/app')
106        self.assertEqual(self.browser.headers['Status'], '200 Ok')
107        self.browser.getLink("Documents").click()
108        self.assertEqual(self.browser.url, self.container_path)
109        self.browser.getLink("Manage").click()
110        self.browser.getControl("Add document").click()
111        self.browser.getControl(name="doctype").value = ['PDFDocument']
112        self.browser.getControl(name="form.title").value = 'My PDF Document'
113        self.browser.getControl(name="form.document_id").value = 'DOC1'
114        self.browser.getControl("Add document").click()
115        self.assertTrue('PDF Document added.' in self.browser.contents)
116        document = self.app['documents']['DOC1']
117
118        # Document can be edited
119        self.browser.getLink("DOC1").click()
120        self.browser.getLink("Manage").click()
121        self.browser.getControl(name="form.title").value = 'My first doc'
122        self.browser.getControl("Save").click()
123        self.assertTrue('Form has been saved.' in self.browser.contents)
124        self.browser.getLink("View").click()
125        self.assertEqual(self.browser.url, self.container_path + '/DOC1/index')
126
127        # File can be uploaded
128        self.browser.getLink("Manage").click()
129        # Create a pseudo image file and select it to be uploaded
130        image = open(SAMPLE_IMAGE, 'rb')
131        ctrl = self.browser.getControl(name='pdfscanmanageupload')
132        file_ctrl = ctrl.mech_control
133        file_ctrl.add_file(image, filename='my_sample_scan.jpg')
134        self.browser.getControl(
135            name='upload_pdfscanmanageupload').click()
136        self.assertTrue(
137            'pdf file format expected' in self.browser.contents)
138        ctrl = self.browser.getControl(name='pdfscanmanageupload')
139        file_ctrl = ctrl.mech_control
140        file_ctrl.add_file(image, filename='my_sample_scan.pdf')
141        self.browser.getControl(
142            name='upload_pdfscanmanageupload').click()
143        self.assertTrue(
144            'Could not determine file type' in self.browser.contents)
145        pdf = open(SAMPLE_PDF, 'rb')
146        ctrl = self.browser.getControl(name='pdfscanmanageupload')
147        file_ctrl = ctrl.mech_control
148        file_ctrl.add_file(pdf, filename='my_sample_scan.pdf')
149        self.browser.getControl(
150            name='upload_pdfscanmanageupload').click()
151        self.assertTrue(
152            'href="http://localhost/app/documents/DOC1/sample.pdf">PDF File</a>'
153            in self.browser.contents)
154        # Browsing the link shows a real pdf only if the document
155        # has been published
156        self.browser.getLink("PDF File").click()
157        self.assertTrue(
158            'The document requested has not yet been published'
159            in self.browser.contents)
160        IWorkflowState(document).setState(PUBLISHED)
161        self.browser.open(self.container_path + '/DOC1/sample.pdf')
162        self.assertEqual(
163            self.browser.headers['content-type'], 'application/pdf')
164
165        # Transitions can be performed
166        self.assertEqual(document.state, 'published')
167        self.browser.open(self.container_path + '/DOC1')
168        self.browser.getLink("Transition").click()
169        self.browser.getControl(name="transition").value = ['retract']
170        self.browser.getControl("Apply now").click()
171        self.assertEqual(document.state, 'created')
172
173        # Documents can be removed
174        self.browser.getLink("Documents").click()
175        self.browser.getLink("Manage").click()
176        ctrl = self.browser.getControl(name='val_id')
177        ctrl.getControl(value=document.document_id).selected = True
178        self.browser.getControl("Remove selected", index=0).click()
179        self.assertTrue('Successfully removed' in self.browser.contents)
180
181        # All actions are being logged
182        logfile = os.path.join(
183            self.app['datacenter'].storage, 'logs', 'main.log')
184        logcontent = open(logfile).read()
185
186        self.assertTrue(
187            'INFO - zope.mgr - %s - Document created' % document.document_id
188            in logcontent)
189        self.assertTrue(
190            'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: PDF Document %s'
191            % document.document_id in logcontent)
192        self.assertTrue(
193            'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - saved: title'
194            % document.document_id in logcontent)
195        self.assertTrue(
196            'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - uploaded: sample.pdf (my_sample_scan.pdf)'
197            % document.document_id in logcontent)
198        self.assertTrue(
199            'INFO - zope.mgr - %s - Document retracted' % document.document_id
200            in logcontent)
201        self.assertTrue(
202            'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s'
203            % document.document_id in logcontent)
204
205    def test_manage_html_document(self):
206        # Managers can access the pages of documentsconter
207        # and can perform actions
208        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
209        self.browser.open('http://localhost/app')
210        self.assertEqual(self.browser.headers['Status'], '200 Ok')
211        self.browser.getLink("Documents").click()
212        self.assertEqual(self.browser.url, self.container_path)
213        self.browser.getLink("Manage").click()
214        self.browser.getControl("Add document").click()
215        self.browser.getControl(name="doctype").value = ['HTMLDocument']
216        self.browser.getControl(name="form.document_id").value = 'DOC2'
217        self.browser.getControl(name="form.title").value = 'My HTML Document'
218        self.browser.getControl("Add document").click()
219        self.assertTrue('HTML Document added.' in self.browser.contents)
220        document = self.app['documents']['DOC2']
221
222        # Document can be edited
223        self.browser.getLink("DOC2").click()
224        self.browser.getLink("Manage").click()
225        self.browser.getControl(name="form.title").value = 'My second doc'
226        self.browser.getControl(name="form.html_multilingual").value = """
227<h1>Hello World</h1>
228>>de<<
229<h1>Hallo Welt</h1>
230"""
231        self.browser.getControl("Save").click()
232        self.assertTrue('Form has been saved.' in self.browser.contents)
233        self.browser.getLink("View").click()
234        self.assertEqual(self.browser.url, self.container_path + '/DOC2/index')
235        self.assertTrue(
236            '<h1>Hello World</h1>' in self.browser.contents)
237        self.assertFalse(
238            '<h1>Hallo Welt</h1>' in self.browser.contents)
239        self.browser.getLink("de").click()
240        self.assertFalse(
241            '<h1>Hello World</h1>' in self.browser.contents)
242        self.assertTrue(
243            '<h1>Hallo Welt</h1>' in self.browser.contents)
244        # The content can't be rendered yet
245        self.browser.open(self.container_path + '/DOC2/display')
246        self.assertTrue(
247            'The document requested has not yet been published'
248            in self.browser.contents)
249        # We have been redirected to the portal root
250        self.assertEqual(self.browser.url, 'http://localhost/app')
251
252        # Transitions can be performed
253        self.browser.open(self.container_path + '/DOC2')
254        self.browser.getLink("Transition").click()
255        self.browser.getControl(name="transition").value = ['publish']
256        self.browser.getControl("Jetzt anwenden").click()
257        self.assertEqual(document.state, 'published')
258
259        # The content can be rendered
260        self.browser.open(self.container_path + '/DOC2/display')
261        self.assertTrue(
262            '<h1>Hallo Welt</h1>' in self.browser.contents)
263
264        # Documents can be removed
265        self.browser.getLink("en", index=2).click()
266        self.browser.getLink("Documents").click()
267        self.browser.getLink("Manage").click()
268        ctrl = self.browser.getControl(name='val_id')
269        ctrl.getControl(value=document.document_id).selected = True
270        self.browser.getControl("Remove selected", index=0).click()
271        self.assertTrue('Successfully removed' in self.browser.contents)
272
273        # All actions are being logged
274        logfile = os.path.join(
275            self.app['datacenter'].storage, 'logs', 'main.log')
276        logcontent = open(logfile).read()
277
278        self.assertTrue(
279            'INFO - zope.mgr - %s - Document created' % document.document_id
280            in logcontent)
281        self.assertTrue(
282            'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: HTML Document %s'
283            % document.document_id in logcontent)
284        self.assertTrue(
285            'INFO - zope.mgr - documents.browser.HTMLDocumentManageFormPage - %s - saved: title + html_multilingual'
286            % document.document_id in logcontent)
287        self.assertTrue(
288            'INFO - zope.mgr - %s - Document published' % document.document_id
289            in logcontent)
290        self.assertTrue(
291            'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s'
292            % document.document_id in logcontent)
293
Note: See TracBrowser for help on using the repository browser.