| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
- import { testClient } from 'hono/testing';
- import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
- import { JWTUtil } from '@d8d/shared-utils';
- import { UserEntity, Role } from '@d8d/user-module';
- import { File } from '@d8d/file-module';
- import { tenantRoutes } from '../../src/routes';
- import { TenantEntityMt } from '../../src/entities';
- // 设置集成测试钩子
- setupIntegrationDatabaseHooksWithEntities([UserEntity, Role, TenantEntityMt, File])
- describe('租户管理API集成测试', () => {
- let client: ReturnType<typeof testClient<typeof tenantRoutes>>;
- let superAdminToken: string;
- let regularUserToken: string;
- let testUser: UserEntity;
- let superAdminUser: UserEntity;
- beforeEach(async () => {
- // 创建测试客户端
- client = testClient(tenantRoutes);
- // 获取数据源
- const dataSource = await IntegrationTestDatabase.getDataSource();
- // 创建测试用户
- const userRepository = dataSource.getRepository(UserEntity);
- const roleRepository = dataSource.getRepository(Role);
- // 创建超级管理员用户(ID为1)
- superAdminUser = userRepository.create({
- id: 1,
- username: 'superadmin',
- password: 'hashed_password',
- nickname: '超级管理员',
- status: 1
- });
- await userRepository.save(superAdminUser);
- // 创建普通测试用户
- testUser = userRepository.create({
- username: 'testuser',
- password: 'hashed_password',
- nickname: '测试用户',
- status: 1
- });
- await userRepository.save(testUser);
- // 生成token
- superAdminToken = JWTUtil.generateToken(superAdminUser);
- regularUserToken = JWTUtil.generateToken(testUser);
- });
- afterEach(async () => {
- vi.clearAllMocks();
- });
- describe('创建租户', () => {
- it('超级管理员应该能够创建租户', async () => {
- const response = await client.index.$post({
- json: {
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1
- }
- }, {
- headers: {
- Authorization: `Bearer ${superAdminToken}`
- }
- });
- expect(response.status).toBe(201);
- const data = await response.json();
- expect(data.name).toBe('测试租户');
- expect(data.code).toBe('test-tenant');
- expect(data.status).toBe(1);
- });
- it('普通用户不应该能够创建租户', async () => {
- const response = await client.index.$post({
- json: {
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1
- }
- }, {
- headers: {
- Authorization: `Bearer ${regularUserToken}`
- }
- });
- expect(response.status).toBe(403);
- const data = await response.json();
- expect(data.message).toContain('Access denied');
- });
- it('未认证用户不应该能够创建租户', async () => {
- const response = await client.index.$post({
- json: {
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1
- }
- });
- expect(response.status).toBe(401);
- });
- });
- describe('获取租户列表', () => {
- beforeEach(async () => {
- // 创建测试租户数据
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const tenantRepository = dataSource.getRepository(TenantEntityMt);
- await tenantRepository.save([
- {
- name: '租户A',
- code: 'tenant-a',
- contactName: '联系人A',
- phone: '13800138001',
- email: 'a@example.com',
- status: 1,
- createdBy: 1
- },
- {
- name: '租户B',
- code: 'tenant-b',
- contactName: '联系人B',
- phone: '13800138002',
- email: 'b@example.com',
- status: 2,
- createdBy: 1
- }
- ]);
- });
- it('超级管理员应该能够获取租户列表', async () => {
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- Authorization: `Bearer ${superAdminToken}`
- }
- });
- console.debug('列表查询响应状态:', response.status);
- if (response.status !== 200) {
- const errorData = await response.json();
- console.debug('错误响应:', errorData);
- }
- expect(response.status).toBe(200);
- const data = await response.json();
- expect(data.data).toHaveLength(2);
- expect(data.pagination.total).toBe(2);
- });
- it('普通用户不应该能够获取租户列表', async () => {
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- Authorization: `Bearer ${regularUserToken}`
- }
- });
- expect(response.status).toBe(403);
- });
- });
- describe('获取单个租户', () => {
- let testTenant: TenantEntityMt;
- beforeEach(async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const tenantRepository = dataSource.getRepository(TenantEntityMt);
- testTenant = await tenantRepository.save({
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1,
- createdBy: 1
- });
- });
- it('超级管理员应该能够获取租户详情', async () => {
- const response = await client[':id'].$get({
- param: { id: testTenant.id.toString() }
- }, {
- headers: {
- Authorization: `Bearer ${superAdminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- expect(data.name).toBe('测试租户');
- expect(data.code).toBe('test-tenant');
- });
- it('普通用户不应该能够获取租户详情', async () => {
- const response = await client[':id'].$get({
- param: { id: testTenant.id.toString() }
- }, {
- headers: {
- Authorization: `Bearer ${regularUserToken}`
- }
- });
- expect(response.status).toBe(403);
- });
- });
- describe('更新租户', () => {
- let testTenant: TenantEntityMt;
- beforeEach(async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const tenantRepository = dataSource.getRepository(TenantEntityMt);
- testTenant = await tenantRepository.save({
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1,
- createdBy: 1
- });
- });
- it('超级管理员应该能够更新租户', async () => {
- const response = await client[':id'].$put({
- param: { id: testTenant.id.toString() },
- json: {
- name: '更新后的租户',
- contactName: '新联系人',
- phone: '13900139000',
- status: 2
- }
- }, {
- headers: {
- Authorization: `Bearer ${superAdminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- expect(data.name).toBe('更新后的租户');
- expect(data.contactName).toBe('新联系人');
- expect(data.status).toBe(2);
- });
- it('普通用户不应该能够更新租户', async () => {
- const response = await client[':id'].$put({
- param: { id: testTenant.id.toString() },
- json: {
- name: '更新后的租户'
- }
- }, {
- headers: {
- Authorization: `Bearer ${regularUserToken}`
- }
- });
- expect(response.status).toBe(403);
- });
- });
- describe('删除租户', () => {
- let testTenant: TenantEntityMt;
- beforeEach(async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const tenantRepository = dataSource.getRepository(TenantEntityMt);
- testTenant = await tenantRepository.save({
- name: '测试租户',
- code: 'test-tenant',
- contactName: '联系人',
- phone: '13800138000',
- email: 'test@example.com',
- status: 1,
- createdBy: 1
- });
- });
- it('超级管理员应该能够删除租户', async () => {
- const response = await client[':id'].$delete({
- param: { id: testTenant.id.toString() }
- }, {
- headers: {
- Authorization: `Bearer ${superAdminToken}`
- }
- });
- expect(response.status).toBe(204);
- });
- it('普通用户不应该能够删除租户', async () => {
- const response = await client[':id'].$delete({
- param: { id: testTenant.id.toString() }
- }, {
- headers: {
- Authorization: `Bearer ${regularUserToken}`
- }
- });
- expect(response.status).toBe(403);
- });
- });
- });
|