from django.test import TestCase from django.contrib.auth.models import User, Permission from rest_framework.test import APIClient from rest_framework import status from basic_info.models import Merchant, UserProfile class UserCreationAPITestCase(TestCase): """测试用户创建 API""" def setUp(self): """设置测试数据""" # 创建商户 self.merchant = Merchant.objects.create( name='测试商户', type=1 # 假设1是有效的MerchantType ) # 创建管理员用户 self.admin_user = User.objects.create_user( username='admin', password='adminpass123', is_staff=True ) # 授予创建用户所需的权限 add_user_perm = Permission.objects.get(codename='add_user') self.admin_user.user_permissions.add(add_user_perm) self.admin_user.save() # 设置 API 客户端 self.client = APIClient() self.client.force_authenticate(user=self.admin_user) def test_create_user_with_profile(self): """测试创建用户和用户资料""" data = { 'username': 'testuser', 'email': 'test@example.com', 'password': 'testpass123', 'is_staff': False, 'description': '测试用户资料', 'merchant_id': self.merchant.id } response = self.client.post('/api/v1/users/create/', data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) # 验证返回的数据 self.assertIn('user', response.data) self.assertIn('profile', response.data) self.assertEqual(response.data['user']['username'], 'testuser') self.assertEqual(response.data['user']['email'], 'test@example.com') self.assertEqual(response.data['user']['is_staff'], False) self.assertEqual(response.data['profile']['description'], '测试用户资料') self.assertEqual(response.data['profile']['merchant'], self.merchant.id) # 验证数据库中的用户 user = User.objects.get(username='testuser') self.assertEqual(user.email, 'test@example.com') self.assertFalse(user.is_staff) # 验证数据库中的用户资料 profile = UserProfile.objects.get(user=user) self.assertEqual(profile.merchant, self.merchant) self.assertEqual(profile.description, '测试用户资料') def test_create_user_with_duplicate_username(self): """测试创建用户时使用重复的用户名""" # 先创建一个用户 User.objects.create_user(username='existinguser', password='pass123') data = { 'username': 'existinguser', # 重复的用户名 'password': 'testpass123', 'merchant_id': self.merchant.id } response = self.client.post('/api/v1/users/create/', data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('username', response.data) def test_create_user_with_invalid_merchant(self): """测试创建用户时使用无效的merchant_id""" data = { 'username': 'testuser', 'password': 'testpass123', 'merchant_id': 999 # 不存在的merchant_id } response = self.client.post('/api/v1/users/create/', data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('merchant_id', response.data) def test_create_user_unauthenticated(self): """测试未认证用户创建用户""" self.client.force_authenticate(user=None) data = { 'username': 'testuser', 'password': 'testpass123', 'merchant_id': self.merchant.id } response = self.client.post('/api/v1/users/create/', data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_create_user_with_short_password(self): """测试创建用户时密码过短""" data = { 'username': 'testuser', 'password': '123', # 密码过短 'merchant_id': self.merchant.id } response = self.client.post('/api/v1/users/create/', data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('password', response.data)