MorphGuard / tests /test_demorph_api.py
juanquy's picture
Initial clean commit of modular MorphGuard
2978bba
Raw
History Blame Contribute Delete
1.33 kB
import pytest
import io
from unittest.mock import patch, MagicMock
def test_demorph_no_image(client):
"""Test /api/demorph returns 400 when no image is provided."""
response = client.post('/api/demorph')
assert response.status_code == 400
assert 'No image provided' in response.get_json()['error']
def test_demorph_success(client, app):
"""Test /api/demorph successfully runs detection and demorphing."""
data = {
'image': (io.BytesIO(b"dummy image data"), 'morph.jpg'),
'method': 'transformer',
'prompt': 'restore face'
}
with patch('src.routes.demorph.os.makedirs'), \
patch('src.routes.demorph.time.time', return_value=123456789):
response = client.post('/api/demorph', data=data, content_type='multipart/form-data')
assert response.status_code == 200
json_data = response.get_json()
assert json_data['status'] == 'success'
assert json_data['is_morphed'] is True
assert json_data['detection_confidence'] == 0.88
assert json_data['method'] == 'transformer'
assert 'output_url' in json_data
# Verify both detection and demorphing were called on the API
app.mg_api.detect_morph.assert_called_once()
app.mg_api.demorph_image.assert_called_once()