Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions apps/web/src/app/(app)/protected-routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { type ReactNode } from 'react';
import type { User } from '@kilocode/db/schema';
import { defineTestUser } from '@/tests/helpers/user.helper';

const mockGetUserFromAuthOrRedirect = jest.fn<Promise<User>, []>();

(globalThis as typeof globalThis & { React: typeof React }).React = React;

jest.mock('@/lib/user/server', () => ({
getUserFromAuthOrRedirect: () => mockGetUserFromAuthOrRedirect(),
}));

jest.mock('@/components/usage-analytics/UsageAnalyticsDashboard', () => ({
UsageAnalyticsDashboard: () => null,
}));

jest.mock('@/components/security-agent/SecurityAgentContext', () => ({
SecurityAgentProvider: ({ children }: { children: ReactNode }) => children,
}));

jest.mock('@/components/security-agent/SecurityAgentLayout', () => ({
SecurityAgentLayout: ({ children }: { children: ReactNode }) => children,
}));

type ProtectedRouteEntry = {
route: string;
render: () => Promise<ReactNode>;
};

async function renderSecurityAgentLayout(): Promise<ReactNode> {
const { default: SecurityAgentRootLayout } = await import('@/app/(app)/security-agent/layout');
return SecurityAgentRootLayout({ children: 'security agent content' });
}

const protectedRouteEntries: ProtectedRouteEntry[] = [
{
route: '/usage',
render: async () => {
const { default: UsagePage } = await import('@/app/(app)/usage/page');
return UsagePage();
},
},
{
route: '/security-agent/config',
render: renderSecurityAgentLayout,
},
{
route: '/security-agent/audit-report',
render: renderSecurityAgentLayout,
},
];

describe('protected app routes', () => {
const redirectSentinel = new Error('NEXT_REDIRECT');

beforeEach(() => {
jest.clearAllMocks();
});

it.each(protectedRouteEntries)('checks authentication before rendering $route', async entry => {
mockGetUserFromAuthOrRedirect.mockResolvedValue(defineTestUser());

await expect(entry.render()).resolves.toBeDefined();
expect(mockGetUserFromAuthOrRedirect).toHaveBeenCalledTimes(1);
});

it.each(protectedRouteEntries)(
'propagates the unauthenticated redirect for $route',
async entry => {
mockGetUserFromAuthOrRedirect.mockRejectedValue(redirectSentinel);

await expect(entry.render()).rejects.toBe(redirectSentinel);
expect(mockGetUserFromAuthOrRedirect).toHaveBeenCalledTimes(1);
}
);
});
5 changes: 4 additions & 1 deletion apps/web/src/app/(app)/security-agent/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { SecurityAgentLayout } from '@/components/security-agent/SecurityAgentLayout';
import { SecurityAgentProvider } from '@/components/security-agent/SecurityAgentContext';
import { getUserFromAuthOrRedirect } from '@/lib/user/server';

export const metadata = {
title: 'Security Agent | Kilo Code',
description: 'Monitor and manage Security Findings synced from Dependabot',
};

export default function SecurityAgentRootLayout({ children }: { children: React.ReactNode }) {
export default async function SecurityAgentRootLayout({ children }: { children: React.ReactNode }) {
await getUserFromAuthOrRedirect();

return (
<SecurityAgentProvider>
<SecurityAgentLayout>{children}</SecurityAgentLayout>
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/app/(app)/usage/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Suspense } from 'react';
import { UsageAnalyticsDashboard } from '@/components/usage-analytics/UsageAnalyticsDashboard';
import { getUserFromAuthOrRedirect } from '@/lib/user/server';

export default async function UsagePage() {
await getUserFromAuthOrRedirect();

export default function UsagePage() {
return (
<Suspense>
<UsageAnalyticsDashboard context="personal" organizationId={null} title="Usage" />
Expand Down