api-server/test/m2.e2e-spec.ts

149 lines
5.7 KiB
TypeScript
Raw Normal View History

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from '../src/app.module';
describe('M2 E2E Tests', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.setGlobalPrefix('api', { exclude: ['admin-api/(.*)', 'internal/(.*)'] });
await app.init();
});
afterAll(async () => { await app.close(); });
async function loginAdmin(): Promise<string> {
const res = await request(app.getHttpServer())
.post('/admin-api/auth/login')
.send({ email: 'admin@zhixi.app', password: 'admin123' });
return res.body?.data?.accessToken || '';
}
// ══════════════════════════════════════════════
// M2-01: User & Account 深化
// ══════════════════════════════════════════════
describe('M2-01 User & Account Deepening', () => {
let token: string;
beforeAll(async () => { token = await loginAdmin(); });
// ── Admin membership management ──
it('GET /admin-api/users/memberships → 200 member list', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.get('/admin-api/users/memberships')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(Array.isArray(res.body.data)).toBe(true);
});
it('POST /admin-api/users/memberships → 200 assign membership', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.post('/admin-api/users/memberships')
.set('Authorization', `Bearer ${token}`)
.send({ userId: 'user1', planId: 'plan-free' })
.expect([200, 201]);
expect(res.body.data).toHaveProperty('id');
});
// ── Admin deletion requests ──
it('GET /admin-api/users/deletion-requests → 200 list', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.get('/admin-api/users/deletion-requests')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(Array.isArray(res.body.data)).toBe(true);
});
it('POST /admin-api/users/deletion-requests/:id/approve → approve', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.post('/admin-api/users/deletion-requests/test-id/approve')
.set('Authorization', `Bearer ${token}`)
.expect([200, 201]);
expect(res.body.data).toHaveProperty('status', 'completed');
});
it('POST /admin-api/users/deletion-requests/:id/reject → reject', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.post('/admin-api/users/deletion-requests/test-id2/reject')
.set('Authorization', `Bearer ${token}`)
.expect([200, 201]);
expect(res.body.data).toHaveProperty('status', 'cancelled');
});
// ── Admin device view ──
it('GET /admin-api/users/:userId/devices → 200 device list', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.get('/admin-api/users/user1/devices')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(Array.isArray(res.body.data)).toBe(true);
});
});
// ══════════════════════════════════════════════
// M2-02: Workspace & KnowledgeBase
// ══════════════════════════════════════════════
describe('M2-02 Workspace & KnowledgeBase', () => {
let token: string;
beforeAll(async () => { token = await loginAdmin(); });
it('POST /api/knowledge-bases → 201 create KB', async () => {
const res = await request(app.getHttpServer())
.post('/api/knowledge-bases')
.send({ title: 'E2E Test KB', description: 'test' })
.expect([200, 201]);
expect(res.body.data).toHaveProperty('id');
});
it('GET /api/knowledge-bases/:id/folders → 200 folder list', async () => {
const kb = await request(app.getHttpServer())
.post('/api/knowledge-bases')
.send({ title: 'Folder Test', description: 'test' });
const kbId = kb.body?.data?.id;
if (!kbId) return;
const res = await request(app.getHttpServer())
.get(`/api/knowledge-bases/${kbId}/folders`)
.expect(200);
expect(Array.isArray(res.body.data)).toBe(true);
});
it('POST /api/knowledge-bases/:id/folders → 201 create folder', async () => {
const kb = await request(app.getHttpServer())
.post('/api/knowledge-bases')
.send({ title: 'Folder Create Test', description: 'test' });
const kbId = kb.body?.data?.id;
if (!kbId) return;
const res = await request(app.getHttpServer())
.post(`/api/knowledge-bases/${kbId}/folders`)
.send({ name: 'Chapter 1' })
.expect([200, 201]);
expect(res.body.data).toHaveProperty('id');
});
it('GET /admin-api/knowledge-bases → 200 admin KB list', async () => {
if (!token) return;
const res = await request(app.getHttpServer())
.get('/admin-api/knowledge-bases')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(res.body.data).toHaveProperty('items');
expect(res.body.data).toHaveProperty('total');
});
});
});