import pytest from django.contrib.auth import get_user_model from rest_framework.authtoken.models import Token from videos.models import Channel, Series, Video User = get_user_model() @pytest.fixture def auth_headers(): user = User.objects.create(email="api@test.com") token = Token.objects.create(user=user) return {"HTTP_AUTHORIZATION": f"Token {token.key}"} @pytest.fixture def channel(): return Channel.objects.create(name="Test Channel") @pytest.fixture def series(): return Series.objects.create( name="Test Series", ) @pytest.fixture def video(channel): return Video.objects.create( title="Test Video", imdb_id="tt1234567", channel=channel, ) @pytest.mark.django_db class TestVideoAPI: def test_list_videos(self, client, auth_headers, video): response = client.get("/api/v1/videos/", **auth_headers) assert response.status_code == 200 assert len(response.data["results"]) == 1 assert response.data["results"][0]["title"] == "Test Video" def test_get_video(self, client, auth_headers, video): response = client.get(f"/api/v1/videos/{video.id}/", **auth_headers) assert response.status_code == 200 assert response.data["title"] == "Test Video" def test_filter_videos_by_channel(self, client, auth_headers, channel, video): response = client.get(f"/api/v1/videos/?channel={channel.id}", **auth_headers) assert response.status_code == 200 assert len(response.data["results"]) == 1 assert response.data["results"][0]["channel"] == channel.id @pytest.mark.django_db class TestChannelAPI: def test_list_channels(self, client, auth_headers, channel): response = client.get("/api/v1/channels/", **auth_headers) assert response.status_code == 200 assert len(response.data["results"]) == 1 assert response.data["results"][0]["name"] == "Test Channel" def test_get_channel(self, client, auth_headers, channel): response = client.get(f"/api/v1/channels/{channel.id}/", **auth_headers) assert response.status_code == 200 assert response.data["name"] == "Test Channel" @pytest.mark.django_db class TestSeriesAPI: def test_list_series(self, client, auth_headers, series): response = client.get("/api/v1/series/", **auth_headers) assert response.status_code == 200 assert len(response.data["results"]) == 1 assert response.data["results"][0]["name"] == "Test Series" def test_get_series(self, client, auth_headers, series): response = client.get(f"/api/v1/series/{series.id}/", **auth_headers) assert response.status_code == 200 assert response.data["name"] == "Test Series" @pytest.mark.django_db class TestVideoAPIUnauthorized: def test_list_videos_unauthenticated(self, client, video): response = client.get("/api/v1/videos/") assert response.status_code == 401 def test_list_channels_unauthenticated(self, client, channel): response = client.get("/api/v1/channels/") assert response.status_code == 401 def test_list_series_unauthenticated(self, client, series): response = client.get("/api/v1/series/") assert response.status_code == 401