forked from mtusi2026/mtusi2026lab7
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import { readGrafanaDatasources } from "../scripts/grafana-datasources";
|
|
const uids = ["prometheus", "openobserve"];
|
|
function fixture(mutator?: (x: any) => void) {
|
|
const calls: string[] = [],
|
|
details = Object.fromEntries(
|
|
uids.map((uid) => [
|
|
uid,
|
|
{
|
|
uid,
|
|
basicAuth: true,
|
|
basicAuthUser: "vp7-query@example.invalid",
|
|
secureJsonFields: { basicAuthPassword: true },
|
|
},
|
|
]),
|
|
);
|
|
const request = async (path: string) => {
|
|
calls.push(path);
|
|
if (path === "/api/datasources")
|
|
return {
|
|
status: 200,
|
|
body: uids.map((uid) => ({ uid, basicAuth: true })),
|
|
};
|
|
const uid = path.split("/")[4]!;
|
|
const response = path.endsWith("/health")
|
|
? { status: 200, body: { status: "OK" } }
|
|
: { status: 200, body: { ...details[uid] } };
|
|
mutator?.(response);
|
|
return response;
|
|
};
|
|
return { request, calls };
|
|
}
|
|
test("native listDTO may omit auth fields; authenticate the exact UID detail and health readers", async () => {
|
|
const f = fixture();
|
|
expect(await readGrafanaDatasources(f.request)).toEqual(uids);
|
|
expect(f.calls).toEqual([
|
|
"/api/datasources",
|
|
"/api/datasources/uid/prometheus",
|
|
"/api/datasources/uid/prometheus/health",
|
|
"/api/datasources/uid/openobserve",
|
|
"/api/datasources/uid/openobserve/health",
|
|
]);
|
|
});
|
|
test("configured root account is rejected before datasource health can masquerade as scoped auth", async () => {
|
|
const f = fixture((x) => {
|
|
if (x.body.basicAuthUser)
|
|
x.body.basicAuthUser = "vp7-owner@example.invalid";
|
|
});
|
|
await expect(readGrafanaDatasources(f.request)).rejects.toThrow(
|
|
"GRAFANA_DATASOURCE_PRINCIPAL_MISMATCH",
|
|
);
|
|
});
|
|
test("HTTP200 health with error body is not a healthy datasource", async () => {
|
|
const f = fixture((x) => {
|
|
if (x.body.status) x.body.status = "ERROR";
|
|
});
|
|
await expect(readGrafanaDatasources(f.request)).rejects.toThrow(
|
|
"GRAFANA_DATASOURCE_HEALTH_FAILED",
|
|
);
|
|
});
|