Files
OpenFrontIO/tests/Censor.test.ts
T
Evan 900cc89067 Better username censoring (#3122)
## Description:

Many inapropriate names bypass the current filter. This PR does the
following:

1. Moves name censoring to server side so inappropriate names are
scrubbed before being sent to the client
2. Requests a list of profane words from the api, this allows us to
quickly add new profane words in the admin panel without having to
redeploy.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
2026-02-09 21:05:59 -08:00

43 lines
1.4 KiB
TypeScript

// Mocks the output of translation functions to return predictable values.
vi.mock("../src/client/Utils", () => ({
translateText: (key: string, vars?: any) =>
vars ? `${key}:${JSON.stringify(vars)}` : key,
}));
import {
MAX_USERNAME_LENGTH,
validateUsername,
} from "../src/core/validations/username";
describe("username.ts functions", () => {
describe("validateUsername", () => {
test("rejects non-string", () => {
// @ts-expect-error: Testing non-string input to validateUsername on purpose
const res = validateUsername(123);
expect(res.isValid).toBe(false);
expect(res.error).toBeDefined();
});
test("rejects too short", () => {
const res = validateUsername("ab");
expect(res.isValid).toBe(false);
});
test("rejects too long", () => {
const long = "a".repeat(MAX_USERNAME_LENGTH + 1);
const res = validateUsername(long);
expect(res.isValid).toBe(false);
});
test("rejects invalid chars", () => {
const res = validateUsername("Invalid!Name");
expect(res.isValid).toBe(false);
});
test("accepts valid ASCII names", () => {
const res = validateUsername("Good_Name123");
expect(res.isValid).toBe(true);
});
test("accepts allowed Unicode like ü", () => {
const res = validateUsername("Üser");
expect(res.isValid).toBe(true);
});
});
});