Browse Source

repair config + tests for revision jpg preview

Guénaël Muller 6 years ago
parent
commit
ad2fad6045
2 changed files with 98 additions and 12 deletions
  1. 17 10
      tracim/config.py
  2. 81 2
      tracim/tests/functional/test_contents.py

+ 17 - 10
tracim/config.py View File

@@ -411,25 +411,26 @@ class CFG(object):
411 411
         #     self.RADICALE_CLIENT_BASE_URL_HOST,
412 412
         #     self.RADICALE_CLIENT_BASE_URL_PREFIX,
413 413
         # )
414
-        preview_jpg_allowed_sizes = settings.get('preview.jpg.allowed_sizes', '')  # nopep8
415
-        self.PREVIEW_JPG_ALLOWED_SIZES = []
416
-        if preview_jpg_allowed_sizes:
417
-            for size in preview_jpg_allowed_sizes.split(','):
418
-                parts = preview_jpg_allowed_sizes.split('x')
414
+        preview_jpg_allowed_sizes_str = settings.get('preview.jpg.allowed_sizes', '')  # nopep8
415
+        allowed_size = []
416
+        if preview_jpg_allowed_sizes_str:
417
+            for sizes in preview_jpg_allowed_sizes_str.split(','):
418
+                parts = sizes.split('x')
419 419
                 assert len(parts) == 2
420 420
                 width, height = parts
421
-                assert width.is_decimal()
422
-                assert height.is_decimal()
421
+                assert width.isdecimal()
422
+                assert height.isdecimal()
423 423
                 size = PreviewSize(int(width), int(height))
424
-                self.PREVIEW_JPG_ALLOWED_SIZES.append(size)
424
+                allowed_size.append(size)
425 425
 
426 426
             self.PREVIEW_JPG_RESTRICTED_SIZES = asbool(settings.get(
427 427
                 'preview.jpg.restricted_sizes', False
428 428
             ))
429
-        if not self.PREVIEW_JPG_ALLOWED_SIZES:
429
+        if not allowed_size:
430 430
             size = PreviewSize(256, 256)
431
-            self.PREVIEW_JPG_ALLOWED_SIZES.append(size)
431
+            allowed_size.append(size)
432 432
 
433
+        self.PREVIEW_JPG_ALLOWED_SIZES = allowed_size
433 434
 
434 435
     def configure_filedepot(self):
435 436
         depot_storage_name = self.DEPOT_STORAGE_NAME
@@ -453,3 +454,9 @@ class PreviewSize(object):
453 454
     def __init__(self, width: int, height: int) -> None:
454 455
         self.width = width
455 456
         self.height = height
457
+
458
+    def __repr__(self):
459
+        return "<PreviewSize width:{width} height:{height}>".format(
460
+            width=self.width,
461
+            height=self.height,
462
+        )

+ 81 - 2
tracim/tests/functional/test_contents.py View File

@@ -1080,9 +1080,15 @@ class TestFiles(FunctionalTest):
1080 1080
             workspace=business_workspace,
1081 1081
             parent=tool_folder,
1082 1082
             label='Test file',
1083
-            do_save=True,
1083
+            do_save=False,
1084 1084
             do_notify=False,
1085 1085
         )
1086
+        test_file.file_extension = '.txt'
1087
+        test_file.depot_file = FileIntent(
1088
+            b'Test file',
1089
+            'Test_file.txt',
1090
+            'text/plain',
1091
+        )
1086 1092
         dbsession.flush()
1087 1093
         transaction.commit()
1088 1094
         content_id = int(test_file.content_id)
@@ -1097,7 +1103,7 @@ class TestFiles(FunctionalTest):
1097 1103
         res = self.testapp.put(
1098 1104
             '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1099 1105
             upload_files=[
1100
-                ('files',image.name, image.getvalue())
1106
+                ('files', image.name, image.getvalue())
1101 1107
             ],
1102 1108
             status=204,
1103 1109
         )
@@ -1163,6 +1169,79 @@ class TestFiles(FunctionalTest):
1163 1169
         new_image = Image.open(io.BytesIO(res.body))
1164 1170
         assert 256, 256 == new_image.size
1165 1171
 
1172
+    def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None:
1173
+        """
1174
+        get 256x256 revision preview of a txt file
1175
+        """
1176
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1177
+        admin = dbsession.query(models.User) \
1178
+            .filter(models.User.email == 'admin@admin.admin') \
1179
+            .one()
1180
+        workspace_api = WorkspaceApi(
1181
+            current_user=admin,
1182
+            session=dbsession,
1183
+            config=self.app_config
1184
+        )
1185
+        content_api = ContentApi(
1186
+            current_user=admin,
1187
+            session=dbsession,
1188
+            config=self.app_config
1189
+        )
1190
+        business_workspace = workspace_api.get_one(1)
1191
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1192
+        test_file = content_api.create(
1193
+            content_type=ContentType.File,
1194
+            workspace=business_workspace,
1195
+            parent=tool_folder,
1196
+            label='Test file',
1197
+            do_save=False,
1198
+            do_notify=False,
1199
+        )
1200
+        test_file.file_extension = '.txt'
1201
+        test_file.depot_file = FileIntent(
1202
+            b'Test file',
1203
+            'Test_file.txt',
1204
+            'text/plain',
1205
+        )
1206
+        dbsession.flush()
1207
+        transaction.commit()
1208
+        content_id = int(test_file.content_id)
1209
+        revision_id = int(test_file.revision_id)
1210
+        image = create_test_image()
1211
+        self.testapp.authorization = (
1212
+            'Basic',
1213
+            (
1214
+                'admin@admin.admin',
1215
+                'admin@admin.admin'
1216
+            )
1217
+        )
1218
+        res = self.testapp.put(
1219
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1220
+            upload_files=[
1221
+                ('files', image.name, image.getvalue())
1222
+            ],
1223
+            status=204,
1224
+        )
1225
+        res = self.testapp.get(
1226
+            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format(
1227
+                content_id=content_id,
1228
+                revision_id=revision_id,
1229
+            ),
1230
+            status=200
1231
+        )
1232
+        assert res.content_type == 'text/plain'
1233
+        res = self.testapp.get(
1234
+            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256'.format(
1235
+                content_id=content_id,
1236
+                revision_id=revision_id,
1237
+            ),
1238
+            status=200
1239
+        )
1240
+        assert res.body != image.getvalue()
1241
+        assert res.content_type == 'image/jpeg'
1242
+        new_image = Image.open(io.BytesIO(res.body))
1243
+        assert 256, 256 == new_image.size
1244
+
1166 1245
     def test_api__get_full_pdf_preview__ok__200__nominal_case(self) -> None:
1167 1246
         """
1168 1247
         get full pdf preview of a txt file