Add comprehensive tests for Parks API and models

- Implemented extensive test cases for the Parks API, covering endpoints for listing, retrieving, creating, updating, and deleting parks.
- Added tests for filtering, searching, and ordering parks in the API.
- Created tests for error handling in the API, including malformed JSON and unsupported methods.
- Developed model tests for Park, ParkArea, Company, and ParkReview models, ensuring validation and constraints are enforced.
- Introduced utility mixins for API and model testing to streamline assertions and enhance test readability.
- Included integration tests to validate complete workflows involving park creation, retrieval, updating, and deletion.
This commit is contained in:
pacnpal
2025-08-17 19:36:20 -04:00
parent 17228e9935
commit c26414ff74
210 changed files with 24155 additions and 833 deletions

View File

@@ -1,3 +1,91 @@
from django.test import TestCase
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from unittest.mock import patch, MagicMock
from .models import User, UserProfile
from .signals import create_default_groups
# Create your tests here.
class SignalsTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username='testuser',
email='testuser@example.com',
password='password'
)
def test_create_user_profile(self):
self.assertTrue(hasattr(self.user, 'profile'))
self.assertIsInstance(self.user.profile, UserProfile)
@patch('accounts.signals.requests.get')
def test_create_user_profile_with_social_avatar(self, mock_get):
# Mock the response from requests.get
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = b'fake-image-content'
mock_get.return_value = mock_response
# Create a social account for the user
social_account = self.user.socialaccount_set.create(
provider='google',
extra_data={'picture': 'http://example.com/avatar.png'}
)
# The signal should have been triggered when the user was created,
# but we can trigger it again to test the avatar download
from .signals import create_user_profile
create_user_profile(sender=User, instance=self.user, created=True)
self.user.profile.refresh_from_db()
self.assertTrue(self.user.profile.avatar.name.startswith('avatars/avatar_testuser'))
def test_save_user_profile(self):
self.user.profile.delete()
self.assertFalse(hasattr(self.user, 'profile'))
self.user.save()
self.assertTrue(hasattr(self.user, 'profile'))
self.assertIsInstance(self.user.profile, UserProfile)
def test_sync_user_role_with_groups(self):
self.user.role = User.Roles.MODERATOR
self.user.save()
self.assertTrue(self.user.groups.filter(name=User.Roles.MODERATOR).exists())
self.assertTrue(self.user.is_staff)
self.user.role = User.Roles.ADMIN
self.user.save()
self.assertFalse(self.user.groups.filter(name=User.Roles.MODERATOR).exists())
self.assertTrue(self.user.groups.filter(name=User.Roles.ADMIN).exists())
self.assertTrue(self.user.is_staff)
self.user.role = User.Roles.SUPERUSER
self.user.save()
self.assertFalse(self.user.groups.filter(name=User.Roles.ADMIN).exists())
self.assertTrue(self.user.groups.filter(name=User.Roles.SUPERUSER).exists())
self.assertTrue(self.user.is_superuser)
self.assertTrue(self.user.is_staff)
self.user.role = User.Roles.USER
self.user.save()
self.assertFalse(self.user.groups.exists())
self.assertFalse(self.user.is_superuser)
self.assertFalse(self.user.is_staff)
def test_create_default_groups(self):
# Create some permissions for testing
content_type = ContentType.objects.get_for_model(User)
Permission.objects.create(codename='change_review', name='Can change review', content_type=content_type)
Permission.objects.create(codename='delete_review', name='Can delete review', content_type=content_type)
Permission.objects.create(codename='change_user', name='Can change user', content_type=content_type)
create_default_groups()
moderator_group = Group.objects.get(name=User.Roles.MODERATOR)
self.assertIsNotNone(moderator_group)
self.assertTrue(moderator_group.permissions.filter(codename='change_review').exists())
self.assertFalse(moderator_group.permissions.filter(codename='change_user').exists())
admin_group = Group.objects.get(name=User.Roles.ADMIN)
self.assertIsNotNone(admin_group)
self.assertTrue(admin_group.permissions.filter(codename='change_review').exists())
self.assertTrue(admin_group.permissions.filter(codename='change_user').exists())