import pytest
from django.contrib.auth import get_user_model
from apps.organizations.models import Organization
from apps.jobs.models import Job

User = get_user_model()

@pytest.fixture
def org():
    return Organization.objects.create(name='测试公司', email='test@test.com')

@pytest.fixture
def admin_user(org):
    return User.objects.create_user(
        username='admin1', password='pass123',
        role='admin', organization=org
    )

@pytest.mark.django_db
class TestJobModel:
    def test_create_job(self, org):
        job = Job.objects.create(
            organization=org, title='Python工程师',
            category='技术', location='北京', salary='20k-30k',
            description='职位描述', status='published'
        )
        assert job.status == 'published'
        assert str(job) == 'Python工程师'

@pytest.mark.django_db
class TestJobAPI:
    def test_public_can_list_published_jobs(self, client, org):
        Job.objects.create(
            organization=org, title='已发布职位',
            status='published', category='技术',
            location='北京', salary='10k'
        )
        Job.objects.create(
            organization=org, title='草稿职位',
            status='draft', category='技术',
            location='北京', salary='10k'
        )
        response = client.get('/api/jobs/public/')
        assert response.status_code == 200
        assert response.data['count'] == 1
