添加后端集成测试套件
- 实现 82 个集成测试用例覆盖所有 API 端点 - 使用真实数据库连接,无 mock 逻辑 - 修复 PUT 请求只更新提供字段的逻辑 - 配置 Vitest 串行执行避免测试竞态 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6df528f187
commit
697430feed
18 files changed
+4333
-8
No files matched your search
@@ -0,0 +1,341 @@
|
||||
import { beforeAll, afterAll, beforeEach, describe, it, expect } from 'vitest';
|
||||
import { createTestApp } from './helpers/app';
|
||||
import { cleanupDatabase, prisma } from './setup';
|
||||
|
||||
describe('推送渠道 API', () => {
|
||||
let app: Awaited<ReturnType<typeof createTestApp>>;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
describe('POST /api/v1/channels', () => {
|
||||
it('应成功创建邮件类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '邮件通知',
|
||||
type: 'email',
|
||||
config: {
|
||||
to: 'test@example.com',
|
||||
subject: '资讯周报',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.message).toBe('推送渠道创建成功');
|
||||
expect(body.data.name).toBe(payload.name);
|
||||
expect(body.data.type).toBe('email');
|
||||
});
|
||||
|
||||
it('应成功创建 Slack 类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: 'Slack 通知',
|
||||
type: 'slack',
|
||||
config: {
|
||||
webhookUrl: 'https://hooks.slack.com/services/xxx',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('slack');
|
||||
});
|
||||
|
||||
it('应成功创建钉钉类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '钉钉通知',
|
||||
type: 'dingtalk',
|
||||
config: {
|
||||
webhookUrl: 'https://oapi.dingtalk.com/robot/send',
|
||||
secret: 'SEC123',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('dingtalk');
|
||||
});
|
||||
|
||||
it('应成功创建企业微信类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '企业微信通知',
|
||||
type: 'wechat',
|
||||
config: {
|
||||
webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('wechat');
|
||||
});
|
||||
|
||||
it('缺少必填字段时应返回 400 错误', async () => {
|
||||
const payload = {
|
||||
name: '不完整渠道',
|
||||
type: 'email',
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(false);
|
||||
expect(body.error).toContain('缺少必填字段');
|
||||
});
|
||||
|
||||
it('enabled 默认值应为 true', async () => {
|
||||
const payload = {
|
||||
name: '测试渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/channels', () => {
|
||||
it('应返回推送渠道列表', async () => {
|
||||
await prisma.channel.createMany({
|
||||
data: [
|
||||
{
|
||||
name: '邮件渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'Slack 渠道',
|
||||
type: 'slack',
|
||||
config: { webhookUrl: 'https://hooks.slack.com/xxx' },
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.pagination.total).toBe(2);
|
||||
});
|
||||
|
||||
it('应支持按类型筛选', async () => {
|
||||
await prisma.channel.createMany({
|
||||
data: [
|
||||
{
|
||||
name: '邮件渠道 1',
|
||||
type: 'email',
|
||||
config: { to: 'test1@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'Slack 渠道 1',
|
||||
type: 'slack',
|
||||
config: { webhookUrl: 'https://hooks.slack.com/xxx' },
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels?type=email',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].type).toBe('email');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/channels/:id', () => {
|
||||
it('应返回单个推送渠道及其关联的主题', async () => {
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '测试渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: '测试主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * *' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.topicChannel.create({
|
||||
data: {
|
||||
topicId: topic.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.id).toBe(channel.id);
|
||||
expect(body.data.topicChannels).toHaveLength(1);
|
||||
expect(body.data.topicChannels[0].topic.name).toBe('测试主题');
|
||||
});
|
||||
|
||||
it('推送渠道不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(false);
|
||||
expect(body.error).toContain('推送渠道不存在');
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/v1/channels/:id', () => {
|
||||
it('应成功更新推送渠道', async () => {
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '原始渠道',
|
||||
type: 'email',
|
||||
config: { to: 'old@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updateData = {
|
||||
name: '更新后渠道',
|
||||
config: { to: 'new@example.com' },
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
payload: updateData,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.name).toBe('更新后渠道');
|
||||
expect(body.data.config.to).toBe('new@example.com');
|
||||
expect(body.data.enabled).toBe(false);
|
||||
expect(body.message).toBe('推送渠道更新成功');
|
||||
});
|
||||
|
||||
it('更新不存在的推送渠道时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/v1/channels/99999',
|
||||
payload: {
|
||||
name: '更新',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/channels/:id', () => {
|
||||
it('应成功删除推送渠道', async () => {
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '待删除渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('推送渠道删除成功');
|
||||
|
||||
const deleted = await prisma.channel.findUnique({
|
||||
where: { id: channel.id },
|
||||
});
|
||||
expect(deleted).toBeNull();
|
||||
});
|
||||
|
||||
it('删除不存在的推送渠道时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: '/api/v1/channels/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user