forked from erp-dev/erp
feat: added user profile model & api & admin, it uses by found user with merchant
This commit is contained in:
153
api_man/tests.py
153
api_man/tests.py
@@ -303,3 +303,156 @@ class EmployeeAPITestCase(TestCase):
|
||||
response = self.client.patch(f'/api/backend/employees/{employee.id}/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['job_type'], '打纸工') # 应该保持不变
|
||||
|
||||
|
||||
class UserProfileAPITestCase(TestCase):
|
||||
"""测试用户资料 API"""
|
||||
|
||||
def setUp(self):
|
||||
"""设置测试数据"""
|
||||
# 创建商户
|
||||
self.merchant = Merchant.objects.create(
|
||||
name='测试商户',
|
||||
type=MerchantTypeEnum.STORE
|
||||
)
|
||||
|
||||
# 创建用户和员工
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpass123',
|
||||
email='test@example.com'
|
||||
)
|
||||
self.employee = Employee.objects.create(
|
||||
merchant=self.merchant,
|
||||
sys_user=self.user,
|
||||
name='测试员工'
|
||||
)
|
||||
|
||||
# 创建另一个用户用于测试
|
||||
self.user2 = User.objects.create_user(
|
||||
username='testuser2',
|
||||
password='testpass123',
|
||||
email='test2@example.com'
|
||||
)
|
||||
|
||||
# 设置 API 客户端
|
||||
self.client = APIClient()
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
def test_create_user_profile(self):
|
||||
"""测试创建用户资料"""
|
||||
data = {
|
||||
'user': self.user2.id,
|
||||
'description': '测试用户资料描述'
|
||||
}
|
||||
response = self.client.post('/api/backend/user-profiles/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data['user'], self.user2.id)
|
||||
self.assertEqual(response.data['merchant'], self.merchant.id) # 应该由BaseViewSet自动设置
|
||||
self.assertEqual(response.data['description'], '测试用户资料描述')
|
||||
|
||||
def test_list_user_profiles(self):
|
||||
"""测试列出所有用户资料"""
|
||||
from basic_info.models import UserProfile
|
||||
UserProfile.objects.create(
|
||||
user=self.user,
|
||||
merchant=self.merchant,
|
||||
description='用户1资料'
|
||||
)
|
||||
UserProfile.objects.create(
|
||||
user=self.user2,
|
||||
merchant=self.merchant,
|
||||
description='用户2资料'
|
||||
)
|
||||
|
||||
response = self.client.get('/api/backend/user-profiles/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
results = response.data['results'] if isinstance(response.data, dict) else response.data
|
||||
self.assertEqual(len(results), 2) # 应该只返回当前商户的用户资料
|
||||
|
||||
def test_retrieve_user_profile(self):
|
||||
"""测试检索单个用户资料"""
|
||||
from basic_info.models import UserProfile
|
||||
profile = UserProfile.objects.create(
|
||||
user=self.user,
|
||||
merchant=self.merchant,
|
||||
description='测试资料'
|
||||
)
|
||||
|
||||
response = self.client.get(f'/api/backend/user-profiles/{profile.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['id'], profile.id)
|
||||
self.assertEqual(response.data['description'], '测试资料')
|
||||
self.assertEqual(response.data['user_detail']['username'], 'testuser') # 检查嵌套用户对象
|
||||
|
||||
def test_update_user_profile(self):
|
||||
"""测试更新用户资料"""
|
||||
from basic_info.models import UserProfile
|
||||
profile = UserProfile.objects.create(
|
||||
user=self.user,
|
||||
merchant=self.merchant,
|
||||
description='原始描述'
|
||||
)
|
||||
|
||||
data = {'description': '更新后的描述'}
|
||||
response = self.client.patch(f'/api/backend/user-profiles/{profile.id}/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['description'], '更新后的描述')
|
||||
|
||||
# 验证数据库
|
||||
profile.refresh_from_db()
|
||||
self.assertEqual(profile.description, '更新后的描述')
|
||||
|
||||
def test_delete_user_profile(self):
|
||||
"""测试删除用户资料"""
|
||||
from basic_info.models import UserProfile
|
||||
profile = UserProfile.objects.create(
|
||||
user=self.user,
|
||||
merchant=self.merchant,
|
||||
description='待删除资料'
|
||||
)
|
||||
|
||||
response = self.client.delete(f'/api/backend/user-profiles/{profile.id}/')
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
self.assertFalse(UserProfile.objects.filter(id=profile.id).exists())
|
||||
|
||||
def test_user_profile_filter_by_merchant(self):
|
||||
"""测试用户资料按商户过滤"""
|
||||
# 创建另一个商户
|
||||
other_merchant = Merchant.objects.create(
|
||||
name='其他商户',
|
||||
type=MerchantTypeEnum.FACTORY
|
||||
)
|
||||
|
||||
# 创建另一个商户的员工
|
||||
other_user = User.objects.create_user(
|
||||
username='otheruser',
|
||||
password='testpass123'
|
||||
)
|
||||
other_employee = Employee.objects.create(
|
||||
merchant=other_merchant,
|
||||
sys_user=other_user,
|
||||
name='其他员工'
|
||||
)
|
||||
|
||||
# 为两个商户分别创建用户资料
|
||||
from basic_info.models import UserProfile
|
||||
UserProfile.objects.create(
|
||||
user=self.user,
|
||||
merchant=self.merchant,
|
||||
description='本商户用户资料'
|
||||
)
|
||||
UserProfile.objects.create(
|
||||
user=other_user,
|
||||
merchant=other_merchant,
|
||||
description='其他商户用户资料'
|
||||
)
|
||||
|
||||
# 使用当前用户(属于self.merchant)访问API
|
||||
response = self.client.get('/api/backend/user-profiles/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
results = response.data['results'] if isinstance(response.data, dict) else response.data
|
||||
self.assertEqual(len(results), 1) # 只应该返回当前商户的用户资料
|
||||
self.assertEqual(results[0]['description'], '本商户用户资料')
|
||||
|
||||
Reference in New Issue
Block a user