end-to-end testing setup

This commit is contained in:
Hayden 2022-09-03 18:42:03 -08:00
parent b4eb7d8ddc
commit ad4c8c9ab4
41 changed files with 544 additions and 313 deletions

View file

@ -0,0 +1,57 @@
import { describe, it, 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';
function client() {
OverrideParts(config.BASE_URL, '/api/v1');
const requests = new Requests('');
return new PublicApi(requests);
}
function userClient(token: string) {
OverrideParts(config.BASE_URL, '/api/v1');
const requests = new Requests('', token);
return new UserApi(requests);
}
describe('[GET] /api/v1/status', () => {
it('basic query parameter', async () => {
const api = client();
const { response, data } = await api.status();
expect(response.status).toBe(200);
expect(data.health).toBe(true);
});
});
describe('first time user workflow (register, login)', () => {
const api = client();
const userData = {
groupName: 'test-group',
user: {
email: 'test-user@email.com',
name: 'test-user',
password: 'test-password',
},
};
it('user should be able to register', async () => {
const { response } = await api.register(userData);
expect(response.status).toBe(204);
});
it('user should be able to login', async () => {
const { response, data } = await api.login(userData.user.email, userData.user.password);
expect(response.status).toBe(200);
expect(data.token).toBeTruthy();
// Cleanup
const userApi = userClient(data.token);
{
const { response } = await userApi.deleteAccount();
expect(response.status).toBe(204);
}
});
});

View file

@ -1,31 +1,28 @@
export const prefix = '/api/v1';
const parts = {
host: 'http://localhost.com',
prefix: '/api/v1',
};
export type QueryValue =
| string
| string[]
| number
| number[]
| boolean
| null
| undefined;
export function UrlBuilder(
rest: string,
params: Record<string, QueryValue> = {}
): string {
// we use a stub base URL to leverage the URL class
const url = new URL(prefix + rest, 'http://localhost.com');
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value) {
url.searchParams.append(key, String(item));
}
} else {
url.searchParams.append(key, String(value));
}
}
// we return the path only, without the base URL
return url.toString().replace('http://localhost.com', '');
export function OverrideParts(host: string, prefix: string) {
parts.host = host;
parts.prefix = prefix;
}
export type QueryValue = string | string[] | number | number[] | boolean | null | undefined;
export function UrlBuilder(rest: string, params: Record<string, QueryValue> = {}): string {
const url = new URL(parts.prefix + rest, parts.host);
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value) {
url.searchParams.append(key, String(item));
}
} else {
url.searchParams.append(key, String(value));
}
}
// we return the path only, without the base URL
return url.toString().replace('http://localhost.com', '');
}

View file

@ -19,7 +19,18 @@ export type RegisterPayload = {
groupName: string;
};
export type StatusResult = {
health: boolean;
versions: string[];
title: string;
message: string;
};
export class PublicApi extends BaseAPI {
public status() {
return this.http.get<StatusResult>(UrlBuilder('/status'));
}
public login(username: string, password: string) {
return this.http.post<LoginPayload, LoginResult>(UrlBuilder('/users/login'), {
username,

View file

@ -36,4 +36,8 @@ export class UserApi extends BaseAPI {
public logout() {
return this.http.post<object, void>(UrlBuilder('/users/logout'), {});
}
public deleteAccount() {
return this.http.delete<void>(UrlBuilder('/users/self'));
}
}