refactoring magic strings in layout.server file (guard), including new routes and tests
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { redirect, type ServerLoadEvent } from '@sveltejs/kit';
|
import { redirect, type ServerLoadEvent } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad } from '../anmeldung/$types';
|
import type { PageServerLoad } from '../anmeldung/$types';
|
||||||
|
|
||||||
|
import { ROUTE_NAMES } from '..';
|
||||||
|
|
||||||
export const load: PageServerLoad = (event: ServerLoadEvent) => {
|
export const load: PageServerLoad = (event: ServerLoadEvent) => {
|
||||||
if (!event.locals.user && event.url.pathname !== '/anmeldung') throw redirect(303, '/anmeldung');
|
if (!event.locals.user && event.url.pathname !== ROUTE_NAMES.ANMELDUNG)
|
||||||
|
throw redirect(303, ROUTE_NAMES.ANMELDUNG);
|
||||||
return {
|
return {
|
||||||
user: event.locals.user
|
user: event.locals.user
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export const ROUTE_NAMES = {
|
|||||||
: `/view/${vorgangToken}/${tatort}`,
|
: `/view/${vorgangToken}/${tatort}`,
|
||||||
|
|
||||||
// Anmeldung: actions
|
// Anmeldung: actions
|
||||||
|
ANMELDUNG: '/anmeldung',
|
||||||
ANMELDUNG_LOGIN: '/anmeldung?/login',
|
ANMELDUNG_LOGIN: '/anmeldung?/login',
|
||||||
ANMELDUNG_LOGOUT: '/anmeldung?/logout'
|
ANMELDUNG_LOGOUT: '/anmeldung?/logout'
|
||||||
};
|
};
|
||||||
|
|||||||
29
tests/Layout.test.ts
Normal file
29
tests/Layout.test.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { load } from '../src/routes/(angemeldet)/+layout.server';
|
||||||
|
import { ROUTE_NAMES } from '../src/routes';
|
||||||
|
import { baseData, mockEvent } from './fixtures';
|
||||||
|
|
||||||
|
describe('+layout.server load(): Teste korrekte URL', () => {
|
||||||
|
test('Werfe redirect zu /anmeldung wenn User nicht eingeloggt', async () => {
|
||||||
|
const mockEvent = {
|
||||||
|
locals: {
|
||||||
|
user: null
|
||||||
|
},
|
||||||
|
url: new URL(`https://example.com/not-anmeldung`)
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
load(mockEvent);
|
||||||
|
throw new Error('Expected load() to throw');
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.status).toBe(303);
|
||||||
|
expect(err.location).toBe(ROUTE_NAMES.ANMELDUNG);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('+layout.server load(): Teste erfolgreichen Pfad', () => {
|
||||||
|
test('Werfe kein Fehler', async () => {
|
||||||
|
const result = load(mockEvent);
|
||||||
|
expect(result).toEqual({ user: baseData.user });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -44,3 +44,10 @@ export const baseData = {
|
|||||||
url: `https://example.com/${testVorgangsList[0].vorgangToken}`,
|
url: `https://example.com/${testVorgangsList[0].vorgangToken}`,
|
||||||
crimeNames: ['modell-A', 'Fall-A']
|
crimeNames: ['modell-A', 'Fall-A']
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const mockEvent = {
|
||||||
|
locals: {
|
||||||
|
user: baseData.user
|
||||||
|
},
|
||||||
|
url: new URL(`https://example.com/anmeldung`)
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user