mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 00:04:48 -07:00
harden
This commit is contained in:
parent
d04aa83258
commit
dcd6dc00f4
83 changed files with 1797 additions and 390 deletions
33
webclient/src/components/Guard/AuthGuard.spec.tsx
Normal file
33
webclient/src/components/Guard/AuthGuard.spec.tsx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { screen } from '@testing-library/react';
|
||||
import { renderWithProviders, connectedState, disconnectedState } from '../../__test-utils__';
|
||||
import AuthGuard from './AuthGuard';
|
||||
|
||||
vi.mock('@app/hooks', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@app/hooks')>();
|
||||
return { ...actual, useWebClient: vi.fn(() => ({})) };
|
||||
});
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
it('redirects to LOGIN when disconnected', () => {
|
||||
renderWithProviders(<AuthGuard />, {
|
||||
preloadedState: disconnectedState,
|
||||
route: '/server',
|
||||
});
|
||||
|
||||
// Navigate triggers a route change — AuthGuard itself renders no text.
|
||||
// We verify it doesn't render any meaningful content.
|
||||
expect(screen.queryByRole('button')).toBeNull();
|
||||
expect(screen.queryByRole('heading')).toBeNull();
|
||||
});
|
||||
|
||||
it('renders nothing visible when connected', () => {
|
||||
const { container } = renderWithProviders(<AuthGuard />, {
|
||||
preloadedState: connectedState,
|
||||
route: '/server',
|
||||
});
|
||||
|
||||
// AuthGuard renders an empty fragment when connected.
|
||||
// The only DOM is from provider wrappers (e.g. ToastProvider's container div).
|
||||
expect(container.textContent).toBe('');
|
||||
});
|
||||
});
|
||||
|
|
@ -8,7 +8,7 @@ const AuthGuard = () => {
|
|||
const isConnected = useAppSelector(ServerSelectors.getIsConnected);
|
||||
return !isConnected
|
||||
? <Navigate to={App.RouteEnum.LOGIN} />
|
||||
: <div></div>;
|
||||
: <></>;
|
||||
};
|
||||
|
||||
export default AuthGuard;
|
||||
|
|
|
|||
38
webclient/src/components/Guard/ModGuard.spec.tsx
Normal file
38
webclient/src/components/Guard/ModGuard.spec.tsx
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { renderWithProviders, connectedState, makeUser } from '../../__test-utils__';
|
||||
import { Data } from '@app/types';
|
||||
import ModGuard from './ModGuard';
|
||||
|
||||
vi.mock('@app/hooks', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@app/hooks')>();
|
||||
return { ...actual, useWebClient: vi.fn(() => ({})) };
|
||||
});
|
||||
|
||||
describe('ModGuard', () => {
|
||||
it('redirects when user is not a moderator', () => {
|
||||
const { container } = renderWithProviders(<ModGuard />, {
|
||||
preloadedState: connectedState,
|
||||
route: '/logs',
|
||||
});
|
||||
|
||||
expect(container.textContent).toBe('');
|
||||
});
|
||||
|
||||
it('renders nothing visible when user is a moderator', () => {
|
||||
const modUser = makeUser({
|
||||
userLevel: Data.ServerInfo_User_UserLevelFlag.IsModerator,
|
||||
});
|
||||
|
||||
const { container } = renderWithProviders(<ModGuard />, {
|
||||
preloadedState: {
|
||||
...connectedState,
|
||||
server: {
|
||||
...(connectedState.server as any),
|
||||
user: modUser,
|
||||
},
|
||||
},
|
||||
route: '/logs',
|
||||
});
|
||||
|
||||
expect(container.textContent).toBe('');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue