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 SourceTests(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='test_tag')

    def test_create_source(self):
        url = reverse('source-list-create')
        data = {
            'title': 'Test Source',
            'url': 'https://example.com',
            'archived_url': 'https://archive.is/example.com',
            'description': 'This is a test source',
            'category': 'test',
            'tags': [self.tag.id]
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Source.objects.count(), 1)
        self.assertEqual(Source.objects.get().title, 'Test Source')

    def test_list_sources(self):
        Source.objects.create(title='Test Source', url='https://example.com', added_by=self.user)
        url = reverse('source-list-create')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)