mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 09:20:47 +00:00
clantag part 1 (#4066)
If this PR fixes an issue, link it below. If not, delete these two lines. Resolves #(issue number) ## Description: adds a check to see if you're in a clan or not. if not, checks to see if the clan exists, if it does, warns the user, if it doesn't, lets them use it. ## 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: w.o.n
This commit is contained in:
@@ -2,20 +2,45 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("../../../src/client/Api", () => ({
|
||||
getApiBase: vi.fn(() => "http://localhost:3000"),
|
||||
getUserMe: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../../src/client/Auth", () => ({
|
||||
getAuthHeader: vi.fn(async () => "Bearer test-token"),
|
||||
}));
|
||||
|
||||
import { getUserMe } from "../../../src/client/Api";
|
||||
import {
|
||||
checkClanTagOwnership,
|
||||
fetchClanDetail,
|
||||
fetchClanExists,
|
||||
fetchClanGames,
|
||||
fetchClanLeaderboard,
|
||||
fetchClanMembers,
|
||||
fetchClanRequests,
|
||||
fetchClans,
|
||||
} from "../../../src/client/ClanApi";
|
||||
import type { UserMeResponse } from "../../../src/core/ApiSchemas";
|
||||
|
||||
const userWithClans = (tags: string[]): UserMeResponse =>
|
||||
({
|
||||
user: {},
|
||||
player: {
|
||||
publicId: "p1",
|
||||
adfree: false,
|
||||
flares: [],
|
||||
achievements: { singleplayerMap: [] },
|
||||
friends: [],
|
||||
subscription: null,
|
||||
clans: tags.map((tag) => ({
|
||||
tag,
|
||||
name: tag,
|
||||
role: "member" as const,
|
||||
joinedAt: "2024-01-01T00:00:00.000Z",
|
||||
memberCount: 1,
|
||||
})),
|
||||
},
|
||||
}) as unknown as UserMeResponse;
|
||||
|
||||
const okJson = (data: unknown, status = 200) => ({
|
||||
ok: true,
|
||||
@@ -37,6 +62,109 @@ beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("fetchClanExists", () => {
|
||||
const status = (s: number) => ({ status: s });
|
||||
|
||||
it("returns true on HTTP 200", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(200))),
|
||||
);
|
||||
await expect(fetchClanExists("ABC")).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it("returns false on HTTP 404", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(404))),
|
||||
);
|
||||
await expect(fetchClanExists("XYZ")).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it("returns null on unexpected status (5xx)", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(503))),
|
||||
);
|
||||
await expect(fetchClanExists("ABC")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("returns null on transport error", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.reject(new Error("offline"))),
|
||||
);
|
||||
await expect(fetchClanExists("ABC")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("uppercases and URL-encodes the tag in the request URL", async () => {
|
||||
const fetchSpy = vi.fn(
|
||||
(_input: string | URL | Request, _init?: RequestInit) =>
|
||||
Promise.resolve(status(200)),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
await fetchClanExists("abc");
|
||||
expect(fetchSpy.mock.calls[0]![0] as string).toContain(
|
||||
"/public/clan/ABC/exists",
|
||||
);
|
||||
await fetchClanExists("a/b");
|
||||
expect(fetchSpy.mock.calls[1]![0] as string).toContain(
|
||||
"/public/clan/A%2FB/exists",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkClanTagOwnership", () => {
|
||||
const status = (s: number) => ({ status: s });
|
||||
|
||||
it("accepts a tag the user is a member of without probing existence", async () => {
|
||||
vi.mocked(getUserMe).mockResolvedValue(userWithClans(["abc"]));
|
||||
const fetchSpy = vi.fn(() => Promise.resolve(status(200)));
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
await expect(checkClanTagOwnership("ABC")).resolves.toEqual({
|
||||
tag: "ABC",
|
||||
error: null,
|
||||
});
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("accepts a fictional tag (clan does not exist)", async () => {
|
||||
vi.mocked(getUserMe).mockResolvedValue(userWithClans(["other"]));
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(404))),
|
||||
);
|
||||
await expect(checkClanTagOwnership("ABC")).resolves.toEqual({
|
||||
tag: "ABC",
|
||||
error: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a real clan the user does not belong to", async () => {
|
||||
vi.mocked(getUserMe).mockResolvedValue(false);
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(200))),
|
||||
);
|
||||
await expect(checkClanTagOwnership("ABC")).resolves.toEqual({
|
||||
tag: null,
|
||||
error: "username.tag_not_member",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects on an inconclusive existence check", async () => {
|
||||
vi.mocked(getUserMe).mockResolvedValue(false);
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(() => Promise.resolve(status(503))),
|
||||
);
|
||||
await expect(checkClanTagOwnership("ABC")).resolves.toEqual({
|
||||
tag: null,
|
||||
error: "username.tag_check_failed",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("fetchClanLeaderboard", () => {
|
||||
const leaderboardData = {
|
||||
start: "2024-01-01T00:00:00.000Z",
|
||||
|
||||
Reference in New Issue
Block a user