Code Duplication    Length = 51-58 lines in 6 locations

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

@@ 3576-3633 (lines=58) @@
3573
        assert dim['width'] == 256
3574
        assert dim['height'] == 256
3575
3576
    def test_api__get_jpeg_preview__ok__200__nominal_case(self) -> None:
3577
        """
3578
        Set one file of a content
3579
        """
3580
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3581
        admin = dbsession.query(models.User) \
3582
            .filter(models.User.email == '[email protected]') \
3583
            .one()
3584
        workspace_api = WorkspaceApi(
3585
            current_user=admin,
3586
            session=dbsession,
3587
            config=self.app_config
3588
        )
3589
        content_api = ContentApi(
3590
            current_user=admin,
3591
            session=dbsession,
3592
            config=self.app_config
3593
        )
3594
        business_workspace = workspace_api.get_one(1)
3595
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3596
        test_file = content_api.create(
3597
            content_type_slug=content_type_list.File.slug,
3598
            workspace=business_workspace,
3599
            parent=tool_folder,
3600
            label='Test file',
3601
            do_save=False,
3602
            do_notify=False,
3603
        )
3604
        test_file.file_extension = '.txt'
3605
        test_file.depot_file = FileIntent(
3606
            b'Test file',
3607
            'Test_file.txt',
3608
            'text/plain',
3609
        )
3610
        dbsession.flush()
3611
        transaction.commit()
3612
        content_id = int(test_file.content_id)
3613
        image = create_1000px_png_test_image()
3614
        self.testapp.authorization = (
3615
            'Basic',
3616
            (
3617
                '[email protected]',
3618
                '[email protected]'
3619
            )
3620
        )
3621
        self.testapp.put(
3622
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3623
            upload_files=[
3624
                ('files', image.name, image.getvalue())
3625
            ],
3626
            status=204,
3627
        )
3628
        res = self.testapp.get(
3629
            '/api/v2/workspaces/1/files/{}/preview/jpg/'.format(content_id),
3630
            status=200
3631
        )
3632
        assert res.body != image.getvalue()
3633
        assert res.content_type == 'image/jpeg'
3634
3635
    def test_api__get_jpeg_preview__ok__200__force_download_case(self) -> None:
3636
        """
@@ 4060-4113 (lines=54) @@
4057
        new_image = Image.open(io.BytesIO(res.body))
4058
        assert 256, 256 == new_image.size
4059
4060
    def test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed(self) -> None:  # nopep8
4061
        """
4062
        get 256x256 preview of a txt file
4063
        """
4064
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4065
        admin = dbsession.query(models.User) \
4066
            .filter(models.User.email == '[email protected]') \
4067
            .one()
4068
        workspace_api = WorkspaceApi(
4069
            current_user=admin,
4070
            session=dbsession,
4071
            config=self.app_config
4072
        )
4073
        content_api = ContentApi(
4074
            current_user=admin,
4075
            session=dbsession,
4076
            config=self.app_config
4077
        )
4078
        business_workspace = workspace_api.get_one(1)
4079
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4080
        test_file = content_api.create(
4081
            content_type_slug=content_type_list.File.slug,
4082
            workspace=business_workspace,
4083
            parent=tool_folder,
4084
            label='Test file',
4085
            do_save=True,
4086
            do_notify=False,
4087
        )
4088
        dbsession.flush()
4089
        transaction.commit()
4090
        content_id = int(test_file.content_id)
4091
        image = create_1000px_png_test_image()
4092
        self.testapp.authorization = (
4093
            'Basic',
4094
            (
4095
                '[email protected]',
4096
                '[email protected]'
4097
            )
4098
        )
4099
        self.testapp.put(
4100
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4101
            upload_files=[
4102
                ('files', image.name, image.getvalue())
4103
            ],
4104
            status=204,
4105
        )
4106
        filename = 'test_image_512x512.jpg'
4107
        res = self.testapp.get(
4108
            '/api/v2/workspaces/1/files/{}/preview/jpg/512x512/{}'.format(content_id, filename),  # nopep8
4109
            status=400
4110
        )
4111
        assert res.json_body
4112
        assert 'code' in res.json_body
4113
        assert res.json_body['code'] == error.PREVIEW_DIM_NOT_ALLOWED
4114
4115
    def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None:  # nopep8
4116
        """
@@ 3761-3814 (lines=54) @@
3758
        assert 'code' in res.json.keys()
3759
        assert res.json_body['code'] == error.UNAIVALABLE_PREVIEW
3760
3761
    def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
3762
        """
3763
        get 256x256 preview of a txt file
3764
        """
3765
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3766
        admin = dbsession.query(models.User) \
3767
            .filter(models.User.email == '[email protected]') \
3768
            .one()
3769
        workspace_api = WorkspaceApi(
3770
            current_user=admin,
3771
            session=dbsession,
3772
            config=self.app_config
3773
        )
3774
        content_api = ContentApi(
3775
            current_user=admin,
3776
            session=dbsession,
3777
            config=self.app_config
3778
        )
3779
        business_workspace = workspace_api.get_one(1)
3780
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3781
        test_file = content_api.create(
3782
            content_type_slug=content_type_list.File.slug,
3783
            workspace=business_workspace,
3784
            parent=tool_folder,
3785
            label='Test file',
3786
            do_save=True,
3787
            do_notify=False,
3788
        )
3789
        dbsession.flush()
3790
        transaction.commit()
3791
        content_id = int(test_file.content_id)
3792
        image = create_1000px_png_test_image()
3793
        self.testapp.authorization = (
3794
            'Basic',
3795
            (
3796
                '[email protected]',
3797
                '[email protected]'
3798
            )
3799
        )
3800
        self.testapp.put(
3801
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3802
            upload_files=[
3803
                ('files', image.name, image.getvalue())
3804
            ],
3805
            status=204,
3806
        )
3807
        res = self.testapp.get(
3808
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/{}'.format(content_id, image.name),  # nopep8
3809
            status=200
3810
        )
3811
        assert res.body != image.getvalue()
3812
        assert res.content_type == 'image/jpeg'
3813
        new_image = Image.open(io.BytesIO(res.body))
3814
        assert 256, 256 == new_image.size
3815
3816
    def test_api__get_sized_jpeg_preview__err_400__UnavailablePreview(self) -> None:
3817
        """
@@ 4415-4467 (lines=53) @@
4412
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
4413
        assert res.content_type == 'application/pdf'
4414
4415
    def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None:  # nopep8
4416
        """
4417
       get full pdf preview of a png image -> error UnavailablePreviewType
4418
        """
4419
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4420
        admin = dbsession.query(models.User) \
4421
            .filter(models.User.email == '[email protected]') \
4422
            .one()
4423
        workspace_api = WorkspaceApi(
4424
            current_user=admin,
4425
            session=dbsession,
4426
            config=self.app_config
4427
        )
4428
        content_api = ContentApi(
4429
            current_user=admin,
4430
            session=dbsession,
4431
            config=self.app_config
4432
        )
4433
        business_workspace = workspace_api.get_one(1)
4434
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4435
        test_file = content_api.create(
4436
            content_type_slug=content_type_list.File.slug,
4437
            workspace=business_workspace,
4438
            parent=tool_folder,
4439
            label='Test file',
4440
            do_save=True,
4441
            do_notify=False,
4442
        )
4443
        dbsession.flush()
4444
        transaction.commit()
4445
        content_id = int(test_file.content_id)
4446
        image = create_1000px_png_test_image()
4447
        self.testapp.authorization = (
4448
            'Basic',
4449
            (
4450
                '[email protected]',
4451
                '[email protected]'
4452
            )
4453
        )
4454
        self.testapp.put(
4455
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4456
            upload_files=[
4457
                ('files', image.name, image.getvalue())
4458
            ],
4459
            status=204,
4460
        )
4461
        res = self.testapp.get(
4462
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, image.name),  # nopep8
4463
            status=400
4464
        )
4465
        assert res.json_body
4466
        assert 'code' in res.json_body
4467
        assert res.json_body['code'] == error.UNAVAILABLE_PREVIEW_TYPE
4468
4469
    def test_api__get_full_pdf_preview__err__400__png_UnavailablePreview(self) -> None:  # nopep8
4470
        """
@@ 3281-3333 (lines=53) @@
3278
        assert 'code' in res.json.keys()
3279
        assert res.json_body['code'] == error.PARENT_NOT_FOUND
3280
3281
    def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
3282
        """
3283
        Set one file of a content
3284
        """
3285
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3286
        admin = dbsession.query(models.User) \
3287
            .filter(models.User.email == '[email protected]') \
3288
            .one()
3289
        workspace_api = WorkspaceApi(
3290
            current_user=admin,
3291
            session=dbsession,
3292
            config=self.app_config
3293
        )
3294
        content_api = ContentApi(
3295
            current_user=admin,
3296
            session=dbsession,
3297
            config=self.app_config
3298
        )
3299
        business_workspace = workspace_api.get_one(1)
3300
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3301
        test_file = content_api.create(
3302
            content_type_slug=content_type_list.File.slug,
3303
            workspace=business_workspace,
3304
            parent=tool_folder,
3305
            label='Test file',
3306
            do_save=False,
3307
            do_notify=False,
3308
        )
3309
        dbsession.flush()
3310
        transaction.commit()
3311
        content_id = int(test_file.content_id)
3312
        image = create_1000px_png_test_image()
3313
        self.testapp.authorization = (
3314
            'Basic',
3315
            (
3316
                '[email protected]',
3317
                '[email protected]'
3318
            )
3319
        )
3320
        self.testapp.put(
3321
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3322
            upload_files=[
3323
                ('files', image.name, image.getvalue())
3324
            ],
3325
            status=204,
3326
        )
3327
        res = self.testapp.get(
3328
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3329
            status=200
3330
        )
3331
        assert res.body == image.getvalue()
3332
        assert res.content_type == 'image/png'
3333
        assert res.content_length == len(image.getvalue())
3334
3335
    def test_api__set_file_raw__ok_200__filename_already_used(self) -> None:
3336
        """
@@ 3402-3452 (lines=51) @@
3399
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER  # nopep8
3400
3401
3402
    def test_api__set_file_raw__err_400__closed_status_file(self) -> None:
3403
        """
3404
        Set one file of a content
3405
        """
3406
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3407
        admin = dbsession.query(models.User) \
3408
            .filter(models.User.email == '[email protected]') \
3409
            .one()
3410
        workspace_api = WorkspaceApi(
3411
            current_user=admin,
3412
            session=dbsession,
3413
            config=self.app_config
3414
        )
3415
        content_api = ContentApi(
3416
            current_user=admin,
3417
            session=dbsession,
3418
            config=self.app_config
3419
        )
3420
        business_workspace = workspace_api.get_one(1)
3421
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3422
        test_file = content_api.create(
3423
            content_type_slug=content_type_list.File.slug,
3424
            workspace=business_workspace,
3425
            parent=tool_folder,
3426
            label='Test file',
3427
            do_save=False,
3428
            do_notify=False,
3429
        )
3430
        test_file.status = 'closed-validated'
3431
        content_api.save(test_file)
3432
        dbsession.flush()
3433
        transaction.commit()
3434
        content_id = int(test_file.content_id)
3435
        image = create_1000px_png_test_image()
3436
        self.testapp.authorization = (
3437
            'Basic',
3438
            (
3439
                '[email protected]',
3440
                '[email protected]'
3441
            )
3442
        )
3443
        res = self.testapp.put(
3444
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3445
            upload_files=[
3446
                ('files', image.name, image.getvalue())
3447
            ],
3448
            status=400,
3449
        )
3450
        assert isinstance(res.json, dict)
3451
        assert 'code' in res.json.keys()
3452
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
3453
3454
    @pytest.mark.xfail(
3455
        raises=AssertionError,