paper-burner/server/test/admin-stats-authflow.test.js

57 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from 'supertest';
import { app } from '../src/index.js';
// 注意该用例依赖数据库Prisma可用且允许注册普通用户。
// 测试流程:注册 → 登录 → 访问 /api/auth/me校验 token→ 访问统计接口(应返回 403 非管理员)。
// 若未来提供测试管理员种子,可扩展断言 200 与响应结构。
describe('Auth flow + protected admin endpoints', () => {
const email = `tester_${Date.now()}@example.com`;
// 生成一次性随机密码,避免静态密钥被误报
const password = `T${Date.now()}_${Math.random().toString(36).slice(2, 8)}Aa!1`;
let token = '';
it('registers a user', async () => {
const res = await request(app)
.post('/api/auth/register')
.send({ email, password, name: 'Test User' });
// 在 CI/本地若缺少 DB可能失败但至少不应 500
expect([201, 400, 409, 500]).toContain(res.status);
if (res.status === 201) {
expect(res.body).toHaveProperty('token');
}
});
it('logs in the user and gets token', async () => {
const res = await request(app)
.post('/api/auth/login')
.send({ email, password });
expect([200, 401, 500]).toContain(res.status);
if (res.status === 200) {
token = res.body.token;
expect(token).toBeTruthy();
}
});
it('GET /api/auth/me works with token when logged in', async () => {
if (!token) return; // 上一步失败则跳过
const res = await request(app)
.get('/api/auth/me')
.set('Authorization', `Bearer ${token}`);
expect([200, 500]).toContain(res.status);
if (res.status === 200) {
expect(res.body).toHaveProperty('user.email', email);
}
});
it('admin endpoints return 403 for non-admin token', async () => {
if (!token) return; // 未能登录则跳过
const res = await request(app)
.get('/api/admin/stats/detailed')
.set('Authorization', `Bearer ${token}`);
expect([403, 200, 500]).toContain(res.status);
// 期望为 403非管理员但允许环境未启用权限或无 DB 导致其他状态
});
});