|
|
@@ -0,0 +1,350 @@
|
|
|
+import { describe, it, expect, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
|
|
|
+import { testClient } from 'hono/testing';
|
|
|
+import {
|
|
|
+ IntegrationTestDatabase,
|
|
|
+ TestDataFactory
|
|
|
+} from '../../../__test_utils__/integration-test-db';
|
|
|
+import { authRoutes } from '../../../api';
|
|
|
+import { AuthService } from '../../../modules/auth/auth.service';
|
|
|
+import { UserService } from '../../../modules/users/user.service';
|
|
|
+
|
|
|
+// 设置集成测试钩子
|
|
|
+beforeAll(async () => {
|
|
|
+ await IntegrationTestDatabase.initialize();
|
|
|
+});
|
|
|
+
|
|
|
+afterEach(async () => {
|
|
|
+ await IntegrationTestDatabase.clearAllData();
|
|
|
+});
|
|
|
+
|
|
|
+afterAll(async () => {
|
|
|
+ await IntegrationTestDatabase.cleanup();
|
|
|
+});
|
|
|
+
|
|
|
+describe('认证API集成测试 (使用hono/testing)', () => {
|
|
|
+ let client: ReturnType<typeof testClient<typeof authRoutes>>['api']['v1'];
|
|
|
+ let authService: AuthService;
|
|
|
+ let userService: UserService;
|
|
|
+ let testToken: string;
|
|
|
+ let testUser: any;
|
|
|
+
|
|
|
+ beforeEach(async () => {
|
|
|
+ // 创建测试客户端
|
|
|
+ client = testClient(authRoutes).api.v1;
|
|
|
+
|
|
|
+ // 获取数据源
|
|
|
+ const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ if (!dataSource) throw new Error('Database not initialized');
|
|
|
+
|
|
|
+ // 初始化服务
|
|
|
+ userService = new UserService(dataSource);
|
|
|
+ authService = new AuthService(userService);
|
|
|
+
|
|
|
+ // 创建测试用户
|
|
|
+ testUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
+ username: 'testuser',
|
|
|
+ password: 'TestPassword123!',
|
|
|
+ email: 'testuser@example.com'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 生成测试用户的token
|
|
|
+ testToken = authService.generateToken(testUser);
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('登录端点测试 (POST /api/v1/auth/login)', () => {
|
|
|
+ it('应该使用正确凭据成功登录', async () => {
|
|
|
+ const loginData = {
|
|
|
+ username: 'testuser',
|
|
|
+ password: 'TestPassword123!'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ if (response.status === 200) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData).toHaveProperty('token');
|
|
|
+ expect(responseData).toHaveProperty('user');
|
|
|
+ expect(responseData.user.username).toBe('testuser');
|
|
|
+ expect(responseData.user.email).toBe('testuser@example.com');
|
|
|
+ expect(typeof responseData.token).toBe('string');
|
|
|
+ expect(responseData.token.length).toBeGreaterThan(0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝错误密码的登录', async () => {
|
|
|
+ const loginData = {
|
|
|
+ username: 'testuser',
|
|
|
+ password: 'WrongPassword123!'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('用户名或密码错误');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝不存在的用户登录', async () => {
|
|
|
+ const loginData = {
|
|
|
+ username: 'nonexistent_user',
|
|
|
+ password: 'TestPassword123!'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('用户名或密码错误');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝禁用账户的登录', async () => {
|
|
|
+ // 创建禁用账户
|
|
|
+ const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ if (!dataSource) throw new Error('Database not initialized');
|
|
|
+
|
|
|
+ const disabledUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
+ username: 'disabled_user',
|
|
|
+ password: 'TestPassword123!',
|
|
|
+ email: 'disabled@example.com',
|
|
|
+ isDisabled: 1
|
|
|
+ });
|
|
|
+
|
|
|
+ const loginData = {
|
|
|
+ username: 'disabled_user',
|
|
|
+ password: 'TestPassword123!'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('用户名或密码错误');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('令牌验证端点测试 (GET /api/v1/auth/sso-verify)', () => {
|
|
|
+ it('应该成功验证有效令牌', async () => {
|
|
|
+ const response = await client.auth['sso-verify'].$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ if (response.status === 200) {
|
|
|
+ const responseText = await response.text();
|
|
|
+ expect(responseText).toBe('OK');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝无效令牌', async () => {
|
|
|
+ const response = await client.auth['sso-verify'].$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': 'Bearer invalid.token.here'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('无效令牌');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝过期令牌', async () => {
|
|
|
+ // 创建过期令牌(这里需要修改JWT配置来创建过期令牌)
|
|
|
+ // 由于generateToken方法不支持参数,我们需要模拟一个过期令牌
|
|
|
+ const expiredToken = 'expired.jwt.token.here';
|
|
|
+
|
|
|
+ // 等待令牌过期
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
+
|
|
|
+ const response = await client.auth['sso-verify'].$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${expiredToken}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('令牌验证失败');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('用户信息端点测试 (GET /api/v1/auth/me)', () => {
|
|
|
+ it('应该成功获取用户信息', async () => {
|
|
|
+ const response = await client.auth.me.$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ if (response.status === 200) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData).toHaveProperty('username');
|
|
|
+ expect(responseData).toHaveProperty('email');
|
|
|
+ expect(responseData.username).toBe('testuser');
|
|
|
+ expect(responseData.email).toBe('testuser@example.com');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝无令牌的用户信息请求', async () => {
|
|
|
+ const response = await client.auth.me.$get();
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('需要认证');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝无效令牌的用户信息请求', async () => {
|
|
|
+ const response = await client.auth.me.$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': 'Bearer invalid.token.here'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData.message).toContain('无效的令牌');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('错误处理测试', () => {
|
|
|
+ it('应该正确处理认证失败错误', async () => {
|
|
|
+ const loginData = {
|
|
|
+ username: 'testuser',
|
|
|
+ password: 'WrongPassword'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData).toHaveProperty('code', 401);
|
|
|
+ expect(responseData).toHaveProperty('message');
|
|
|
+ expect(responseData.message).toContain('用户名或密码错误');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该正确处理令牌过期错误', async () => {
|
|
|
+ // 模拟过期令牌
|
|
|
+ const expiredToken = 'expired.jwt.token.here';
|
|
|
+
|
|
|
+ const response = await client.auth['sso-verify'].$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${expiredToken}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(response.status).toBe(401);
|
|
|
+ if (response.status === 401) {
|
|
|
+ const responseData = await response.json();
|
|
|
+ expect(responseData).toHaveProperty('code', 401);
|
|
|
+ expect(responseData.message).toContain('令牌验证失败');
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该正确处理权限不足错误', async () => {
|
|
|
+ // 创建普通用户(无管理员权限)
|
|
|
+ const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ if (!dataSource) throw new Error('Database not initialized');
|
|
|
+
|
|
|
+ const regularUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
+ username: 'regular_user',
|
|
|
+ password: 'TestPassword123!',
|
|
|
+ email: 'regular@example.com'
|
|
|
+ });
|
|
|
+
|
|
|
+ const regularToken = authService.generateToken(regularUser);
|
|
|
+
|
|
|
+ // 尝试访问需要认证的端点(这里使用/me端点)
|
|
|
+ const response = await client.auth.me.$get({
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${regularToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 普通用户应该能够访问自己的信息
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('性能基准测试', () => {
|
|
|
+ it('登录操作响应时间应小于200ms', async () => {
|
|
|
+ const loginData = {
|
|
|
+ username: 'testuser',
|
|
|
+ password: 'TestPassword123!'
|
|
|
+ };
|
|
|
+
|
|
|
+ const startTime = Date.now();
|
|
|
+ const response = await client.auth.login.$post({
|
|
|
+ json: loginData
|
|
|
+ });
|
|
|
+ const endTime = Date.now();
|
|
|
+ const responseTime = endTime - startTime;
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
|
|
|
+ });
|
|
|
+
|
|
|
+ it('令牌验证操作响应时间应小于200ms', async () => {
|
|
|
+ const startTime = Date.now();
|
|
|
+ const response = await client.auth['sso-verify'].$get(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ const endTime = Date.now();
|
|
|
+ const responseTime = endTime - startTime;
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|