Initial commit - Video Service for Coolify
This commit is contained in:
120
tests/test_api.py
Executable file
120
tests/test_api.py
Executable file
@@ -0,0 +1,120 @@
|
||||
"""
|
||||
API Tests for Video-Service
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Test client fixture."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_key():
|
||||
"""API key for testing."""
|
||||
return "change-me-in-production"
|
||||
|
||||
|
||||
class TestHealthEndpoints:
|
||||
"""Tests for health check endpoints."""
|
||||
|
||||
def test_root_health(self, client):
|
||||
"""Test root health endpoint."""
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "ok"
|
||||
|
||||
def test_api_health(self, client):
|
||||
"""Test API health endpoint."""
|
||||
response = client.get("/api/v1/health")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "status" in data
|
||||
assert "version" in data
|
||||
assert "ffmpeg_available" in data
|
||||
|
||||
|
||||
class TestAuthentication:
|
||||
"""Tests for authentication."""
|
||||
|
||||
def test_missing_api_key(self, client):
|
||||
"""Test request without API key."""
|
||||
response = client.get("/api/v1/videos")
|
||||
assert response.status_code == 401
|
||||
assert "Missing API key" in response.json()["detail"]
|
||||
|
||||
def test_invalid_api_key(self, client):
|
||||
"""Test request with invalid API key."""
|
||||
response = client.get(
|
||||
"/api/v1/videos",
|
||||
headers={"X-API-Key": "invalid-key"},
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert "Invalid API key" in response.json()["detail"]
|
||||
|
||||
def test_valid_api_key(self, client, api_key):
|
||||
"""Test request with valid API key."""
|
||||
response = client.get(
|
||||
"/api/v1/videos",
|
||||
headers={"X-API-Key": api_key},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
class TestVideoEndpoints:
|
||||
"""Tests for video endpoints."""
|
||||
|
||||
def test_list_videos_empty(self, client, api_key):
|
||||
"""Test listing videos when empty."""
|
||||
response = client.get(
|
||||
"/api/v1/videos",
|
||||
headers={"X-API-Key": api_key},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "videos" in data
|
||||
assert "total" in data
|
||||
|
||||
def test_get_nonexistent_video(self, client, api_key):
|
||||
"""Test getting non-existent video."""
|
||||
response = client.get(
|
||||
"/api/v1/videos/nonexistent/status",
|
||||
headers={"X-API-Key": api_key},
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_delete_nonexistent_video(self, client, api_key):
|
||||
"""Test deleting non-existent video."""
|
||||
response = client.delete(
|
||||
"/api/v1/videos/nonexistent",
|
||||
headers={"X-API-Key": api_key},
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
class TestStreamEndpoints:
|
||||
"""Tests for streaming endpoints."""
|
||||
|
||||
def test_stream_without_token(self, client):
|
||||
"""Test streaming without token."""
|
||||
response = client.get("/api/v1/stream/test/master.m3u8")
|
||||
assert response.status_code == 422 # Missing required parameter
|
||||
|
||||
def test_stream_with_invalid_token(self, client):
|
||||
"""Test streaming with invalid token."""
|
||||
response = client.get("/api/v1/stream/test/master.m3u8?token=invalid")
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
class TestThumbnailEndpoint:
|
||||
"""Tests for thumbnail endpoint."""
|
||||
|
||||
def test_thumbnail_not_found(self, client):
|
||||
"""Test thumbnail for non-existent video."""
|
||||
response = client.get("/api/v1/videos/nonexistent/thumbnail")
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user