- Implemented BlogBodySanitizer to clean HTML content before saving articles, ensuring security against XSS attacks. - Added tests for BlogBodySanitizer to verify that unsafe tags and attributes are stripped from the content. - Introduced ApiLeastPrivilegeTest to ensure that unauthorized users cannot access sensitive API routes, maintaining strict access control.
129 lines
5.7 KiB
TypeScript
129 lines
5.7 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { Routes, Route } from 'react-router';
|
|
import { renderWithProviders } from '@/test/utils';
|
|
|
|
vi.mock('@ckeditor/ckeditor5-react', () => ({ CKEditor: () => null }));
|
|
vi.mock('@ckeditor/ckeditor5-build-classic', () => ({ default: {} }));
|
|
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
|
|
vi.mock('@/components/ui/SearchableSelect', () => ({ default: () => null }));
|
|
vi.mock('@/lib/api', () => ({
|
|
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
|
|
ApiError: class extends Error {},
|
|
}));
|
|
|
|
import { api } from '@/lib/api';
|
|
import BlogFormPage from '@/pages/BlogFormPage';
|
|
|
|
const post = api.post as ReturnType<typeof vi.fn>;
|
|
const get = api.get as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
post.mockReset();
|
|
// فرم شهرها را برای انتخابگر «سراسری / شهر» میگیرد
|
|
get.mockReset();
|
|
get.mockResolvedValue({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } });
|
|
});
|
|
|
|
describe('BlogFormPage — اعتبارسنجی zod (حالت ساخت)', () => {
|
|
it('submit با فیلدهای خالی → خطای عنوان و جلوگیری از فراخوانی API', async () => {
|
|
renderWithProviders(<BlogFormPage />, { route: '/admin/blogs/new' });
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
|
|
|
|
expect(await screen.findByText('عنوان الزامی است')).toBeInTheDocument();
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('عنوان کوتاهتر از ۳ کاراکتر هم خطا میدهد', async () => {
|
|
renderWithProviders(<BlogFormPage />, { route: '/admin/blogs/new' });
|
|
|
|
await userEvent.type(screen.getByPlaceholderText('عنوان جذاب بنویسید...'), 'اب');
|
|
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
|
|
|
|
expect(await screen.findByText('عنوان الزامی است')).toBeInTheDocument();
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
const BLOG_UUID = '56fd9a20-9594-4aa1-a651-346fa86720bd';
|
|
|
|
function renderEditPage() {
|
|
return renderWithProviders(
|
|
<Routes>
|
|
<Route path="/admin/blogs/:uuid/edit" element={<BlogFormPage />} />
|
|
</Routes>,
|
|
{ route: `/admin/blogs/${BLOG_UUID}/edit` }
|
|
);
|
|
}
|
|
|
|
describe('BlogFormPage — حالت ویرایش', () => {
|
|
it('پیشنویس را از اندپوینت ادمین میگیرد و فرم را پر میکند', async () => {
|
|
get.mockImplementation((url: string) =>
|
|
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
|
|
? Promise.resolve({
|
|
data: {
|
|
data: {
|
|
uuid: BLOG_UUID,
|
|
title: 'عنوان پیشنویس',
|
|
body: '<p>محتوای تست</p>',
|
|
summary: 'خلاصهٔ تست',
|
|
status: 'draft',
|
|
tags: ['الف', 'ب'],
|
|
},
|
|
},
|
|
})
|
|
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
|
|
);
|
|
|
|
renderEditPage();
|
|
|
|
expect(await screen.findByDisplayValue('عنوان پیشنویس')).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue('خلاصهٔ تست')).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue('الف, ب')).toBeInTheDocument();
|
|
// اندپوینت عمومی (که پیشنویس را ۴۰۴ میکرد) نباید صدا زده شود
|
|
expect(get).not.toHaveBeenCalledWith(`/api/v1/blog/${BLOG_UUID}`);
|
|
});
|
|
|
|
it('خطای بارگذاری → کارت خطا بهجای فرم خالیِ قابلثبت', async () => {
|
|
get.mockImplementation((url: string) =>
|
|
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
|
|
? Promise.reject(new Error('مقاله یافت نشد'))
|
|
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
|
|
);
|
|
|
|
renderEditPage();
|
|
|
|
expect(await screen.findByText('مقاله یافت نشد')).toBeInTheDocument();
|
|
expect(screen.queryByRole('button', { name: 'بروزرسانی' })).not.toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'بازگشت به فهرست مقالات' })).toBeInTheDocument();
|
|
});
|
|
|
|
it('مقالهٔ سراسری با faq و کلیدواژهٔ خالی بدون خطا بارگذاری میشود', async () => {
|
|
get.mockImplementation((url: string) =>
|
|
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
|
|
? Promise.resolve({
|
|
data: {
|
|
data: {
|
|
uuid: BLOG_UUID,
|
|
title: 'مقالهٔ سراسری',
|
|
body: '<p>x</p>',
|
|
status: 'draft',
|
|
tags: [],
|
|
faq: [],
|
|
secondary_keywords: [],
|
|
city: null,
|
|
},
|
|
},
|
|
})
|
|
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
|
|
);
|
|
|
|
renderEditPage();
|
|
|
|
expect(await screen.findByDisplayValue('مقالهٔ سراسری')).toBeInTheDocument();
|
|
expect(screen.getByText('سوالی افزوده نشده است.')).toBeInTheDocument();
|
|
});
|
|
});
|