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

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

Modify redirect after adding documents.

Ensure that keys don't exist when adding documents.

  • Property svn:keywords set to Id
File size: 15.1 KB
Line 
1## $Id: test_browser.py 12413 2015-01-07 09:20:45Z 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.getControl(name="form.title").value = 'My first doc'
120        self.browser.getControl("Save").click()
121        self.assertTrue('Form has been saved.' in self.browser.contents)
122        self.browser.getLink("View").click()
123        self.assertEqual(self.browser.url, self.container_path + '/DOC1/index')
124
125        # File can be uploaded
126        self.browser.getLink("Manage").click()
127        # Create a pseudo image file and select it to be uploaded
128        image = open(SAMPLE_IMAGE, 'rb')
129        ctrl = self.browser.getControl(name='pdfscanmanageupload')
130        file_ctrl = ctrl.mech_control
131        file_ctrl.add_file(image, filename='my_sample_scan.jpg')
132        self.browser.getControl(
133            name='upload_pdfscanmanageupload').click()
134        self.assertTrue(
135            'pdf file format expected' in self.browser.contents)
136        ctrl = self.browser.getControl(name='pdfscanmanageupload')
137        file_ctrl = ctrl.mech_control
138        file_ctrl.add_file(image, filename='my_sample_scan.pdf')
139        self.browser.getControl(
140            name='upload_pdfscanmanageupload').click()
141        self.assertTrue(
142            'Could not determine file type' in self.browser.contents)
143        pdf = open(SAMPLE_PDF, 'rb')
144        ctrl = self.browser.getControl(name='pdfscanmanageupload')
145        file_ctrl = ctrl.mech_control
146        file_ctrl.add_file(pdf, filename='my_sample_scan.pdf')
147        self.browser.getControl(
148            name='upload_pdfscanmanageupload').click()
149        self.assertTrue(
150            'href="http://localhost/app/documents/DOC1/sample.pdf">PDF File</a>'
151            in self.browser.contents)
152        # Browsing the link shows a real pdf only if the document
153        # has been published
154        self.browser.getLink("PDF File").click()
155        self.assertTrue(
156            'The document requested has not yet been published'
157            in self.browser.contents)
158        IWorkflowState(document).setState(PUBLISHED)
159        self.browser.open(self.container_path + '/DOC1/sample.pdf')
160        self.assertEqual(
161            self.browser.headers['content-type'], 'application/pdf')
162
163        # Transitions can be performed
164        self.assertEqual(document.state, 'published')
165        self.browser.open(self.container_path + '/DOC1')
166        self.browser.getLink("Transition").click()
167        self.browser.getControl(name="transition").value = ['retract']
168        self.browser.getControl("Apply now").click()
169        self.assertEqual(document.state, 'created')
170
171        # Documents can be removed
172        self.browser.getLink("Documents").click()
173        self.browser.getLink("Manage").click()
174        ctrl = self.browser.getControl(name='val_id')
175        ctrl.getControl(value=document.document_id).selected = True
176        self.browser.getControl("Remove selected", index=0).click()
177        self.assertTrue('Successfully removed' in self.browser.contents)
178
179        # All actions are being logged
180        logfile = os.path.join(
181            self.app['datacenter'].storage, 'logs', 'main.log')
182        logcontent = open(logfile).read()
183
184        self.assertTrue(
185            'INFO - zope.mgr - %s - Document created' % document.document_id
186            in logcontent)
187        self.assertTrue(
188            'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: PDF Document %s'
189            % document.document_id in logcontent)
190        self.assertTrue(
191            'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - saved: title'
192            % document.document_id in logcontent)
193        self.assertTrue(
194            'INFO - zope.mgr - documents.browser.DocumentManageFormPage - %s - uploaded: sample.pdf (my_sample_scan.pdf)'
195            % document.document_id in logcontent)
196        self.assertTrue(
197            'INFO - zope.mgr - %s - Document retracted' % document.document_id
198            in logcontent)
199        self.assertTrue(
200            'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s'
201            % document.document_id in logcontent)
202
203    def test_manage_html_document(self):
204        # Managers can access the pages of documentsconter
205        # and can perform actions
206        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
207        self.browser.open('http://localhost/app')
208        self.assertEqual(self.browser.headers['Status'], '200 Ok')
209        self.browser.getLink("Documents").click()
210        self.assertEqual(self.browser.url, self.container_path)
211        self.browser.getLink("Manage").click()
212        self.browser.getControl("Add document").click()
213        self.browser.getControl(name="doctype").value = ['HTMLDocument']
214        self.browser.getControl(name="form.document_id").value = 'DOC2'
215        self.browser.getControl(name="form.title").value = 'My HTML Document'
216        self.browser.getControl("Add document").click()
217        self.assertTrue('HTML Document added.' in self.browser.contents)
218        document = self.app['documents']['DOC2']
219
220        # Document can be edited
221        self.browser.getControl(name="form.title").value = 'My second doc'
222        self.browser.getControl(name="form.html_multilingual").value = """
223<h1>Hello World</h1>
224>>de<<
225<h1>Hallo Welt</h1>
226"""
227        self.browser.getControl("Save").click()
228        self.assertTrue('Form has been saved.' in self.browser.contents)
229        self.browser.getLink("View").click()
230        self.assertEqual(self.browser.url, self.container_path + '/DOC2/index')
231        self.assertTrue(
232            '<h1>Hello World</h1>' in self.browser.contents)
233        self.assertFalse(
234            '<h1>Hallo Welt</h1>' in self.browser.contents)
235        self.browser.getLink("de").click()
236        self.assertFalse(
237            '<h1>Hello World</h1>' in self.browser.contents)
238        self.assertTrue(
239            '<h1>Hallo Welt</h1>' in self.browser.contents)
240        # The content can't be rendered yet
241        self.browser.open(self.container_path + '/DOC2/display')
242        self.assertTrue(
243            'The document requested has not yet been published'
244            in self.browser.contents)
245        # We have been redirected to the portal root
246        self.assertEqual(self.browser.url, 'http://localhost/app')
247
248        # Transitions can be performed
249        self.browser.open(self.container_path + '/DOC2')
250        self.browser.getLink("Transition").click()
251        self.browser.getControl(name="transition").value = ['publish']
252        self.browser.getControl("Jetzt anwenden").click()
253        self.assertEqual(document.state, 'published')
254
255        # The content can be rendered
256        self.browser.open(self.container_path + '/DOC2/display')
257        self.assertTrue(
258            '<h1>Hallo Welt</h1>' in self.browser.contents)
259
260        # Documents can be removed
261        self.browser.getLink("en", index=2).click()
262        self.browser.getLink("Documents").click()
263        self.browser.getLink("Manage").click()
264        ctrl = self.browser.getControl(name='val_id')
265        ctrl.getControl(value=document.document_id).selected = True
266        self.browser.getControl("Remove selected", index=0).click()
267        self.assertTrue('Successfully removed' in self.browser.contents)
268
269        # All actions are being logged
270        logfile = os.path.join(
271            self.app['datacenter'].storage, 'logs', 'main.log')
272        logcontent = open(logfile).read()
273
274        self.assertTrue(
275            'INFO - zope.mgr - %s - Document created' % document.document_id
276            in logcontent)
277        self.assertTrue(
278            'INFO - zope.mgr - documents.browser.DocumentAddFormPage - added: HTML Document %s'
279            % document.document_id in logcontent)
280        self.assertTrue(
281            'INFO - zope.mgr - documents.browser.HTMLDocumentManageFormPage - %s - saved: title + html_multilingual'
282            % document.document_id in logcontent)
283        self.assertTrue(
284            'INFO - zope.mgr - %s - Document published' % document.document_id
285            in logcontent)
286        self.assertTrue(
287            'INFO - zope.mgr - documents.browser.DocumentsContainerManageFormPage - removed: %s'
288            % document.document_id in logcontent)
289
290    def test_manage_rest_document(self):
291        # Managers can access the pages of documentsconter
292        # and can perform actions
293        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
294        self.browser.open('http://localhost/app')
295        self.assertEqual(self.browser.headers['Status'], '200 Ok')
296        self.browser.getLink("Documents").click()
297        self.assertEqual(self.browser.url, self.container_path)
298        self.browser.getLink("Manage").click()
299        self.browser.getControl("Add document").click()
300        self.browser.getControl(name="doctype").value = ['RESTDocument']
301        self.browser.getControl(name="form.document_id").value = 'DOC3'
302        self.browser.getControl(name="form.title").value = 'My REST Document'
303        self.browser.getControl("Add document").click()
304        self.assertTrue('REST Document added.' in self.browser.contents)
305        document = self.app['documents']['DOC3']
306
307        # Document can be edited
308        self.browser.getControl(name="form.rest_multilingual").value = """
309----------
310Main Title
311----------
312
313Subtitle
314========
315>>de<<
316----------
317Haupttitel
318----------
319
320Untertitel
321==========
322"""
323        self.browser.getControl("Save").click()
324        self.assertTrue('Form has been saved.' in self.browser.contents)
325        self.browser.getLink("View").click()
326        self.assertEqual(self.browser.url, self.container_path + '/DOC3/index')
327        self.assertTrue(
328            '<h1 class="title">Main Title</h1>' in self.browser.contents)
329        self.assertTrue(
330            '<h2 class="subtitle" id="subtitle">Subtitle</h2>'
331            in self.browser.contents)
332        self.assertFalse(
333            '<h1 class="title">Haupttitel</h1>' in self.browser.contents)
334        self.browser.getLink("de").click()
335        self.assertFalse(
336            '<h1 class="title">Main Title</h1>' in self.browser.contents)
337        self.assertTrue(
338            '<h1 class="title">Haupttitel</h1>' in self.browser.contents)
339        # The content can be rendered
340        IWorkflowState(document).setState(PUBLISHED)
341        self.browser.open(self.container_path + '/DOC3/display')
342        self.assertTrue(
343            '<h1 class="title">Haupttitel</h1>' in self.browser.contents)
344        # The page label (object title) is not displayed
345        self.assertFalse(
346            '<h1 class="ikoba-content-label">My REST Document</h1>'
347            in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.