From 414aba6ccff32d715379d7345f8766f71896bc01 Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Sun, 13 Jul 2025 14:20:46 +0300 Subject: [PATCH] tests tests --- tests/api/test_media_listings.py | 67 ++++++++++ tests/api/test_media_listings_TOWRITE.py | 10 -- tests/api/test_search.py | 114 ++++++++++++++++++ tests/api/test_search_TOWRITE.py | 10 -- tests/settings/test_portal_workflow.py | 97 +++++++++++++++ .../settings/test_portal_workflow_TOWRITE.py | 11 -- tests/test_imports.py | 51 ++++++++ 7 files changed, 329 insertions(+), 31 deletions(-) create mode 100644 tests/api/test_media_listings.py delete mode 100644 tests/api/test_media_listings_TOWRITE.py create mode 100644 tests/api/test_search.py delete mode 100644 tests/api/test_search_TOWRITE.py create mode 100644 tests/settings/test_portal_workflow.py delete mode 100644 tests/settings/test_portal_workflow_TOWRITE.py create mode 100644 tests/test_imports.py diff --git a/tests/api/test_media_listings.py b/tests/api/test_media_listings.py new file mode 100644 index 00000000..3a7f74dd --- /dev/null +++ b/tests/api/test_media_listings.py @@ -0,0 +1,67 @@ +from django.core.files import File +from django.test import Client, TestCase + +from files.models import Media +from files.tests import create_account + + +class TestMediaListings(TestCase): + fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] + + def setUp(self): + self.client = Client() + self.password = 'this_is_a_fake_password' + self.user = create_account(password=self.password) + + # Create a test media item + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + self.media = Media.objects.create( + title="Test Media", description="Test Description", user=self.user, state="public", encoding_status="success", is_reviewed=True, listable=True, media_file=myfile + ) + + def test_media_list_endpoint(self): + """Test the media list API endpoint""" + url = '/api/v1/media' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Media list endpoint should return 200") + self.assertIn('results', response.data, "Response should contain results") + self.assertIn('count', response.data, "Response should contain count") + + # Check if our test media is in the results + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media.title, media_titles, "Test media should be in the results") + + def test_featured_media_listing(self): + """Test the featured media listing""" + # Mark our test media as featured + self.media.featured = True + self.media.save() + + url = '/api/v1/media?show=featured' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Featured media endpoint should return 200") + + # Check if our featured media is in the results + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media.title, media_titles, "Featured media should be in the results") + + def test_user_media_listing(self): + """Test listing media for a specific user""" + url = f'/api/v1/media?author={self.user.username}' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "User media endpoint should return 200") + + # Check if our user's media is in the results + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media.title, media_titles, "User's media should be in the results") + + def test_recommended_media_listing(self): + """Test the recommended media listing""" + url = '/api/v1/media?show=recommended' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Recommended media endpoint should return 200") diff --git a/tests/api/test_media_listings_TOWRITE.py b/tests/api/test_media_listings_TOWRITE.py deleted file mode 100644 index d6bbfdb4..00000000 --- a/tests/api/test_media_listings_TOWRITE.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.test import TestCase - - -class TestX(TestCase): - fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] - - def test_X(self): - # test a number of listings works (index, featured, user etc) - - pass diff --git a/tests/api/test_search.py b/tests/api/test_search.py new file mode 100644 index 00000000..e14f998f --- /dev/null +++ b/tests/api/test_search.py @@ -0,0 +1,114 @@ +from django.core.files import File +from django.test import Client, TestCase + +from files.models import Category, Media, Tag +from files.tests import create_account + + +class TestSearch(TestCase): + fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] + + def setUp(self): + self.client = Client() + self.password = 'this_is_a_fake_password' + self.user = create_account(password=self.password) + + # Create test media items with different attributes for search testing + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + self.media1 = Media.objects.create(title="Python Tutorial", description="Learn Python programming", user=self.user, media_file=myfile) + + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + self.media2 = Media.objects.create( + title="Django Framework", + description="Web development with Django", + user=self.user, + media_file=myfile, + ) + + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + self.media3 = Media.objects.create( + title="JavaScript Basics", + description="Introduction to JavaScript", + user=self.user, + media_file=myfile, + ) + # Add categories and tags + self.category = Category.objects.first() + self.tag = Tag.objects.create(title="programming", user=self.user) + + self.media1.category.add(self.category) + self.media2.category.add(self.category) + self.media1.tags.add(self.tag) + self.media2.tags.add(self.tag) + + # Update search vectors + self.media1.update_search_vector() + self.media2.update_search_vector() + self.media3.update_search_vector() + + def test_search_by_title(self): + """Test searching media by title""" + url = '/api/v1/search?q=python' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Search endpoint should return 200") + + # Check if our media with "Python" in the title is in the results + + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media1.title, media_titles, "Media with 'Python' in title should be in results") + self.assertNotIn(self.media3.title, media_titles, "Media without 'Python' should not be in results") + + def test_search_by_category(self): + """Test searching media by category""" + url = f'/api/v1/search?c={self.category.title}' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Category search endpoint should return 200") + + # Check if media in the category are in the results + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media1.title, media_titles, "Media in category should be in results") + self.assertIn(self.media2.title, media_titles, "Media in category should be in results") + self.assertNotIn(self.media3.title, media_titles, "Media not in category should not be in results") + + def test_search_by_tag(self): + """Test searching media by tag""" + url = f'/api/v1/search?t={self.tag.title}' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Tag search endpoint should return 200") + + # Check if media with the tag are in the results + media_titles = [item['title'] for item in response.data['results']] + self.assertIn(self.media1.title, media_titles, "Media with tag should be in results") + self.assertIn(self.media2.title, media_titles, "Media with tag should be in results") + self.assertNotIn(self.media3.title, media_titles, "Media without tag should not be in results") + + def test_search_with_media_type_filter(self): + """Test searching with media type filter""" + url = '/api/v1/search?q=tutorial&media_type=video' + response = self.client.get(url) + + self.assertEqual(response.status_code, 200, "Media type filtered search should return 200") + + # Create an image media with the same search term + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + image_media = Media.objects.create( + title="Tutorial Image", + description="Tutorial image description", + user=self.user, + media_file=myfile, + ) + image_media.update_search_vector() + + # Search with media_type=video + url = '/api/v1/search?q=tutorial&media_type=video' + response = self.client.get(url) + + media_titles = [item['title'] for item in response.data['results']] + self.assertNotIn(image_media.title, media_titles, "Image media should not be in results") diff --git a/tests/api/test_search_TOWRITE.py b/tests/api/test_search_TOWRITE.py deleted file mode 100644 index 2564e24e..00000000 --- a/tests/api/test_search_TOWRITE.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.test import TestCase - - -class TestX(TestCase): - fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] - - def test_X(self): - # add a few files, check search different cases that work - - pass diff --git a/tests/settings/test_portal_workflow.py b/tests/settings/test_portal_workflow.py new file mode 100644 index 00000000..f8b5f3a8 --- /dev/null +++ b/tests/settings/test_portal_workflow.py @@ -0,0 +1,97 @@ +from django.conf import settings +from django.core.files import File +from django.test import TestCase, override_settings + +from files.helpers import get_default_state, get_portal_workflow +from files.models import Media +from files.tests import create_account + + +class TestPortalWorkflow(TestCase): + fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] + + def setUp(self): + self.user = create_account() + self.advanced_user = create_account(username="advanced_user") + self.advanced_user.advancedUser = True + self.advanced_user.save() + + def test_default_portal_workflow(self): + """Test the default portal workflow setting""" + workflow = get_portal_workflow() + self.assertEqual(workflow, settings.PORTAL_WORKFLOW, "get_portal_workflow should return the PORTAL_WORKFLOW setting") + + @override_settings(PORTAL_WORKFLOW='public') + def test_public_workflow(self): + """Test the public workflow setting""" + # Check that get_portal_workflow returns the correct value + self.assertEqual(get_portal_workflow(), 'public', "get_portal_workflow should return 'public'") + + # Check that get_default_state returns the correct value + self.assertEqual(get_default_state(), 'public', "get_default_state should return 'public'") + + # Check that a new media gets the correct state + # Mock the media_file requirement by patching the save method + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + media = Media.objects.create(title="Test Media", description="Test Description", user=self.user, media_file=myfile) + self.assertEqual(media.state, 'public', "Media state should be 'public' in public workflow") + + @override_settings(PORTAL_WORKFLOW='unlisted') + def test_unlisted_workflow(self): + """Test the unlisted workflow setting""" + # Check that get_portal_workflow returns the correct value + self.assertEqual(get_portal_workflow(), 'unlisted', "get_portal_workflow should return 'unlisted'") + + # Check that get_default_state returns the correct value + self.assertEqual(get_default_state(), 'unlisted', "get_default_state should return 'unlisted'") + + # Check that a new media gets the correct state + # Mock the media_file requirement by patching the save method + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + media = Media.objects.create(title="Test Media", description="Test Description", user=self.user, media_file=myfile) + self.assertEqual(media.state, 'unlisted', "Media state should be 'unlisted' in unlisted workflow") + + @override_settings(PORTAL_WORKFLOW='private') + def test_private_workflow(self): + """Test the private workflow setting""" + # Check that get_portal_workflow returns the correct value + self.assertEqual(get_portal_workflow(), 'private', "get_portal_workflow should return 'private'") + + # Check that get_default_state returns the correct value + self.assertEqual(get_default_state(), 'private', "get_default_state should return 'private'") + + # Check that a new media gets the correct state + # Mock the media_file requirement by patching the save method + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + media = Media.objects.create(title="Test Media", description="Test Description", user=self.user, media_file=myfile) + self.assertEqual(media.state, 'private', "Media state should be 'private' in private workflow") + + @override_settings(PORTAL_WORKFLOW='private_verified') + def test_private_verified_workflow(self): + """Test the private_verified workflow setting""" + # Check that get_portal_workflow returns the correct value + self.assertEqual(get_portal_workflow(), 'private_verified', "get_portal_workflow should return 'private_verified'") + + # Check that get_default_state returns the correct value for regular user + self.assertEqual(get_default_state(user=self.user), 'private', "get_default_state should return 'private' for regular user") + + # Check that get_default_state returns the correct value for advanced user + self.advanced_user.advancedUser = True + self.advanced_user.save() + self.assertEqual(get_default_state(user=self.advanced_user), 'unlisted', "get_default_state should return 'unlisted' for advanced user") + + # Check that a new media gets the correct state for regular user + # Mock the media_file requirement by patching the save method + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + media = Media.objects.create(title="Test Media", description="Test Description", user=self.user, media_file=myfile) + self.assertEqual(media.state, 'private', "Media state should be 'private' for regular user in private_verified workflow") + + # Check that a new media gets the correct state for advanced user + with open('fixtures/test_image2.jpg', "rb") as f: + myfile = File(f) + media = Media.objects.create(title="Advanced Test Media", description="Test Description", user=self.advanced_user, media_file=myfile) + self.assertEqual(media.state, 'unlisted', "Media state should be 'unlisted' for advanced user in private_verified workflow") diff --git a/tests/settings/test_portal_workflow_TOWRITE.py b/tests/settings/test_portal_workflow_TOWRITE.py deleted file mode 100644 index 179d282c..00000000 --- a/tests/settings/test_portal_workflow_TOWRITE.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.test import TestCase - - -class TestX(TestCase): - fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"] - - def test_X(self): - # test what is the default portal workflow - # change it and make sure nothing strange happens (public/unlisted/private) - - pass diff --git a/tests/test_imports.py b/tests/test_imports.py new file mode 100644 index 00000000..068bd8ee --- /dev/null +++ b/tests/test_imports.py @@ -0,0 +1,51 @@ +from django.test import TestCase + + +class TestImports(TestCase): + """Test that all models and views can be imported correctly""" + + def test_model_imports(self): + """Test that all models can be imported""" + # Import all models + from files.models import Category # noqa: F401 + from files.models import Comment # noqa: F401 + from files.models import EncodeProfile # noqa: F401 + from files.models import Encoding # noqa: F401 + from files.models import Language # noqa: F401 + from files.models import License # noqa: F401 + from files.models import Media # noqa: F401 + from files.models import MediaPermission # noqa: F401 + from files.models import Playlist # noqa: F401 + from files.models import PlaylistMedia # noqa: F401 + from files.models import Rating # noqa: F401 + from files.models import RatingCategory # noqa: F401 + from files.models import Subtitle # noqa: F401 + from files.models import Tag # noqa: F401 + from files.models import VideoChapterData # noqa: F401 + from files.models import VideoTrimRequest # noqa: F401 + + # Simple assertion to verify imports worked + self.assertTrue(True, "All model imports succeeded") + + def test_view_imports(self): + """Test that all views can be imported""" + # Import all views + from files.views import CategoryList # noqa: F401 + from files.views import CommentDetail # noqa: F401 + from files.views import CommentList # noqa: F401 + from files.views import EncodeProfileList # noqa: F401 + from files.views import EncodingDetail # noqa: F401 + from files.views import MediaActions # noqa: F401 + from files.views import MediaBulkUserActions # noqa: F401 + from files.views import MediaDetail # noqa: F401 + from files.views import MediaList # noqa: F401 + from files.views import MediaSearch # noqa: F401 + from files.views import PlaylistDetail # noqa: F401 + from files.views import PlaylistList # noqa: F401 + from files.views import TagList # noqa: F401 + from files.views import TaskDetail # noqa: F401 + from files.views import TasksList # noqa: F401 + from files.views import UserActions # noqa: F401 + + # Simple assertion to verify imports worked + self.assertTrue(True, "All view imports succeeded")