|
|
@@ -5,7 +5,7 @@ import { RoleServiceMt } from '../../src/services/role.service.mt';
|
|
|
import { UserEntityMt } from '../../src/entities/user.entity';
|
|
|
import { RoleMt } from '../../src/entities/role.entity';
|
|
|
import { AppDataSource, initializeDataSource } from '@d8d/shared-utils';
|
|
|
-import { File } from '@d8d/file-module';
|
|
|
+import { FileMt } from '@d8d/file-module-mt';
|
|
|
|
|
|
// 确保测试环境变量被设置
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
@@ -17,7 +17,7 @@ describe('User Integration Tests', () => {
|
|
|
|
|
|
beforeAll(() => {
|
|
|
// 使用预先配置的数据源
|
|
|
- initializeDataSource([UserEntityMt, RoleMt, File])
|
|
|
+ initializeDataSource([UserEntityMt, RoleMt, FileMt])
|
|
|
dataSource = AppDataSource;
|
|
|
})
|
|
|
|
|
|
@@ -37,8 +37,8 @@ describe('User Integration Tests', () => {
|
|
|
|
|
|
|
|
|
|
|
|
- describe('User CRUD Operations', () => {
|
|
|
- it('should create and retrieve a user', async () => {
|
|
|
+ describe('用户CRUD操作', () => {
|
|
|
+ it('应该创建并检索用户', async () => {
|
|
|
// Create user
|
|
|
const userData = {
|
|
|
username: 'integrationuser',
|
|
|
@@ -51,7 +51,7 @@ describe('User Integration Tests', () => {
|
|
|
...userData
|
|
|
}
|
|
|
|
|
|
- const createdUser = await userService.createUser(userData);
|
|
|
+ const createdUser = await userService.createUser(userData, 1);
|
|
|
|
|
|
expect(createdUser.id).toBeDefined();
|
|
|
expect(createdUser.username).toBe(userData.username);
|
|
|
@@ -65,7 +65,7 @@ describe('User Integration Tests', () => {
|
|
|
expect(retrievedUser?.username).toBe(userData.username);
|
|
|
});
|
|
|
|
|
|
- it('should update user information', async () => {
|
|
|
+ it('应该更新用户信息', async () => {
|
|
|
// Create user first
|
|
|
const userData = {
|
|
|
username: 'updateuser',
|
|
|
@@ -73,7 +73,7 @@ describe('User Integration Tests', () => {
|
|
|
email: 'update@example.com'
|
|
|
};
|
|
|
|
|
|
- const createdUser = await userService.createUser(userData);
|
|
|
+ const createdUser = await userService.createUser(userData, 1);
|
|
|
|
|
|
// Update user
|
|
|
const updateData = {
|
|
|
@@ -88,14 +88,14 @@ describe('User Integration Tests', () => {
|
|
|
expect(updatedUser?.nickname).toBe(updateData.nickname);
|
|
|
});
|
|
|
|
|
|
- it('should delete user', async () => {
|
|
|
+ it('应该删除用户', async () => {
|
|
|
// Create user first
|
|
|
const userData = {
|
|
|
username: 'deleteuser',
|
|
|
password: 'password123'
|
|
|
};
|
|
|
|
|
|
- const createdUser = await userService.createUser(userData);
|
|
|
+ const createdUser = await userService.createUser(userData, 1);
|
|
|
|
|
|
// Delete user
|
|
|
const deleteResult = await userService.deleteUser(createdUser.id);
|
|
|
@@ -106,27 +106,27 @@ describe('User Integration Tests', () => {
|
|
|
expect(retrievedUser).toBeNull();
|
|
|
});
|
|
|
|
|
|
- it('should get user by username', async () => {
|
|
|
+ it('应该根据用户名获取用户', async () => {
|
|
|
const userData = {
|
|
|
username: 'usernameuser',
|
|
|
password: 'password123'
|
|
|
};
|
|
|
|
|
|
- await userService.createUser(userData);
|
|
|
+ await userService.createUser(userData, 1);
|
|
|
|
|
|
const foundUser = await userService.getUserByUsername('usernameuser');
|
|
|
expect(foundUser).toBeDefined();
|
|
|
expect(foundUser?.username).toBe('usernameuser');
|
|
|
});
|
|
|
|
|
|
- it('should get user by account (username or email)', async () => {
|
|
|
+ it('应该根据账号(用户名或邮箱)获取用户', async () => {
|
|
|
const userData = {
|
|
|
username: 'accountuser',
|
|
|
password: 'password123',
|
|
|
email: 'account@example.com'
|
|
|
};
|
|
|
|
|
|
- await userService.createUser(userData);
|
|
|
+ await userService.createUser(userData, 1);
|
|
|
|
|
|
// Find by username
|
|
|
const byUsername = await userService.getUserByAccount('accountuser');
|
|
|
@@ -140,30 +140,32 @@ describe('User Integration Tests', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('User-Role Relationship', () => {
|
|
|
- it('should assign roles to user', async () => {
|
|
|
+ describe('用户角色关系', () => {
|
|
|
+ it('应该为用户分配角色', async () => {
|
|
|
// Create user
|
|
|
const userData = {
|
|
|
username: 'roleuser',
|
|
|
password: 'password123'
|
|
|
};
|
|
|
- const user = await userService.createUser(userData);
|
|
|
+ const user = await userService.createUser(userData, 1);
|
|
|
|
|
|
// Create roles
|
|
|
const adminRole = await roleService.create({
|
|
|
name: 'admin',
|
|
|
description: 'Administrator role',
|
|
|
- permissions: ['user:create', 'user:delete']
|
|
|
+ permissions: ['user:create', 'user:delete'],
|
|
|
+ tenantId: 1
|
|
|
});
|
|
|
|
|
|
const userRole = await roleService.create({
|
|
|
name: 'user',
|
|
|
description: 'Regular user role',
|
|
|
- permissions: ['user:read']
|
|
|
+ permissions: ['user:read'],
|
|
|
+ tenantId: 1
|
|
|
});
|
|
|
|
|
|
// Assign roles to user
|
|
|
- const updatedUser = await userService.assignRoles(user.id, [adminRole.id, userRole.id]);
|
|
|
+ const updatedUser = await userService.assignRoles(user.id, [adminRole.id, userRole.id], 1);
|
|
|
|
|
|
expect(updatedUser).toBeDefined();
|
|
|
expect(updatedUser?.roles).toHaveLength(2);
|
|
|
@@ -172,8 +174,8 @@ describe('User Integration Tests', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('User List Operations', () => {
|
|
|
- it('should get all users', async () => {
|
|
|
+ describe('用户列表操作', () => {
|
|
|
+ it('应该获取所有用户', async () => {
|
|
|
// Create multiple users
|
|
|
const usersData = [
|
|
|
{ username: 'user1', password: 'password123' },
|
|
|
@@ -182,7 +184,7 @@ describe('User Integration Tests', () => {
|
|
|
];
|
|
|
|
|
|
for (const userData of usersData) {
|
|
|
- await userService.createUser(userData);
|
|
|
+ await userService.createUser(userData, 1);
|
|
|
}
|
|
|
|
|
|
const allUsers = await userService.getUsers();
|
|
|
@@ -194,26 +196,26 @@ describe('User Integration Tests', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('Password Verification', () => {
|
|
|
- it('should verify correct password', async () => {
|
|
|
+ describe('密码验证', () => {
|
|
|
+ it('应该验证正确密码', async () => {
|
|
|
const userData = {
|
|
|
username: 'verifyuser',
|
|
|
password: 'correctpassword'
|
|
|
};
|
|
|
|
|
|
- const user = await userService.createUser(userData);
|
|
|
+ const user = await userService.createUser(userData, 1);
|
|
|
|
|
|
const isCorrect = await userService.verifyPassword(user, 'correctpassword');
|
|
|
expect(isCorrect).toBe(true);
|
|
|
});
|
|
|
|
|
|
- it('should reject incorrect password', async () => {
|
|
|
+ it('应该拒绝错误密码', async () => {
|
|
|
const userData = {
|
|
|
username: 'verifyuser2',
|
|
|
password: 'correctpassword'
|
|
|
};
|
|
|
|
|
|
- const user = await userService.createUser(userData);
|
|
|
+ const user = await userService.createUser(userData, 1);
|
|
|
|
|
|
const isCorrect = await userService.verifyPassword(user, 'wrongpassword');
|
|
|
expect(isCorrect).toBe(false);
|