Browse Source

fix preview endpoint + test related

Guénaël Muller 6 years ago
parent
commit
9cf537ad27

+ 2 - 1
tracim/lib/core/content.py View File

748
                     content_id=content_id
748
                     content_id=content_id
749
                 ),
749
                 ),
750
             )
750
             )
751
-        jpg_preview_path = self.preview_manager.get_jpeg_preview(
751
+        jpg_preview_path = self.preview_manager.get_pdf_preview(
752
             file_path,
752
             file_path,
753
             page=page
753
             page=page
754
         )
754
         )
769
             self._config.PREVIEW_JPG_RESTRICTED_DIMS,
769
             self._config.PREVIEW_JPG_RESTRICTED_DIMS,
770
             self._config.PREVIEW_JPG_ALLOWED_DIMS,
770
             self._config.PREVIEW_JPG_ALLOWED_DIMS,
771
         )
771
         )
772
+
772
     def get_jpg_preview_path(
773
     def get_jpg_preview_path(
773
         self,
774
         self,
774
         content_id: int,
775
         content_id: int,

+ 200 - 0
tracim/tests/functional/test_contents.py View File

1459
             status=400
1459
             status=400
1460
         )
1460
         )
1461
 
1461
 
1462
+    def test_api__get_pdf_preview__ok__200__nominal_case(self) -> None:
1463
+        """
1464
+        get full pdf preview of a txt file
1465
+        """
1466
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1467
+        admin = dbsession.query(models.User) \
1468
+            .filter(models.User.email == 'admin@admin.admin') \
1469
+            .one()
1470
+        workspace_api = WorkspaceApi(
1471
+            current_user=admin,
1472
+            session=dbsession,
1473
+            config=self.app_config
1474
+        )
1475
+        content_api = ContentApi(
1476
+            current_user=admin,
1477
+            session=dbsession,
1478
+            config=self.app_config
1479
+        )
1480
+        business_workspace = workspace_api.get_one(1)
1481
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1482
+        test_file = content_api.create(
1483
+            content_type=ContentType.File,
1484
+            workspace=business_workspace,
1485
+            parent=tool_folder,
1486
+            label='Test file',
1487
+            do_save=True,
1488
+            do_notify=False,
1489
+        )
1490
+        with new_revision(
1491
+                session=dbsession,
1492
+                tm=transaction.manager,
1493
+                content=test_file,
1494
+        ):
1495
+            test_file.file_extension = '.txt'
1496
+            test_file.depot_file = FileIntent(
1497
+                b'Test file',
1498
+                'Test_file.txt',
1499
+                'text/plain',
1500
+            )
1501
+            content_api.update_content(test_file, 'Test_file', '<p>description</p>')
1502
+        dbsession.flush()
1503
+        transaction.commit()
1504
+        content_id = int(test_file.content_id)
1505
+        self.testapp.authorization = (
1506
+            'Basic',
1507
+            (
1508
+                'admin@admin.admin',
1509
+                'admin@admin.admin'
1510
+            )
1511
+        )
1512
+        res = self.testapp.put(
1513
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1514
+            upload_files=[
1515
+                ('files', test_file.file_name, test_file.depot_file.file.read())
1516
+            ],
1517
+            status=204,
1518
+        )
1519
+        params = {'page': 0}
1520
+        res = self.testapp.get(
1521
+            '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
1522
+            status=200,
1523
+            params=params,
1524
+        )
1525
+        assert res.content_type == 'application/pdf'
1526
+
1527
+    def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None:
1528
+        """
1529
+        get full pdf preview of a txt file
1530
+        """
1531
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1532
+        admin = dbsession.query(models.User) \
1533
+            .filter(models.User.email == 'admin@admin.admin') \
1534
+            .one()
1535
+        workspace_api = WorkspaceApi(
1536
+            current_user=admin,
1537
+            session=dbsession,
1538
+            config=self.app_config
1539
+        )
1540
+        content_api = ContentApi(
1541
+            current_user=admin,
1542
+            session=dbsession,
1543
+            config=self.app_config
1544
+        )
1545
+        business_workspace = workspace_api.get_one(1)
1546
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1547
+        test_file = content_api.create(
1548
+            content_type=ContentType.File,
1549
+            workspace=business_workspace,
1550
+            parent=tool_folder,
1551
+            label='Test file',
1552
+            do_save=True,
1553
+            do_notify=False,
1554
+        )
1555
+        with new_revision(
1556
+                session=dbsession,
1557
+                tm=transaction.manager,
1558
+                content=test_file,
1559
+        ):
1560
+            test_file.file_extension = '.txt'
1561
+            test_file.depot_file = FileIntent(
1562
+                b'Test file',
1563
+                'Test_file.txt',
1564
+                'text/plain',
1565
+            )
1566
+            content_api.update_content(test_file, 'Test_file', '<p>description</p>')
1567
+        dbsession.flush()
1568
+        transaction.commit()
1569
+        content_id = int(test_file.content_id)
1570
+        self.testapp.authorization = (
1571
+            'Basic',
1572
+            (
1573
+                'admin@admin.admin',
1574
+                'admin@admin.admin'
1575
+            )
1576
+        )
1577
+        res = self.testapp.put(
1578
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1579
+            upload_files=[
1580
+                ('files', test_file.file_name, test_file.depot_file.file.read())
1581
+            ],
1582
+            status=204,
1583
+        )
1584
+        params = {'page': 1}
1585
+        res = self.testapp.get(
1586
+            '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
1587
+            status=400,
1588
+            params=params,
1589
+        )
1590
+
1591
+    def test_api__get_pdf_revision_preview__ok__200__nominal_case(self) -> None:
1592
+        """
1593
+        get pdf revision preview of content
1594
+        """
1595
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1596
+        admin = dbsession.query(models.User) \
1597
+            .filter(models.User.email == 'admin@admin.admin') \
1598
+            .one()
1599
+        workspace_api = WorkspaceApi(
1600
+            current_user=admin,
1601
+            session=dbsession,
1602
+            config=self.app_config
1603
+        )
1604
+        content_api = ContentApi(
1605
+            current_user=admin,
1606
+            session=dbsession,
1607
+            config=self.app_config
1608
+        )
1609
+        business_workspace = workspace_api.get_one(1)
1610
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1611
+        test_file = content_api.create(
1612
+            content_type=ContentType.File,
1613
+            workspace=business_workspace,
1614
+            parent=tool_folder,
1615
+            label='Test file',
1616
+            do_save=False,
1617
+            do_notify=False,
1618
+        )
1619
+        test_file.file_extension = '.txt'
1620
+        test_file.depot_file = FileIntent(
1621
+            b'Test file',
1622
+            'Test_file.txt',
1623
+            'text/plain',
1624
+        )
1625
+        dbsession.flush()
1626
+        transaction.commit()
1627
+        content_id = int(test_file.content_id)
1628
+        revision_id = int(test_file.revision_id)
1629
+        image = create_test_image()
1630
+        self.testapp.authorization = (
1631
+            'Basic',
1632
+            (
1633
+                'admin@admin.admin',
1634
+                'admin@admin.admin'
1635
+            )
1636
+        )
1637
+        res = self.testapp.put(
1638
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1639
+            upload_files=[
1640
+                ('files', image.name, image.getvalue())
1641
+            ],
1642
+            status=204,
1643
+        )
1644
+        res = self.testapp.get(
1645
+            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format(
1646
+                content_id=content_id,
1647
+                revision_id=revision_id,
1648
+            ),
1649
+            status=200
1650
+        )
1651
+        assert res.content_type == 'text/plain'
1652
+        params = {'page': 0}
1653
+        res = self.testapp.get(
1654
+            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf'.format(
1655
+                content_id=content_id,
1656
+                revision_id=revision_id,
1657
+            ),
1658
+            status=200
1659
+        )
1660
+        assert res.content_type == 'application/pdf'
1661
+
1462
 
1662
 
1463
 class TestThreads(FunctionalTest):
1663
 class TestThreads(FunctionalTest):
1464
     """
1664
     """

+ 5 - 0
tracim/views/contents_api/file_controller.py View File

36
 from tracim.models.contents import file_type
36
 from tracim.models.contents import file_type
37
 from tracim.models.revision_protection import new_revision
37
 from tracim.models.revision_protection import new_revision
38
 from tracim.exceptions import EmptyLabelNotAllowed
38
 from tracim.exceptions import EmptyLabelNotAllowed
39
+from tracim.exceptions import PageOfPreviewNotFound
39
 from tracim.exceptions import PreviewDimNotAllowed
40
 from tracim.exceptions import PreviewDimNotAllowed
40
 
41
 
41
 FILE_ENDPOINTS_TAG = 'Files'
42
 FILE_ENDPOINTS_TAG = 'Files'
130
     @require_workspace_role(UserRoleInWorkspace.READER)
131
     @require_workspace_role(UserRoleInWorkspace.READER)
131
     @require_content_types([file_type])
132
     @require_content_types([file_type])
132
     @hapic.handle_exception(UnavailablePreviewType, HTTPStatus.BAD_REQUEST)
133
     @hapic.handle_exception(UnavailablePreviewType, HTTPStatus.BAD_REQUEST)
134
+    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
133
     @hapic.input_query(PageQuerySchema())
135
     @hapic.input_query(PageQuerySchema())
134
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
136
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
135
     @hapic.output_file([])
137
     @hapic.output_file([])
204
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
206
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
205
     @require_workspace_role(UserRoleInWorkspace.READER)
207
     @require_workspace_role(UserRoleInWorkspace.READER)
206
     @require_content_types([file_type])
208
     @require_content_types([file_type])
209
+    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
207
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
210
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
208
     @hapic.input_query(PageQuerySchema())
211
     @hapic.input_query(PageQuerySchema())
209
     @hapic.output_file([])
212
     @hapic.output_file([])
231
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
234
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
232
     @require_workspace_role(UserRoleInWorkspace.READER)
235
     @require_workspace_role(UserRoleInWorkspace.READER)
233
     @require_content_types([file_type])
236
     @require_content_types([file_type])
237
+    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
234
     @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
238
     @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
235
     @hapic.input_query(PageQuerySchema())
239
     @hapic.input_query(PageQuerySchema())
236
     @hapic.input_path(ContentPreviewSizedPathSchema())
240
     @hapic.input_path(ContentPreviewSizedPathSchema())
258
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
262
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
259
     @require_workspace_role(UserRoleInWorkspace.READER)
263
     @require_workspace_role(UserRoleInWorkspace.READER)
260
     @require_content_types([file_type])
264
     @require_content_types([file_type])
265
+    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
261
     @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
266
     @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
262
     @hapic.input_path(RevisionPreviewSizedPathSchema())
267
     @hapic.input_path(RevisionPreviewSizedPathSchema())
263
     @hapic.input_query(PageQuerySchema())
268
     @hapic.input_query(PageQuerySchema())