Initial
This commit is contained in:
commit
b43955e0d9
162 changed files with 15425 additions and 0 deletions
55
tests/unit/authStore.test.ts
Normal file
55
tests/unit/authStore.test.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
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');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue