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
38 changes: 37 additions & 1 deletion src/hooks.server.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import { describe, it, expect, vi } from 'vitest';
import type { RequestEvent } from '@sveltejs/kit';
import { authGuard } from './hooks.server';
import { authGuard, supabase } from './hooks.server';

vi.mock('$env/static/public', () => ({
PUBLIC_SUPABASE_URL: 'https://test.supabase.co',
PUBLIC_SUPABASE_ANON_KEY: 'test-anon-key'
}));

vi.mock('@supabase/ssr', () => ({
createServerClient: vi.fn().mockImplementation(() => {
return {
auth: {
getSession: vi.fn().mockResolvedValue({ data: { session: null } })
}
};
})
}));

describe('supabase middleware', () => {
it('safeGetSession returns { session: null, user: null } when no session is found', async () => {
const resolve = vi.fn().mockResolvedValue(new Response());

const mockCookies = {
getAll: vi.fn().mockReturnValue([]),
set: vi.fn()
};

const event = {
locals: {},
cookies: mockCookies
} as unknown as RequestEvent;

await supabase({ event, resolve });

const result = await event.locals.safeGetSession();
expect(result).toEqual({ session: null, user: null });
});
});

describe('authGuard middleware', () => {
it('redirects unauthenticated users from private paths to /auth', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sequence } from '@sveltejs/kit/hooks';

import { PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public';

const supabase: Handle = async ({ event, resolve }) => {
export const supabase: Handle = async ({ event, resolve }) => {
/**
* Creates a Supabase client specific to this server request.
*
Expand Down
Loading