Code Duplication    Length = 54-63 lines in 3 locations

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

@@ 2369-2431 (lines=63) @@
2366
        assert res.body != image.getvalue()
2367
        assert res.content_type == 'image/jpeg'
2368
2369
    def test_api__get_jpeg_preview__ok__200__force_download_case(self) -> None:
2370
        """
2371
        Set one file of a content
2372
        """
2373
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2374
        admin = dbsession.query(models.User) \
2375
            .filter(models.User.email == '[email protected]') \
2376
            .one()
2377
        workspace_api = WorkspaceApi(
2378
            current_user=admin,
2379
            session=dbsession,
2380
            config=self.app_config
2381
        )
2382
        content_api = ContentApi(
2383
            current_user=admin,
2384
            session=dbsession,
2385
            config=self.app_config
2386
        )
2387
        business_workspace = workspace_api.get_one(1)
2388
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2389
        test_file = content_api.create(
2390
            content_type_slug=CONTENT_TYPES.File.slug,
2391
            workspace=business_workspace,
2392
            parent=tool_folder,
2393
            label='Test file',
2394
            do_save=False,
2395
            do_notify=False,
2396
        )
2397
        test_file.file_extension = '.txt'
2398
        test_file.depot_file = FileIntent(
2399
            b'Test file',
2400
            'Test_file.txt',
2401
            'text/plain',
2402
        )
2403
        dbsession.flush()
2404
        transaction.commit()
2405
        content_id = int(test_file.content_id)
2406
        image = create_1000px_png_test_image()
2407
        self.testapp.authorization = (
2408
            'Basic',
2409
            (
2410
                '[email protected]',
2411
                '[email protected]'
2412
            )
2413
        )
2414
        self.testapp.put(
2415
            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
2416
            upload_files=[
2417
                ('files', image.name, image.getvalue())
2418
            ],
2419
            status=204,
2420
        )
2421
        params = {
2422
            'force_download': 1,
2423
        }
2424
        res = self.testapp.get(
2425
            '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
2426
            status=200,
2427
            params=params
2428
        )
2429
        assert res.headers['Content-Disposition'] == 'attachment; filename="test_image_page_1.jpg"'  # nopep8
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
        """
@@ 2594-2652 (lines=59) @@
2591
            params=params,
2592
        )
2593
2594
    def test_api__get_sized_jpeg_preview__ok__200__force_download_case(self) -> None:
2595
        """
2596
        get 256x256 preview of a txt file
2597
        """
2598
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2599
        admin = dbsession.query(models.User) \
2600
            .filter(models.User.email == '[email protected]') \
2601
            .one()
2602
        workspace_api = WorkspaceApi(
2603
            current_user=admin,
2604
            session=dbsession,
2605
            config=self.app_config
2606
        )
2607
        content_api = ContentApi(
2608
            current_user=admin,
2609
            session=dbsession,
2610
            config=self.app_config
2611
        )
2612
        business_workspace = workspace_api.get_one(1)
2613
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2614
        test_file = content_api.create(
2615
            content_type_slug=CONTENT_TYPES.File.slug,
2616
            workspace=business_workspace,
2617
            parent=tool_folder,
2618
            label='Test file',
2619
            do_save=True,
2620
            do_notify=False,
2621
        )
2622
        dbsession.flush()
2623
        transaction.commit()
2624
        content_id = int(test_file.content_id)
2625
        image = create_1000px_png_test_image()
2626
        self.testapp.authorization = (
2627
            'Basic',
2628
            (
2629
                '[email protected]',
2630
                '[email protected]'
2631
            )
2632
        )
2633
        self.testapp.put(
2634
            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
2635
            upload_files=[
2636
                ('files', image.name, image.getvalue())
2637
            ],
2638
            status=204,
2639
        )
2640
        params = {
2641
            'force_download': 1,
2642
        }
2643
        res = self.testapp.get(
2644
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id),  # nopep8
2645
            status=200,
2646
            params=params,
2647
        )
2648
        assert res.body != image.getvalue()
2649
        assert res.headers['Content-Disposition'] == 'attachment; filename="test_image_page_1_256x256.jpg"'  # nopep8
2650
        assert res.content_type == 'image/jpeg'
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
        """
@@ 2486-2539 (lines=54) @@
2483
            params=params
2484
        )
2485
2486
    def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
2487
        """
2488
        get 256x256 preview of a txt file
2489
        """
2490
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2491
        admin = dbsession.query(models.User) \
2492
            .filter(models.User.email == '[email protected]') \
2493
            .one()
2494
        workspace_api = WorkspaceApi(
2495
            current_user=admin,
2496
            session=dbsession,
2497
            config=self.app_config
2498
        )
2499
        content_api = ContentApi(
2500
            current_user=admin,
2501
            session=dbsession,
2502
            config=self.app_config
2503
        )
2504
        business_workspace = workspace_api.get_one(1)
2505
        tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
2506
        test_file = content_api.create(
2507
            content_type_slug=CONTENT_TYPES.File.slug,
2508
            workspace=business_workspace,
2509
            parent=tool_folder,
2510
            label='Test file',
2511
            do_save=True,
2512
            do_notify=False,
2513
        )
2514
        dbsession.flush()
2515
        transaction.commit()
2516
        content_id = int(test_file.content_id)
2517
        image = create_1000px_png_test_image()
2518
        self.testapp.authorization = (
2519
            'Basic',
2520
            (
2521
                '[email protected]',
2522
                '[email protected]'
2523
            )
2524
        )
2525
        self.testapp.put(
2526
            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
2527
            upload_files=[
2528
                ('files', image.name, image.getvalue())
2529
            ],
2530
            status=204,
2531
        )
2532
        res = self.testapp.get(
2533
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id),  # nopep8
2534
            status=200
2535
        )
2536
        assert res.body != image.getvalue()
2537
        assert res.content_type == 'image/jpeg'
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
        """