test(api): Auth & /me — fill gaps + lifecycle test

This commit is contained in:
Bas van Rossem
2026-07-22 11:50:32 +02:00
parent 2fa8aa9154
commit e6e4ca0a1e
5 changed files with 133 additions and 1 deletions

View File

@@ -9,6 +9,17 @@ describe('auth', () => {
expect(token).toBeTruthy();
});
it('rejects sign-in with a wrong password', async () => {
const app = createApp();
await authToken(app, 'wrong-pw@example.com'); // create the user first
const res = await app.request('/api/auth/sign-in/email', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ email: 'wrong-pw@example.com', password: 'not-the-password' }),
});
expect(res.status).toBeGreaterThanOrEqual(400);
});
it('rejects public sign-up (admin creates users)', async () => {
const app = createApp();
const res = await app.request('/api/auth/sign-up/email', {

View File

@@ -0,0 +1,32 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { createApp } from '../../src/app';
import { authToken, bearer, resetDb } from '../helpers';
describe('auth lifecycle (create worker → sign in → /api/me → admin route 403)', () => {
beforeEach(resetDb);
it('issues a bearer that authorizes /api/me as a worker but not admin routes', async () => {
const app = createApp();
const token = await authToken(app, 'auth-lifecycle@example.com', 'worker');
// /api/me returns the full worker identity for the freshly-issued token.
const meRes = await app.request('/api/me', { headers: bearer(token) });
expect(meRes.status).toBe(200);
const { user } = await meRes.json();
expect(user).toEqual({
id: expect.any(String),
email: 'auth-lifecycle@example.com',
name: 'auth-lifecycle',
role: 'worker',
});
// The same token is rejected by an admin-only route (worker is not an admin).
const adminRes = await app.request('/api/admin/users', { headers: bearer(token) });
expect(adminRes.status).toBe(403);
// The bearer keeps working across repeated requests (stateless token, not one-shot).
for (let i = 0; i < 3; i++) {
expect((await app.request('/api/me', { headers: bearer(token) })).status).toBe(200);
}
});
});

View File

@@ -1,8 +1,11 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, afterEach, vi } from 'vitest';
import { createApp } from '../src/app';
import { auth } from '../src/auth';
import { authToken, bearer } from './helpers';
describe('GET /api/me', () => {
afterEach(() => vi.restoreAllMocks());
it('rejects an unauthenticated request', async () => {
const app = createApp();
const res = await app.request('/api/me');
@@ -39,4 +42,24 @@ describe('GET /api/me', () => {
const body = await res.json();
expect(body.user.role).toBe('admin');
});
it("defaults role to 'worker' when the session user has no role", async () => {
const app = createApp();
// A role-less session can't arise from a real sign-in (the admin plugin always
// sets a role), so stub getSession to hit the `?? 'worker'` fallback branch.
vi.spyOn(auth.api, 'getSession').mockResolvedValue({
user: { id: 'u1', email: 'norole@example.com', name: 'No Role' },
session: {},
} as never);
const res = await app.request('/api/me', { headers: bearer('irrelevant') });
expect(res.status).toBe(200);
const body = await res.json();
expect(body.user).toEqual({
id: 'u1',
email: 'norole@example.com',
name: 'No Role',
role: 'worker',
});
});
});

View File

@@ -0,0 +1,43 @@
import { describe, it, expect, afterEach, vi } from 'vitest';
import type { Context } from 'hono';
import { auth } from '../src/auth';
import { getSessionUser, isAdmin } from '../src/lib/require-user';
// getSessionUser/isAdmin are the auth gate's building blocks. The role/banned
// fallbacks can't be reached through a real sign-in (the admin plugin always sets
// a role), so exercise them by stubbing the one thing getSessionUser reads:
// auth.api.getSession. vi is already the suite's mocking tool (see env.test.ts).
const fakeCtx = { req: { raw: { headers: new Headers() } } } as unknown as Context;
function mockSession(sessionUser: Record<string, unknown> | null) {
vi.spyOn(auth.api, 'getSession').mockResolvedValue(
(sessionUser ? { user: sessionUser, session: {} } : null) as never,
);
}
describe('getSessionUser', () => {
afterEach(() => vi.restoreAllMocks());
it('returns null when there is no session', async () => {
mockSession(null);
expect(await getSessionUser(fakeCtx)).toBeNull();
});
it('maps id, role and banned from the session user', async () => {
mockSession({ id: 'u1', role: 'admin', banned: true });
expect(await getSessionUser(fakeCtx)).toEqual({ id: 'u1', role: 'admin', banned: true });
});
it("defaults role to 'worker' and banned to false when they are unset", async () => {
mockSession({ id: 'u2' });
expect(await getSessionUser(fakeCtx)).toEqual({ id: 'u2', role: 'worker', banned: false });
});
});
describe('isAdmin', () => {
it('is true only for the admin role', () => {
expect(isAdmin({ id: 'a', role: 'admin', banned: false })).toBe(true);
expect(isAdmin({ id: 'w', role: 'worker', banned: false })).toBe(false);
expect(isAdmin(null)).toBe(false);
});
});