test: add regression tests for Critical+High FLAWS_REPORT fixes
- Tests caught 2 real bugs:
- Zod NaN bypass in orders/offers schemas (Number(v) || fallback)
- invoiceTotalWithVat using Number() on { toNumber() } objects
- 7 new test files covering auth, env, exchange rates, NAS paths,
schema NaN rejection, invoice VAT calculation, customer validation
- 45 tests passing, build clean
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
49
src/__tests__/auth.service.test.ts
Normal file
49
src/__tests__/auth.service.test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { verifyAccessToken, hashToken } from "../services/auth";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { config } from "../config/env";
|
||||
|
||||
describe("auth service", () => {
|
||||
describe("verifyAccessToken", () => {
|
||||
it("returns null and logs error for invalid JWT", async () => {
|
||||
const consoleSpy = vi
|
||||
.spyOn(console, "error")
|
||||
.mockImplementation(() => {});
|
||||
const result = await verifyAccessToken("invalid-token");
|
||||
expect(result).toBeNull();
|
||||
expect(consoleSpy).toHaveBeenCalled();
|
||||
expect(consoleSpy.mock.calls[0][0]).toMatch(/JWT verification error/);
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("returns null for expired JWT", async () => {
|
||||
const consoleSpy = vi
|
||||
.spyOn(console, "error")
|
||||
.mockImplementation(() => {});
|
||||
const expiredToken = jwt.sign(
|
||||
{ sub: 1, username: "test", role: "user" },
|
||||
config.jwt.secret,
|
||||
{ expiresIn: -1 },
|
||||
);
|
||||
const result = await verifyAccessToken(expiredToken);
|
||||
expect(result).toBeNull();
|
||||
expect(consoleSpy).toHaveBeenCalled();
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("hashToken", () => {
|
||||
it("produces deterministic SHA-256 hex output", () => {
|
||||
const t1 = hashToken("hello");
|
||||
const t2 = hashToken("hello");
|
||||
expect(t1).toBe(t2);
|
||||
expect(t1).toMatch(/^[a-f0-9]{64}$/);
|
||||
});
|
||||
|
||||
it("produces different hashes for different inputs", () => {
|
||||
const t1 = hashToken("a");
|
||||
const t2 = hashToken("b");
|
||||
expect(t1).not.toBe(t2);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user