Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+315
@@ -0,0 +1,315 @@
|
||||
// @vitest-environment node
|
||||
import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
grantAdminRlsTestPermissions,
|
||||
removeAdminRlsTestRole,
|
||||
} from "./admin-rbac-rls-test-helper.js";
|
||||
|
||||
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
||||
const describeIfDatabase = ADMIN_URL ? describe : describe.skip;
|
||||
const APP_PASSWORD = "hivegrid_admin_telephony_app_test_only";
|
||||
const WORKER_PASSWORD = "hivegrid_admin_telephony_worker_test_only";
|
||||
const OWNER_ID = "admin_telephony_rls_owner";
|
||||
const FOREIGN_ID = "admin_telephony_rls_foreign";
|
||||
const OWNER_ROLE_ID = "admin_telephony_rls_owner_role";
|
||||
const DATA_CONNECTION_ID = "admin_telephony_rls_data_connection";
|
||||
const PHONE_CONNECTION_ID = "admin_telephony_rls_phone_connection";
|
||||
const CALL_ID = "admin_telephony_rls_call";
|
||||
const EVENT_ID = "admin_telephony_rls_event";
|
||||
const PARTICIPANT_ID = "admin_telephony_rls_participant";
|
||||
const POLICY_ID = "admin_telephony_rls_policy";
|
||||
const CONTACT_ID = "admin_telephony_rls_contact";
|
||||
const OUTBOUND_REQUEST_ID = "admin_telephony_rls_outbound";
|
||||
const RECORDING_ID = "admin_telephony_rls_recording";
|
||||
const WEBHOOK_HASH = "c".repeat(64);
|
||||
|
||||
function roleUrl(adminUrl: string, role: string, password: string): string {
|
||||
const url = new URL(adminUrl);
|
||||
url.username = role;
|
||||
url.password = password;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
describeIfDatabase("admin telephony workspace RLS", () => {
|
||||
let admin: pg.Client;
|
||||
let app: pg.Client;
|
||||
let worker: pg.Client;
|
||||
|
||||
beforeAll(async () => {
|
||||
admin = new pg.Client({ connectionString: ADMIN_URL });
|
||||
await admin.connect();
|
||||
await admin.query(`ALTER ROLE hivegrid_app PASSWORD '${APP_PASSWORD}'`);
|
||||
await admin.query(
|
||||
`ALTER ROLE hivegrid_worker PASSWORD '${WORKER_PASSWORD}'`
|
||||
);
|
||||
await cleanup();
|
||||
await admin.query(
|
||||
`INSERT INTO "AdminUser"
|
||||
("id", "email", "firstName", "lastName", "status", "updatedAt")
|
||||
VALUES
|
||||
($1, 'admin-telephony-owner@example.test', 'Phone', 'Owner', 'active', now()),
|
||||
($2, 'admin-telephony-foreign@example.test', 'Phone', 'Foreign', 'active', now())`,
|
||||
[OWNER_ID, FOREIGN_ID]
|
||||
);
|
||||
await admin.query(
|
||||
`INSERT INTO "AdminDataConnection"
|
||||
("id", "adminUserId", "provider", "capability", "label",
|
||||
"externalAccountId", "status", "scopes", "updatedAt")
|
||||
VALUES ($1, $2, 'aircall', 'telephony', 'Aircall',
|
||||
'aircall-admin-telephony', 'active', ARRAY['public_api'], now())`,
|
||||
[DATA_CONNECTION_ID, OWNER_ID]
|
||||
);
|
||||
await admin.query(
|
||||
`INSERT INTO "AdminContact"
|
||||
("id", "firstName", "lastName", "phone", "normalizedPhone",
|
||||
"ownerAdminUserId", "updatedAt")
|
||||
VALUES ($1, 'RLS', 'Contact', '+49 30 12345678', '+493012345678',
|
||||
$2, now())`,
|
||||
[CONTACT_ID, OWNER_ID]
|
||||
);
|
||||
await admin.query(
|
||||
`INSERT INTO "AdminPhoneConnection"
|
||||
("id", "adminUserId", "connectionId", "providerCompanyId",
|
||||
"providerUserId", "webhookTokenHash", "webhookVerifiedAt", "updatedAt")
|
||||
VALUES ($1, $2, $3, 'company-1', 'provider-user-1', $4, now(), now())`,
|
||||
[PHONE_CONNECTION_ID, OWNER_ID, DATA_CONNECTION_ID, WEBHOOK_HASH]
|
||||
);
|
||||
await grantAdminRlsTestPermissions(admin, {
|
||||
userId: OWNER_ID,
|
||||
roleId: OWNER_ROLE_ID,
|
||||
permissions: [
|
||||
"admin_sales.phone.call",
|
||||
"admin_sales.phone.recording",
|
||||
],
|
||||
});
|
||||
app = new pg.Client({
|
||||
connectionString: roleUrl(ADMIN_URL!, "hivegrid_app", APP_PASSWORD),
|
||||
});
|
||||
worker = new pg.Client({
|
||||
connectionString: roleUrl(
|
||||
ADMIN_URL!,
|
||||
"hivegrid_worker",
|
||||
WORKER_PASSWORD
|
||||
),
|
||||
});
|
||||
await Promise.all([app.connect(), worker.connect()]);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all([app?.end(), worker?.end()]);
|
||||
if (admin) {
|
||||
await cleanup();
|
||||
await admin.end();
|
||||
}
|
||||
});
|
||||
|
||||
async function cleanup() {
|
||||
for (const [table, column, value] of [
|
||||
["AdminCallEvent", "id", EVENT_ID],
|
||||
["AdminCallParticipant", "id", PARTICIPANT_ID],
|
||||
["AdminOutboundCallRequest", "id", OUTBOUND_REQUEST_ID],
|
||||
["AdminCallRecording", "id", RECORDING_ID],
|
||||
["AdminCallSession", "id", CALL_ID],
|
||||
["AdminTelephonyPolicy", "id", POLICY_ID],
|
||||
["AdminPhoneConnection", "id", PHONE_CONNECTION_ID],
|
||||
["AdminDataConnection", "id", DATA_CONNECTION_ID],
|
||||
["AdminContact", "id", CONTACT_ID],
|
||||
] as const) {
|
||||
await admin.query(`DELETE FROM "${table}" WHERE "${column}" = $1`, [
|
||||
value,
|
||||
]);
|
||||
}
|
||||
await admin.query(`DELETE FROM "AdminUser" WHERE "id" IN ($1, $2)`, [
|
||||
OWNER_ID,
|
||||
FOREIGN_ID,
|
||||
]);
|
||||
await removeAdminRlsTestRole(admin, OWNER_ROLE_ID);
|
||||
}
|
||||
|
||||
it("resolves the token hash narrowly and exposes rows only to its active owner", async () => {
|
||||
const resolved = await app.query<{
|
||||
tenant_id: string | null;
|
||||
resource_id: string;
|
||||
user_id: string;
|
||||
}>(
|
||||
`SELECT tenant_id, resource_id, user_id
|
||||
FROM public.hivegrid_resolve_admin_aircall_webhook($1)`,
|
||||
[WEBHOOK_HASH]
|
||||
);
|
||||
expect(resolved.rows).toEqual([
|
||||
{
|
||||
tenant_id: null,
|
||||
resource_id: PHONE_CONNECTION_ID,
|
||||
user_id: OWNER_ID,
|
||||
},
|
||||
]);
|
||||
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
OWNER_ID,
|
||||
]);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminTelephonyPolicy"
|
||||
("id", "adminUserId", "outboundEnabled", "recordingEnabled",
|
||||
"recordingRetentionDays", "dncEnforced", "workDays", "updatedAt")
|
||||
VALUES ($1, $2, false, false, 90, true, ARRAY[]::integer[], now())`,
|
||||
[POLICY_ID, OWNER_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminCallSession"
|
||||
("id", "adminUserId", "phoneConnectionId", "providerCallId",
|
||||
"direction", "status", "fromNumber", "toNumber", "externalNumber",
|
||||
"startedAt", "matchStatus", "updatedAt")
|
||||
VALUES ($1, $2, $3, 'provider-call-1', 'inbound', 'ringing',
|
||||
'+493012345678', '+493099999999', '+493012345678',
|
||||
now(), 'unmatched', now())`,
|
||||
[CALL_ID, OWNER_ID, PHONE_CONNECTION_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminCallParticipant"
|
||||
("id", "adminUserId", "callSessionId", "kind", "phone")
|
||||
VALUES ($1, $2, $3, 'external', '+493012345678')`,
|
||||
[PARTICIPANT_ID, OWNER_ID, CALL_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminCallRecording"
|
||||
("id", "adminUserId", "callSessionId", "retainedUntil",
|
||||
"providerUrlWrappedDek", "providerUrlCiphertext",
|
||||
"providerUrlKeyVersion", "updatedAt")
|
||||
VALUES ($1, $2, $3, now() + interval '90 days',
|
||||
decode('010203', 'hex'), 'encrypted-provider-url', 1, now())`,
|
||||
[RECORDING_ID, OWNER_ID, CALL_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminOutboundCallRequest"
|
||||
("id", "adminUserId", "phoneConnectionId", "contactId",
|
||||
"idempotencyKey", "destinationE164", "updatedAt")
|
||||
VALUES ($1, $2, $3, $4, 'admin-telephony-rls-outbound-key',
|
||||
'+493012345678', now())`,
|
||||
[OUTBOUND_REQUEST_ID, OWNER_ID, PHONE_CONNECTION_ID, CONTACT_ID]
|
||||
);
|
||||
const visible = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminCallSession"`
|
||||
);
|
||||
expect(visible.rows[0]?.count).toBe("1");
|
||||
await app.query("COMMIT");
|
||||
} catch (error) {
|
||||
await app.query("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
FOREIGN_ID,
|
||||
]);
|
||||
const hidden = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminCallSession"`
|
||||
);
|
||||
expect(hidden.rows[0]?.count).toBe("0");
|
||||
const hiddenOutbound = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminOutboundCallRequest"`
|
||||
);
|
||||
expect(hiddenOutbound.rows[0]?.count).toBe("0");
|
||||
const hiddenRecording = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminCallRecording"`
|
||||
);
|
||||
expect(hiddenRecording.rows[0]?.count).toBe("0");
|
||||
} finally {
|
||||
await app.query("ROLLBACK");
|
||||
}
|
||||
});
|
||||
|
||||
it("lets the worker append provider events but preserves their immutability", async () => {
|
||||
await worker.query(
|
||||
`INSERT INTO "AdminCallEvent"
|
||||
("id", "adminUserId", "callSessionId", "dedupeKey",
|
||||
"providerEvent", "status", "occurredAt")
|
||||
VALUES ($1, $2, $3, 'admin-telephony-rls-dedupe',
|
||||
'call.answered', 'answered', now())`,
|
||||
[EVENT_ID, OWNER_ID, CALL_ID]
|
||||
);
|
||||
await worker.query(
|
||||
`UPDATE "AdminCallSession"
|
||||
SET "status" = 'answered', "answeredAt" = now(), "updatedAt" = now()
|
||||
WHERE "id" = $1`,
|
||||
[CALL_ID]
|
||||
);
|
||||
await worker.query(
|
||||
`UPDATE "AdminOutboundCallRequest"
|
||||
SET "status" = 'processing', "processingAt" = now(),
|
||||
"attemptCount" = "attemptCount" + 1, "updatedAt" = now()
|
||||
WHERE "id" = $1`,
|
||||
[OUTBOUND_REQUEST_ID]
|
||||
);
|
||||
await worker.query(
|
||||
`UPDATE "AdminCallRecording"
|
||||
SET "status" = 'processing', "processingAt" = now(),
|
||||
"attemptCount" = "attemptCount" + 1, "updatedAt" = now()
|
||||
WHERE "id" = $1`,
|
||||
[RECORDING_ID]
|
||||
);
|
||||
const event = await worker.query<{ providerEvent: string }>(
|
||||
`SELECT "providerEvent" FROM "AdminCallEvent" WHERE "id" = $1`,
|
||||
[EVENT_ID]
|
||||
);
|
||||
expect(event.rows).toEqual([{ providerEvent: "call.answered" }]);
|
||||
const outbound = await worker.query<{ status: string }>(
|
||||
`SELECT "status" FROM "AdminOutboundCallRequest" WHERE "id" = $1`,
|
||||
[OUTBOUND_REQUEST_ID]
|
||||
);
|
||||
expect(outbound.rows).toEqual([{ status: "processing" }]);
|
||||
const recording = await worker.query<{ status: string }>(
|
||||
`SELECT "status" FROM "AdminCallRecording" WHERE "id" = $1`,
|
||||
[RECORDING_ID]
|
||||
);
|
||||
expect(recording.rows).toEqual([{ status: "processing" }]);
|
||||
await expect(
|
||||
worker.query(
|
||||
`UPDATE "AdminCallEvent" SET "providerEvent" = 'tampered'
|
||||
WHERE "id" = $1`,
|
||||
[EVENT_ID]
|
||||
)
|
||||
).rejects.toThrow(/permission denied|row-level security/iu);
|
||||
await expect(
|
||||
worker.query(`DELETE FROM "AdminCallSession" WHERE "id" = $1`, [
|
||||
CALL_ID,
|
||||
])
|
||||
).rejects.toThrow(/permission denied|row-level security/iu);
|
||||
await expect(
|
||||
worker.query(`DELETE FROM "AdminOutboundCallRequest" WHERE "id" = $1`, [
|
||||
OUTBOUND_REQUEST_ID,
|
||||
])
|
||||
).rejects.toThrow(/permission denied|row-level security/iu);
|
||||
await expect(
|
||||
worker.query(`DELETE FROM "AdminCallRecording" WHERE "id" = $1`, [
|
||||
RECORDING_ID,
|
||||
])
|
||||
).rejects.toThrow(/permission denied|row-level security/iu);
|
||||
});
|
||||
|
||||
it("hides owner-bound rows as soon as the AdminUser is disabled", async () => {
|
||||
await admin.query(
|
||||
`UPDATE "AdminUser" SET "status" = 'disabled' WHERE "id" = $1`,
|
||||
[OWNER_ID]
|
||||
);
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
OWNER_ID,
|
||||
]);
|
||||
const hidden = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminPhoneConnection"`
|
||||
);
|
||||
expect(hidden.rows[0]?.count).toBe("0");
|
||||
} finally {
|
||||
await app.query("ROLLBACK");
|
||||
await admin.query(
|
||||
`UPDATE "AdminUser" SET "status" = 'active' WHERE "id" = $1`,
|
||||
[OWNER_ID]
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user