|
| 1 | +from ..testing import INTEGRATION_TESTING |
| 2 | +from plone import api |
| 3 | +from plone.app.testing import login |
| 4 | +from plone.app.testing import logout |
| 5 | +from plone.app.testing import setRoles |
| 6 | +from plone.app.testing import TEST_USER_ID |
| 7 | +from plone.app.testing import TEST_USER_NAME |
| 8 | +from plone.testing.z2 import Browser |
| 9 | +from transaction import commit |
| 10 | +from zope.component import getMultiAdapter |
| 11 | + |
| 12 | +import csv |
| 13 | +import unittest |
| 14 | +import uuid |
| 15 | + |
| 16 | + |
| 17 | +class TestExport(unittest.TestCase): |
| 18 | + """Test PDF export functionality""" |
| 19 | + |
| 20 | + layer = INTEGRATION_TESTING |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + self.app = self.layer["app"] |
| 24 | + self.portal = self.layer["portal"] |
| 25 | + self.request = self.layer["request"] |
| 26 | + self.portal_url = self.portal.absolute_url() |
| 27 | + setRoles(self.portal, TEST_USER_ID, ["Manager"]) |
| 28 | + login(self.portal, TEST_USER_NAME) |
| 29 | + |
| 30 | + self.doc = api.content.create( |
| 31 | + container=self.portal, |
| 32 | + type="Document", |
| 33 | + id="test-document", |
| 34 | + title="Test Document", |
| 35 | + description="This is a test document", |
| 36 | + ) |
| 37 | + api.content.transition(self.doc, to_state="published") |
| 38 | + commit() |
| 39 | + |
| 40 | + self.browser = Browser(self.app) |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + api.content.delete(self.doc) |
| 44 | + commit() |
| 45 | + logout() |
| 46 | + |
| 47 | + def test_pdf_export_view_exists(self): |
| 48 | + """Test that the PDF export view is registered and accessible |
| 49 | +
|
| 50 | + TODO: view must be accessible only for IExportViewDownload context |
| 51 | + """ |
| 52 | + view = getMultiAdapter((self.doc, self.request), name="export_pdf") |
| 53 | + self.assertTrue(view is not None) |
| 54 | + |
| 55 | + def test_searchblock_pdf(self): |
| 56 | + block_id = uuid.uuid4().hex |
| 57 | + self.doc.blocks = { |
| 58 | + block_id: { |
| 59 | + "@type": "search", |
| 60 | + "query": { |
| 61 | + "query": [ |
| 62 | + { |
| 63 | + "i": "portal_type", |
| 64 | + "o": "plone.app.querystring.operation.selection.is", |
| 65 | + "v": "Document", |
| 66 | + } |
| 67 | + ] |
| 68 | + }, |
| 69 | + } |
| 70 | + } |
| 71 | + self.doc.blocks_layout = {"items": [block_id]} |
| 72 | + commit() |
| 73 | + |
| 74 | + self.browser.open( |
| 75 | + f"{self.doc.absolute_url()}/searchblock/@@download/{block_id}.pdf" |
| 76 | + ) |
| 77 | + |
| 78 | + # Check response headers |
| 79 | + self.assertEqual(self.browser.headers["Content-Type"], "application/pdf") |
| 80 | + self.assertIn( |
| 81 | + "attachment;filename=", self.browser.headers["Content-Disposition"] |
| 82 | + ) |
| 83 | + # Check that the response is not empty and looks like a PDF |
| 84 | + self.assertTrue(b"%PDF-" in self.browser.contents[:10]) |
| 85 | + |
| 86 | + # TODO |
| 87 | + # api.portal.get_tool("portal_transforms").convertTo("plain/text", self.browser.contents, mimetype="application/pdf") |
| 88 | + # oppure con pypdf |
| 89 | + |
| 90 | + def test_searchblock_csv(self): |
| 91 | + block_id = uuid.uuid4().hex |
| 92 | + self.doc.blocks = { |
| 93 | + block_id: { |
| 94 | + "@type": "search", |
| 95 | + "query": { |
| 96 | + "query": [ |
| 97 | + { |
| 98 | + "i": "portal_type", |
| 99 | + "o": "plone.app.querystring.operation.selection.is", |
| 100 | + "v": "Document", |
| 101 | + } |
| 102 | + ] |
| 103 | + }, |
| 104 | + } |
| 105 | + } |
| 106 | + self.doc.blocks_layout = {"items": [block_id]} |
| 107 | + commit() |
| 108 | + |
| 109 | + self.browser.open( |
| 110 | + f"{self.doc.absolute_url()}/searchblock/@@download/{block_id}.csv" |
| 111 | + ) |
| 112 | + |
| 113 | + # Check response headers |
| 114 | + self.assertEqual( |
| 115 | + self.browser.headers["Content-Type"], "text/csv; charset=utf-8-sig" |
| 116 | + ) |
| 117 | + self.assertIn( |
| 118 | + "attachment;filename=", self.browser.headers["Content-Disposition"] |
| 119 | + ) |
| 120 | + |
| 121 | + # Check that the response is a CSV with expected data |
| 122 | + reader = csv.DictReader(self.browser.contents) |
| 123 | + self.assertEqual(reader.fieldnames, ["Titolo"]) |
| 124 | + self.assertEqual([row for row in reader], [{"Titolo": "Test Document"}]) |
0 commit comments