feat(auth): implement RBAC audit logging and session security

Integrate granular audit logging for admin actions, transition to role-based API access via `requireAdminApiSession`, and update session tokens to persist user roles and IDs for consistent multi-admin management.
This commit is contained in:
Team Dark Devil committed 2026-11-20 18:48:54 +06:00
1 parent 2873b9904a
commit 5c977d6977
37 files changed
+5939 -349

No files matched your search

+130
View File
@@ -476,3 +476,133 @@ test("DEPLOY-BUG-02, 03 & 04: Port 4000 memoizes schema DDLs, hydrates rotated J
ingestionServer.stopTimers();
}
});
test("V2-Deploy-Fix-01 (DEPLOY-V2-BUG-01, 02 & 03): middleware permits GET /api/prometheus Bearer scrapes, Port 4000 blocks unhydrated DB pruning, and prisma/schema.prisma mirrors all 7 V2 tables", async () => {
// 1. DEPLOY-V2-BUG-01: middleware.ts allows GET /api/prometheus without a browser cookie so route handler can verify Bearer token
const { middleware } = await import("../middleware");
const { NextRequest } = await import("next/server");
const promReq = new NextRequest("http://localhost:3000/api/prometheus", {
method: "GET",
headers: {
authorization: "Bearer prom_scrape_token_123",
},
});
const promMwRes = await middleware(promReq);
assert.notEqual(
promMwRes.status,
401,
"middleware.ts must not block GET /api/prometheus with 401 before route handler verifies Bearer token",
);
assert.equal(promMwRes.headers.get("x-middleware-next"), "1");
// Meanwhile, protected admin APIs like GET /api/metrics still require admin session cookie in middleware
const prevSecret = process.env.DATASTACK_ADMIN_SESSION_SECRET;
process.env.DATASTACK_ADMIN_SESSION_SECRET =
"test-session-secret-32-characters-minimum-length";
try {
const metricsReq = new NextRequest("http://localhost:3000/api/metrics", {
method: "GET",
});
const metricsMwRes = await middleware(metricsReq);
assert.equal(metricsMwRes.status, 401);
} finally {
if (prevSecret === undefined) {
delete process.env.DATASTACK_ADMIN_SESSION_SECRET;
} else {
process.env.DATASTACK_ADMIN_SESSION_SECRET = prevSecret;
}
}
// 2. DEPLOY-V2-BUG-02: Port 4000 isDbReadyForMaintenance() is false when dbPool is configured but unhydrated
const prevDisableListen = process.env.DATASTACK_DISABLE_LISTEN;
process.env.DATASTACK_DISABLE_LISTEN = "1";
const ingestionServer = require("../server/index.js") as {
setDbPoolForTests: (pool: unknown) => void;
isDbReadyForMaintenance: () => boolean;
syncSettingsFromDatabase: () => Promise<boolean>;
stopTimers: () => void;
};
if (prevDisableListen === undefined) {
delete process.env.DATASTACK_DISABLE_LISTEN;
} else {
process.env.DATASTACK_DISABLE_LISTEN = prevDisableListen;
}
try {
// Without dbPool (in-memory mode), maintenance is ready immediately
ingestionServer.setDbPoolForTests(null);
assert.equal(ingestionServer.isDbReadyForMaintenance(), true);
// With dbPool attached but before hydration succeeds, maintenance is blocked
let shouldFailDb = true;
const fakePool = {
async query(sql: string) {
if (shouldFailDb) {
throw new Error("DB starting up");
}
if (/SELECT\s+max_events_per_sec/i.test(sql)) {
return { rowCount: 0, rows: [] };
}
return { rowCount: 0, rows: [] };
},
};
ingestionServer.setDbPoolForTests(fakePool);
assert.equal(
ingestionServer.isDbReadyForMaintenance(),
false,
"Port 4000 must block background pruning while DATABASE_URL pool is unhydrated",
);
assert.equal(await ingestionServer.syncSettingsFromDatabase(), false);
assert.equal(ingestionServer.isDbReadyForMaintenance(), false);
// Once DB hydrates, maintenance becomes ready
shouldFailDb = false;
assert.equal(await ingestionServer.syncSettingsFromDatabase(), true);
assert.equal(ingestionServer.isDbReadyForMaintenance(), true);
} finally {
ingestionServer.setDbPoolForTests(null);
ingestionServer.stopTimers();
}
// 3. DEPLOY-V2-BUG-03: prisma/schema.prisma mirrors all 7 V2 tables and V2 columns
const prismaSrc = readFileSync(
resolve(process.cwd(), "prisma/schema.prisma"),
"utf8",
);
for (const table of [
"client_sdks",
"admin_settings",
"deleted_clients",
"admin_users",
"admin_login_attempts",
"admin_team_members",
"admin_audit_logs",
]) {
assert.match(
prismaSrc,
new RegExp(`@@map\\("${table}"\\)`),
`prisma/schema.prisma must map ${table}`,
);
}
for (const col of [
"storage_quota_mb",
"allowed_ips",
"expires_at",
"compress_after_days",
"enable_s3_backup",
"s3_secret_access_key_enc",
"disk_pause_threshold_pct",
"enable_webhook_alerts",
"webhook_secret_enc",
"enable_prometheus_metrics",
"prometheus_metrics_token_enc",
]) {
assert.match(
prismaSrc,
new RegExp(`@map\\("${col}"\\)`),
`prisma/schema.prisma must map V2 column ${col}`,
);
}
});
+173 -113
View File
@@ -9,11 +9,11 @@ import test from "node:test";
import { NextRequest } from "next/server";
import { io as ioClient, type Socket } from "socket.io-client";
import { PATCH as patchClientRoute } from "../app/api/clients/[id]/route";
import { GET as clientsGet, POST as clientsPost } from "../app/api/clients/route";
import { POST as clientsPost } from "../app/api/clients/route";
import { POST as eventsPost } from "../app/api/events/route";
import { GET as exportGet } from "../app/api/export/route";
import { GET as prometheusGet } from "../app/api/prometheus/route";
import { GET as settingsGet, POST as settingsPost } from "../app/api/settings/route";
import { POST as settingsPost } from "../app/api/settings/route";
import { POST as webhookTestPost } from "../app/api/settings/webhook-test/route";
import { SettingsManager } from "../core/adminSettings/settingsManager";
import {
@@ -113,16 +113,16 @@ test("V2-Phase-02 [IP Allowlist & Token Expiry]: validateAllowedIpsInput, isClie
// Expiration validation & evaluation
assert.deepEqual(validateClientExpiresAtInput(null), {
valid: true,
normalized: null,
iso: null,
});
assert.deepEqual(validateClientExpiresAtInput(""), {
valid: true,
normalized: null,
iso: null,
});
const validExp = validateClientExpiresAtInput("2030-06-15T12:00:00.000Z");
assert.equal(validExp.valid, true);
if (validExp.valid) {
assert.equal(validExp.normalized, "2030-06-15T12:00:00.000Z");
assert.equal(validExp.iso, "2030-06-15T12:00:00.000Z");
}
const badExp = validateClientExpiresAtInput("not-a-timestamp");
assert.equal(badExp.valid, false);
@@ -170,7 +170,7 @@ test("V2-Phase-02 [Webhook Notifier & /api/settings/webhook-test]: HMAC-SHA256 s
try {
// 1. Format & signature verification
const formatted = formatOutboundWebhookBody({
const formattedJsonStr = formatOutboundWebhookBody({
event: "DISK_SPACE_LOW",
severity: "critical",
title: "Disk Threshold Reached",
@@ -179,34 +179,37 @@ test("V2-Phase-02 [Webhook Notifier & /api/settings/webhook-test]: HMAC-SHA256 s
timestamp: "2026-11-19T12:00:00.000Z",
details: { usedPct: 95.4 },
});
const formatted = JSON.parse(formattedJsonStr) as {
text: string;
content: string;
};
assert.ok(formatted.text.includes("DISK_SPACE_LOW"));
assert.equal(formatted.content, formatted.text);
const bodyJson = JSON.stringify(formatted);
const sig = buildWebhookSignature(bodyJson, "webhook_secret_xyz");
const sig = buildWebhookSignature(formattedJsonStr, "webhook_secret_xyz");
assert.ok(sig.startsWith("sha256="));
// 2. Cooldown deduplication test
const first = await dispatchWebhookAlertAsync(
{
enabled: true,
url: webhookUrl,
secret: "webhook_secret_xyz",
cooldownSec: 120,
},
{
event: "CLIENT_QUOTA_EXCEEDED",
event: "STORAGE_QUOTA_EXCEEDED",
severity: "warning",
title: "Client Quota Exceeded",
message: "Quota reached for svc_alpha",
clientId: "svc_alpha",
},
{
enabled: true,
webhookUrl,
webhookSecret: "webhook_secret_xyz",
cooldownSec: 120,
},
);
assert.equal(first.ok, true);
assert.equal(receivedRequests.length, 1);
assert.equal(
receivedRequests[0].headers["x-datastack-event"],
"CLIENT_QUOTA_EXCEEDED",
"STORAGE_QUOTA_EXCEEDED",
);
assert.equal(
receivedRequests[0].headers["x-datastack-signature"],
@@ -216,21 +219,22 @@ test("V2-Phase-02 [Webhook Notifier & /api/settings/webhook-test]: HMAC-SHA256 s
// Duplicate event for same client within cooldown window is suppressed
const second = await dispatchWebhookAlertAsync(
{
enabled: true,
url: webhookUrl,
secret: "webhook_secret_xyz",
cooldownSec: 120,
},
{
event: "CLIENT_QUOTA_EXCEEDED",
event: "STORAGE_QUOTA_EXCEEDED",
severity: "warning",
title: "Client Quota Exceeded",
message: "Quota reached again for svc_alpha",
clientId: "svc_alpha",
},
{
enabled: true,
webhookUrl,
webhookSecret: "webhook_secret_xyz",
cooldownSec: 120,
},
);
assert.equal(second.ok, true);
assert.equal(second.suppressedByCooldown, true);
assert.equal(second.skipped, true);
assert.equal(second.reason, "COOLDOWN_ACTIVE");
assert.equal(receivedRequests.length, 1);
// 3. Test POST /api/settings/webhook-test route (200 & 502 WEBHOOK_DELIVERY_FAILED V2-E06)
@@ -271,17 +275,44 @@ test("V2-Phase-02 [Webhook Notifier & /api/settings/webhook-test]: HMAC-SHA256 s
assert.equal(failTestData.code, "WEBHOOK_DELIVERY_FAILED");
} finally {
await new Promise<void>((resolve) => mockWebhookServer.close(() => resolve()));
await DataBufferEngine.shutdown();
resetAdminStateForTests();
}
});
test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (403 V2-E07) & Token expiration (401 V2-E08)", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "datastack-v2-p2-sec-"));
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
const prevDisableListen = process.env.DATASTACK_DISABLE_LISTEN;
const prevDbUrl = process.env.DATABASE_URL;
process.env.DATASTACK_STORAGE_DIR = tmpDir;
CSVStorageManager.resetForTests(tmpDir);
await CSVStorageManager.initializeAsync();
process.env.DATASTACK_DISABLE_LISTEN = "1";
delete process.env.DATABASE_URL;
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
CSVStorageManager.baseStorageDir = tmpDir;
CSVStorageManager.clearQuotaCaches();
clearWebhookCooldownCacheForTests();
const serverMod = require("../server/index.js") as {
server: http.Server;
io: { close: (cb: () => void) => void };
writer: { baseDir: string; clearQuotaCaches: () => void };
verifyClientInDb: (
clientId: string,
token: string,
bypassCache?: boolean,
clientIp?: string,
) => Promise<{ ok: boolean; reason?: string }>;
setDbPoolForTests: (pool: unknown) => void;
stopTimers: () => void;
};
const origWriterBaseDir = serverMod.writer.baseDir;
serverMod.writer.baseDir = tmpDir;
serverMod.writer.clearQuotaCaches();
const openSockets: Socket[] = [];
try {
const adminCookie = await makeAdminCookie();
@@ -295,6 +326,7 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
"content-type": "application/json",
},
body: JSON.stringify({
clientId: "ip_restricted_svc",
name: "IP Restricted Client",
rateLimit: 100,
storageQuotaMb: 0,
@@ -303,7 +335,7 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
}),
}),
);
assert.equal(createRes.status, 200);
assert.equal(createRes.status, 201);
const createData = await createRes.json();
const client = createData.client;
assert.equal(client.allowedIps, "10.10.0.0/16");
@@ -320,7 +352,7 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
},
body: JSON.stringify({
eventId: "evt_ip_denied",
payload: { step: 1 },
data: { step: 1 },
}),
}),
);
@@ -339,7 +371,7 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
},
body: JSON.stringify({
eventId: "evt_ip_allowed",
payload: { step: 2 },
data: { step: 2 },
}),
}),
);
@@ -377,7 +409,7 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
},
body: JSON.stringify({
eventId: "evt_expired",
payload: { step: 3 },
data: { step: 3 },
}),
}),
);
@@ -386,93 +418,120 @@ test("V2-Phase-02 [HTTP & Socket Ingestion Security]: Per-client IP allowlist (4
assert.equal(expiredJson.code, "CLIENT_TOKEN_EXPIRED");
// 5. Verify Port 4000 Socket.IO server enforces IP allowlist & token expiry
process.env.DATASTACK_DISABLE_LISTEN = "1";
const ingestionApp = require("../server/index.js");
let mockExpiresAt: string | null = pastIso;
const mockAllowedIps = "10.10.0.0/16";
serverMod.setDbPoolForTests({
query: async (sql: string) => {
if (sql.includes("FROM client_sdks")) {
return {
rowCount: 1,
rows: [
{
client_id: client.clientId,
token: client.token,
status: "active",
rate_limit: 100,
storage_quota_mb: 0,
allowed_ips: mockAllowedIps,
expires_at: mockExpiresAt,
},
],
};
}
return { rowCount: 0, rows: [] };
},
});
// Sync expired client to Port 4000 in-memory cache and verify verifyClientInDb returns CLIENT_TOKEN_EXPIRED
await new Promise<void>((resolve) =>
ingestionApp.server.listen(0, "127.0.0.1", () => resolve()),
const expiredCheck = await serverMod.verifyClientInDb(
client.clientId,
client.token,
true,
"10.10.42.9",
);
const port4000 = (ingestionApp.server.address() as AddressInfo).port;
assert.equal(expiredCheck.allowed, false);
assert.equal(expiredCheck.code, "CLIENT_TOKEN_EXPIRED");
try {
const expiredCheck = await ingestionApp.verifyClientInDb(
client.clientId,
client.token,
true,
"10.10.42.9",
);
assert.equal(expiredCheck.ok, false);
assert.equal(expiredCheck.reason, "CLIENT_TOKEN_EXPIRED");
// Clear expiration, keep IP allowlist 10.10.0.0/16, and check 127.0.0.1
mockExpiresAt = null;
const ipForbiddenCheck = await serverMod.verifyClientInDb(
client.clientId,
client.token,
true,
"127.0.0.1",
);
assert.equal(ipForbiddenCheck.allowed, false);
assert.equal(ipForbiddenCheck.code, "CLIENT_IP_FORBIDDEN");
// Clear expiration via PATCH, keep IP allowlist 10.10.0.0/16, and check Socket.IO handshake from 127.0.0.1
await patchClientRoute(
new NextRequest(
`http://localhost:3000/api/clients/${encodeURIComponent(client.clientId)}`,
{
method: "PATCH",
headers: {
cookie: adminCookie,
"content-type": "application/json",
},
body: JSON.stringify({
expiresAt: null,
}),
},
),
{ params: Promise.resolve({ id: client.clientId }) },
);
// Start Port 4000 server on ephemeral port and verify Socket.IO handshake from 127.0.0.1 is rejected with CLIENT_IP_FORBIDDEN
await new Promise<void>((resolve) =>
serverMod.server.listen(0, "127.0.0.1", () => resolve()),
);
const port4000 = (serverMod.server.address() as AddressInfo).port;
const ipForbiddenCheck = await ingestionApp.verifyClientInDb(
client.clientId,
client.token,
true,
"127.0.0.1",
);
assert.equal(ipForbiddenCheck.ok, false);
assert.equal(ipForbiddenCheck.reason, "CLIENT_IP_FORBIDDEN");
// Socket.IO connection from 127.0.0.1 should fail handshake with CLIENT_IP_FORBIDDEN
const socketError = await new Promise<string>((resolve) => {
const sock: Socket = ioClient(`http://127.0.0.1:${port4000}`, {
auth: { clientId: client.clientId, token: client.token },
transports: ["websocket"],
reconnection: false,
});
sock.on("connect_error", (err) => {
sock.close();
resolve(err.message);
});
sock.on("connect", () => {
sock.close();
resolve("CONNECTED_UNEXPECTEDLY");
});
const socketError = await new Promise<string>((resolve) => {
const sock: Socket = ioClient(`http://127.0.0.1:${port4000}`, {
auth: { clientId: client.clientId, token: client.token },
transports: ["websocket"],
reconnection: false,
timeout: 3000,
});
assert.ok(socketError.includes("CLIENT_IP_FORBIDDEN"));
} finally {
await new Promise<void>((resolve) =>
ingestionApp.server.close(() => resolve()),
);
}
openSockets.push(sock);
sock.on("connect_error", (err) => {
sock.disconnect();
resolve(err.message);
});
sock.on("connect", () => {
sock.disconnect();
resolve("CONNECTED_UNEXPECTEDLY");
});
});
assert.ok(socketError.includes("CLIENT_IP_FORBIDDEN"));
} finally {
await DataBufferEngine.flushAllAsync();
SDKManager.stopActivityFlushTimerForTests();
if (prevStorageDir === undefined) {
delete process.env.DATASTACK_STORAGE_DIR;
} else {
process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
for (const s of openSockets) {
try {
s.disconnect();
} catch {
// ignore
}
}
CSVStorageManager.resetForTests();
serverMod.writer.baseDir = origWriterBaseDir;
serverMod.writer.clearQuotaCaches();
serverMod.setDbPoolForTests(null);
serverMod.stopTimers();
await new Promise<void>((resolve) => {
serverMod.io.close(() => {
if (serverMod.server.listening) {
serverMod.server.close(() => resolve());
} else {
resolve();
}
});
});
SDKManager.stopActivityFlushTimerForTests();
CSVStorageManager.clearQuotaCaches();
await DataBufferEngine.shutdown();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
if (prevDisableListen === undefined) delete process.env.DATASTACK_DISABLE_LISTEN;
else process.env.DATASTACK_DISABLE_LISTEN = prevDisableListen;
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
await fs.rm(tmpDir, { recursive: true, force: true });
resetAdminStateForTests();
}
});
test("V2-Phase-02 [Prometheus Metrics & NDJSON Streaming Export]: GET /api/prometheus (V2-FEAT-04 / V2-E09) & GET /api/export?format=ndjson (V2-FEAT-06)", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "datastack-v2-p2-prom-"));
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
const prevDbUrl = process.env.DATABASE_URL;
process.env.DATASTACK_STORAGE_DIR = tmpDir;
CSVStorageManager.resetForTests(tmpDir);
await CSVStorageManager.initializeAsync();
delete process.env.DATABASE_URL;
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
CSVStorageManager.baseStorageDir = tmpDir;
CSVStorageManager.clearQuotaCaches();
try {
const adminCookie = await makeAdminCookie();
@@ -546,7 +605,7 @@ test("V2-Phase-02 [Prometheus Metrics & NDJSON Streaming Export]: GET /api/prome
assert.ok(metricsBody.includes("datastack_disk_used_percent"));
// 5. Seed events and export as streaming NDJSON (GET /api/export?format=ndjson) (V2-FEAT-06)
await CSVStorageManager.appendBatchAsync("client_ndjson", [
await CSVStorageManager.appendBatch([
{
clientId: "client_ndjson",
eventId: "evt_nd_1",
@@ -575,10 +634,10 @@ test("V2-Phase-02 [Prometheus Metrics & NDJSON Streaming Export]: GET /api/prome
.get("content-type")
?.includes("application/x-ndjson"),
);
const disposition = ndjsonRes.headers.get("content-disposition") || "";
assert.ok(
ndjsonRes.headers
.get("content-disposition")
?.includes("datastack-client_ndjson.ndjson"),
disposition.includes("client_ndjson_export_") &&
disposition.endsWith('.ndjson"'),
);
const ndjsonText = await ndjsonRes.text();
const lines = ndjsonText
@@ -610,8 +669,6 @@ test("V2-Phase-02 [Prometheus Metrics & NDJSON Streaming Export]: GET /api/prome
DEFAULT_ALERTING_SETTINGS,
);
} finally {
await DataBufferEngine.flushAllAsync();
SDKManager.stopActivityFlushTimerForTests();
await SettingsManager.updateSettingsAsync({
enableWebhookAlerts: false,
webhookUrl: "",
@@ -620,12 +677,15 @@ test("V2-Phase-02 [Prometheus Metrics & NDJSON Streaming Export]: GET /api/prome
enablePrometheusMetrics: false,
clearPrometheusToken: true,
});
if (prevStorageDir === undefined) {
delete process.env.DATASTACK_STORAGE_DIR;
} else {
process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
}
CSVStorageManager.resetForTests();
SDKManager.stopActivityFlushTimerForTests();
await DataBufferEngine.shutdown();
CSVStorageManager.clearQuotaCaches();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
await fs.rm(tmpDir, { recursive: true, force: true });
resetAdminStateForTests();
}
});
+621
View File
@@ -0,0 +1,621 @@
import assert from "node:assert/strict";
import { after, beforeEach, describe, it } from "node:test";
import { NextRequest } from "next/server";
import { GET as auditGet } from "@/app/api/audit/route";
import { POST as loginPost } from "@/app/api/auth/login/route";
import { GET as profileGet, PUT as profilePut } from "@/app/api/auth/profile/route";
import {
DELETE as clientDelete,
GET as clientGet,
PATCH as clientPatch,
} from "@/app/api/clients/[id]/route";
import { GET as clientsGet, POST as clientsPost } from "@/app/api/clients/route";
import { GET as settingsGet, POST as settingsPost } from "@/app/api/settings/route";
import { POST as webhookTestPost } from "@/app/api/settings/webhook-test/route";
import { GET as storageGet, POST as storagePost } from "@/app/api/storage/route";
import {
DELETE as teamMemberDelete,
PATCH as teamMemberPatch,
} from "@/app/api/team/[id]/route";
import { GET as teamGet, POST as teamPost } from "@/app/api/team/route";
import {
createAdminSessionToken,
getAdminSessionSecret,
} from "@/core/auth/adminSession";
import {
createInitialAdmin,
resetAdminStateForTests,
} from "@/core/auth/adminUsers";
import { DataBufferEngine } from "@/core/data/buffer";
const ORIGINAL_ENV = { ...process.env };
function restoreEnv() {
for (const key of Object.keys(process.env)) {
if (!(key in ORIGINAL_ENV)) {
delete process.env[key];
}
}
Object.assign(process.env, ORIGINAL_ENV);
}
describe("V2-Phase-03: Multi-Admin RBAC & Immutable Admin Audit Log", () => {
beforeEach(async () => {
restoreEnv();
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
resetAdminStateForTests();
const g = globalThis as {
__dataStackClientsMap?: Map<string, unknown>;
__dataStackDeletedClients?: Set<string>;
};
g.__dataStackClientsMap?.clear();
g.__dataStackDeletedClients?.clear();
});
after(async () => {
restoreEnv();
resetAdminStateForTests();
await DataBufferEngine.shutdown();
});
it("1. Enforces Owner, Admin/Operator, and Viewer RBAC matrix (V2-E10 INSUFFICIENT_ROLE_PERMISSION)", async () => {
const createdOwner = await createInitialAdmin(
"owner@datastack.dev",
"OwnerPass123!",
);
assert.equal(createdOwner, true);
const secret = getAdminSessionSecret()!;
// Owner session cookie (backward-compatible 4-arg call defaults to owner)
const ownerCookie = await createAdminSessionToken(
"owner@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
// Owner creates an Admin/Operator team member and a Viewer team member
const createAdminRes = await teamPost(
new NextRequest("http://localhost:3000/api/team", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({
email: "operator@datastack.dev",
password: "OperatorPass123!",
role: "admin",
}),
}),
);
assert.equal(createAdminRes.status, 201);
const createdAdminMember = (await createAdminRes.json()).member;
assert.equal(createdAdminMember.role, "admin");
const createViewerRes = await teamPost(
new NextRequest("http://localhost:3000/api/team", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({
email: "viewer@datastack.dev",
password: "ViewerPass123!",
role: "viewer",
}),
}),
);
assert.equal(createViewerRes.status, 201);
const createdViewerMember = (await createViewerRes.json()).member;
assert.equal(createdViewerMember.role, "viewer");
// Sign in via POST /api/auth/login as Operator and Viewer
const operatorLoginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: "operator@datastack.dev",
password: "OperatorPass123!",
}),
}),
);
assert.equal(operatorLoginRes.status, 200);
const operatorCookie =
operatorLoginRes.cookies.get("datastack_admin_session")?.value;
assert.ok(operatorCookie);
const viewerLoginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: "viewer@datastack.dev",
password: "ViewerPass123!",
}),
}),
);
assert.equal(viewerLoginRes.status, 200);
const viewerCookie =
viewerLoginRes.cookies.get("datastack_admin_session")?.value;
assert.ok(viewerCookie);
// Check /api/auth/profile reflects each member's role
const viewerProfileRes = await profileGet(
new NextRequest("http://localhost:3000/api/auth/profile", {
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
}),
);
assert.equal(viewerProfileRes.status, 200);
const viewerProfile = (await viewerProfileRes.json()).profile;
assert.equal(viewerProfile.role, "Viewer");
assert.equal(viewerProfile.roleKey, "viewer");
// Viewer can read GET /api/clients, GET /api/settings, GET /api/storage, GET /api/team
const viewerGetClients = await clientsGet(
new NextRequest("http://localhost:3000/api/clients", {
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
}),
);
assert.equal(viewerGetClients.status, 200);
const viewerGetSettings = await settingsGet(
new NextRequest("http://localhost:3000/api/settings", {
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
}),
);
assert.equal(viewerGetSettings.status, 200);
const viewerGetStorage = await storageGet(
new NextRequest("http://localhost:3000/api/storage", {
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
}),
);
assert.equal(viewerGetStorage.status, 200);
// Viewer CANNOT mutate settings, clients, storage, or team -> 403 INSUFFICIENT_ROLE_PERMISSION
const viewerPostSettings = await settingsPost(
new NextRequest("http://localhost:3000/api/settings", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${viewerCookie}`,
},
body: JSON.stringify({ maxEventsPerSec: 250 }),
}),
);
assert.equal(viewerPostSettings.status, 403);
assert.equal(
(await viewerPostSettings.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
const viewerPostClients = await clientsPost(
new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${viewerCookie}`,
},
body: JSON.stringify({
clientId: "viewer_blocked_client",
name: "Blocked",
}),
}),
);
assert.equal(viewerPostClients.status, 403);
assert.equal(
(await viewerPostClients.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
const viewerPostStorage = await storagePost(
new NextRequest("http://localhost:3000/api/storage", {
method: "POST",
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
}),
);
assert.equal(viewerPostStorage.status, 403);
assert.equal(
(await viewerPostStorage.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
// Operator CAN create, update, rotate token, and delete SDK clients
const operatorCreateClient = await clientsPost(
new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${operatorCookie}`,
},
body: JSON.stringify({
clientId: "op_managed_client",
name: "Operator Client",
rateLimit: 300,
}),
}),
);
assert.equal(operatorCreateClient.status, 201);
// Viewer CANNOT reveal full client JWT via ?includeToken=true
const viewerRevealToken = await clientGet(
new NextRequest(
"http://localhost:3000/api/clients/op_managed_client?includeToken=true",
{
headers: { cookie: `datastack_admin_session=${viewerCookie}` },
},
),
{ params: Promise.resolve({ id: "op_managed_client" }) },
);
assert.equal(viewerRevealToken.status, 403);
assert.equal(
(await viewerRevealToken.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
// Operator CAN reveal full client JWT and rotate it
const operatorRotateToken = await clientPatch(
new NextRequest("http://localhost:3000/api/clients/op_managed_client", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${operatorCookie}`,
},
body: JSON.stringify({ action: "regenerate_token" }),
}),
{ params: Promise.resolve({ id: "op_managed_client" }) },
);
assert.equal(operatorRotateToken.status, 200);
// Operator CANNOT modify platform settings or manage team members (Owner-only)
const operatorPostSettings = await settingsPost(
new NextRequest("http://localhost:3000/api/settings", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${operatorCookie}`,
},
body: JSON.stringify({ maxEventsPerSec: 500 }),
}),
);
assert.equal(operatorPostSettings.status, 403);
assert.equal(
(await operatorPostSettings.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
const operatorWebhookTest = await webhookTestPost(
new NextRequest("http://localhost:3000/api/settings/webhook-test", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${operatorCookie}`,
},
body: JSON.stringify({ webhookUrl: "https://hooks.example.com/test" }),
}),
);
assert.equal(operatorWebhookTest.status, 403);
assert.equal(
(await operatorWebhookTest.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
const operatorPostTeam = await teamPost(
new NextRequest("http://localhost:3000/api/team", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${operatorCookie}`,
},
body: JSON.stringify({
email: "unauthorized@datastack.dev",
password: "Password123!",
role: "viewer",
}),
}),
);
assert.equal(operatorPostTeam.status, 403);
assert.equal(
(await operatorPostTeam.json()).code,
"INSUFFICIENT_ROLE_PERMISSION",
);
});
it("2. Protects Primary Owner (id=1 / owner-1) from deletion or demotion (V2-E11 CANNOT_REVOKE_PRIMARY_OWNER) and revokes team sessions on role/status change", async () => {
await createInitialAdmin("primary@datastack.dev", "PrimaryPass123!");
const secret = getAdminSessionSecret()!;
const ownerCookie = await createAdminSessionToken(
"primary@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
// Attempt to delete primary owner -> 400 CANNOT_REVOKE_PRIMARY_OWNER
const delOwnerRes = await teamMemberDelete(
new NextRequest("http://localhost:3000/api/team/owner-1", {
method: "DELETE",
headers: { cookie: `datastack_admin_session=${ownerCookie}` },
}),
{ params: Promise.resolve({ id: "owner-1" }) },
);
assert.equal(delOwnerRes.status, 400);
assert.equal(
(await delOwnerRes.json()).code,
"CANNOT_REVOKE_PRIMARY_OWNER",
);
// Attempt to demote or disable primary owner -> 400 CANNOT_REVOKE_PRIMARY_OWNER
const patchOwnerRes = await teamMemberPatch(
new NextRequest("http://localhost:3000/api/team/owner-1", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({ role: "viewer", status: "disabled" }),
}),
{ params: Promise.resolve({ id: "owner-1" }) },
);
assert.equal(patchOwnerRes.status, 400);
assert.equal(
(await patchOwnerRes.json()).code,
"CANNOT_REVOKE_PRIMARY_OWNER",
);
// Create a secondary member, log in, then disable them and verify session cookie is immediately invalidated
const createdRes = await teamPost(
new NextRequest("http://localhost:3000/api/team", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({
email: "temp@datastack.dev",
password: "TempPassword123!",
role: "viewer",
}),
}),
);
assert.equal(createdRes.status, 201);
const memberId = (await createdRes.json()).member.id as string;
const loginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: "temp@datastack.dev",
password: "TempPassword123!",
}),
}),
);
assert.equal(loginRes.status, 200);
const tempCookie = loginRes.cookies.get("datastack_admin_session")?.value;
assert.ok(tempCookie);
// Disable team member -> bumps token_version and sets status='disabled'
const disableRes = await teamMemberPatch(
new NextRequest(`http://localhost:3000/api/team/${memberId}`, {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({ status: "disabled", revokeSessions: true }),
}),
{ params: Promise.resolve({ id: memberId }) },
);
assert.equal(disableRes.status, 200);
// Old cookie must now be rejected 401
const afterDisableCheck = await clientsGet(
new NextRequest("http://localhost:3000/api/clients", {
headers: { cookie: `datastack_admin_session=${tempCookie}` },
}),
);
assert.equal(afterDisableCheck.status, 401);
});
it("3. Records immutable admin audit logs across login, team, client, and settings mutations and supports filtered GET /api/audit queries", async () => {
await createInitialAdmin("audit-owner@datastack.dev", "AuditPass123!");
const loginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
"content-type": "application/json",
"x-real-ip": "203.0.113.77",
},
body: JSON.stringify({
email: "audit-owner@datastack.dev",
password: "AuditPass123!",
}),
}),
);
assert.equal(loginRes.status, 200);
const ownerCookie = loginRes.cookies.get("datastack_admin_session")?.value!;
// Create a client, update settings, and delete the client
const createClientRes = await clientsPost(
new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
"x-real-ip": "203.0.113.77",
},
body: JSON.stringify({
clientId: "audited_sdk_client",
name: "Audited Client",
rateLimit: 120,
}),
}),
);
assert.equal(createClientRes.status, 201);
const updateSettingsRes = await settingsPost(
new NextRequest("http://localhost:3000/api/settings", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
"x-real-ip": "203.0.113.77",
},
body: JSON.stringify({ maxEventsPerSec: 400 }),
}),
);
assert.equal(updateSettingsRes.status, 200);
const deleteClientRes = await clientDelete(
new NextRequest(
"http://localhost:3000/api/clients/audited_sdk_client?purgeFiles=false",
{
method: "DELETE",
headers: {
cookie: `datastack_admin_session=${ownerCookie}`,
"x-real-ip": "203.0.113.77",
},
},
),
{ params: Promise.resolve({ id: "audited_sdk_client" }) },
);
assert.equal(deleteClientRes.status, 200);
// Query GET /api/audit
const auditRes = await auditGet(
new NextRequest("http://localhost:3000/api/audit?limit=50", {
headers: { cookie: `datastack_admin_session=${ownerCookie}` },
}),
);
assert.equal(auditRes.status, 200);
const auditJson = await auditRes.json();
assert.ok(Array.isArray(auditJson.logs));
const actions = auditJson.logs.map((l: { action: string }) => l.action);
assert.ok(actions.includes("ADMIN_LOGIN"));
assert.ok(actions.includes("CLIENT_CREATED"));
assert.ok(actions.includes("SETTINGS_UPDATED"));
assert.ok(actions.includes("CLIENT_DELETED"));
// Filter by action=CLIENT_CREATED
const filteredRes = await auditGet(
new NextRequest(
"http://localhost:3000/api/audit?action=CLIENT_CREATED&q=audited_sdk_client",
{
headers: { cookie: `datastack_admin_session=${ownerCookie}` },
},
),
);
assert.equal(filteredRes.status, 200);
const filteredJson = await filteredRes.json();
assert.equal(filteredJson.logs.length, 1);
assert.equal(filteredJson.logs[0].target, "audited_sdk_client");
assert.equal(filteredJson.logs[0].ipAddress, "203.0.113.77");
assert.equal(filteredJson.logs[0].actorEmail, "audit-owner@datastack.dev");
// Team listing includes primary owner + team members
const teamListRes = await teamGet(
new NextRequest("http://localhost:3000/api/team", {
headers: { cookie: `datastack_admin_session=${ownerCookie}` },
}),
);
assert.equal(teamListRes.status, 200);
const teamListJson = await teamListRes.json();
assert.equal(teamListJson.members[0].id, "owner-1");
assert.equal(teamListJson.members[0].isPrimaryOwner, true);
// Restore default settings before shutdown
await settingsPost(
new NextRequest("http://localhost:3000/api/settings", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({ maxEventsPerSec: 100 }),
}),
);
await DataBufferEngine.shutdown();
});
it("4. Allows secondary team member to rotate their own password via PUT /api/auth/profile and invalidates prior cookie", async () => {
await createInitialAdmin("root@datastack.dev", "RootPassword123!");
const secret = getAdminSessionSecret()!;
const ownerCookie = await createAdminSessionToken(
"root@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
await teamPost(
new NextRequest("http://localhost:3000/api/team", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${ownerCookie}`,
},
body: JSON.stringify({
email: "selfupdate@datastack.dev",
password: "InitialPassword123!",
role: "operator",
}),
}),
);
const loginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
email: "selfupdate@datastack.dev",
password: "InitialPassword123!",
}),
}),
);
assert.equal(loginRes.status, 200);
const v1Cookie = loginRes.cookies.get("datastack_admin_session")?.value!;
const rotateRes = await profilePut(
new NextRequest("http://localhost:3000/api/auth/profile", {
method: "PUT",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${v1Cookie}`,
},
body: JSON.stringify({
email: "selfupdate@datastack.dev",
currentPassword: "InitialPassword123!",
newPassword: "RotatedPassword456!",
}),
}),
);
assert.equal(rotateRes.status, 200);
const v2Cookie = rotateRes.cookies.get("datastack_admin_session")?.value!;
assert.ok(v2Cookie);
// Old v1 cookie is now revoked
const staleRes = await profileGet(
new NextRequest("http://localhost:3000/api/auth/profile", {
headers: { cookie: `datastack_admin_session=${v1Cookie}` },
}),
);
assert.equal(staleRes.status, 401);
// New v2 cookie retains Operator role
const freshRes = await profileGet(
new NextRequest("http://localhost:3000/api/auth/profile", {
headers: { cookie: `datastack_admin_session=${v2Cookie}` },
}),
);
assert.equal(freshRes.status, 200);
const freshJson = await freshRes.json();
assert.equal(freshJson.profile.role, "Operator");
assert.equal(freshJson.profile.roleKey, "operator");
});
});
+398
View File
@@ -0,0 +1,398 @@
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import {
DataStack,
DataStackAuthError,
DataStackConfigError,
DataStackFileError,
DataStackQuotaExceededError,
SDK_VERSION,
makeDeterministicFileEventId,
} from '../sdk/node/datastack';
function makeFakeJwt(clientId: string): string {
const header = Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString('base64url');
const payload = Buffer.from(JSON.stringify({ clientId, iat: 1700000000 })).toString('base64url');
return `${header}.${payload}.fakesignature`;
}
test('V2-Phase-04 (1): Node.js / TypeScript SDK packaging metadata, JWT clientId verification, and V2-E12 config validation', async () => {
const pkgRaw = await fs.readFile(
path.join(process.cwd(), 'sdk', 'node', 'datastack', 'package.json'),
'utf8'
);
const pkg = JSON.parse(pkgRaw) as {
name: string;
version: string;
engines?: { node?: string };
};
assert.equal(pkg.name, '@datastack/sdk');
assert.equal(pkg.version, '2.2.0');
assert.equal(SDK_VERSION, '2.2.0');
assert.equal(pkg.engines?.node, '>=20.0.0');
const token = makeFakeJwt('billing_service');
// Mismatched clientId vs JWT token claim must throw DataStackConfigError (V2-E12)
assert.throws(
() =>
new DataStack({
serverUrl: 'http://localhost:4000',
apiUrl: 'http://localhost:3000',
token,
clientId: 'different_service',
autoConnect: false,
}),
(err: unknown) =>
err instanceof DataStackConfigError &&
err.code === 'INVALID_SDK_CONFIG' &&
/does not match JWT token subject/i.test(err.message)
);
// Invalid serverUrl scheme must throw DataStackConfigError
assert.throws(
() =>
new DataStack({
serverUrl: 'ftp://invalid-endpoint',
apiUrl: 'http://localhost:3000',
token,
autoConnect: false,
}),
(err: unknown) => err instanceof DataStackConfigError
);
// Matching normalized clientId succeeds
const client = new DataStack({
serverUrl: 'http://localhost:4000',
apiUrl: 'http://localhost:3000',
token,
clientId: ' BILLING_SERVICE ',
autoConnect: false,
offlineSpoolEnabled: false,
});
assert.equal(client.clientId, 'billing_service');
// Empty payload after metadata stripping throws DataStackConfigError
await assert.rejects(
() => client.send({ eventId: 'evt_only_meta', timestamp: '2026-11-20T00:00:00Z' }),
(err: unknown) => err instanceof DataStackConfigError
);
});
test('V2-Phase-04 (2): HTTP fallback single/batch send, recursive 413 batch splitting, 429 partial retry, and terminal V2-E12 error mapping', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ds-node-sdk-http-'));
try {
const token = makeFakeJwt('checkout_api');
const calls: Array<{ body: Record<string, unknown> }> = [];
let simulateMode:
| 'ok'
| 'split_413'
| 'partial_429'
| 'expired_401'
| 'ip_403'
| 'quota_507' = 'ok';
let partial429Count = 0;
const mockFetch: typeof fetch = async (_input, init) => {
const body = JSON.parse(String(init?.body || '{}')) as Record<string, unknown>;
calls.push({ body });
if (simulateMode === 'expired_401') {
return new Response(
JSON.stringify({
success: false,
code: 'CLIENT_TOKEN_EXPIRED',
error: 'Client credential has expired',
}),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
if (simulateMode === 'ip_403') {
return new Response(
JSON.stringify({
success: false,
code: 'CLIENT_IP_FORBIDDEN',
error: 'Client IP not in allowlist',
}),
{ status: 403, headers: { 'Content-Type': 'application/json' } }
);
}
if (simulateMode === 'quota_507') {
return new Response(
JSON.stringify({
success: false,
code: 'STORAGE_QUOTA_EXCEEDED',
error: 'Client storage quota exceeded',
}),
{ status: 507, headers: { 'Content-Type': 'application/json' } }
);
}
const batch = Array.isArray(body.batch)
? (body.batch as Array<{ eventId: string; data: Record<string, unknown> }>)
: null;
if (simulateMode === 'split_413' && batch && batch.length > 2) {
return new Response(
JSON.stringify({
success: false,
code: 'PAYLOAD_TOO_LARGE',
error: 'Batch exceeds maxPayloadSizeKb',
}),
{ status: 413, headers: { 'Content-Type': 'application/json' } }
);
}
if (simulateMode === 'partial_429' && batch && partial429Count === 0) {
partial429Count += 1;
const rateLimitedIds = [batch[batch.length - 1].eventId];
return new Response(
JSON.stringify({
success: true,
accepted: batch.length - 1,
rateLimitedEventIds: rateLimitedIds,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
}
return new Response(
JSON.stringify({
success: true,
eventId: body.eventId || 'evt_ok',
accepted: batch ? batch.length : 1,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
};
const client = new DataStack({
serverUrl: 'http://localhost:4000',
apiUrl: 'http://localhost:3000',
token,
autoConnect: false,
offlineSpoolDir: tmpDir,
fetchImpl: mockFetch,
});
// 1. Single event via HTTP fallback
const singleRes = await client.send({
event_type: 'payment_authorized',
amount: 99.5,
});
assert.equal(singleRes.success, true);
assert.equal(singleRes.transport, 'http');
assert.equal(singleRes.accepted, 1);
// 2. Recursive 413 PAYLOAD_TOO_LARGE splitting (4 items -> split into 2 + 2)
simulateMode = 'split_413';
const splitRes = await client.sendBatch([
{ seq: 1 },
{ seq: 2 },
{ seq: 3 },
{ seq: 4 },
]);
assert.equal(splitRes.success, true);
assert.equal(splitRes.accepted, 4);
// 3. Partial 429 RATE_LIMIT_EXCEEDED retry
simulateMode = 'partial_429';
const retryRes = await client.sendBatch([
{ item: 'a' },
{ item: 'b' },
{ item: 'c' },
]);
assert.equal(retryRes.success, true);
assert.equal(retryRes.accepted, 3);
// 4. Terminal error mapping (401 CLIENT_TOKEN_EXPIRED, 403 CLIENT_IP_FORBIDDEN, 507 STORAGE_QUOTA_EXCEEDED)
simulateMode = 'expired_401';
await assert.rejects(
() => client.send({ test: 'expired' }),
(err: unknown) =>
err instanceof DataStackAuthError &&
err.code === 'CLIENT_TOKEN_EXPIRED' &&
err.statusCode === 401
);
simulateMode = 'ip_403';
await assert.rejects(
() => client.send({ test: 'ip_blocked' }),
(err: unknown) =>
err instanceof DataStackAuthError &&
err.code === 'CLIENT_IP_FORBIDDEN' &&
err.statusCode === 403
);
simulateMode = 'quota_507';
await assert.rejects(
() => client.send({ test: 'quota_full' }),
(err: unknown) =>
err instanceof DataStackQuotaExceededError &&
err.code === 'STORAGE_QUOTA_EXCEEDED' &&
err.statusCode === 507
);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('V2-Phase-04 (3): Offline disk spooling (.datastack_spool/spool.jsonl), flushSpool replay, and graceful shutdown drain', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ds-node-sdk-spool-'));
try {
const token = makeFakeJwt('iot_gateway');
let networkOnline = false;
const deliveredEvents: Array<Record<string, unknown>> = [];
const mockFetch: typeof fetch = async (_input, init) => {
if (!networkOnline) {
throw new Error('ECONNREFUSED: Network unreachable');
}
const body = JSON.parse(String(init?.body || '{}')) as Record<string, unknown>;
if (Array.isArray(body.batch)) {
for (const item of body.batch as Array<Record<string, unknown>>) {
deliveredEvents.push(item);
}
return new Response(
JSON.stringify({
success: true,
accepted: body.batch.length,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
}
deliveredEvents.push(body);
return new Response(
JSON.stringify({
success: true,
accepted: 1,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
};
const client = new DataStack({
serverUrl: 'http://localhost:4000',
apiUrl: 'http://localhost:3000',
token,
autoConnect: false,
offlineSpoolEnabled: true,
offlineSpoolDir: tmpDir,
fetchImpl: mockFetch,
});
// While offline, events spool to disk
const s1 = await client.send({ sensor: 'pressure', kpa: 101.3 });
assert.equal(s1.success, true);
assert.equal(s1.transport, 'spool');
assert.equal(s1.spooled, 1);
const s2 = await client.sendBatch([
{ sensor: 'temp', c: 22.1 },
{ sensor: 'temp', c: 22.4 },
]);
assert.equal(s2.success, true);
assert.equal(s2.transport, 'spool');
assert.equal(s2.spooled, 2);
assert.equal(client.status().spooledCount, 3);
// Network comes back online -> shutdown(5000) automatically drains the spool
networkOnline = true;
const shutdownRes = await client.shutdown(5000);
assert.equal(shutdownRes.drained, true);
assert.equal(shutdownRes.remainingInFlight, 0);
assert.equal(shutdownRes.remainingSpooled, 0);
assert.equal(deliveredEvents.length, 3);
assert.equal(client.status().spooledCount, 0);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('V2-Phase-04 (4): Direct file ingestion (sendFile for .csv, .json, .jsonl) with deterministic SHA-256 eventId and /vcon/sdk multi-language tabs', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ds-node-sdk-files-'));
try {
const token = makeFakeJwt('batch_importer');
const receivedBatches: Array<Array<{ eventId: string; data: Record<string, unknown> }>> = [];
const mockFetch: typeof fetch = async (_input, init) => {
const body = JSON.parse(String(init?.body || '{}')) as {
batch?: Array<{ eventId: string; data: Record<string, unknown> }>;
};
if (Array.isArray(body.batch)) {
receivedBatches.push(body.batch);
}
return new Response(
JSON.stringify({
success: true,
accepted: body.batch?.length ?? 1,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
};
const client = new DataStack({
serverUrl: 'http://localhost:4000',
apiUrl: 'http://localhost:3000',
token,
autoConnect: false,
offlineSpoolEnabled: false,
fetchImpl: mockFetch,
});
// 1. CSV file ingestion
const csvPath = path.join(tmpDir, 'sensors.csv');
await fs.writeFile(
csvPath,
'device,reading,status\ndev_01,42.5,OK\ndev_02,19.8,WARN\n',
'utf8'
);
const csvRes = await client.sendFile(csvPath, { chunkSize: 10 });
assert.equal(csvRes.success, true);
assert.equal(csvRes.totalRows, 2);
assert.equal(csvRes.acceptedRows, 2);
assert.equal(receivedBatches[0][0].eventId, makeDeterministicFileEventId('sensors', 0, {
device: 'dev_01',
reading: '42.5',
status: 'OK',
}));
// 2. JSONL file ingestion
const jsonlPath = path.join(tmpDir, 'events.jsonl');
await fs.writeFile(
jsonlPath,
'{"event":"login","user":"u1"}\n{"event":"logout","user":"u1"}\n',
'utf8'
);
const jsonlRes = await client.sendFile(jsonlPath);
assert.equal(jsonlRes.success, true);
assert.equal(jsonlRes.totalRows, 2);
assert.equal(jsonlRes.acceptedRows, 2);
// 3. Unsupported extension throws DataStackFileError
const badPath = path.join(tmpDir, 'notes.txt');
await fs.writeFile(badPath, 'hello', 'utf8');
await assert.rejects(
() => client.sendFile(badPath),
(err: unknown) => err instanceof DataStackFileError
);
// 4. Verify /vcon/sdk page includes Python SDK, Node.js / TypeScript SDK, and HTTP / cURL tabs
const sdkPageSrc = await fs.readFile(
path.join(process.cwd(), 'app', 'vcon', 'sdk', 'page.tsx'),
'utf8'
);
assert.match(sdkPageSrc, /Python SDK \(v2\.2\.0\)/);
assert.match(sdkPageSrc, /Node\.js \/ TypeScript SDK \(v2\.2\.0\)/);
assert.match(sdkPageSrc, /@datastack\/sdk/);
assert.match(sdkPageSrc, /datastack\.config\.json/);
assert.match(sdkPageSrc, /await client\.shutdown\(5000\)/);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});