homebox/frontend/lib/api/__test__/test-utils.ts
Hayden 79f7ad40cb
feat: user profiles (#32)
* add user profiles and theme selectors

* lowercase buttons by default

* basic layout

* (wip) init token APIs

* refactor server to support variable options

* fix types

* api refactor / registration tests

* implement UI for url and join

* remove console.logs

* rename repository factory

* fix upload size
2022-10-06 21:54:09 -05:00

59 lines
1.6 KiB
TypeScript

import { beforeAll, expect } from "vitest";
import { Requests } from "../../requests";
import { overrideParts } from "../base/urls";
import { PublicApi } from "../public";
import * as config from "../../../test/config";
import { UserClient } from "../user";
export function client() {
overrideParts(config.BASE_URL, "/api/v1");
const requests = new Requests("");
return new PublicApi(requests);
}
export function userClient(token: string) {
overrideParts(config.BASE_URL, "/api/v1");
const requests = new Requests("", token);
return new UserClient(requests);
}
const cache = {
token: "",
};
/*
* Shared UserApi token for tests where the creation of a user is _not_ import
* to the test. This is useful for tests that are testing the user API itself.
*/
export async function sharedUserClient(): Promise<UserClient> {
if (cache.token) {
return userClient(cache.token);
}
const testUser = {
groupName: "test-group",
email: "__test__@__test__.com",
name: "__test__",
password: "__test__",
};
const api = client();
const { response: tryLoginResp, data } = await api.login(testUser.email, testUser.password);
if (tryLoginResp.status === 200) {
cache.token = data.token;
return userClient(cache.token);
}
const { response: registerResp } = await api.register(testUser);
expect(registerResp.status).toBe(204);
const { response: loginResp, data: loginData } = await api.login(testUser.email, testUser.password);
expect(loginResp.status).toBe(200);
cache.token = loginData.token;
return userClient(data.token);
}
beforeAll(async () => {
await sharedUserClient();
});