feat(auth): add OTP verification for email changes on profile update

Email changes via PATCH /api/v1/auth/profile now require OTP verification
instead of updating immediately. A 6-digit code is sent to the new email
address and must be confirmed via POST /api/v1/auth/profile/verify-email
within 10 minutes. Also fixes pre-existing web valuation test failures
(formatPrice output format, removed comparables section, missing
QueryClientProvider wrapper).

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-16 04:23:06 +07:00
parent baaeb56849
commit 43f9e23b28
19 changed files with 429 additions and 76 deletions

View File

@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { describe, expect, it, vi } from 'vitest';
import { ValuationForm } from '../valuation-form';
@@ -7,6 +8,11 @@ vi.mock('@hookform/resolvers/zod', () => ({
zodResolver: () => vi.fn(),
}));
// Mock useProjectSearch hook used by ValuationForm
vi.mock('@/lib/hooks/use-valuation', () => ({
useProjectSearch: () => ({ data: [], isLoading: false }),
}));
// Mock valuation validation
vi.mock('@/lib/validations/valuation', () => ({
valuationFormSchema: {},
@@ -23,84 +29,93 @@ vi.mock('@/lib/validations/valuation', () => ({
],
}));
function createWrapper() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return function Wrapper({ children }: { children: React.ReactNode }) {
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
};
}
describe('ValuationForm', () => {
it('renders form title', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByText('Định giá bất động sản')).toBeInTheDocument();
});
it('renders property type select', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Loại bất động sản *')).toBeInTheDocument();
});
it('renders city select', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Tỉnh/Thành phố *')).toBeInTheDocument();
});
it('renders district input', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Quận/Huyện *')).toBeInTheDocument();
});
it('renders area input', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Diện tích (m²) *')).toBeInTheDocument();
});
it('renders bedroom, bathroom, floors inputs', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Phòng ngủ')).toBeInTheDocument();
expect(screen.getByLabelText('Phòng tắm')).toBeInTheDocument();
expect(screen.getByLabelText('Số tầng')).toBeInTheDocument();
});
it('renders frontage and road width inputs', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Mặt tiền (m)')).toBeInTheDocument();
expect(screen.getByLabelText('Độ rộng đường (m)')).toBeInTheDocument();
});
it('renders year built input', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Năm xây dựng')).toBeInTheDocument();
});
it('renders legal paper checkbox', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByLabelText('Có sổ đỏ/giấy tờ hợp pháp')).toBeInTheDocument();
});
it('renders submit button', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByText('Định giá ngay')).toBeInTheDocument();
});
it('shows loading text when isLoading', () => {
render(<ValuationForm onSubmit={vi.fn()} isLoading={true} />);
render(<ValuationForm onSubmit={vi.fn()} isLoading={true} />, { wrapper: createWrapper() });
expect(screen.getByText('Đang định giá...')).toBeInTheDocument();
});
it('disables submit button when loading', () => {
render(<ValuationForm onSubmit={vi.fn()} isLoading={true} />);
render(<ValuationForm onSubmit={vi.fn()} isLoading={true} />, { wrapper: createWrapper() });
expect(screen.getByText('Đang định giá...')).toBeDisabled();
});
it('renders property type options', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByText('Căn hộ')).toBeInTheDocument();
expect(screen.getByText('Nhà riêng')).toBeInTheDocument();
});
it('renders city options', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByText('Hồ Chí Minh')).toBeInTheDocument();
expect(screen.getByText('Hà Nội')).toBeInTheDocument();
});
it('renders description text', () => {
render(<ValuationForm onSubmit={vi.fn()} />);
render(<ValuationForm onSubmit={vi.fn()} />, { wrapper: createWrapper() });
expect(screen.getByText(/Nhập thông tin bất động sản để nhận ước tính giá từ AI/)).toBeInTheDocument();
});
});

View File

@@ -92,8 +92,8 @@ describe('ValuationHistory', () => {
onSelect={vi.fn()}
/>,
);
expect(screen.getByText('5.00 tỷ')).toBeInTheDocument();
expect(screen.getByText('8.50 tỷ')).toBeInTheDocument();
expect(screen.getByText('5 tỷ')).toBeInTheDocument();
expect(screen.getByText('8.5 tỷ')).toBeInTheDocument();
});
it('calls onSelect when an item is clicked', async () => {

View File

@@ -43,7 +43,7 @@ const mockResult: ValuationResult = {
describe('ValuationResults', () => {
it('renders estimated price', () => {
render(<ValuationResults result={mockResult} />);
expect(screen.getByText('5.00 tỷ VNĐ')).toBeInTheDocument();
expect(screen.getByText('5 tỷ VNĐ')).toBeInTheDocument();
});
it('renders confidence percentage', () => {
@@ -58,7 +58,8 @@ describe('ValuationResults', () => {
it('renders price range', () => {
render(<ValuationResults result={mockResult} />);
expect(screen.getByText(/4\.50 tỷ.*5\.50 tỷ/)).toBeInTheDocument();
expect(screen.getByText('4.5 tỷ')).toBeInTheDocument();
expect(screen.getByText('5.5 tỷ')).toBeInTheDocument();
});
it('renders price drivers section', () => {
@@ -78,33 +79,10 @@ describe('ValuationResults', () => {
expect(screen.getByText('-5.2%')).toBeInTheDocument();
});
it('renders comparables section', () => {
render(<ValuationResults result={mockResult} />);
expect(screen.getByText('Bất động sản tương tự')).toBeInTheDocument();
expect(screen.getByText('Căn hộ tương tự A')).toBeInTheDocument();
expect(screen.getByText('Căn hộ tương tự B')).toBeInTheDocument();
});
it('shows comparable count', () => {
render(<ValuationResults result={mockResult} />);
expect(screen.getByText(/2 bất động sản/)).toBeInTheDocument();
});
it('shows similarity percentage for comparables', () => {
render(<ValuationResults result={mockResult} />);
expect(screen.getByText('92% tương tự')).toBeInTheDocument();
expect(screen.getByText('85% tương tự')).toBeInTheDocument();
});
it('hides drivers section when empty', () => {
const noDrivers = { ...mockResult, priceDrivers: [] };
render(<ValuationResults result={noDrivers} />);
expect(screen.queryByText('Yếu tố ảnh hưởng giá')).not.toBeInTheDocument();
});
it('hides comparables section when empty', () => {
const noComps = { ...mockResult, comparables: [] };
render(<ValuationResults result={noComps} />);
expect(screen.queryByText('Bất động sản tương tự')).not.toBeInTheDocument();
});
});