mega-plant/lib/auth-policy.test.ts

31 lines
1.4 KiB
TypeScript
Raw Normal View History

import { test } from "node:test";
import assert from "node:assert/strict";
import { validateLogin, validateNewPassword, validateNewUser, authDestination } from "./auth-policy";
test("login rejects missing credentials and malformed email", () => {
for (const [email, password] of [["", "mega2026"], ["person@example.com", ""], ["person", "mega2026"]]) {
assert.ok(validateLogin(email, password));
}
assert.equal(validateLogin("person@example.com", "mega2026"), null);
});
test("chosen password requires eight characters, confirmation and replacement of the starter password", () => {
assert.ok(validateNewPassword("short", "short"));
assert.ok(validateNewPassword("password", "different"));
assert.ok(validateNewPassword("mega2026", "mega2026"));
assert.equal(validateNewPassword("abcdefgh", "abcdefgh"), null);
assert.equal(validateNewPassword("a b c d ", "a b c d "), null);
});
test("add user requires a name and valid email", () => {
assert.ok(validateNewUser(" ", "person@example.com"));
assert.ok(validateNewUser("Person", "broken-email"));
assert.equal(validateNewUser("Person", "person@example.com"), null);
});
test("anonymous and first-login users cannot enter the dashboard", () => {
assert.equal(authDestination(null), "/login");
assert.equal(authDestination({ passwordChanged: false }), "/change-password");
assert.equal(authDestination({ passwordChanged: true }), "/");
});