test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import { render,screen,waitFor } from '@testing-library/react';
|
||||
import { MemoryRouter,Route,Routes } from 'react-router-dom';
|
||||
import { beforeEach,describe,expect,it,vi } from 'vitest';
|
||||
import api from '../services/api';
|
||||
import { useAdminAuthStore } from '../store/adminAuthStore';
|
||||
import type { AdminUser } from '../types/admin';
|
||||
import ProtectedRoute from './ProtectedRoute';
|
||||
|
||||
vi.mock('../services/api', () => ({
|
||||
default: {
|
||||
post: vi.fn(),
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('ProtectedRoute Guard Component', () => {
|
||||
const superAdminUser: AdminUser = {
|
||||
id: 'user-1',
|
||||
mobile: '09120000000',
|
||||
role: 'SUPER_ADMIN',
|
||||
};
|
||||
|
||||
const regularAdminUser: AdminUser = {
|
||||
id: 'user-2',
|
||||
mobile: '09121111111',
|
||||
role: 'ADMIN',
|
||||
};
|
||||
|
||||
const customerUser: AdminUser = {
|
||||
id: 'user-3',
|
||||
mobile: '09122222222',
|
||||
role: 'CUSTOMER',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
useAdminAuthStore.getState().clearAuth();
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
const renderWithRouter = () => {
|
||||
return render(
|
||||
<MemoryRouter initialEntries={['/admin/dashboard']}>
|
||||
<Routes>
|
||||
<Route path="/login" element={<div>Login Page</div>} />
|
||||
<Route element={<ProtectedRoute />}>
|
||||
<Route path="/admin/dashboard" element={<div>Admin Dashboard Content</div>} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</MemoryRouter>
|
||||
);
|
||||
};
|
||||
|
||||
it('renders protected child content immediately when already authenticated with ADMIN role', async () => {
|
||||
useAdminAuthStore.getState().setAuth('valid-token', regularAdminUser);
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders protected child content immediately when authenticated with SUPER_ADMIN role', async () => {
|
||||
useAdminAuthStore.getState().setAuth('valid-token', superAdminUser);
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects to /login if user is authenticated but does not have ADMIN or SUPER_ADMIN role', async () => {
|
||||
useAdminAuthStore.getState().setAuth('valid-token', customerUser);
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Login Page')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Admin Dashboard Content')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('redirects to /login when completely unauthenticated (no token, no user)', async () => {
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Login Page')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Admin Dashboard Content')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('refreshes token successfully via /auth/refresh when accessToken exists but state is unauthenticated', async () => {
|
||||
useAdminAuthStore.setState({
|
||||
accessToken: 'stale-token',
|
||||
isAuthenticated: false,
|
||||
adminUser: null,
|
||||
});
|
||||
|
||||
vi.mocked(api.post).mockResolvedValueOnce({
|
||||
data: {
|
||||
success: true,
|
||||
data: {
|
||||
accessToken: 'fresh-token',
|
||||
user: regularAdminUser,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument();
|
||||
expect(api.post).toHaveBeenCalledWith('/auth/refresh');
|
||||
expect(useAdminAuthStore.getState().accessToken).toBe('fresh-token');
|
||||
expect(useAdminAuthStore.getState().isAuthenticated).toBe(true);
|
||||
});
|
||||
|
||||
it('falls back to /users/profile when /auth/refresh fails, and succeeds if profile is valid', async () => {
|
||||
useAdminAuthStore.setState({
|
||||
accessToken: 'persisted-token',
|
||||
isAuthenticated: false,
|
||||
adminUser: null,
|
||||
});
|
||||
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new Error('Refresh failed'));
|
||||
vi.mocked(api.get).mockResolvedValueOnce({
|
||||
data: {
|
||||
success: true,
|
||||
data: superAdminUser,
|
||||
},
|
||||
});
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument();
|
||||
expect(api.get).toHaveBeenCalledWith('/users/profile');
|
||||
expect(useAdminAuthStore.getState().isAuthenticated).toBe(true);
|
||||
});
|
||||
|
||||
it('clears auth and redirects to /login when both refresh and profile validation fail', async () => {
|
||||
useAdminAuthStore.setState({
|
||||
accessToken: 'invalid-token',
|
||||
isAuthenticated: false,
|
||||
adminUser: null,
|
||||
});
|
||||
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new Error('Refresh failed'));
|
||||
vi.mocked(api.get).mockRejectedValueOnce(new Error('Profile failed'));
|
||||
|
||||
renderWithRouter();
|
||||
|
||||
expect(await screen.findByText('Login Page')).toBeInTheDocument();
|
||||
await waitFor(() => {
|
||||
expect(useAdminAuthStore.getState().isAuthenticated).toBe(false);
|
||||
expect(useAdminAuthStore.getState().accessToken).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user