You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

44 lines
1.6 KiB

import { describe, it, expect } from "bun:test";
import {
hashPassword,
verifyPassword,
validatePasswordStrength,
isPasswordInHistory,
addPasswordToHistory,
} from "../lib/password";
describe("password utils", () => {
it("hashes and verifies password correctly", async () => {
const hash = await hashPassword("Str0ng!Pass");
expect(await verifyPassword("Str0ng!Pass", hash)).toBe(true);
expect(await verifyPassword("WrongPass", hash)).toBe(false);
});
it("validates password strength", () => {
const result = validatePasswordStrength("Str0ng!Pass");
expect(result.valid).toBe(true);
expect(validatePasswordStrength("weak").valid).toBe(false);
expect(validatePasswordStrength("nodigits!").valid).toBe(false);
expect(validatePasswordStrength("NOLOWER1!").valid).toBe(false);
expect(validatePasswordStrength("noupper1!").valid).toBe(false);
expect(validatePasswordStrength("NoSpecial123").valid).toBe(false);
});
it("detects password reuse", async () => {
const hash1 = await hashPassword("ReusedPass1!");
const hash2 = await hashPassword("Different2!");
const history = [hash1, hash2];
expect(await isPasswordInHistory("ReusedPass1!", history)).toBe(true);
expect(await isPasswordInHistory("NewPass!", history)).toBe(false);
});
it("manages password history size", async () => {
const hashes: string[] = [];
for (let i = 0; i < 7; i++) {
const h = await hashPassword(`Pass${i}!`);
hashes.push(h);
}
const updated = addPasswordToHistory(hashes[6], hashes.slice(0, 6));
expect(updated.length).toBe(5);
});
});