Files
anything-llm/server/__tests__/models/user.test.js
Marcello Fitton afa3073893 Refine and Standardize Username Constraints (#4828)
* Implement Unix username standard validations on username creation and updating.

* Remove leading underscore permissibility | Replace hardcoded username rules with a centralized USERNAME_REQUIREMENTS_TEXT for better maintainability.

* Add username requirements translations for invite and admin user creation | Replace hardcoded username requirements with localized strings in user modals

* Refactor username requirements localization

* Remove unneeded comment | Move Regex comment to validator fn

* Remove username validation utility function to keep validation responsibilities on the server | Allow onboarding flow multi-user mode username creation step to send pre-validated credentials to server.

* Enhance error handling in system endpoints by returning a JSON response with error details instead of a plain status for internal server errors.

* Update username requirement localization in AccountModal and UserSetup components to use centralized translation key.

* test enforcements
allow users to keep existing usernames without collision

* Normalize Translations (#4861)

* normalize translations

* add translations

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2026-01-26 16:18:11 -08:00

46 lines
2.1 KiB
JavaScript

const { User } = require("../../models/user");
describe("username validation restrictions", () => {
beforeEach(() => {
jest.clearAllMocks();
});
const failureMessages = [
"Username cannot be longer than 32 characters",
"Username must be at least 2 characters",
"Username must start with a lowercase letter and only contain lowercase letters, numbers, underscores, hyphens, and periods",
];
it("should throw an error if the username is longer than 32 characters", () => {
expect(() => User.validations.username("a".repeat(33))).toThrow(failureMessages[0]);
});
it("should throw an error if the username is less than 2 characters", () => {
expect(() => User.validations.username("a")).toThrow(failureMessages[1]);
});
it("should throw an error if the username does not start with a lowercase letter", () => {
expect(() => User.validations.username("Aa1")).toThrow(failureMessages[2]);
});
it("should throw an error if the username contains invalid characters", () => {
expect(() => User.validations.username("ad-123_456.789*")).toThrow(failureMessages[2]);
expect(() => User.validations.username("ad-123_456#456")).toThrow(failureMessages[2]);
expect(() => User.validations.username("ad-123_456!456")).toThrow(failureMessages[2]);
});
it("should return the username if it is valid or an email address", () => {
expect(User.validations.username("a123_456.789@")).toBe("a123_456.789@");
expect(User.validations.username("a123_456.789@example.com")).toBe("a123_456.789@example.com");
});
it("should throw an error if the username is not a string", () => {
expect(() => User.validations.username(123)).toThrow(failureMessages[2]);
expect(() => User.validations.username(null)).not.toThrow();
expect(() => User.validations.username(undefined)).toThrow(failureMessages[1]);
expect(() => User.validations.username({})).toThrow(failureMessages[3]);
expect(() => User.validations.username([])).toThrow(failureMessages[3]);
expect(() => User.validations.username(true)).not.toThrow();
expect(() => User.validations.username(false)).not.toThrow();
});
});