test(auth): tighten native-token hash assertions, isolate hmac env, expand coverage scope (#136)

Three small follow-ups to the v1.3.3 audit:

- src/lib/auth/__tests__/hmac.test.ts now stubs API_TOKEN_HMAC_KEY via
  vi.stubEnv per test instead of mutating process.env in beforeAll, so the
  variable is restored cleanly after every case (and worker leaks under
  --no-isolate are gone).
- The two native-token issuance tests (password login + passkey login) now
  mock hashToken as (raw) => "hashed:" + raw and assert the persisted
  tokenHash equals hashToken(plaintext) instead of accepting any string.
  Catches a regression where the route would hash the wrong value.
- vitest.config.mts adopts coverage.include for the full src/ tree (the
  vitest 4 replacement for coverage.all=true) so untouched files surface
  with 0% rather than being silently omitted, and excludes the future
  tests/integration/** suite so unit-test runs stay fast.

No production code touched; suite stays at 454 green.
This commit is contained in:
Marc-André Bombeck authored and GitHub committed 2026-05-08 13:22:46 +02:00
1 parent 725cace865
commit b5bd730ec2
4 files changed
+49 -12

No files matched your search

+8 -6
View File
@@ -1,11 +1,15 @@
import { describe, it, expect, beforeAll } from "vitest";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { verifyHmacSignature, hashToken } from "../hmac";
import { createHmac } from "node:crypto";
const TEST_HMAC_KEY = "test-hmac-key-for-unit-tests";
beforeAll(() => {
process.env.API_TOKEN_HMAC_KEY = TEST_HMAC_KEY;
beforeEach(() => {
vi.stubEnv("API_TOKEN_HMAC_KEY", TEST_HMAC_KEY);
});
afterEach(() => {
vi.unstubAllEnvs();
});
describe("verifyHmacSignature", () => {
@@ -75,11 +79,9 @@ describe("hashToken", () => {
});
it("throws if API_TOKEN_HMAC_KEY is not set", () => {
const original = process.env.API_TOKEN_HMAC_KEY;
delete process.env.API_TOKEN_HMAC_KEY;
vi.stubEnv("API_TOKEN_HMAC_KEY", "");
expect(() => hashToken("test")).toThrow(
"API_TOKEN_HMAC_KEY env var must be set",
);
process.env.API_TOKEN_HMAC_KEY = original;
});
});