refactor: 完成静态网站转换,移除所有 CMS 和动态功能
- 删除数据库相关代码 (src/db/) - 删除 API 路由 (src/app/api/) - 删除认证相关代码 (src/lib/auth/, src/providers/) - 删除监控和安全中间件 (src/lib/security/, src/lib/monitoring/) - 删除 hooks (use-news, use-products, use-services) - 更新组件为静态数据源 - 添加 nginx 静态配置和部署脚本 - 添加 static-link 组件
This commit is contained in:
1 parent
cd1d6aa28a
commit
6403489954
197 files changed
+654
-24762
No files matched your search
@@ -1,209 +0,0 @@
|
||||
import { auth } from './auth';
|
||||
import { db } from '@/db';
|
||||
import { users } from '@/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
jest.mock('@/db', () => ({
|
||||
db: {
|
||||
select: jest.fn(() => ({
|
||||
from: jest.fn(() => ({
|
||||
where: jest.fn(() => ({
|
||||
limit: jest.fn(),
|
||||
})),
|
||||
})),
|
||||
})),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('bcryptjs');
|
||||
|
||||
describe('auth', () => {
|
||||
const mockUser = {
|
||||
id: '1',
|
||||
email: 'test@example.com',
|
||||
name: 'Test User',
|
||||
passwordHash: 'hashedpassword',
|
||||
isAdmin: true,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('auth configuration', () => {
|
||||
it('应该导出auth对象', () => {
|
||||
expect(auth).toBeDefined();
|
||||
expect(typeof auth).toBe('function');
|
||||
});
|
||||
|
||||
it('应该支持signIn方法', () => {
|
||||
expect(typeof auth).toBe('function');
|
||||
});
|
||||
|
||||
it('应该支持signOut方法', () => {
|
||||
expect(typeof auth).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('CredentialsProvider 验证逻辑', () => {
|
||||
it('应该成功验证正确的邮箱和密码', async () => {
|
||||
const mockLimit = jest.fn().mockResolvedValue([mockUser]);
|
||||
const mockWhere = jest.fn().mockReturnValue({ limit: mockLimit });
|
||||
const mockFrom = jest.fn().mockReturnValue({ where: mockWhere });
|
||||
const mockSelect = jest.fn().mockReturnValue({ from: mockFrom });
|
||||
|
||||
(db.select as jest.Mock).mockImplementation(() => ({ from: mockFrom }));
|
||||
(bcrypt.compare as jest.Mock).mockResolvedValue(true);
|
||||
|
||||
const credentials = {
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
const userResult = await db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.email, credentials.email as string))
|
||||
.limit(1);
|
||||
|
||||
const user = userResult[0];
|
||||
const isValid = await bcrypt.compare(
|
||||
credentials.password as string,
|
||||
user.passwordHash || ''
|
||||
);
|
||||
|
||||
expect(user).toEqual(mockUser);
|
||||
expect(isValid).toBe(true);
|
||||
});
|
||||
|
||||
it('应该拒绝不存在的用户', async () => {
|
||||
const mockLimit = jest.fn().mockResolvedValue([]);
|
||||
const mockWhere = jest.fn().mockReturnValue({ limit: mockLimit });
|
||||
const mockFrom = jest.fn().mockReturnValue({ where: mockWhere });
|
||||
|
||||
(db.select as jest.Mock).mockImplementation(() => ({ from: mockFrom }));
|
||||
|
||||
const credentials = {
|
||||
email: 'nonexistent@example.com',
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
const userResult = await db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.email, credentials.email as string))
|
||||
.limit(1);
|
||||
|
||||
expect(userResult).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('应该拒绝错误的密码', async () => {
|
||||
const mockLimit = jest.fn().mockResolvedValue([mockUser]);
|
||||
const mockWhere = jest.fn().mockReturnValue({ limit: mockLimit });
|
||||
const mockFrom = jest.fn().mockReturnValue({ where: mockWhere });
|
||||
|
||||
(db.select as jest.Mock).mockImplementation(() => ({ from: mockFrom }));
|
||||
(bcrypt.compare as jest.Mock).mockResolvedValue(false);
|
||||
|
||||
const credentials = {
|
||||
email: 'test@example.com',
|
||||
password: 'wrongpassword',
|
||||
};
|
||||
|
||||
const userResult = await db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.email, credentials.email as string))
|
||||
.limit(1);
|
||||
|
||||
const user = userResult[0];
|
||||
const isValid = await bcrypt.compare(
|
||||
credentials.password as string,
|
||||
user.passwordHash || ''
|
||||
);
|
||||
|
||||
expect(isValid).toBe(false);
|
||||
});
|
||||
|
||||
it('应该拒绝缺少邮箱的凭证', async () => {
|
||||
const credentials = {
|
||||
password: 'password123',
|
||||
};
|
||||
|
||||
expect(credentials.email).toBeUndefined();
|
||||
});
|
||||
|
||||
it('应该拒绝缺少密码的凭证', async () => {
|
||||
const credentials = {
|
||||
email: 'test@example.com',
|
||||
};
|
||||
|
||||
expect(credentials.password).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('JWT callback 逻辑', () => {
|
||||
it('应该在用户登录时添加token信息', async () => {
|
||||
const token = {};
|
||||
const user = {
|
||||
id: '1',
|
||||
email: 'test@example.com',
|
||||
name: 'Test User',
|
||||
isAdmin: true,
|
||||
};
|
||||
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.isAdmin = user.isAdmin;
|
||||
}
|
||||
|
||||
expect(token).toEqual({
|
||||
id: user.id,
|
||||
isAdmin: user.isAdmin,
|
||||
});
|
||||
});
|
||||
|
||||
it('应该在用户不存在时保持token不变', async () => {
|
||||
const token = { id: '1', isAdmin: true };
|
||||
const user = undefined;
|
||||
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.isAdmin = user.isAdmin;
|
||||
}
|
||||
|
||||
expect(token).toEqual({ id: '1', isAdmin: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('Session callback 逻辑', () => {
|
||||
it('应该在会话中添加用户信息', async () => {
|
||||
const session = { user: { name: 'Test User' } };
|
||||
const token = { id: '1', isAdmin: true };
|
||||
|
||||
if (session.user) {
|
||||
session.user.id = token.id as string;
|
||||
session.user.isAdmin = token.isAdmin as boolean;
|
||||
}
|
||||
|
||||
expect(session.user).toEqual({
|
||||
...session.user,
|
||||
id: token.id,
|
||||
isAdmin: token.isAdmin,
|
||||
});
|
||||
});
|
||||
|
||||
it('应该处理没有user的session', async () => {
|
||||
const session = {};
|
||||
const token = { id: '1', isAdmin: true };
|
||||
|
||||
if (session.user) {
|
||||
session.user.id = token.id as string;
|
||||
session.user.isAdmin = token.isAdmin as boolean;
|
||||
}
|
||||
|
||||
expect(session).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user