|
|
@@ -0,0 +1,378 @@
|
|
|
+import { describe, it, expect, beforeEach } 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 channelRoutes from '../../src/routes/channel.routes';
|
|
|
+import { Channel } from '../../src/entities/channel.entity';
|
|
|
+
|
|
|
+// 设置集成测试钩子
|
|
|
+setupIntegrationDatabaseHooksWithEntities([UserEntity, Role, Channel])
|
|
|
+
|
|
|
+describe('渠道管理API集成测试', () => {
|
|
|
+ let client: ReturnType<typeof testClient<typeof channelRoutes>>;
|
|
|
+ let testToken: string;
|
|
|
+ let testUser: UserEntity;
|
|
|
+
|
|
|
+ beforeEach(async () => {
|
|
|
+ // 创建测试客户端
|
|
|
+ client = testClient(channelRoutes);
|
|
|
+
|
|
|
+ // 获取数据源
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+
|
|
|
+ // 创建测试用户
|
|
|
+ const userRepository = dataSource.getRepository(UserEntity);
|
|
|
+ testUser = userRepository.create({
|
|
|
+ username: `test_user_${Date.now()}`,
|
|
|
+ password: 'test_password',
|
|
|
+ nickname: '测试用户',
|
|
|
+ registrationSource: 'web'
|
|
|
+ });
|
|
|
+ await userRepository.save(testUser);
|
|
|
+
|
|
|
+ // 生成测试用户的token
|
|
|
+ testToken = JWTUtil.generateToken({
|
|
|
+ id: testUser.id,
|
|
|
+ username: testUser.username,
|
|
|
+ roles: [{name:'user'}]
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('POST /channel/createChannel', () => {
|
|
|
+ it('应该成功创建渠道', async () => {
|
|
|
+ const createData = {
|
|
|
+ channelName: '微信小程序渠道',
|
|
|
+ channelType: '小程序',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ description: '微信小程序渠道描述'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.createChannel.$post({
|
|
|
+ json: createData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('创建渠道响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.success).toBe(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该验证渠道名称重复', async () => {
|
|
|
+ // 先创建一个渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+ const existingChannel = channelRepository.create({
|
|
|
+ channelName: '微信小程序渠道',
|
|
|
+ channelType: '小程序',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(existingChannel);
|
|
|
+
|
|
|
+ // 尝试创建同名渠道
|
|
|
+ const createData = {
|
|
|
+ channelName: '微信小程序渠道', // 重复的名称
|
|
|
+ channelType: '小程序',
|
|
|
+ contactPerson: '李四',
|
|
|
+ contactPhone: '13900139000'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.createChannel.$post({
|
|
|
+ json: createData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(400);
|
|
|
+ if (response.status === 400) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.message).toContain('渠道名称已存在');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝未认证用户的访问', async () => {
|
|
|
+ const createData = {
|
|
|
+ channelName: '测试渠道',
|
|
|
+ channelType: '测试'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.createChannel.$post({
|
|
|
+ json: createData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('POST /channel/deleteChannel', () => {
|
|
|
+ it('应该成功删除渠道', async () => {
|
|
|
+ // 先创建一个渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+ const testChannel = channelRepository.create({
|
|
|
+ channelName: `待删除渠道_${Date.now()}`,
|
|
|
+ channelType: '测试',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(testChannel);
|
|
|
+
|
|
|
+ const deleteData = {
|
|
|
+ channelId: testChannel.channelId
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.deleteChannel.$post({
|
|
|
+ json: deleteData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('删除渠道响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.success).toBe(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证渠道状态变为0(软删除)
|
|
|
+ const deletedChannel = await channelRepository.findOne({
|
|
|
+ where: { channelId: testChannel.channelId }
|
|
|
+ });
|
|
|
+ expect(deletedChannel?.status).toBe(0);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('POST /channel/updateChannel', () => {
|
|
|
+ it('应该成功更新渠道', async () => {
|
|
|
+ // 先创建一个渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+ const testChannel = channelRepository.create({
|
|
|
+ channelName: `原始渠道_${Date.now()}`,
|
|
|
+ channelType: '原始类型',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(testChannel);
|
|
|
+
|
|
|
+ const updateData = {
|
|
|
+ channelId: testChannel.channelId,
|
|
|
+ channelName: '更新后的渠道名称',
|
|
|
+ channelType: '更新类型',
|
|
|
+ contactPerson: '李四',
|
|
|
+ contactPhone: '13900139000'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.updateChannel.$post({
|
|
|
+ json: updateData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('更新渠道响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.success).toBe(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证渠道已更新
|
|
|
+ const updatedChannel = await channelRepository.findOne({
|
|
|
+ where: { channelId: testChannel.channelId }
|
|
|
+ });
|
|
|
+ expect(updatedChannel?.channelName).toBe(updateData.channelName);
|
|
|
+ expect(updatedChannel?.channelType).toBe(updateData.channelType);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该验证更新时的渠道名称重复', async () => {
|
|
|
+ // 创建两个渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+
|
|
|
+ const channel1 = channelRepository.create({
|
|
|
+ channelName: '渠道A',
|
|
|
+ channelType: '类型A',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(channel1);
|
|
|
+
|
|
|
+ const channel2 = channelRepository.create({
|
|
|
+ channelName: '渠道B',
|
|
|
+ channelType: '类型B',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(channel2);
|
|
|
+
|
|
|
+ // 尝试将渠道B的名称改为渠道A的名称
|
|
|
+ const updateData = {
|
|
|
+ channelId: channel2.channelId,
|
|
|
+ channelName: '渠道A' // 重复的名称
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.updateChannel.$post({
|
|
|
+ json: updateData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(400);
|
|
|
+ if (response.status === 400) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.message).toContain('渠道名称已存在');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('GET /channel/getAllChannels', () => {
|
|
|
+ it('应该返回渠道列表', async () => {
|
|
|
+ // 创建一些测试渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+
|
|
|
+ for (let i = 0; i < 3; i++) {
|
|
|
+ const channel = channelRepository.create({
|
|
|
+ channelName: `测试渠道${i}`,
|
|
|
+ channelType: '测试',
|
|
|
+ contactPerson: `联系人${i}`,
|
|
|
+ contactPhone: `1380013800${i}`,
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(channel);
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await client.getAllChannels.$get({
|
|
|
+ query: { skip: 0, take: 10 }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('渠道列表响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data).toHaveProperty('data');
|
|
|
+ expect(data).toHaveProperty('total');
|
|
|
+ expect(Array.isArray(data.data)).toBe(true);
|
|
|
+ expect(data.total).toBeGreaterThanOrEqual(3);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该支持分页参数', async () => {
|
|
|
+ const response = await client.getAllChannels.$get({
|
|
|
+ query: { skip: 0, take: 5 }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('GET /channel/searchChannels', () => {
|
|
|
+ it('应该按名称搜索渠道', async () => {
|
|
|
+ // 创建测试渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+
|
|
|
+ const channel = channelRepository.create({
|
|
|
+ channelName: '微信小程序搜索测试',
|
|
|
+ channelType: '小程序',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(channel);
|
|
|
+
|
|
|
+ const response = await client.searchChannels.$get({
|
|
|
+ query: { name: '微信', skip: 0, take: 10 }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('搜索渠道响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.data.length).toBeGreaterThan(0);
|
|
|
+ expect(data.data[0].channelName).toContain('微信');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('GET /channel/getChannel/:id', () => {
|
|
|
+ it('应该返回指定渠道的详情', async () => {
|
|
|
+ // 先创建一个渠道
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const channelRepository = dataSource.getRepository(Channel);
|
|
|
+ const testChannel = channelRepository.create({
|
|
|
+ channelName: `详情测试渠道_${Date.now()}`,
|
|
|
+ channelType: '测试',
|
|
|
+ contactPerson: '张三',
|
|
|
+ contactPhone: '13800138000',
|
|
|
+ status: 1
|
|
|
+ });
|
|
|
+ await channelRepository.save(testChannel);
|
|
|
+
|
|
|
+ const response = await client['getChannel/:id'].$get({
|
|
|
+ param: { id: testChannel.channelId }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('渠道详情响应状态:', response.status);
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data.channelId).toBe(testChannel.channelId);
|
|
|
+ expect(data.channelName).toBe(testChannel.channelName);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该处理不存在的渠道', async () => {
|
|
|
+ const response = await client['getChannel/:id'].$get({
|
|
|
+ param: { id: 999999 }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ const data = await response.json();
|
|
|
+ expect(data).toBeNull();
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|