mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-06 12:27:44 +00:00
test: add password tests
This commit is contained in:
56
packages/better-auth/src/crypto/password.test.ts
Normal file
56
packages/better-auth/src/crypto/password.test.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { hashPassword, verifyPassword } from "./password";
|
||||||
|
|
||||||
|
describe("Password hashing and verification", () => {
|
||||||
|
it("should hash a password", async () => {
|
||||||
|
const password = "mySecurePassword123!";
|
||||||
|
const hash = await hashPassword(password);
|
||||||
|
expect(hash).toBeTruthy();
|
||||||
|
expect(hash.split(":").length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should verify a correct password", async () => {
|
||||||
|
const password = "correctPassword123!";
|
||||||
|
const hash = await hashPassword(password);
|
||||||
|
const isValid = await verifyPassword(hash, password);
|
||||||
|
expect(isValid).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject an incorrect password", async () => {
|
||||||
|
const correctPassword = "correctPassword123!";
|
||||||
|
const incorrectPassword = "wrongPassword456!";
|
||||||
|
const hash = await hashPassword(correctPassword);
|
||||||
|
const isValid = await verifyPassword(hash, incorrectPassword);
|
||||||
|
expect(isValid).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should generate different hashes for the same password", async () => {
|
||||||
|
const password = "samePassword123!";
|
||||||
|
const hash1 = await hashPassword(password);
|
||||||
|
const hash2 = await hashPassword(password);
|
||||||
|
expect(hash1).not.toBe(hash2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle long passwords", async () => {
|
||||||
|
const password = "a".repeat(1000);
|
||||||
|
const hash = await hashPassword(password);
|
||||||
|
const isValid = await verifyPassword(hash, password);
|
||||||
|
expect(isValid).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be case-sensitive", async () => {
|
||||||
|
const password = "CaseSensitivePassword123!";
|
||||||
|
const hash = await hashPassword(password);
|
||||||
|
const isValidLower = await verifyPassword(hash, password.toLowerCase());
|
||||||
|
const isValidUpper = await verifyPassword(hash, password.toUpperCase());
|
||||||
|
expect(isValidLower).toBe(false);
|
||||||
|
expect(isValidUpper).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle Unicode characters", async () => {
|
||||||
|
const password = "пароль123!";
|
||||||
|
const hash = await hashPassword(password);
|
||||||
|
const isValid = await verifyPassword(hash, password);
|
||||||
|
expect(isValid).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user