Files
nixi-api/tests/AccountEntity.test.ts

57 lines
1.9 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
import {
InvalidEmailFormatError,
WeakPasswordError,
} from "../src/domain/account/errors/AccountErrors";
import { MIN_PASSWORD_LENGTH } from "../src/domain/account/validation/AccountValidation";
describe("AccountEntity", () => {
describe("create", () => {
it("should create an account with valid email and password", () => {
const email = "test@example.com";
const password = "a".repeat(MIN_PASSWORD_LENGTH); // Use minimum length
const account = AccountEntity.create(email, password);
expect(account.email).toBe(email);
expect(account.roleId).toBe(1);
expect(account.id).toBeDefined();
expect(account.createdAt).toBeInstanceOf(Date);
expect(account.updatedAt).toBeInstanceOf(Date);
});
it("should throw InvalidEmailFormatError for invalid email", () => {
const invalidEmail = "invalid-email";
const password = "password123";
expect(() => {
AccountEntity.create(invalidEmail, password);
}).toThrow(InvalidEmailFormatError);
});
it("should throw WeakPasswordError for short password", () => {
const email = "test@example.com";
const shortPassword = "a".repeat(MIN_PASSWORD_LENGTH - 1); // One less than minimum
expect(() => {
AccountEntity.create(email, shortPassword);
}).toThrow(WeakPasswordError);
});
});
describe("verifyPassword", () => {
it("should return true for correct password", () => {
const account = AccountEntity.create("test@example.com", "password123");
expect(account.verifyPassword("password123")).toBe(true);
});
it("should return false for incorrect password", () => {
const account = AccountEntity.create("test@example.com", "password123");
expect(account.verifyPassword("wrongpassword")).toBe(false);
});
});
});