mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* fix: Exclude suspended users from mention suggestions * test: Remove findByQuery test
351 lines
8.5 KiB
TypeScript
351 lines
8.5 KiB
TypeScript
import { vi } from "vitest";
|
|
import { UserPreference } from "@shared/types";
|
|
import User from "./User";
|
|
import stores from "~/stores";
|
|
import { client } from "~/utils/ApiClient";
|
|
|
|
const post = vi.mocked(client.post);
|
|
|
|
describe("User model", () => {
|
|
const users = stores.users;
|
|
|
|
describe("initial", () => {
|
|
it("should return first character of name uppercased", () => {
|
|
const user = new User(
|
|
{
|
|
id: "123",
|
|
name: "alice smith",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initial).toBe("A");
|
|
});
|
|
|
|
it("should return first character when name is already uppercase", () => {
|
|
const user = new User(
|
|
{
|
|
id: "124",
|
|
name: "Bob Johnson",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initial).toBe("B");
|
|
});
|
|
|
|
it("should return ? when name is empty", () => {
|
|
const user = new User(
|
|
{
|
|
id: "125",
|
|
name: "",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initial).toBe("?");
|
|
});
|
|
|
|
it("should return ? when name is null", () => {
|
|
const user = new User(
|
|
{
|
|
id: "126",
|
|
name: null,
|
|
},
|
|
users
|
|
);
|
|
expect(user.initial).toBe("?");
|
|
});
|
|
|
|
it("should return ? when name is undefined", () => {
|
|
const user = new User(
|
|
{
|
|
id: "127",
|
|
name: undefined,
|
|
},
|
|
users
|
|
);
|
|
expect(user.initial).toBe("?");
|
|
});
|
|
});
|
|
|
|
describe("initials", () => {
|
|
it("should return empty string when name is empty", () => {
|
|
const user = new User(
|
|
{
|
|
id: "201",
|
|
name: "",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("");
|
|
});
|
|
|
|
it("should return empty string when name is null", () => {
|
|
const user = new User(
|
|
{
|
|
id: "202",
|
|
name: null,
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("");
|
|
});
|
|
|
|
it("should return single character uppercased for single word name", () => {
|
|
const user = new User(
|
|
{
|
|
id: "203",
|
|
name: "alice",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("A");
|
|
});
|
|
|
|
it("should return single character uppercased for single word name already uppercase", () => {
|
|
const user = new User(
|
|
{
|
|
id: "204",
|
|
name: "BOB",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("B");
|
|
});
|
|
|
|
it("should return first and last initials for two word name", () => {
|
|
const user = new User(
|
|
{
|
|
id: "205",
|
|
name: "alice smith",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("AS");
|
|
});
|
|
|
|
it("should return first and last initials for three word name", () => {
|
|
const user = new User(
|
|
{
|
|
id: "206",
|
|
name: "alice marie smith",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("AS");
|
|
});
|
|
|
|
it("should return first and last initials for many word name", () => {
|
|
const user = new User(
|
|
{
|
|
id: "207",
|
|
name: "alice marie jane doe smith",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("AS");
|
|
});
|
|
|
|
it("should handle names with extra spaces", () => {
|
|
const user = new User(
|
|
{
|
|
id: "208",
|
|
name: " alice smith ",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("AS");
|
|
});
|
|
|
|
it("should handle names with mixed case", () => {
|
|
const user = new User(
|
|
{
|
|
id: "209",
|
|
name: "aLiCe sMiTh",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("AS");
|
|
});
|
|
|
|
it("should handle names with special characters", () => {
|
|
const user = new User(
|
|
{
|
|
id: "210",
|
|
name: "Jean-Pierre O'Connor",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("JO");
|
|
});
|
|
|
|
it("should handle single letter names", () => {
|
|
const user = new User(
|
|
{
|
|
id: "211",
|
|
name: "X",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("X");
|
|
});
|
|
|
|
it("should handle names with unicode characters", () => {
|
|
const user = new User(
|
|
{
|
|
id: "212",
|
|
name: "José García",
|
|
},
|
|
users
|
|
);
|
|
expect(user.initials).toBe("JG");
|
|
});
|
|
});
|
|
|
|
describe("documentMemberships", () => {
|
|
const user = new User({ id: "300", name: "Member" }, users);
|
|
|
|
beforeEach(() => {
|
|
stores.documents.clear();
|
|
stores.userMemberships.clear();
|
|
});
|
|
|
|
const addDocument = (id: string, parentDocumentId?: string) =>
|
|
stores.documents.add({
|
|
id,
|
|
title: id,
|
|
parentDocumentId,
|
|
collectionId: "collection",
|
|
});
|
|
|
|
const addMembership = (id: string, documentId: string) =>
|
|
stores.userMemberships.add({
|
|
id,
|
|
userId: user.id,
|
|
documentId,
|
|
sourceId: null,
|
|
index: id,
|
|
});
|
|
|
|
it("should only include the highest shared document of a tree", () => {
|
|
addDocument("parent");
|
|
addDocument("child", "parent");
|
|
addDocument("grandchild", "child");
|
|
addMembership("m1", "grandchild");
|
|
addMembership("m2", "child");
|
|
addMembership("m3", "parent");
|
|
|
|
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
|
|
"parent",
|
|
]);
|
|
});
|
|
|
|
it("should include a moved document that is still in a cached tree", () => {
|
|
addDocument("parent");
|
|
addDocument("child");
|
|
addMembership("m1", "child");
|
|
const parent = addMembership("m2", "parent");
|
|
parent.node = {
|
|
id: "parent",
|
|
title: "parent",
|
|
url: "/doc/parent",
|
|
children: [
|
|
{ id: "child", title: "child", url: "/doc/child", children: [] },
|
|
],
|
|
};
|
|
|
|
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
|
|
"child",
|
|
"parent",
|
|
]);
|
|
});
|
|
|
|
it("should include a document whose parent is not shared", () => {
|
|
addDocument("parent");
|
|
addDocument("child", "parent");
|
|
addDocument("other");
|
|
addMembership("m1", "child");
|
|
addMembership("m2", "other");
|
|
|
|
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
|
|
"child",
|
|
"other",
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("savePreference", () => {
|
|
beforeEach(() => {
|
|
post.mockReset();
|
|
});
|
|
|
|
it("should send only preferences and keep other values", async () => {
|
|
const user = users.add({
|
|
id: "pref-success",
|
|
name: "Alice",
|
|
preferences: { [UserPreference.RememberLastPath]: true },
|
|
});
|
|
post.mockResolvedValueOnce({ data: { id: "pref-success" } });
|
|
|
|
const promise = user.savePreference(
|
|
UserPreference.UseCursorPointer,
|
|
false
|
|
);
|
|
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
|
|
await promise;
|
|
|
|
expect(post).toHaveBeenCalledWith("/users.update", {
|
|
id: "pref-success",
|
|
preferences: {
|
|
[UserPreference.RememberLastPath]: true,
|
|
[UserPreference.UseCursorPointer]: false,
|
|
},
|
|
});
|
|
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
|
|
expect(user.getPreference(UserPreference.RememberLastPath)).toBe(true);
|
|
});
|
|
|
|
it("should revert the preference on failure", async () => {
|
|
const user = users.add({
|
|
id: "pref-failure",
|
|
name: "Alice",
|
|
preferences: { [UserPreference.UseCursorPointer]: true },
|
|
});
|
|
post.mockRejectedValueOnce(new Error("server error"));
|
|
|
|
const promise = user.savePreference(
|
|
UserPreference.UseCursorPointer,
|
|
false
|
|
);
|
|
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
|
|
|
|
await expect(promise).rejects.toThrow("server error");
|
|
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("searchSuppressed", () => {
|
|
it("should be false for an active user", () => {
|
|
const user = new User(
|
|
{ id: "600", name: "Active", isSuspended: false },
|
|
users
|
|
);
|
|
expect(user.searchSuppressed).toBe(false);
|
|
});
|
|
|
|
it("should be true for a suspended user", () => {
|
|
const user = new User(
|
|
{ id: "601", name: "Suspended", isSuspended: true },
|
|
users
|
|
);
|
|
expect(user.searchSuppressed).toBe(true);
|
|
});
|
|
|
|
it("should be true for a deleted user", () => {
|
|
const user = new User(
|
|
{ id: "602", name: "Deleted", deletedAt: new Date().toISOString() },
|
|
users
|
|
);
|
|
expect(user.searchSuppressed).toBe(true);
|
|
});
|
|
});
|
|
});
|