Code Duplication    Length = 47-51 lines in 6 locations

backend/tracim_backend/tests/functional/test_contents.py 6 locations

@@ 2541-2591 (lines=51) @@
2538
        new_image = Image.open(io.BytesIO(res.body))
2539
        assert 256, 256 == new_image.size
2540
2541
    def test_api__get_sized_jpeg_preview__err_400__UnavailablePreview(self) -> None:
2542
        """
2543
        Set one file of a content
2544
        """
2545
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2546
        admin = dbsession.query(models.User) \
2547
            .filter(models.User.email == '[email protected]') \
2548
            .one()
2549
        workspace_api = WorkspaceApi(
2550
            current_user=admin,
2551
            session=dbsession,
2552
            config=self.app_config
2553
        )
2554
        content_api = ContentApi(
2555
            current_user=admin,
2556
            session=dbsession,
2557
            config=self.app_config
2558
        )
2559
        business_workspace = workspace_api.get_one(1)
2560
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2561
        test_file = content_api.create(
2562
            content_type_slug=CONTENT_TYPES.File.slug,
2563
            workspace=business_workspace,
2564
            parent=tool_folder,
2565
            label='Test file',
2566
            do_save=False,
2567
            do_notify=False,
2568
        )
2569
        content_api.update_file_data(
2570
            test_file,
2571
            'Test_file.bin',
2572
            new_mimetype='application/octet-stream',
2573
            new_content=bytes(100),
2574
        )
2575
        dbsession.flush()
2576
        transaction.commit()
2577
        content_id = int(test_file.content_id)
2578
        self.testapp.authorization = (
2579
            'Basic',
2580
            (
2581
                '[email protected]',
2582
                '[email protected]'
2583
            )
2584
        )
2585
        params = {
2586
            'force_download': 0,
2587
        }
2588
        res = self.testapp.get(
2589
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id),  # nopep8
2590
            status=400,
2591
            params=params,
2592
        )
2593
2594
    def test_api__get_sized_jpeg_preview__ok__200__force_download_case(self) -> None:
@@ 2433-2483 (lines=51) @@
2430
        assert res.body != image.getvalue()
2431
        assert res.content_type == 'image/jpeg'
2432
2433
    def test_api__get_jpeg_preview__err_400__UnavailablePreview(self) -> None:
2434
        """
2435
        Set one file of a content
2436
        """
2437
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2438
        admin = dbsession.query(models.User) \
2439
            .filter(models.User.email == '[email protected]') \
2440
            .one()
2441
        workspace_api = WorkspaceApi(
2442
            current_user=admin,
2443
            session=dbsession,
2444
            config=self.app_config
2445
        )
2446
        content_api = ContentApi(
2447
            current_user=admin,
2448
            session=dbsession,
2449
            config=self.app_config
2450
        )
2451
        business_workspace = workspace_api.get_one(1)
2452
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2453
        test_file = content_api.create(
2454
            content_type_slug=CONTENT_TYPES.File.slug,
2455
            workspace=business_workspace,
2456
            parent=tool_folder,
2457
            label='Test file',
2458
            do_save=False,
2459
            do_notify=False,
2460
        )
2461
        content_api.update_file_data(
2462
            test_file,
2463
            'Test_file.bin',
2464
            new_mimetype='application/octet-stream',
2465
            new_content=bytes(100),
2466
        )
2467
        dbsession.flush()
2468
        transaction.commit()
2469
        content_id = int(test_file.content_id)
2470
        self.testapp.authorization = (
2471
            'Basic',
2472
            (
2473
                '[email protected]',
2474
                '[email protected]'
2475
            )
2476
        )
2477
        params = {
2478
            'force_download': 0,
2479
        }
2480
        res = self.testapp.get(
2481
            '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
2482
            status=400,
2483
            params=params
2484
        )
2485
2486
    def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
@@ 3152-3200 (lines=49) @@
3149
        )
3150
        assert res.content_type == 'application/pdf'
3151
3152
    def test_api__get_pdf_preview_err__400__UnavailablePreview(self) -> None:
3153
        """
3154
        get full pdf preview of a txt file
3155
        """
3156
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3157
        admin = dbsession.query(models.User) \
3158
            .filter(models.User.email == '[email protected]') \
3159
            .one()
3160
        workspace_api = WorkspaceApi(
3161
            current_user=admin,
3162
            session=dbsession,
3163
            config=self.app_config
3164
        )
3165
        content_api = ContentApi(
3166
            current_user=admin,
3167
            session=dbsession,
3168
            config=self.app_config
3169
        )
3170
        business_workspace = workspace_api.get_one(1)
3171
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
3172
        test_file = content_api.create(
3173
            content_type_slug=CONTENT_TYPES.File.slug,
3174
            workspace=business_workspace,
3175
            parent=tool_folder,
3176
            label='Test file',
3177
            do_save=False,
3178
            do_notify=False,
3179
        )
3180
        content_api.update_file_data(
3181
            test_file,
3182
            'Test_file.bin',
3183
            new_mimetype='application/octet-stream',
3184
            new_content=bytes(100),
3185
        )
3186
        dbsession.flush()
3187
        transaction.commit()
3188
        content_id = int(test_file.content_id)
3189
        self.testapp.authorization = (
3190
            'Basic',
3191
            (
3192
                '[email protected]',
3193
                '[email protected]'
3194
            )
3195
        )
3196
        params = {'page': 1}
3197
        res = self.testapp.get(
3198
            '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
3199
            status=400,
3200
            params=params,
3201
        )
3202
3203
    def test_api__get_pdf_preview__ok__200__force_download_case(self) -> None:
@@ 2987-3035 (lines=49) @@
2984
        assert res.headers['Content-Disposition'] == 'attachment; filename="Test_file.pdf"'  # nopep8
2985
        assert res.content_type == 'application/pdf'
2986
2987
    def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None:  # nopep8
2988
        """
2989
       get full pdf preview of a png image -> error UnavailablePreviewType
2990
        """
2991
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2992
        admin = dbsession.query(models.User) \
2993
            .filter(models.User.email == '[email protected]') \
2994
            .one()
2995
        workspace_api = WorkspaceApi(
2996
            current_user=admin,
2997
            session=dbsession,
2998
            config=self.app_config
2999
        )
3000
        content_api = ContentApi(
3001
            current_user=admin,
3002
            session=dbsession,
3003
            config=self.app_config
3004
        )
3005
        business_workspace = workspace_api.get_one(1)
3006
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
3007
        test_file = content_api.create(
3008
            content_type_slug=CONTENT_TYPES.File.slug,
3009
            workspace=business_workspace,
3010
            parent=tool_folder,
3011
            label='Test file',
3012
            do_save=True,
3013
            do_notify=False,
3014
        )
3015
        dbsession.flush()
3016
        transaction.commit()
3017
        content_id = int(test_file.content_id)
3018
        image = create_1000px_png_test_image()
3019
        self.testapp.authorization = (
3020
            'Basic',
3021
            (
3022
                '[email protected]',
3023
                '[email protected]'
3024
            )
3025
        )
3026
        self.testapp.put(
3027
            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
3028
            upload_files=[
3029
                ('files', image.name, image.getvalue())
3030
            ],
3031
            status=204,
3032
        )
3033
        self.testapp.get(
3034
            '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id),  # nopep8
3035
            status=400
3036
        )
3037
3038
    def test_api__get_full_pdf_preview__err__400__png_UnavailablePreview(self) -> None:  # nopep8
@@ 2654-2702 (lines=49) @@
2651
        new_image = Image.open(io.BytesIO(res.body))
2652
        assert 256, 256 == new_image.size
2653
2654
    def test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed(self) -> None:  # nopep8
2655
        """
2656
        get 256x256 preview of a txt file
2657
        """
2658
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2659
        admin = dbsession.query(models.User) \
2660
            .filter(models.User.email == '[email protected]') \
2661
            .one()
2662
        workspace_api = WorkspaceApi(
2663
            current_user=admin,
2664
            session=dbsession,
2665
            config=self.app_config
2666
        )
2667
        content_api = ContentApi(
2668
            current_user=admin,
2669
            session=dbsession,
2670
            config=self.app_config
2671
        )
2672
        business_workspace = workspace_api.get_one(1)
2673
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2674
        test_file = content_api.create(
2675
            content_type_slug=CONTENT_TYPES.File.slug,
2676
            workspace=business_workspace,
2677
            parent=tool_folder,
2678
            label='Test file',
2679
            do_save=True,
2680
            do_notify=False,
2681
        )
2682
        dbsession.flush()
2683
        transaction.commit()
2684
        content_id = int(test_file.content_id)
2685
        image = create_1000px_png_test_image()
2686
        self.testapp.authorization = (
2687
            'Basic',
2688
            (
2689
                '[email protected]',
2690
                '[email protected]'
2691
            )
2692
        )
2693
        self.testapp.put(
2694
            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
2695
            upload_files=[
2696
                ('files', image.name, image.getvalue())
2697
            ],
2698
            status=204,
2699
        )
2700
        self.testapp.get(
2701
            '/api/v2/workspaces/1/files/{}/preview/jpg/512x512'.format(content_id),  # nopep8
2702
            status=400
2703
        )
2704
2705
    def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None:  # nopep8
@@ 3038-3084 (lines=47) @@
3035
            status=400
3036
        )
3037
3038
    def test_api__get_full_pdf_preview__err__400__png_UnavailablePreview(self) -> None:  # nopep8
3039
        """
3040
       get full pdf preview of a png image -> error UnavailablePreviewType
3041
        """
3042
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3043
        admin = dbsession.query(models.User) \
3044
            .filter(models.User.email == '[email protected]') \
3045
            .one()
3046
        workspace_api = WorkspaceApi(
3047
            current_user=admin,
3048
            session=dbsession,
3049
            config=self.app_config
3050
        )
3051
        content_api = ContentApi(
3052
            current_user=admin,
3053
            session=dbsession,
3054
            config=self.app_config
3055
        )
3056
        business_workspace = workspace_api.get_one(1)
3057
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
3058
        test_file = content_api.create(
3059
            content_type_slug=CONTENT_TYPES.File.slug,
3060
            workspace=business_workspace,
3061
            parent=tool_folder,
3062
            label='Test file',
3063
            do_save=False,
3064
            do_notify=False,
3065
        )
3066
        content_api.update_file_data(
3067
            test_file,
3068
            'Test_file.bin',
3069
            new_mimetype='application/octet-stream',
3070
            new_content=bytes(100),
3071
        )
3072
        dbsession.flush()
3073
        content_id = test_file.content_id
3074
        transaction.commit()
3075
        self.testapp.authorization = (
3076
            'Basic',
3077
            (
3078
                '[email protected]',
3079
                '[email protected]'
3080
            )
3081
        )
3082
        self.testapp.get(
3083
            '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id),  # nopep8
3084
            status=400
3085
        )
3086
3087
    def test_api__get_pdf_preview__ok__200__nominal_case(self) -> None: