fix: resolve lint errors in test files — group imports before vi.mock blocks
- local.strategy.spec.ts: move LocalStrategy import above vi.mock calls - media-storage.service.spec.ts: move MinioMediaStorageService import above vi.mock calls - Vitest hoists vi.mock regardless of source order, so grouping imports is safe Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { LocalStrategy } from '../strategies/local.strategy';
|
||||
|
||||
// Mock passport-local before importing LocalStrategy
|
||||
vi.mock('passport-local', () => {
|
||||
return {
|
||||
Strategy: class MockStrategy {
|
||||
constructor(_options: any) {}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('@nestjs/passport', () => {
|
||||
return {
|
||||
PassportStrategy: (StrategyClass: any) => {
|
||||
return class extends StrategyClass {};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('@modules/shared', () => {
|
||||
class UnauthorizedException extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'UnauthorizedException';
|
||||
}
|
||||
}
|
||||
return {
|
||||
UnauthorizedException,
|
||||
normalizeVietnamPhone: (phone: string) => {
|
||||
if (phone.startsWith('+84') && phone.length === 12) return phone;
|
||||
if (phone.startsWith('0') && phone.length === 10) return '+84' + phone.slice(1);
|
||||
return null;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe('LocalStrategy', () => {
|
||||
let strategy: LocalStrategy;
|
||||
let mockUserRepo: {
|
||||
findByPhone: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockUserRepo = {
|
||||
findByPhone: vi.fn(),
|
||||
};
|
||||
strategy = new LocalStrategy(mockUserRepo as any);
|
||||
});
|
||||
|
||||
it('throws when phone number is invalid', async () => {
|
||||
await expect(strategy.validate('12345', 'password')).rejects.toThrow(
|
||||
'Số điện thoại không hợp lệ',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when user is not found', async () => {
|
||||
mockUserRepo.findByPhone.mockResolvedValue(null);
|
||||
|
||||
await expect(strategy.validate('0912345678', 'password')).rejects.toThrow(
|
||||
'Số điện thoại hoặc mật khẩu không đúng',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when user has no password hash', async () => {
|
||||
mockUserRepo.findByPhone.mockResolvedValue({
|
||||
id: 'user-1',
|
||||
passwordHash: null,
|
||||
isActive: true,
|
||||
phone: { value: '+84912345678' },
|
||||
role: 'BUYER',
|
||||
});
|
||||
|
||||
await expect(strategy.validate('0912345678', 'password')).rejects.toThrow(
|
||||
'Số điện thoại hoặc mật khẩu không đúng',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when user is inactive', async () => {
|
||||
mockUserRepo.findByPhone.mockResolvedValue({
|
||||
id: 'user-1',
|
||||
passwordHash: { compare: vi.fn().mockResolvedValue(true) },
|
||||
isActive: false,
|
||||
phone: { value: '+84912345678' },
|
||||
role: 'BUYER',
|
||||
});
|
||||
|
||||
await expect(strategy.validate('0912345678', 'password')).rejects.toThrow(
|
||||
'Tài khoản đã bị vô hiệu hóa',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when password is wrong', async () => {
|
||||
mockUserRepo.findByPhone.mockResolvedValue({
|
||||
id: 'user-1',
|
||||
passwordHash: { compare: vi.fn().mockResolvedValue(false) },
|
||||
isActive: true,
|
||||
phone: { value: '+84912345678' },
|
||||
role: 'BUYER',
|
||||
});
|
||||
|
||||
await expect(strategy.validate('0912345678', 'password')).rejects.toThrow(
|
||||
'Số điện thoại hoặc mật khẩu không đúng',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns user info with valid credentials', async () => {
|
||||
mockUserRepo.findByPhone.mockResolvedValue({
|
||||
id: 'user-1',
|
||||
passwordHash: { compare: vi.fn().mockResolvedValue(true) },
|
||||
isActive: true,
|
||||
phone: { value: '+84912345678' },
|
||||
role: 'BUYER',
|
||||
});
|
||||
|
||||
const result = await strategy.validate('0912345678', 'P@ssw0rd!');
|
||||
|
||||
expect(result).toEqual({
|
||||
id: 'user-1',
|
||||
phone: '+84912345678',
|
||||
role: 'BUYER',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user