2022-09-09 22:46:53 +00:00
|
|
|
import { beforeAll, expect } from "vitest";
|
2023-11-16 02:19:51 +00:00
|
|
|
import { faker } from "@faker-js/faker";
|
2022-10-07 02:54:09 +00:00
|
|
|
import { UserClient } from "../user";
|
2022-10-09 17:23:21 +00:00
|
|
|
import { factories } from "./factories";
|
2022-09-05 00:37:37 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2022-10-07 02:54:09 +00:00
|
|
|
export async function sharedUserClient(): Promise<UserClient> {
|
2022-09-05 00:37:37 +00:00
|
|
|
if (cache.token) {
|
2022-10-09 17:23:21 +00:00
|
|
|
return factories.client.user(cache.token);
|
2022-09-05 00:37:37 +00:00
|
|
|
}
|
|
|
|
const testUser = {
|
2023-11-16 02:19:51 +00:00
|
|
|
email: faker.internet.email(),
|
|
|
|
name: faker.person.fullName(),
|
|
|
|
password: faker.internet.password(),
|
2022-10-09 13:03:24 +00:00
|
|
|
token: "",
|
2022-09-05 00:37:37 +00:00
|
|
|
};
|
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
const api = factories.client.public();
|
2022-09-27 23:52:13 +00:00
|
|
|
const { response: tryLoginResp, data } = await api.login(testUser.email, testUser.password);
|
2022-09-05 00:37:37 +00:00
|
|
|
|
|
|
|
if (tryLoginResp.status === 200) {
|
|
|
|
cache.token = data.token;
|
2022-10-09 17:23:21 +00:00
|
|
|
return factories.client.user(cache.token);
|
2022-09-05 00:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const { response: registerResp } = await api.register(testUser);
|
|
|
|
expect(registerResp.status).toBe(204);
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
const { response: loginResp, data: loginData } = await api.login(testUser.email, testUser.password);
|
2022-09-05 00:37:37 +00:00
|
|
|
expect(loginResp.status).toBe(200);
|
|
|
|
|
|
|
|
cache.token = loginData.token;
|
2022-10-09 17:23:21 +00:00
|
|
|
return factories.client.user(data.token);
|
2022-09-05 00:37:37 +00:00
|
|
|
}
|
2022-09-05 00:46:05 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await sharedUserClient();
|
|
|
|
});
|