133 lines
4.9 KiB
Python
133 lines
4.9 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
|
|
|
|
from backend.main import app
|
|
|
|
|
|
class TestValidationAPI:
|
|
|
|
@pytest.fixture
|
|
def client(self):
|
|
return TestClient(app)
|
|
|
|
def test_validate_url_success_standard(self, client):
|
|
"""Test successful validation of standard YouTube URL."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtube.com/watch?v=dQw4w9WgXcQ"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is True
|
|
assert data["video_id"] == "dQw4w9WgXcQ"
|
|
assert data["video_url"] == "https://youtube.com/watch?v=dQw4w9WgXcQ"
|
|
assert data["error"] is None
|
|
|
|
def test_validate_url_success_short(self, client):
|
|
"""Test successful validation of short YouTube URL."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtu.be/dQw4w9WgXcQ"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is True
|
|
assert data["video_id"] == "dQw4w9WgXcQ"
|
|
|
|
def test_validate_url_success_embed(self, client):
|
|
"""Test successful validation of embed YouTube URL."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtube.com/embed/dQw4w9WgXcQ"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is True
|
|
assert data["video_id"] == "dQw4w9WgXcQ"
|
|
|
|
def test_validate_url_invalid_format(self, client):
|
|
"""Test validation failure for invalid URL format."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://vimeo.com/123456789"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is False
|
|
assert data["video_id"] is None
|
|
assert data["video_url"] is None
|
|
assert data["error"] is not None
|
|
assert data["error"]["code"] == "INVALID_URL"
|
|
assert "supported_formats" in data["error"]["details"]
|
|
|
|
def test_validate_url_playlist(self, client):
|
|
"""Test validation failure for playlist URLs."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtube.com/playlist?list=PLrAXtmErZgOeiKm4sgNOknGvNjby9efdf"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is False
|
|
assert data["error"] is not None
|
|
assert data["error"]["code"] == "UNSUPPORTED_FORMAT"
|
|
assert "playlist" in data["error"]["message"].lower()
|
|
assert "suggestion" in data["error"]["details"]
|
|
|
|
def test_validate_url_empty(self, client):
|
|
"""Test validation failure for empty URL."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": ""}
|
|
)
|
|
# FastAPI will validate this at the request model level
|
|
assert response.status_code == 422 # Unprocessable Entity
|
|
|
|
def test_validate_url_with_timestamp(self, client):
|
|
"""Test validation of URL with timestamp parameter."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtube.com/watch?v=dQw4w9WgXcQ&t=30s"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is True
|
|
assert data["video_id"] == "dQw4w9WgXcQ"
|
|
|
|
def test_validate_url_mobile(self, client):
|
|
"""Test validation of mobile YouTube URL."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://m.youtube.com/watch?v=dQw4w9WgXcQ"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is True
|
|
assert data["video_id"] == "dQw4w9WgXcQ"
|
|
|
|
def test_validate_url_invalid_video_id_length(self, client):
|
|
"""Test validation failure for invalid video ID length."""
|
|
response = client.post(
|
|
"/api/validate-url",
|
|
json={"url": "https://youtube.com/watch?v=short"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_valid"] is False
|
|
assert data["error"]["code"] == "INVALID_URL"
|
|
|
|
def test_get_supported_formats(self, client):
|
|
"""Test getting supported URL formats."""
|
|
response = client.get("/api/supported-formats")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "supported_formats" in data
|
|
assert isinstance(data["supported_formats"], list)
|
|
assert len(data["supported_formats"]) > 0
|
|
assert "examples" in data
|
|
assert "notes" in data |