Fix symatically wrong usage in tests (#1075)

* fix symatically wrong usage in tests

* add target-240p.jpg everywhere

* combine both target
This commit is contained in:
Harisreedhar
2026-04-07 22:41:19 +05:30
committed by GitHub
parent cdd7c25586
commit ee5fde1c60
+77 -53
View File
@@ -1,3 +1,4 @@
import subprocess
import tempfile
from typing import Iterator
@@ -19,6 +20,7 @@ def before_all() -> None:
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.mp3',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])
@pytest.fixture(scope = 'module')
@@ -35,7 +37,6 @@ def before_each() -> None:
asset_store.clear()
#todo: target file uploaded as type=source is semantically wrong, split into separate source and target uploads
def test_upload_asset(test_client : TestClient) -> None:
upload_response = test_client.post('/assets?type=source')
@@ -50,49 +51,56 @@ def test_upload_asset(test_client : TestClient) -> None:
session_id = session_manager.find_session_id(access_token)
source_path = get_test_example_file('source.jpg')
target_path = get_test_example_file('target-240p.mp4')
target_image_path = get_test_example_file('target-240p.jpg')
target_video_path = get_test_example_file('target-240p.mp4')
with open(source_path, 'rb') as source_file, open(target_path, 'rb') as target_file:
source_content = source_file.read()
target_content = target_file.read()
with open(source_path, 'rb') as source_file:
upload_response = test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.jpg', source_content, 'image/jpeg')),
('file', ('target.mp4', target_content, 'video/mp4'))
('file', ('source.jpg', source_file.read(), 'image/jpeg'))
])
upload_body = upload_response.json()
asset_ids = upload_body.get('asset_ids')
asset_ids = upload_response.json().get('asset_ids')
asset = asset_store.get_asset(session_id, asset_ids[0])
assert asset.get('media') == 'image'
assert asset.get('type') == 'source'
assert asset.get('format') == 'jpeg'
assert upload_response.status_code == 201
asset = asset_store.get_asset(session_id, asset_ids[1])
assert asset.get('media') == 'video'
assert asset.get('type') == 'source'
assert asset.get('format') == 'mp4'
with open(target_image_path, 'rb') as target_image_file, open(target_video_path, 'rb') as target_video_file:
upload_response = test_client.post('/assets?type=target', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('target-240p.jpg', target_image_file.read(), 'image/jpeg')),
('file', ('target-240p.mp4', target_video_file.read(), 'video/mp4'))
])
asset_ids = upload_response.json().get('asset_ids')
assert asset_store.get_asset(session_id, asset_ids[0]).get('media') == 'image'
assert asset_store.get_asset(session_id, asset_ids[0]).get('type') == 'target'
assert asset_store.get_asset(session_id, asset_ids[0]).get('format') == 'jpeg'
assert asset_store.get_asset(session_id, asset_ids[1]).get('media') == 'video'
assert asset_store.get_asset(session_id, asset_ids[1]).get('type') == 'target'
assert asset_store.get_asset(session_id, asset_ids[1]).get('format') == 'mp4'
assert upload_response.status_code == 201
audio_path = get_test_example_file('source.mp3')
with open(audio_path, 'rb') as audio_file:
audio_content = audio_file.read()
upload_response = test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.mp3', audio_content, 'audio/mpeg'))
('file', ('source.mp3', audio_file.read(), 'audio/mpeg'))
])
upload_body = upload_response.json()
asset = asset_store.get_asset(session_id, upload_body.get('asset_ids')[0])
asset_ids = upload_response.json().get('asset_ids')
asset = asset_store.get_asset(session_id, asset_ids[0])
assert asset.get('media') == 'audio'
assert asset.get('type') == 'source'
@@ -123,7 +131,6 @@ def test_upload_asset(test_client : TestClient) -> None:
assert upload_response.status_code == 415
#todo: target file uploaded as type=source is semantically wrong, split into separate source and target uploads
def test_get_assets(test_client : TestClient) -> None:
get_response = test_client.get('/assets')
@@ -147,18 +154,26 @@ def test_get_assets(test_client : TestClient) -> None:
assert get_response.status_code == 200
source_path = get_test_example_file('source.jpg')
target_path = get_test_example_file('target-240p.mp4')
target_image_path = get_test_example_file('target-240p.jpg')
target_video_path = get_test_example_file('target-240p.mp4')
with open(source_path, 'rb') as source_file, open(target_path, 'rb') as target_file:
source_content = source_file.read()
target_content = target_file.read()
with open(source_path, 'rb') as source_file:
test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.jpg', source_content, 'image/jpeg')),
('file', ('target.mp4', target_content, 'video/mp4'))
('file', ('source.jpg', source_file.read(), 'image/jpeg'))
])
with open(target_image_path, 'rb') as target_image_file, open(target_video_path, 'rb') as target_video_file:
test_client.post('/assets?type=target', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('target-240p.jpg', target_image_file.read(), 'image/jpeg')),
('file', ('target-240p.mp4', target_video_file.read(), 'video/mp4'))
])
get_response = test_client.get('/assets', headers =
@@ -168,9 +183,10 @@ def test_get_assets(test_client : TestClient) -> None:
get_body = get_response.json()
assets = get_body.get('assets')
assert len(assets) == 2
assert len(assets) == 3
assert assets[0].get('media') == 'image'
assert assets[1].get('media') == 'video'
assert assets[1].get('media') == 'image'
assert assets[2].get('media') == 'video'
assert get_response.status_code == 200
@@ -190,16 +206,14 @@ def test_get_asset(test_client : TestClient) -> None:
source_path = get_test_example_file('source.jpg')
with open(source_path, 'rb') as source_file:
source_content = source_file.read()
upload_response = test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.jpg', source_content, 'image/jpeg'))
('file', ('source.jpg', source_file.read(), 'image/jpeg'))
])
upload_body = upload_response.json()
asset_id = upload_body.get('asset_ids')[0]
asset_ids = upload_response.json().get('asset_ids')
second_session_response = test_client.post('/session', json =
{
@@ -208,20 +222,20 @@ def test_get_asset(test_client : TestClient) -> None:
second_session_body = second_session_response.json()
second_access_token = second_session_body.get('access_token')
get_response = test_client.get('/assets/' + asset_id, headers =
get_response = test_client.get('/assets/' + asset_ids[0], headers =
{
'Authorization': 'Bearer ' + second_access_token
})
assert get_response.status_code == 404
get_response = test_client.get('/assets/' + asset_id, headers =
get_response = test_client.get('/assets/' + asset_ids[0], headers =
{
'Authorization': 'Bearer ' + access_token
})
get_body = get_response.json()
assert get_body.get('id') == asset_id
assert get_body.get('id') == asset_ids[0]
assert get_body.get('type') == 'source'
assert get_body.get('media') == 'image'
assert get_body.get('format') == 'jpeg'
@@ -242,18 +256,16 @@ def test_delete_assets(test_client : TestClient) -> None:
source_path = get_test_example_file('source.jpg')
with open(source_path, 'rb') as source_file:
source_content = source_file.read()
upload_response = test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.jpg', source_content, 'image/jpeg'))
('file', ('source.jpg', source_file.read(), 'image/jpeg'))
])
upload_body = upload_response.json()
asset_id = upload_body.get('asset_ids')[0]
asset_ids = upload_response.json().get('asset_ids')
assert asset_store.get_asset(session_id, asset_id)
assert asset_store.get_asset(session_id, asset_ids[0])
second_session_response = test_client.post('/session', json =
{
@@ -267,7 +279,7 @@ def test_delete_assets(test_client : TestClient) -> None:
'Authorization': 'Bearer ' + second_access_token
}, json =
{
'asset_ids': [ asset_id ]
'asset_ids': asset_ids
})
assert delete_response.status_code == 404
@@ -277,7 +289,7 @@ def test_delete_assets(test_client : TestClient) -> None:
'Authorization': 'Bearer ' + access_token
}, json =
{
'asset_ids': [ asset_id ]
'asset_ids': asset_ids
})
assert delete_response.status_code == 200
@@ -287,16 +299,16 @@ def test_delete_assets(test_client : TestClient) -> None:
'Authorization': 'Bearer ' + access_token
}, json =
{
'asset_ids': [ asset_id ]
'asset_ids': asset_ids
})
assert delete_response.status_code == 404
#todo: target file uploaded as type=source is semantically wrong, upload as type=target with target_paths
def test_upload_asset_security_strategies(test_client : TestClient) -> None:
source_path = get_test_example_file('source.jpg')
target_path = get_test_example_file('target-240p.mp4')
target_image_path = get_test_example_file('target-240p.jpg')
target_video_path = get_test_example_file('target-240p.mp4')
for strategy in [ 'strict', 'moderate' ]:
state_manager.init_item('api_security_strategy', strategy)
@@ -308,21 +320,33 @@ def test_upload_asset_security_strategies(test_client : TestClient) -> None:
access_token = create_session_response.json().get('access_token')
session_id = session_manager.find_session_id(access_token)
with open(source_path, 'rb') as source_file, open(target_path, 'rb') as target_file:
upload_response = test_client.post('/assets?type=source', headers =
with open(source_path, 'rb') as source_file:
source_upload_response = test_client.post('/assets?type=source', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('source.jpg', source_file.read(), 'image/jpeg')),
('file', ('target.mp4', target_file.read(), 'video/mp4'))
('file', ('source.jpg', source_file.read(), 'image/jpeg'))
])
assert upload_response.status_code == 201
with open(target_image_path, 'rb') as target_image_file, open(target_video_path, 'rb') as target_video_file:
target_upload_response = test_client.post('/assets?type=target', headers =
{
'Authorization': 'Bearer ' + access_token
}, files =
[
('file', ('target-240p.jpg', target_image_file.read(), 'image/jpeg')),
('file', ('target-240p.mp4', target_video_file.read(), 'video/mp4'))
])
asset_ids = upload_response.json().get('asset_ids')
assert source_upload_response.status_code == 201
assert target_upload_response.status_code == 201
assert asset_store.get_asset(session_id, asset_ids[0]).get('media') == 'image'
assert asset_store.get_asset(session_id, asset_ids[1]).get('media') == 'video'
source_asset_id = source_upload_response.json().get('asset_ids')[0]
target_asset_ids = target_upload_response.json().get('asset_ids')
assert asset_store.get_asset(session_id, source_asset_id).get('media') == 'image'
assert asset_store.get_asset(session_id, target_asset_ids[0]).get('media') == 'image'
assert asset_store.get_asset(session_id, target_asset_ids[1]).get('media') == 'video'
state_manager.init_item('api_security_strategy', 'strict')