- 新增验证码登录接口文档 (POST /auth/verification-code-login) - 新增发送登录验证码接口文档 (POST /auth/send-login-verification-code) - 更新接口列表和数量统计 (21个 -> 23个接口) - 添加验证码登录测试场景和cURL示例 - 完善错误码说明和响应格式 - 确保文档与当前实现完全一致
155 lines
4.4 KiB
TypeScript
155 lines
4.4 KiB
TypeScript
/**
|
|
* 登录业务服务测试
|
|
*/
|
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { LoginService } from './login.service';
|
|
import { LoginCoreService } from '../../../core/login_core/login_core.service';
|
|
|
|
describe('LoginService', () => {
|
|
let service: LoginService;
|
|
let loginCoreService: jest.Mocked<LoginCoreService>;
|
|
|
|
const mockUser = {
|
|
id: BigInt(1),
|
|
username: 'testuser',
|
|
email: 'test@example.com',
|
|
phone: '+8613800138000',
|
|
password_hash: '$2b$12$hashedpassword',
|
|
nickname: '测试用户',
|
|
github_id: null as string | null,
|
|
avatar_url: null as string | null,
|
|
role: 1,
|
|
email_verified: false,
|
|
status: 'active' as any,
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const mockLoginCoreService = {
|
|
login: jest.fn(),
|
|
register: jest.fn(),
|
|
githubOAuth: jest.fn(),
|
|
sendPasswordResetCode: jest.fn(),
|
|
resetPassword: jest.fn(),
|
|
changePassword: jest.fn(),
|
|
sendEmailVerification: jest.fn(),
|
|
verifyEmailCode: jest.fn(),
|
|
resendEmailVerification: jest.fn(),
|
|
verificationCodeLogin: jest.fn(),
|
|
sendLoginVerificationCode: jest.fn(),
|
|
debugVerificationCode: jest.fn(),
|
|
};
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
LoginService,
|
|
{
|
|
provide: LoginCoreService,
|
|
useValue: mockLoginCoreService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<LoginService>(LoginService);
|
|
loginCoreService = module.get(LoginCoreService);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe('login', () => {
|
|
it('should login successfully', async () => {
|
|
loginCoreService.login.mockResolvedValue({
|
|
user: mockUser,
|
|
isNewUser: false
|
|
});
|
|
|
|
const result = await service.login({
|
|
identifier: 'testuser',
|
|
password: 'password123'
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.data?.user.username).toBe('testuser');
|
|
expect(result.data?.access_token).toBeDefined();
|
|
});
|
|
|
|
it('should handle login failure', async () => {
|
|
loginCoreService.login.mockRejectedValue(new Error('用户名或密码错误'));
|
|
|
|
const result = await service.login({
|
|
identifier: 'testuser',
|
|
password: 'wrongpassword'
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error_code).toBe('LOGIN_FAILED');
|
|
});
|
|
});
|
|
|
|
describe('register', () => {
|
|
it('should register successfully', async () => {
|
|
loginCoreService.register.mockResolvedValue({
|
|
user: mockUser,
|
|
isNewUser: true
|
|
});
|
|
|
|
const result = await service.register({
|
|
username: 'testuser',
|
|
password: 'password123',
|
|
nickname: '测试用户'
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.data?.user.username).toBe('testuser');
|
|
expect(result.data?.is_new_user).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('verificationCodeLogin', () => {
|
|
it('should login with verification code successfully', async () => {
|
|
loginCoreService.verificationCodeLogin.mockResolvedValue({
|
|
user: mockUser,
|
|
isNewUser: false
|
|
});
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
identifier: 'test@example.com',
|
|
verificationCode: '123456'
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.data?.user.email).toBe('test@example.com');
|
|
});
|
|
|
|
it('should handle verification code login failure', async () => {
|
|
loginCoreService.verificationCodeLogin.mockRejectedValue(new Error('验证码错误'));
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
identifier: 'test@example.com',
|
|
verificationCode: '999999'
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error_code).toBe('VERIFICATION_CODE_LOGIN_FAILED');
|
|
});
|
|
});
|
|
|
|
describe('sendLoginVerificationCode', () => {
|
|
it('should send login verification code successfully', async () => {
|
|
loginCoreService.sendLoginVerificationCode.mockResolvedValue({
|
|
code: '123456',
|
|
isTestMode: true
|
|
});
|
|
|
|
const result = await service.sendLoginVerificationCode('test@example.com');
|
|
|
|
expect(result.success).toBe(false); // 测试模式下返回false
|
|
expect(result.data?.verification_code).toBe('123456');
|
|
expect(result.error_code).toBe('TEST_MODE_ONLY');
|
|
});
|
|
});
|
|
}); |