2022-09-09 22:46:53 +00:00
|
|
|
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 { UserApi } from "../user";
|
2022-09-05 00:37:37 +00:00
|
|
|
|
|
|
|
export function client() {
|
2022-09-09 22:46:53 +00:00
|
|
|
overrideParts(config.BASE_URL, "/api/v1");
|
|
|
|
const requests = new Requests("");
|
2022-09-05 00:37:37 +00:00
|
|
|
return new PublicApi(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function userClient(token: string) {
|
2022-09-09 22:46:53 +00:00
|
|
|
overrideParts(config.BASE_URL, "/api/v1");
|
|
|
|
const requests = new Requests("", token);
|
2022-09-05 00:37:37 +00:00
|
|
|
return new UserApi(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cache = {
|
2022-09-09 22:46:53 +00:00
|
|
|
token: "",
|
2022-09-05 00:37:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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<UserApi> {
|
|
|
|
if (cache.token) {
|
|
|
|
return userClient(cache.token);
|
|
|
|
}
|
|
|
|
const testUser = {
|
2022-09-09 22:46:53 +00:00
|
|
|
groupName: "test-group",
|
2022-09-05 00:37:37 +00:00
|
|
|
user: {
|
2022-09-09 22:46:53 +00:00
|
|
|
email: "__test__@__test__.com",
|
|
|
|
name: "__test__",
|
|
|
|
password: "__test__",
|
2022-09-05 00:37:37 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const api = client();
|
|
|
|
const { response: tryLoginResp, data } = await api.login(testUser.user.email, testUser.user.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.user.email, testUser.user.password);
|
|
|
|
expect(loginResp.status).toBe(200);
|
|
|
|
|
|
|
|
cache.token = loginData.token;
|
|
|
|
return userClient(data.token);
|
|
|
|
}
|
2022-09-05 00:46:05 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await sharedUserClient();
|
|
|
|
});
|