Spaces:
Sleeping
Sleeping
| import pytest | |
| import io | |
| from unittest.mock import patch, MagicMock | |
| def test_detect_unauthorized(client): | |
| """Test that detection endpoints require authorization.""" | |
| response = client.post('/api/detect') | |
| assert response.status_code == 401 | |
| assert 'Unauthorized' in response.get_json()['error'] | |
| response = client.post('/api/detect/deepfake') | |
| assert response.status_code == 401 | |
| assert 'Unauthorized' in response.get_json()['error'] | |
| def test_detect_no_image(client): | |
| """Test /api/detect with authorized session but no image.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| sess['role'] = 'admin' | |
| response = client.post('/api/detect') | |
| assert response.status_code == 400 | |
| assert 'No image provided' in response.get_json()['error'] | |
| def test_detect_success(client, app): | |
| """Test /api/detect successfully detects morphing.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| sess['role'] = 'admin' | |
| # Create a mock image file | |
| data = { | |
| 'image': (io.BytesIO(b"dummy image data"), 'test.jpg'), | |
| 'mode': 'fast', | |
| 'detection_type': 'morph' | |
| } | |
| with patch('src.routes.detection.secure_filename', return_value='test.jpg'), \ | |
| patch('src.routes.detection.os.path.join', return_value='static/uploads/test.jpg'), \ | |
| patch('src.routes.detection.open', create=True) as mock_open: | |
| # Mocking file save | |
| mock_file = MagicMock() | |
| data['image'] = (mock_file, 'test.jpg') | |
| response = client.post('/api/detect', data=data, content_type='multipart/form-data') | |
| assert response.status_code == 200 | |
| json_data = response.get_json() | |
| assert json_data['is_morphed'] is True | |
| assert json_data['confidence'] == 0.88 | |
| assert json_data['tier'] == 'Tier1_Fast' | |
| app.mg_api.detect_morph.assert_called_once() | |
| def test_detect_deepfake_standalone_success(client, app): | |
| """Test /api/detect/deepfake standalone endpoint.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| data = { | |
| 'image': (io.BytesIO(b"dummy image data"), 'test.jpg') | |
| } | |
| with patch('src.routes.detection.secure_filename', return_value='test.jpg'), \ | |
| patch('src.routes.detection.os.path.join', return_value='static/uploads/test.jpg'): | |
| response = client.post('/api/detect/deepfake', data=data, content_type='multipart/form-data') | |
| assert response.status_code == 200 | |
| json_data = response.get_json() | |
| assert json_data['is_deepfake'] is True | |
| assert json_data['deepfake_score'] == 0.95 | |
| assert 'artifacts' in json_data | |
| app.deepfake_detector.detect.assert_called_once() | |
| def test_detect_invalid_file_extension(client): | |
| """Test /api/detect returns 400 for invalid file extensions.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| data = { | |
| 'image': (io.BytesIO(b"dummy data"), 'test.txt') | |
| } | |
| response = client.post('/api/detect', data=data, content_type='multipart/form-data') | |
| assert response.status_code == 400 | |
| assert 'Invalid file type' in response.get_json()['error'] | |
| def test_detect_invalid_mode(client): | |
| """Test /api/detect returns 400 for invalid modes.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| data = { | |
| 'image': (io.BytesIO(b"dummy data"), 'test.jpg'), | |
| 'mode': 'super_deep' | |
| } | |
| response = client.post('/api/detect', data=data, content_type='multipart/form-data') | |
| assert response.status_code == 400 | |
| assert 'Invalid mode' in response.get_json()['error'] | |
| def test_detect_v1_success(client, app): | |
| """Test /api/v1/detect successfully detects morphing.""" | |
| with client.session_transaction() as sess: | |
| sess['logged_in'] = True | |
| data = { | |
| 'image': (io.BytesIO(b"dummy image data"), 'test.jpg'), | |
| 'mode': 'fast' | |
| } | |
| with patch('src.routes.detection.secure_filename', return_value='test.jpg'), \ | |
| patch('src.routes.detection.os.path.join', return_value='static/uploads/test.jpg'): | |
| response = client.post('/api/v1/detect', data=data, content_type='multipart/form-data') | |
| assert response.status_code == 200 | |
| json_data = response.get_json() | |
| assert json_data['is_morphed'] is True | |