121 lines
3.4 KiB
TypeScript
121 lines
3.4 KiB
TypeScript
import { http, HttpResponse, HttpResponseResolver, PathParams } from "msw";
|
|
import { setupServer } from "msw/node";
|
|
import appsFixtures from "./apps.fixtures.json";
|
|
|
|
export const CAPROVER_TEST_DOMAIN = "caprover.test";
|
|
export const CAPROVER_TEST_PASSWORD = "password";
|
|
const TEST_TOKEN = "123";
|
|
const BASE_URI = `https://${CAPROVER_TEST_DOMAIN}/api/v2`;
|
|
|
|
const withAuth = (resolver: HttpResponseResolver): HttpResponseResolver => {
|
|
return (input) => {
|
|
const headers = input.request.headers;
|
|
if (
|
|
headers.get("x-namespace") !== "captain" ||
|
|
headers.get("x-captain-auth") !== TEST_TOKEN
|
|
) {
|
|
return HttpResponse.json({}, { status: 401 });
|
|
}
|
|
return resolver(input);
|
|
};
|
|
};
|
|
|
|
// @see https://github.com/caprover/caprover-cli/blob/master/src/api/ApiManager.ts
|
|
// @see https://github.com/caprover/caprover/tree/master/src/routes
|
|
const handlers = [
|
|
http.post<PathParams, { password: string }>(
|
|
`${BASE_URI}/login`,
|
|
async ({ request }) => {
|
|
const credentials = await request.json();
|
|
if (
|
|
credentials.password !== CAPROVER_TEST_PASSWORD ||
|
|
request.headers.get("x-namespace") !== "captain"
|
|
) {
|
|
return HttpResponse.json({
|
|
status: 1106,
|
|
description: "Auth token corrupted",
|
|
data: {},
|
|
});
|
|
}
|
|
return HttpResponse.json({ data: { token: TEST_TOKEN } });
|
|
}
|
|
),
|
|
http.get(
|
|
`${BASE_URI}/user/apps/appDefinitions`,
|
|
withAuth(() => {
|
|
return HttpResponse.json({
|
|
data: {
|
|
appDefinitions: appsFixtures,
|
|
},
|
|
});
|
|
})
|
|
),
|
|
http.post<
|
|
{ name: string },
|
|
{
|
|
captainDefinitionContent?: string;
|
|
tarballFile?: string;
|
|
gitHash?: string;
|
|
}
|
|
>(`${BASE_URI}/user/apps/appData/:name`, async ({ request, params }) => {
|
|
const body = await request.json();
|
|
if (/* !body.tarballFile && */ !body.captainDefinitionContent) {
|
|
return HttpResponse.json({
|
|
status: 1100,
|
|
description:
|
|
"Either tarballfile or captainDefinitionContent should be present.",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
const app = appsFixtures.find((app) => app.appName === params.name);
|
|
if (!app) {
|
|
return HttpResponse.json({
|
|
status: 1000,
|
|
description: `App (${params.name}) could not be found. Make sure that you have created the app.`,
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
const newVersion = JSON.parse(body.captainDefinitionContent).imageName;
|
|
app.deployedVersion = app.versions.length;
|
|
app.versions.push({
|
|
version: app.versions.length,
|
|
deployedImageName: newVersion,
|
|
gitHash: body.gitHash ?? "",
|
|
timeStamp: new Date().toISOString(),
|
|
});
|
|
|
|
return HttpResponse.json({
|
|
status: 100,
|
|
description: "Deploy is done",
|
|
data: {},
|
|
});
|
|
}),
|
|
http.get(
|
|
`${BASE_URI}/user/apps/appData/:name/logs`,
|
|
withAuth(({ params }) => {
|
|
const app = appsFixtures.find((app) => app.appName === params.name);
|
|
if (!app) {
|
|
return HttpResponse.json({
|
|
status: 1000,
|
|
description: `App (${params.name}) could not be found. Make sure that you have created the app.`,
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
return HttpResponse.json({
|
|
status: 100,
|
|
description: "App runtime logs are retrieved",
|
|
data: {
|
|
logs: `${params.name} logs`,
|
|
},
|
|
});
|
|
})
|
|
),
|
|
];
|
|
|
|
const server = setupServer(...handlers);
|
|
|
|
export default server;
|