49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
isValidPassword,
|
|
isValidUsername,
|
|
otherRole,
|
|
} from "./userManagement";
|
|
|
|
describe("isValidUsername", () => {
|
|
it("accepts simple names", () => {
|
|
expect(isValidUsername("alice")).toBe(true);
|
|
expect(isValidUsername("A1_b-c.d")).toBe(true);
|
|
});
|
|
|
|
it("rejects empty and overlong names", () => {
|
|
expect(isValidUsername("")).toBe(false);
|
|
expect(isValidUsername("a".repeat(65))).toBe(false);
|
|
});
|
|
|
|
it("rejects names starting with a non-alphanumeric", () => {
|
|
expect(isValidUsername("-alice")).toBe(false);
|
|
expect(isValidUsername(".alice")).toBe(false);
|
|
});
|
|
|
|
it("rejects illegal characters", () => {
|
|
expect(isValidUsername("al ice")).toBe(false);
|
|
expect(isValidUsername("alice!")).toBe(false);
|
|
expect(isValidUsername("爱丽丝")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isValidPassword", () => {
|
|
it("requires at least 8 characters", () => {
|
|
expect(isValidPassword("1234567")).toBe(false);
|
|
expect(isValidPassword("12345678")).toBe(true);
|
|
});
|
|
|
|
it("rejects passwords over 256 characters", () => {
|
|
expect(isValidPassword("x".repeat(256))).toBe(true);
|
|
expect(isValidPassword("x".repeat(257))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("otherRole", () => {
|
|
it("flips admin and user", () => {
|
|
expect(otherRole("admin")).toBe("user");
|
|
expect(otherRole("user")).toBe("admin");
|
|
});
|
|
});
|