pokerogue/test/plugins/api/pokerogue-admin-api.test.ts

233 lines
9.5 KiB
TypeScript
Raw Permalink Normal View History

[Refactor] Pokerogue API client (#4583) * start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 12:57:21 -08:00
import type {
LinkAccountToDiscordIdRequest,
LinkAccountToGoogledIdRequest,
SearchAccountRequest,
SearchAccountResponse,
UnlinkAccountFromDiscordIdRequest,
UnlinkAccountFromGoogledIdRequest,
} from "#app/@types/PokerogueAdminApi";
import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api";
import { getApiBaseUrl } from "#test/testUtils/testUtils";
[Refactor] Pokerogue API client (#4583) * start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 12:57:21 -08:00
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const adminApi = new PokerogueAdminApi(apiBase);
const { server } = global;
afterEach(() => {
server.resetHandlers();
});
describe("Pokerogue Admin API", () => {
beforeEach(() => {
vi.spyOn(console, "warn");
});
describe("Link Account to Discord", () => {
const params: LinkAccountToDiscordIdRequest = { username: "test", discordId: "test-12575756" };
it("should return null on SUCCESS", async () => {
server.use(http.post(`${apiBase}/admin/account/discordLink`, () => HttpResponse.json(true)));
const success = await adminApi.linkAccountToDiscord(params);
expect(success).toBeNull();
});
it("should return a ERR_GENERIC and report a warning on FAILURE", async () => {
server.use(http.post(`${apiBase}/admin/account/discordLink`, () => new HttpResponse("", { status: 400 })));
const success = await adminApi.linkAccountToDiscord(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not link account with discord!", 400, "Bad Request");
});
it("should return a ERR_USERNAME_NOT_FOUND and report a warning on 404", async () => {
server.use(http.post(`${apiBase}/admin/account/discordLink`, () => new HttpResponse("", { status: 404 })));
const success = await adminApi.linkAccountToDiscord(params);
expect(success).toBe(adminApi.ERR_USERNAME_NOT_FOUND);
expect(console.warn).toHaveBeenCalledWith("Could not link account with discord!", 404, "Not Found");
});
it("should return a ERR_GENERIC and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/admin/account/discordLink`, () => HttpResponse.error()));
const success = await adminApi.linkAccountToDiscord(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not link account with discord!", expect.any(Error));
});
});
describe("Unlink Account from Discord", () => {
const params: UnlinkAccountFromDiscordIdRequest = { username: "test", discordId: "test-12575756" };
it("should return null on SUCCESS", async () => {
server.use(http.post(`${apiBase}/admin/account/discordUnlink`, () => HttpResponse.json(true)));
const success = await adminApi.unlinkAccountFromDiscord(params);
expect(success).toBeNull();
});
it("should return a ERR_GENERIC and report a warning on FAILURE", async () => {
server.use(http.post(`${apiBase}/admin/account/discordUnlink`, () => new HttpResponse("", { status: 400 })));
const success = await adminApi.unlinkAccountFromDiscord(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from discord!", 400, "Bad Request");
});
it("should return a ERR_USERNAME_NOT_FOUND and report a warning on 404", async () => {
server.use(http.post(`${apiBase}/admin/account/discordUnlink`, () => new HttpResponse("", { status: 404 })));
const success = await adminApi.unlinkAccountFromDiscord(params);
expect(success).toBe(adminApi.ERR_USERNAME_NOT_FOUND);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from discord!", 404, "Not Found");
});
it("should return a ERR_GENERIC and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/admin/account/discordUnlink`, () => HttpResponse.error()));
const success = await adminApi.unlinkAccountFromDiscord(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from discord!", expect.any(Error));
});
});
describe("Link Account to Google", () => {
const params: LinkAccountToGoogledIdRequest = { username: "test", googleId: "test-12575756" };
it("should return null on SUCCESS", async () => {
server.use(http.post(`${apiBase}/admin/account/googleLink`, () => HttpResponse.json(true)));
const success = await adminApi.linkAccountToGoogleId(params);
expect(success).toBeNull();
});
it("should return a ERR_GENERIC and report a warning on FAILURE", async () => {
server.use(http.post(`${apiBase}/admin/account/googleLink`, () => new HttpResponse("", { status: 400 })));
const success = await adminApi.linkAccountToGoogleId(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not link account with google!", 400, "Bad Request");
});
it("should return a ERR_USERNAME_NOT_FOUND and report a warning on 404", async () => {
server.use(http.post(`${apiBase}/admin/account/googleLink`, () => new HttpResponse("", { status: 404 })));
const success = await adminApi.linkAccountToGoogleId(params);
expect(success).toBe(adminApi.ERR_USERNAME_NOT_FOUND);
expect(console.warn).toHaveBeenCalledWith("Could not link account with google!", 404, "Not Found");
});
it("should return a ERR_GENERIC and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/admin/account/googleLink`, () => HttpResponse.error()));
const success = await adminApi.linkAccountToGoogleId(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not link account with google!", expect.any(Error));
});
});
describe("Unlink Account from Google", () => {
const params: UnlinkAccountFromGoogledIdRequest = { username: "test", googleId: "test-12575756" };
it("should return null on SUCCESS", async () => {
server.use(http.post(`${apiBase}/admin/account/googleUnlink`, () => HttpResponse.json(true)));
const success = await adminApi.unlinkAccountFromGoogleId(params);
expect(success).toBeNull();
});
it("should return a ERR_GENERIC and report a warning on FAILURE", async () => {
server.use(http.post(`${apiBase}/admin/account/googleUnlink`, () => new HttpResponse("", { status: 400 })));
const success = await adminApi.unlinkAccountFromGoogleId(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from google!", 400, "Bad Request");
});
it("should return a ERR_USERNAME_NOT_FOUND and report a warning on 404", async () => {
server.use(http.post(`${apiBase}/admin/account/googleUnlink`, () => new HttpResponse("", { status: 404 })));
const success = await adminApi.unlinkAccountFromGoogleId(params);
expect(success).toBe(adminApi.ERR_USERNAME_NOT_FOUND);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from google!", 404, "Not Found");
});
it("should return a ERR_GENERIC and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/admin/account/googleUnlink`, () => HttpResponse.error()));
const success = await adminApi.unlinkAccountFromGoogleId(params);
expect(success).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not unlink account from google!", expect.any(Error));
});
});
describe("Search Account", () => {
const params: SearchAccountRequest = { username: "test" };
it("should return [data, undefined] on SUCCESS", async () => {
const responseData: SearchAccountResponse = {
username: "test",
discordId: "discord-test-123",
googleId: "google-test-123",
lastLoggedIn: "2022-01-01",
registered: "2022-01-01",
};
server.use(http.get(`${apiBase}/admin/account/adminSearch`, () => HttpResponse.json(responseData)));
const [ data, err ] = await adminApi.searchAccount(params);
expect(data).toStrictEqual(responseData);
expect(err).toBeUndefined();
});
it("should return [undefined, ERR_GENERIC] and report a warning on on FAILURE", async () => {
server.use(http.get(`${apiBase}/admin/account/adminSearch`, () => new HttpResponse("", { status: 400 })));
const [ data, err ] = await adminApi.searchAccount(params);
expect(data).toBeUndefined();
expect(err).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not find account!", 400, "Bad Request");
});
it("should return [undefined, ERR_USERNAME_NOT_FOUND] and report a warning on on 404", async () => {
server.use(http.get(`${apiBase}/admin/account/adminSearch`, () => new HttpResponse("", { status: 404 })));
const [ data, err ] = await adminApi.searchAccount(params);
expect(data).toBeUndefined();
expect(err).toBe(adminApi.ERR_USERNAME_NOT_FOUND);
expect(console.warn).toHaveBeenCalledWith("Could not find account!", 404, "Not Found");
});
it("should return [undefined, ERR_GENERIC] and report a warning on on ERROR", async () => {
server.use(http.get(`${apiBase}/admin/account/adminSearch`, () => HttpResponse.error()));
const [ data, err ] = await adminApi.searchAccount(params);
expect(data).toBeUndefined();
expect(err).toBe(adminApi.ERR_GENERIC);
expect(console.warn).toHaveBeenCalledWith("Could not find account!", expect.any(Error));
});
});
});