"""配置页三接口测试:机构信息获取 / 所属机构科室列表 / 医学生信息配置。""" from rest_framework.test import APIClient from apps.user.models import Department, Institution from .conftest import ( CacheTestCase, USER_INSTITUTION_INFO_URL, USER_MY_DEPARTMENTS_URL, USER_PROFILE_CONFIG_URL, create_test_user, get_auth_client, ensure_institution, ) class InstitutionInfoTest(CacheTestCase): """机构信息获取接口。""" def test_returns_institution_with_banner(self): """配置了 banner_url 的机构 → 返回拼好的完整 URL。""" inst = ensure_institution(name='测试医院', code='TEST-HOSP-001') inst.banner_url = 'institutions/test.png' inst.save(update_fields=['banner_url']) user = create_test_user(phone='13900200001', institution=inst) client = get_auth_client(user) resp = client.get(USER_INSTITUTION_INFO_URL) self.assertEqual(resp.status_code, 200, resp.content) data = resp.json() self.assertEqual(data['id'], inst.id) self.assertEqual(data['name'], '测试医院') self.assertTrue(data['banner_url'].startswith('http')) self.assertTrue(data['banner_url'].endswith('/static/institutions/test.png')) def test_empty_banner_falls_back_to_default(self): """机构未配 banner_url → 回退到默认医院图。""" inst = ensure_institution(name='无图医院', code='TEST-HOSP-002') user = create_test_user(phone='13900200002', institution=inst) client = get_auth_client(user) resp = client.get(USER_INSTITUTION_INFO_URL) self.assertEqual(resp.status_code, 200, resp.content) self.assertTrue( resp.json()['banner_url'].endswith('/static/institutions/default_hospital.png') ) def test_full_url_banner_passthrough(self): """banner_url 已是完整 URL → 原样返回。""" inst = ensure_institution(name='CDN医院', code='TEST-HOSP-003') inst.banner_url = 'https://cdn.example.com/a.png' inst.save(update_fields=['banner_url']) user = create_test_user(phone='13900200003', institution=inst) client = get_auth_client(user) resp = client.get(USER_INSTITUTION_INFO_URL) self.assertEqual(resp.json()['banner_url'], 'https://cdn.example.com/a.png') def test_user_without_institution_404(self): """账号未关联机构 → 404。""" user = create_test_user(phone='13900200004', institution=None) client = get_auth_client(user) resp = client.get(USER_INSTITUTION_INFO_URL) self.assertEqual(resp.status_code, 404, resp.content) def test_requires_auth(self): """未登录 → 401。""" resp = APIClient().get(USER_INSTITUTION_INFO_URL) self.assertEqual(resp.status_code, 401, resp.content) class MyDepartmentsTest(CacheTestCase): """所属机构科室列表接口(不分页)。""" def test_returns_all_global_departments_no_pagination(self): """返回全部全局科室(与机构无关),不分页(直接为列表)。""" Department.objects.create(name='内科', category='临床') Department.objects.create(name='外科', category='临床') Department.objects.create(name='儿科', category='临床') user = create_test_user(phone='13900200010') client = get_auth_client(user) resp = client.get(USER_MY_DEPARTMENTS_URL) self.assertEqual(resp.status_code, 200, resp.content) data = resp.json() self.assertIsInstance(data, list) # 不分页:顶层是列表 names = {d['name'] for d in data} self.assertEqual(names, {'内科', '外科', '儿科'}) def test_no_institution_still_returns_departments(self): """科室全局,用户没机构也能拿到全部科室。""" Department.objects.create(name='内科', category='临床') user = create_test_user(phone='13900200011', institution=None) client = get_auth_client(user) resp = client.get(USER_MY_DEPARTMENTS_URL) self.assertEqual(resp.status_code, 200, resp.content) self.assertEqual({d['name'] for d in resp.json()}, {'内科'}) class ProfileConfigTest(CacheTestCase): """医学生信息配置接口。""" def test_config_success(self): """录入 科室/职称/执业年限 → 落库成功。""" inst = ensure_institution(name='测试医院', code='TEST-HOSP-001') dept = Department.objects.create(name='内科', category='临床') user = create_test_user(phone='13900200020', institution=inst) client = get_auth_client(user) resp = client.post(USER_PROFILE_CONFIG_URL, { 'department': dept.id, 'title_name': '住院医师', 'practice_years': '1-3年', }) self.assertEqual(resp.status_code, 200, resp.content) user.refresh_from_db() self.assertEqual(user.department_id, dept.id) self.assertEqual(user.title_name, '住院医师') self.assertEqual(user.practice_years, '1-3年') self.assertEqual(resp.json()['user']['practice_years'], '1-3年') def test_any_global_department_accepted(self): """科室全局:可选择任意科室(不再校验机构归属)。""" dept = Department.objects.create(name='外科', category='临床') user = create_test_user(phone='13900200021', institution=None) client = get_auth_client(user) resp = client.post(USER_PROFILE_CONFIG_URL, { 'department': dept.id, 'title_name': '住院医师', 'practice_years': '1-3年', }) self.assertEqual(resp.status_code, 200, resp.content) user.refresh_from_db() self.assertEqual(user.department_id, dept.id) def test_missing_fields_rejected(self): """缺少必填字段 → 400。""" inst = ensure_institution(name='测试医院', code='TEST-HOSP-001') user = create_test_user(phone='13900200022', institution=inst) client = get_auth_client(user) resp = client.post(USER_PROFILE_CONFIG_URL, {'title_name': '住院医师'}) self.assertEqual(resp.status_code, 400, resp.content) def test_requires_auth(self): resp = APIClient().post(USER_PROFILE_CONFIG_URL, {}) self.assertEqual(resp.status_code, 401, resp.content)