Browse Source
feat(tests): add comprehensive test script for video and podcast API endpoints
feat(tests): add comprehensive test script for video and podcast API endpoints
- Introduced a new test script `test_apis.py` to verify all video and podcast API endpoints. - Implemented a function to test individual endpoints with support for authenticated and anonymous users. - Included detailed logging of test results, including status codes and errors, to facilitate debugging and ensure API reliability. - Enhanced test coverage for both video and podcast APIs, ensuring all critical endpoints are validated.master
5 changed files with 254 additions and 35 deletions
-
23apps/bookmark/migrations/0005_auto_20251202_1245.py
-
4apps/bookmark/models/bookmark.py
-
4apps/bookmark/models/rate.py
-
52apps/podcast/views.py
-
206test_apis.py
@ -0,0 +1,23 @@ |
|||
# Generated by Django 3.2.4 on 2025-12-02 12:45 |
|||
|
|||
from django.db import migrations, models |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('bookmark', '0004_auto_20251130_1758'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.AlterField( |
|||
model_name='bookmark', |
|||
name='service', |
|||
field=models.CharField(choices=[('library', 'Library'), ('podcast', 'Podcast'), ('podcast_playlist', 'Podcast Playlist'), ('hadith', 'Hadith'), ('video', 'Video'), ('video_playlist', 'Video Playlist'), ('article', 'Article')], max_length=20, verbose_name='Service'), |
|||
), |
|||
migrations.AlterField( |
|||
model_name='rate', |
|||
name='service', |
|||
field=models.CharField(choices=[('library', 'Library'), ('podcast', 'Podcast'), ('podcast_playlist', 'Podcast Playlist'), ('hadith', 'Hadith'), ('video', 'Video'), ('video_playlist', 'Video Playlist')], max_length=20, verbose_name='Service'), |
|||
), |
|||
] |
|||
@ -0,0 +1,206 @@ |
|||
#!/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() |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue