55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('$api', () => {
|
|
const state = { authed: false, username: 'demo' };
|
|
return {
|
|
setUnauthorizedHandler: vi.fn(),
|
|
authApi: {
|
|
session: vi.fn(async () => ({ isAuthenticated: state.authed })),
|
|
whoami: vi.fn(async () => ({ username: state.username, is_staff: false })),
|
|
login: vi.fn(async () => {
|
|
state.authed = true;
|
|
state.username = 'demo';
|
|
return { detail: 'ok' };
|
|
}),
|
|
logout: vi.fn(async () => {
|
|
state.authed = false;
|
|
state.username = 'demo';
|
|
}),
|
|
register: vi.fn(async (u: string) => {
|
|
state.authed = true;
|
|
state.username = u;
|
|
return { detail: 'ok' };
|
|
})
|
|
}
|
|
};
|
|
});
|
|
vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
|
|
|
|
import { get } from 'svelte/store';
|
|
import { authStore } from '$auth';
|
|
|
|
beforeEach(() => authStore.logout());
|
|
|
|
describe('authStore', () => {
|
|
it('starts unknown then resolves anonymous', async () => {
|
|
const s = await authStore.refresh();
|
|
expect(s.status).toBe('anonymous');
|
|
expect(get(authStore).username).toBeNull();
|
|
});
|
|
|
|
it('becomes authenticated after login', async () => {
|
|
await authStore.login('demo', 'demo');
|
|
const s = get(authStore);
|
|
expect(s.status).toBe('authenticated');
|
|
expect(s.username).toBe('demo');
|
|
expect(s.isStaff).toBe(false);
|
|
});
|
|
|
|
it('registers, auto-logs-in, and exposes the new username', async () => {
|
|
await authStore.register('newbie', 'newbie@example.com', 's3curePass!');
|
|
const s = get(authStore);
|
|
expect(s.status).toBe('authenticated');
|
|
expect(s.username).toBe('newbie');
|
|
});
|
|
});
|