#!/usr/bin/env python3 """ Test script to verify all video and podcast API endpoints """ import os import django import sys os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') django.setup() from django.test import RequestFactory from django.contrib.auth import get_user_model from apps.video.views import ( VideoCategoryListAPIView, PinnedVideoCollectionListView, MiddleVideoCollectionListView, VideoPlaylistListAPIView, VideoPlaylistDetailAPIView, ) from apps.podcast.views import ( PodcastCategoryListAPIView, PinnedPodcastCollectionListView, MiddlePodcastCollectionListView, PodcastListAPIView, PodcastDetailAPIView, ) from apps.video.models import VideoPlaylist from apps.podcast.models import Podcast User = get_user_model() # Create a request factory factory = RequestFactory() def test_endpoint(view_class, url, method='GET', user=None, slug=None): """Test a single endpoint""" try: request = factory.get(url) if user: request.user = user else: # Create an anonymous user from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser() view = view_class.as_view() if slug: response = view(request, slug=slug) else: response = view(request) status_code = response.status_code return { 'status': 'SUCCESS' if status_code == 200 else 'FAILED', 'status_code': status_code, 'error': None } except Exception as e: return { 'status': 'ERROR', 'status_code': None, 'error': str(e) } def main(): print("=" * 80) print("TESTING VIDEO AND PODCAST APIs") print("=" * 80) # Get or create a test user try: user = User.objects.first() if not user: print("\n⚠️ No users found in database. Testing with anonymous user only.\n") except Exception as e: print(f"\n⚠️ Error getting user: {e}. Testing with anonymous user only.\n") user = None # VIDEO API TESTS print("\n" + "=" * 80) print("VIDEO APIs") print("=" * 80) video_tests = [ { 'name': 'Video Categories List', 'view': VideoCategoryListAPIView, 'url': '/api/videos/categories/', 'user': None }, { 'name': 'Pinned Video Collections List', 'view': PinnedVideoCollectionListView, 'url': '/api/videos/pinned-collections/', 'user': user }, { 'name': 'Middle Video Collections List', 'view': MiddleVideoCollectionListView, 'url': '/api/videos/collections/', 'user': user }, { 'name': 'Video Playlists List', 'view': VideoPlaylistListAPIView, 'url': '/api/videos/playlists/', 'user': None }, ] # Test detail endpoint if we have a playlist try: first_playlist = VideoPlaylist.objects.filter(status=True).first() if first_playlist: video_tests.append({ 'name': f'Video Playlist Detail (slug: {first_playlist.slug})', 'view': VideoPlaylistDetailAPIView, 'url': f'/api/videos/playlists/{first_playlist.slug}/', 'user': None, 'slug': first_playlist.slug }) except Exception as e: print(f"\n⚠️ Could not fetch video playlist for detail test: {e}") for test in video_tests: result = test_endpoint( test['view'], test['url'], user=test.get('user'), slug=test.get('slug') ) status_symbol = "✅" if result['status'] == 'SUCCESS' else "❌" print(f"\n{status_symbol} {test['name']}") print(f" URL: {test['url']}") print(f" Status: {result['status']} (HTTP {result['status_code']})") if result['error']: print(f" Error: {result['error']}") # PODCAST API TESTS print("\n" + "=" * 80) print("PODCAST APIs") print("=" * 80) podcast_tests = [ { 'name': 'Podcast Categories List', 'view': PodcastCategoryListAPIView, 'url': '/api/podcast/categories/', 'user': None }, { 'name': 'Pinned Podcast Collections List', 'view': PinnedPodcastCollectionListView, 'url': '/api/podcast/pinned-collections/', 'user': user }, { 'name': 'Middle Podcast Collections List', 'view': MiddlePodcastCollectionListView, 'url': '/api/podcast/collections/', 'user': user }, { 'name': 'Podcasts List', 'view': PodcastListAPIView, 'url': '/api/podcast/list/', 'user': None }, ] # Test detail endpoint if we have a podcast try: first_podcast = Podcast.objects.filter(status=True).first() if first_podcast: podcast_tests.append({ 'name': f'Podcast Detail (slug: {first_podcast.slug})', 'view': PodcastDetailAPIView, 'url': f'/api/podcast/detail/{first_podcast.slug}/', 'user': None, 'slug': first_podcast.slug }) except Exception as e: print(f"\n⚠️ Could not fetch podcast for detail test: {e}") for test in podcast_tests: result = test_endpoint( test['view'], test['url'], user=test.get('user'), slug=test.get('slug') ) status_symbol = "✅" if result['status'] == 'SUCCESS' else "❌" print(f"\n{status_symbol} {test['name']}") print(f" URL: {test['url']}") print(f" Status: {result['status']} (HTTP {result['status_code']})") if result['error']: print(f" Error: {result['error']}") print("\n" + "=" * 80) print("TEST COMPLETE") print("=" * 80 + "\n") if __name__ == '__main__': main()