website refactor
This commit is contained in:
@@ -6,14 +6,6 @@ import { SessionGateway } from '../gateways/SessionGateway';
|
||||
import { AuthRedirectBuilder } from './AuthRedirectBuilder';
|
||||
import type { AuthSessionDTO } from '../types/generated/AuthSessionDTO';
|
||||
|
||||
// Hoist the mock redirect function
|
||||
const mockRedirect = vi.hoisted(() => vi.fn());
|
||||
|
||||
// Mock next/navigation
|
||||
vi.mock('next/navigation', () => ({
|
||||
redirect: mockRedirect,
|
||||
}));
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('./PathnameInterpreter');
|
||||
vi.mock('./RouteAccessPolicy');
|
||||
@@ -69,14 +61,14 @@ describe('RouteGuard', () => {
|
||||
mockPolicy.isAuthPage.mockReturnValue(false);
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockInterpreter.interpret).toHaveBeenCalledWith(pathname);
|
||||
expect(mockPolicy.isPublic).toHaveBeenCalledWith('/public/page');
|
||||
expect(mockPolicy.isAuthPage).toHaveBeenCalledWith('/public/page');
|
||||
expect(mockGateway.getSession).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ type: 'allow' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -90,11 +82,11 @@ describe('RouteGuard', () => {
|
||||
mockGateway.getSession.mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
expect(mockRedirect).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ type: 'allow' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,7 +106,7 @@ describe('RouteGuard', () => {
|
||||
mockBuilder.awayFromAuthPage.mockReturnValue('/dashboard');
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
@@ -122,7 +114,7 @@ describe('RouteGuard', () => {
|
||||
session: mockSession,
|
||||
currentPathname: '/login',
|
||||
});
|
||||
expect(mockRedirect).toHaveBeenCalledWith('/dashboard');
|
||||
expect(result).toEqual({ type: 'redirect', to: '/dashboard' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -137,12 +129,12 @@ describe('RouteGuard', () => {
|
||||
mockBuilder.toLogin.mockReturnValue('/login?redirect=/protected/dashboard');
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
expect(mockBuilder.toLogin).toHaveBeenCalledWith({ currentPathname: '/protected/dashboard' });
|
||||
expect(mockRedirect).toHaveBeenCalledWith('/login?redirect=/protected/dashboard');
|
||||
expect(result).toEqual({ type: 'redirect', to: '/login?redirect=/protected/dashboard' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -163,13 +155,13 @@ describe('RouteGuard', () => {
|
||||
mockBuilder.toLogin.mockReturnValue('/login?redirect=/admin/panel');
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
expect(mockPolicy.requiredRoles).toHaveBeenCalledWith('/admin/panel');
|
||||
expect(mockBuilder.toLogin).toHaveBeenCalledWith({ currentPathname: '/admin/panel' });
|
||||
expect(mockRedirect).toHaveBeenCalledWith('/login?redirect=/admin/panel');
|
||||
expect(result).toEqual({ type: 'redirect', to: '/login?redirect=/admin/panel' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -189,12 +181,12 @@ describe('RouteGuard', () => {
|
||||
mockPolicy.requiredRoles.mockReturnValue(['admin']);
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
expect(mockPolicy.requiredRoles).toHaveBeenCalledWith('/admin/panel');
|
||||
expect(mockRedirect).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ type: 'allow' });
|
||||
});
|
||||
|
||||
it('should allow access when no specific roles required', async () => {
|
||||
@@ -212,12 +204,12 @@ describe('RouteGuard', () => {
|
||||
mockPolicy.requiredRoles.mockReturnValue(null);
|
||||
|
||||
// Act
|
||||
await routeGuard.enforce({ pathname });
|
||||
const result = await routeGuard.enforce({ pathname });
|
||||
|
||||
// Assert
|
||||
expect(mockGateway.getSession).toHaveBeenCalled();
|
||||
expect(mockPolicy.requiredRoles).toHaveBeenCalledWith('/dashboard');
|
||||
expect(mockRedirect).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ type: 'allow' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user