from django.test import TestCase from django.contrib.auth import get_user_model from rest_framework.test import APIClient from rest_framework import status from django.urls import reverse from apps.bookmark.models import Bookmark from dj_language.models import Language User = get_user_model() class BookmarkListViewTestCase(TestCase): def setUp(self): self.client = APIClient() Language.objects.get_or_create(id=69, defaults={'name': 'English', 'code': 'en', 'countries': 'US'}) self.user = User.objects.create_user(username='testuser', email='testuser@example.com', password='password123') self.other_user = User.objects.create_user(username='otheruser', email='otheruser@example.com', password='password123') self.url = reverse('bookmark:bookmark_list') def test_unauthenticated_access(self): response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_get_bookmarks_authenticated(self): self.client.force_authenticate(user=self.user) # Create active bookmarks for self.user Bookmark.objects.create(user=self.user, service=Bookmark.ServiceChoices.HADITH, content_id=101, status=True) Bookmark.objects.create(user=self.user, service=Bookmark.ServiceChoices.LIBRARY, content_id=202, status=True) # Create inactive bookmark Bookmark.objects.create(user=self.user, service=Bookmark.ServiceChoices.HADITH, content_id=303, status=False) # Create bookmark for another user Bookmark.objects.create(user=self.other_user, service=Bookmark.ServiceChoices.HADITH, content_id=404, status=True) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 2) content_ids = [item['content_id'] for item in response.data] self.assertIn(101, content_ids) self.assertIn(202, content_ids) self.assertNotIn(303, content_ids) self.assertNotIn(404, content_ids) def test_filter_by_service(self): self.client.force_authenticate(user=self.user) Bookmark.objects.create(user=self.user, service=Bookmark.ServiceChoices.HADITH, content_id=101, status=True) Bookmark.objects.create(user=self.user, service=Bookmark.ServiceChoices.LIBRARY, content_id=202, status=True) response = self.client.get(self.url, {'service': 'hadith'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['content_id'], 101) self.assertEqual(response.data[0]['service'], 'hadith')