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

158 lines
5.6 KiB
TypeScript
Raw 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 { AccountInfoResponse } from "#app/@types/PokerogueAccountApi";
import { SESSION_ID_COOKIE_NAME } from "#app/constants";
import { PokerogueAccountApi } from "#app/plugins/api/pokerogue-account-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 * as Utils from "#app/utils";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const accountApi = new PokerogueAccountApi(apiBase);
const { server } = global;
afterEach(() => {
server.resetHandlers();
});
describe("Pokerogue Account API", () => {
beforeEach(() => {
vi.spyOn(console, "warn");
});
describe("Get Info", () => {
it("should return account-info & 200 on SUCCESS", async () => {
const expectedAccountInfo: AccountInfoResponse = {
username: "test",
lastSessionSlot: -1,
discordId: "23235353543535",
googleId: "1ed1d1d11d1d1d1d1d1",
hasAdminRole: false,
};
server.use(http.get(`${apiBase}/account/info`, () => HttpResponse.json(expectedAccountInfo)));
const [ accountInfo, status ] = await accountApi.getInfo();
expect(accountInfo).toEqual(expectedAccountInfo);
expect(status).toBe(200);
});
it("should return null + status-code anad report a warning on FAILURE", async () => {
server.use(http.get(`${apiBase}/account/info`, () => new HttpResponse("", { status: 401 })));
const [ accountInfo, status ] = await accountApi.getInfo();
expect(accountInfo).toBeNull();
expect(status).toBe(401);
expect(console.warn).toHaveBeenCalledWith("Could not get account info!", 401, "Unauthorized");
});
it("should return null + 500 anad report a warning on ERROR", async () => {
server.use(http.get(`${apiBase}/account/info`, () => HttpResponse.error()));
const [ accountInfo, status ] = await accountApi.getInfo();
expect(accountInfo).toBeNull();
expect(status).toBe(500);
expect(console.warn).toHaveBeenCalledWith("Could not get account info!", expect.any(Error));
});
});
describe("Register", () => {
const registerParams = { username: "test", password: "test" };
it("should return null on SUCCESS", async () => {
server.use(http.post(`${apiBase}/account/register`, () => HttpResponse.text()));
const error = await accountApi.register(registerParams);
expect(error).toBeNull();
});
it("should return error message on FAILURE", async () => {
server.use(
http.post(`${apiBase}/account/register`, () => new HttpResponse("Username is already taken", { status: 400 }))
);
const error = await accountApi.register(registerParams);
expect(error).toBe("Username is already taken");
});
it("should return \"Unknown error\" and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/account/register`, () => HttpResponse.error()));
const error = await accountApi.register(registerParams);
expect(error).toBe("Unknown error!");
expect(console.warn).toHaveBeenCalledWith("Register failed!", expect.any(Error));
});
});
describe("Login", () => {
const loginParams = { username: "test", password: "test" };
it("should return null and set the cookie on SUCCESS", async () => {
vi.spyOn(Utils, "setCookie");
server.use(http.post(`${apiBase}/account/login`, () => HttpResponse.json({ token: "abctest" })));
const error = await accountApi.login(loginParams);
expect(error).toBeNull();
expect(Utils.setCookie).toHaveBeenCalledWith(SESSION_ID_COOKIE_NAME, "abctest");
});
it("should return error message and report a warning on FAILURE", async () => {
server.use(
http.post(`${apiBase}/account/login`, () => new HttpResponse("Password is incorrect", { status: 401 }))
);
const error = await accountApi.login(loginParams);
expect(error).toBe("Password is incorrect");
expect(console.warn).toHaveBeenCalledWith("Login failed!", 401, "Unauthorized");
});
it("should return \"Unknown error\" and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/account/login`, () => HttpResponse.error()));
const error = await accountApi.login(loginParams);
expect(error).toBe("Unknown error!");
expect(console.warn).toHaveBeenCalledWith("Login failed!", expect.any(Error));
});
});
describe("Logout", () => {
beforeEach(() => {
vi.spyOn(Utils, "removeCookie");
});
it("should remove cookie on success", async () => {
vi.spyOn(Utils, "setCookie");
server.use(http.get(`${apiBase}/account/logout`, () => new HttpResponse("", { status: 200 })));
await accountApi.logout();
expect(Utils.removeCookie).toHaveBeenCalledWith(SESSION_ID_COOKIE_NAME);
});
it("should report a warning on and remove cookie on FAILURE", async () => {
server.use(http.get(`${apiBase}/account/logout`, () => new HttpResponse("", { status: 401 })));
await accountApi.logout();
expect(Utils.removeCookie).toHaveBeenCalledWith(SESSION_ID_COOKIE_NAME);
expect(console.warn).toHaveBeenCalledWith("Log out failed!", expect.any(Error));
});
it("should report a warning on and remove cookie on ERROR", async () => {
server.use(http.get(`${apiBase}/account/logout`, () => HttpResponse.error()));
await accountApi.logout();
expect(Utils.removeCookie).toHaveBeenCalledWith(SESSION_ID_COOKIE_NAME);
expect(console.warn).toHaveBeenCalledWith("Log out failed!", expect.any(Error));
});
});
});