from django.urls import reverse
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.test import APITestCase
from your_app.models import Source, Tag

User = get_user_model()

class SearchTests(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='testuser', password='testpassword123')
        self.client.force_authenticate(user=self.user)
        self.tag = Tag.objects.create(name='covid')
        self.source = Source.objects.create(
            title='COVID-19 Research',
            url='https://example.com/covid',
            description='Latest research on COVID-19',
            added_by=self.user
        )
        self.source.tags.add(self.tag)

    def test_search_source(self):
        url = reverse('search')
        response = self.client.get(url, {'q': 'COVID'})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['title'], 'COVID-19 Research')