feat(auth): setup base auth domain

This commit is contained in:
2026-01-24 03:44:24 +08:00
parent 037e36f4f4
commit c8dc3b19a5
68 changed files with 2089 additions and 928 deletions

View File

@@ -0,0 +1,145 @@
import { describe, expect, test } from "vitest";
import { AuthVerificationEntity } from "./auth-verifications.entity.js";
import { VerificationAlreadyAccepted } from "./errors/VerificationAlreadyAccepted.js";
import { VerificationAlreadyRevoked } from "./errors/VerificationAlreadyRevoked.js";
describe("Users - UserVerificationEntity", () => {
test("should create a user verification", () => {
const authVerification = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
null,
false,
false,
);
expect(authVerification).toBeDefined();
expect(authVerification.id).toBe("1");
expect(authVerification.identityId).toBe("identity-1");
expect(authVerification.magicToken).toBe("token-1");
expect(authVerification.createdAt).toBeInstanceOf(Date);
expect(authVerification.acceptedAt).toBeNull();
expect(authVerification.isAccepted).toBeFalsy();
expect(authVerification.isRevoked).toBeFalsy();
expect(authVerification.isVerified()).toBeFalsy();
});
test("should accept user verification", () => {
const authVerification = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
null,
false,
false,
);
authVerification.accept();
expect(authVerification).toBeDefined();
expect(authVerification.id).toBe("1");
expect(authVerification.identityId).toBe("identity-1");
expect(authVerification.magicToken).toBe("token-1");
expect(authVerification.createdAt).toBeInstanceOf(Date);
expect(authVerification.acceptedAt).toBeInstanceOf(Date);
expect(authVerification.isAccepted).toBeTruthy();
expect(authVerification.isRevoked).toBeFalsy();
expect(authVerification.isVerified()).toBeTruthy();
});
test("should revoke user verification (not yet accepted)", () => {
const userVerification = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
null,
false,
false,
);
userVerification.revoke();
expect(userVerification).toBeDefined();
expect(userVerification.id).toBe("1");
expect(userVerification.identityId).toBe("identity-1");
expect(userVerification.magicToken).toBe("token-1");
expect(userVerification.createdAt).toBeInstanceOf(Date);
expect(userVerification.acceptedAt).toBeNull();
expect(userVerification.isAccepted).toBeFalsy();
expect(userVerification.isRevoked).toBeTruthy();
expect(userVerification.isVerified()).toBeFalsy();
});
test("should revoke user verification (already accepted)", () => {
const authVerification = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
null,
false,
false,
);
authVerification.accept();
authVerification.revoke();
expect(authVerification).toBeDefined();
expect(authVerification.id).toBe("1");
expect(authVerification.identityId).toBe("identity-1");
expect(authVerification.magicToken).toBe("token-1");
expect(authVerification.createdAt).toBeInstanceOf(Date);
expect(authVerification.acceptedAt).toBeInstanceOf(Date);
expect(authVerification.isAccepted).toBeTruthy();
expect(authVerification.isRevoked).toBeTruthy();
expect(authVerification.isVerified()).toBeFalsy();
});
test("should throw an error when trying to accept while already accepted", () => {
const authVerification = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
new Date(),
true,
false,
);
expect(() => {
authVerification.accept();
}).toThrowError(VerificationAlreadyAccepted);
});
test("should throw an error when trying to revoke while already revoked", () => {
const notAccepted = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
null,
false,
true,
);
const accepted = new AuthVerificationEntity(
"1",
"identity-1",
"token-1",
new Date(),
new Date(),
true,
true,
);
expect(() => {
notAccepted.revoke();
}).toThrowError(VerificationAlreadyRevoked);
expect(() => {
accepted.revoke();
}).toThrowError(VerificationAlreadyRevoked);
});
});