feat: add profile api
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
"""配置页三接口测试:机构信息获取 / 所属机构科室列表 / 医学生信息配置。"""
|
||||
|
||||
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_departments_no_pagination(self):
|
||||
"""返回本机构全部科室,且不分页(直接为列表)。"""
|
||||
inst = ensure_institution(name='测试医院', code='TEST-HOSP-001')
|
||||
Department.objects.create(institution=inst, name='内科', category='临床')
|
||||
Department.objects.create(institution=inst, name='外科', category='临床')
|
||||
# 另一机构的科室不应出现
|
||||
other = ensure_institution(name='其他医院', code='TEST-HOSP-OTHER')
|
||||
Department.objects.create(institution=other, name='儿科', category='临床')
|
||||
|
||||
user = create_test_user(phone='13900200010', institution=inst)
|
||||
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_user_without_institution_404(self):
|
||||
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, 404, resp.content)
|
||||
|
||||
|
||||
class ProfileConfigTest(CacheTestCase):
|
||||
"""医学生信息配置接口。"""
|
||||
|
||||
def test_config_success(self):
|
||||
"""录入 科室/职称/执业年限 → 落库成功。"""
|
||||
inst = ensure_institution(name='测试医院', code='TEST-HOSP-001')
|
||||
dept = Department.objects.create(institution=inst, 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_department_from_other_institution_rejected(self):
|
||||
"""所选科室不属于本机构 → 校验失败 400。"""
|
||||
inst = ensure_institution(name='测试医院', code='TEST-HOSP-001')
|
||||
other = ensure_institution(name='其他医院', code='TEST-HOSP-OTHER')
|
||||
other_dept = Department.objects.create(institution=other, name='外科', category='临床')
|
||||
user = create_test_user(phone='13900200021', institution=inst)
|
||||
|
||||
client = get_auth_client(user)
|
||||
resp = client.post(USER_PROFILE_CONFIG_URL, {
|
||||
'department': other_dept.id,
|
||||
'title_name': '住院医师',
|
||||
'practice_years': '1-3年',
|
||||
})
|
||||
self.assertEqual(resp.status_code, 400, resp.content)
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user