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
+452
@@ -0,0 +1,452 @@
|
||||
// @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_email_app_test_only";
|
||||
const WORKER_PASSWORD = "hivegrid_admin_email_worker_test_only";
|
||||
const OWNER_ID = "admin_email_rls_owner";
|
||||
const FOREIGN_ID = "admin_email_rls_foreign";
|
||||
const OWNER_ROLE_ID = "admin_email_rls_owner_role";
|
||||
const MANAGER_ROLE_ID = "admin_email_rls_manager_role";
|
||||
const CONNECTION_ID = "admin_email_rls_connection";
|
||||
const MAILBOX_ID = "admin_email_rls_mailbox";
|
||||
const CURSOR_ID = "admin_email_rls_cursor";
|
||||
const THREAD_ID = "admin_email_rls_thread";
|
||||
const MESSAGE_ID = "admin_email_rls_message";
|
||||
const PARTICIPANT_ID = "admin_email_rls_participant";
|
||||
const ATTACHMENT_ID = "admin_email_rls_attachment";
|
||||
const TEMPLATE_ID = "admin_email_rls_template";
|
||||
const POLICY_ID = "admin_email_rls_policy";
|
||||
const SCHEDULED_ID = "admin_email_rls_scheduled";
|
||||
const EVENT_ID = "admin_email_rls_event";
|
||||
const LINK_ID = "admin_email_rls_link";
|
||||
const NOTIFICATION_ID = "admin_email_rls_notification";
|
||||
const SENDING_DOMAIN_ID = "admin_email_rls_sending_domain";
|
||||
const BCC_ADDRESS_ID = "admin_email_rls_bcc_address";
|
||||
|
||||
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 email 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-email-owner@example.test', 'Email', 'Owner', 'active', now()),
|
||||
($2, 'admin-email-foreign@example.test', 'Email', '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, 'google_mail', 'email', 'Sales',
|
||||
'google-admin-email', 'active', ARRAY['scope'], now())`,
|
||||
[CONNECTION_ID, OWNER_ID]
|
||||
);
|
||||
await grantAdminRlsTestPermissions(admin, {
|
||||
userId: OWNER_ID,
|
||||
roleId: OWNER_ROLE_ID,
|
||||
permissions: [
|
||||
"admin_sales.email.connect",
|
||||
"admin_sales.email.send",
|
||||
],
|
||||
});
|
||||
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, id] of [
|
||||
["NotificationDelivery", NOTIFICATION_ID],
|
||||
["AdminEmailEvent", EVENT_ID],
|
||||
["AdminEmailLink", LINK_ID],
|
||||
["AdminScheduledEmail", SCHEDULED_ID],
|
||||
["AdminEmailAttachment", ATTACHMENT_ID],
|
||||
["AdminEmailParticipant", PARTICIPANT_ID],
|
||||
["AdminEmailMessage", MESSAGE_ID],
|
||||
["AdminEmailThread", THREAD_ID],
|
||||
["AdminMailboxSyncCursor", CURSOR_ID],
|
||||
["AdminEmailTemplate", TEMPLATE_ID],
|
||||
["AdminEmailOutboundPolicy", POLICY_ID],
|
||||
["AdminBccAddress", BCC_ADDRESS_ID],
|
||||
["AdminSendingDomain", SENDING_DOMAIN_ID],
|
||||
["AdminMailbox", MAILBOX_ID],
|
||||
["AdminDataConnection", CONNECTION_ID],
|
||||
] as const) {
|
||||
await admin.query(`DELETE FROM "${table}" WHERE "id" = $1`, [id]);
|
||||
}
|
||||
await admin.query(`DELETE FROM "AdminUser" WHERE "id" IN ($1, $2)`, [
|
||||
OWNER_ID,
|
||||
FOREIGN_ID,
|
||||
]);
|
||||
await removeAdminRlsTestRole(admin, OWNER_ROLE_ID);
|
||||
await removeAdminRlsTestRole(admin, MANAGER_ROLE_ID);
|
||||
}
|
||||
|
||||
it("binds mail, template and policy rows to the active owner", async () => {
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
OWNER_ID,
|
||||
]);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminMailbox"
|
||||
("id", "adminUserId", "connectionId", "emailAddress",
|
||||
"normalizedEmail", "providerAccountId", "status", "updatedAt")
|
||||
VALUES ($1, $2, $3, 'sales@hivegrid.de', 'sales@hivegrid.de',
|
||||
'google-admin-email', 'active', now())`,
|
||||
[MAILBOX_ID, OWNER_ID, CONNECTION_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminMailboxSyncCursor"
|
||||
("id", "adminUserId", "mailboxId", "externalFolderId",
|
||||
"cursorToken", "syncStatus", "updatedAt")
|
||||
VALUES ($1, $2, $3, '*', 'history-1', 'complete', now())`,
|
||||
[CURSOR_ID, OWNER_ID, MAILBOX_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailThread"
|
||||
("id", "adminUserId", "mailboxId", "providerThreadId",
|
||||
"latestMessageAt", "updatedAt")
|
||||
VALUES ($1, $2, $3, 'provider-thread-1', now(), now())`,
|
||||
[THREAD_ID, OWNER_ID, MAILBOX_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailMessage"
|
||||
("id", "adminUserId", "mailboxId", "threadId",
|
||||
"providerMessageId", "direction", "fromAddress", "sentAt",
|
||||
"matchStatus", "updatedAt")
|
||||
VALUES ($1, $2, $3, $4, 'provider-message-1', 'inbound',
|
||||
'buyer@example.test', now(), 'unmatched', now())`,
|
||||
[MESSAGE_ID, OWNER_ID, MAILBOX_ID, THREAD_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailParticipant"
|
||||
("id", "adminUserId", "messageId", "kind", "emailAddress")
|
||||
VALUES ($1, $2, $3, 'from', 'buyer@example.test')`,
|
||||
[PARTICIPANT_ID, OWNER_ID, MESSAGE_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailTemplate"
|
||||
("id", "adminUserId", "name", "subjectTemplate",
|
||||
"htmlTemplate", "updatedAt")
|
||||
VALUES ($1, $2, 'Demo', 'Hallo {{firstName}}',
|
||||
'<p>Hallo</p>', now())`,
|
||||
[TEMPLATE_ID, OWNER_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailOutboundPolicy"
|
||||
("id", "adminUserId", "timeZone", "workdayStartMinutes",
|
||||
"workdayEndMinutes", "dailySendLimit", "bulkDailySendLimit",
|
||||
"updatedAt")
|
||||
VALUES ($1, $2, 'Europe/Berlin', 540, 1020, 50, 25, now())`,
|
||||
[POLICY_ID, OWNER_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminScheduledEmail"
|
||||
("id", "adminUserId", "mailboxId", "toAddresses", "subject",
|
||||
"bodyHtml", "scheduledFor", "timeZone", "status",
|
||||
"idempotencyKey", "updatedAt")
|
||||
VALUES ($1, $2, $3, ARRAY['buyer@example.test'], 'Demo',
|
||||
'<p>Hallo</p>', now(), 'Europe/Berlin', 'queued',
|
||||
'admin-email-rls-send', now())`,
|
||||
[SCHEDULED_ID, OWNER_ID, MAILBOX_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminSendingDomain"
|
||||
("id", "adminUserId", "domain", "spfRecordName",
|
||||
"spfExpectedValue", "dkimRecordName", "dkimExpectedValue",
|
||||
"spfStatus", "dkimStatus", "updatedAt")
|
||||
VALUES ($1, $2, 'hivegrid.de', 'hivegrid.de',
|
||||
'v=spf1 include:spf.mailjet.com ~all',
|
||||
'mailjet._domainkey.hivegrid.de', 'k=rsa; p=fixture',
|
||||
'pending', 'pending', now())`,
|
||||
[SENDING_DOMAIN_ID, OWNER_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminBccAddress"
|
||||
("id", "adminUserId", "mailboxId", "localPartHash",
|
||||
"displayAddress", "updatedAt")
|
||||
VALUES ($1, $2, $3, $4, 'signed@bcc.hivegrid.de', now())`,
|
||||
[BCC_ADDRESS_ID, OWNER_ID, MAILBOX_ID, "a".repeat(64)]
|
||||
);
|
||||
const visible = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "AdminEmailMessage"`
|
||||
);
|
||||
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,
|
||||
]);
|
||||
for (const table of [
|
||||
"AdminMailbox",
|
||||
"AdminEmailMessage",
|
||||
"AdminEmailTemplate",
|
||||
"AdminEmailOutboundPolicy",
|
||||
"AdminSendingDomain",
|
||||
"AdminBccAddress",
|
||||
]) {
|
||||
const hidden = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "${table}"`
|
||||
);
|
||||
expect(hidden.rows[0]?.count).toBe("0");
|
||||
}
|
||||
} finally {
|
||||
await app.query("ROLLBACK");
|
||||
}
|
||||
});
|
||||
|
||||
it("lets a communication-read-all manager read team mail without write access", async () => {
|
||||
await grantAdminRlsTestPermissions(admin, {
|
||||
userId: FOREIGN_ID,
|
||||
roleId: MANAGER_ROLE_ID,
|
||||
permissions: ["admin_sales.communication.read_all"],
|
||||
});
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
FOREIGN_ID,
|
||||
]);
|
||||
const visible = await app.query<{ id: string }>(
|
||||
`SELECT "id" FROM "AdminEmailMessage" WHERE "id" = $1`,
|
||||
[MESSAGE_ID]
|
||||
);
|
||||
expect(visible.rows).toEqual([{ id: MESSAGE_ID }]);
|
||||
const update = await app.query(
|
||||
`UPDATE "AdminEmailMessage" SET "isRead" = true WHERE "id" = $1`,
|
||||
[MESSAGE_ID]
|
||||
);
|
||||
expect(update.rowCount).toBe(0);
|
||||
} finally {
|
||||
await app.query("ROLLBACK");
|
||||
}
|
||||
});
|
||||
|
||||
it("lets the reviewed worker advance cursors but never delete mail", async () => {
|
||||
await worker.query(
|
||||
`UPDATE "AdminMailboxSyncCursor"
|
||||
SET "cursorToken" = 'history-2' WHERE "id" = $1`,
|
||||
[CURSOR_ID]
|
||||
);
|
||||
const cursor = await worker.query<{ cursorToken: string }>(
|
||||
`SELECT "cursorToken" FROM "AdminMailboxSyncCursor" WHERE "id" = $1`,
|
||||
[CURSOR_ID]
|
||||
);
|
||||
expect(cursor.rows).toEqual([{ cursorToken: "history-2" }]);
|
||||
await worker.query(
|
||||
`UPDATE "AdminScheduledEmail"
|
||||
SET "status" = 'sending', "claimedAt" = now(),
|
||||
"sendAttemptCount" = "sendAttemptCount" + 1
|
||||
WHERE "id" = $1`,
|
||||
[SCHEDULED_ID]
|
||||
);
|
||||
const scheduled = await worker.query<{
|
||||
status: string;
|
||||
sendAttemptCount: number;
|
||||
}>(
|
||||
`SELECT "status", "sendAttemptCount"
|
||||
FROM "AdminScheduledEmail" WHERE "id" = $1`,
|
||||
[SCHEDULED_ID]
|
||||
);
|
||||
expect(scheduled.rows).toEqual([
|
||||
{ status: "sending", sendAttemptCount: 1 },
|
||||
]);
|
||||
await worker.query(
|
||||
`INSERT INTO "AdminEmailAttachment"
|
||||
("id", "adminUserId", "messageId", "providerAttachmentId",
|
||||
"filename", "mimeType", "sizeBytes", "storageBucket",
|
||||
"storageKey", "checksumSha256", "scannedAt")
|
||||
VALUES ($1, $2, $3, 'provider-attachment-1', 'angebot.pdf',
|
||||
'application/pdf', 128, 'hivegrid-files',
|
||||
'admin-email/attachment-1', $4, now())`,
|
||||
[ATTACHMENT_ID, OWNER_ID, MESSAGE_ID, "a".repeat(64)]
|
||||
);
|
||||
const attachment = await worker.query<{ filename: string }>(
|
||||
`SELECT "filename" FROM "AdminEmailAttachment" WHERE "id" = $1`,
|
||||
[ATTACHMENT_ID]
|
||||
);
|
||||
expect(attachment.rows).toEqual([{ filename: "angebot.pdf" }]);
|
||||
await worker.query(
|
||||
`UPDATE "AdminSendingDomain"
|
||||
SET "status" = 'verified', "spfStatus" = 'verified',
|
||||
"dkimStatus" = 'verified', "lastCheckedAt" = now()
|
||||
WHERE "id" = $1`,
|
||||
[SENDING_DOMAIN_ID]
|
||||
);
|
||||
const domain = await worker.query<{
|
||||
status: string;
|
||||
spfStatus: string;
|
||||
dkimStatus: string;
|
||||
}>(
|
||||
`SELECT "status", "spfStatus", "dkimStatus"
|
||||
FROM "AdminSendingDomain" WHERE "id" = $1`,
|
||||
[SENDING_DOMAIN_ID]
|
||||
);
|
||||
expect(domain.rows).toEqual([
|
||||
{
|
||||
status: "verified",
|
||||
spfStatus: "verified",
|
||||
dkimStatus: "verified",
|
||||
},
|
||||
]);
|
||||
await expect(
|
||||
worker.query(`DELETE FROM "AdminEmailMessage" WHERE "id" = $1`, [
|
||||
MESSAGE_ID,
|
||||
])
|
||||
).rejects.toThrow(/permission denied|row-level security/iu);
|
||||
});
|
||||
|
||||
it("hides every owned row when its 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 "AdminEmailMessage"`
|
||||
);
|
||||
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]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps engagement and live-open notifications owner-visible while the worker can redact raw data", async () => {
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.user_id', $1, true)", [
|
||||
OWNER_ID,
|
||||
]);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailLink"
|
||||
("id", "adminUserId", "messageId", "destinationUrl", "trackingTokenHash")
|
||||
VALUES ($1, $2, $3, 'https://example.test/demo', 'admin-email-rls-token')`,
|
||||
[LINK_ID, OWNER_ID, MESSAGE_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "AdminEmailEvent"
|
||||
("id", "adminUserId", "messageId", "type", "providerEventId",
|
||||
"linkId", "occurredAt", "rawExpiresAt", "metadata")
|
||||
VALUES ($1, $2, $3, 'clicked', 'admin-email-rls-click',
|
||||
$4, now(), now() + interval '180 days', '{"provider":"fixture"}')`,
|
||||
[EVENT_ID, OWNER_ID, MESSAGE_ID, LINK_ID]
|
||||
);
|
||||
await app.query(
|
||||
`INSERT INTO "NotificationDelivery"
|
||||
("id", "tenantId", "userId", "adminUserId", "channel", "category",
|
||||
"messageKey", "sourceKey", "status", "updatedAt")
|
||||
VALUES ($1, NULL, NULL, $2, 'IN_APP', 'admin-sales-email',
|
||||
'admin.sales.email.notifications.opened',
|
||||
'admin-email-open:rls', 'PENDING', now())`,
|
||||
[NOTIFICATION_ID, OWNER_ID]
|
||||
);
|
||||
const ownerVisible = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "NotificationDelivery"
|
||||
WHERE "id" = $1`,
|
||||
[NOTIFICATION_ID]
|
||||
);
|
||||
expect(ownerVisible.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 foreignVisible = await app.query<{ count: string }>(
|
||||
`SELECT count(*)::text AS count FROM "NotificationDelivery"
|
||||
WHERE "id" = $1`,
|
||||
[NOTIFICATION_ID]
|
||||
);
|
||||
expect(foreignVisible.rows[0]?.count).toBe("0");
|
||||
} finally {
|
||||
await app.query("ROLLBACK");
|
||||
}
|
||||
|
||||
await worker.query(
|
||||
`UPDATE "AdminEmailEvent"
|
||||
SET "metadata" = NULL, "recipient" = NULL, "linkId" = NULL,
|
||||
"rawExpiresAt" = NULL
|
||||
WHERE "id" = $1`,
|
||||
[EVENT_ID]
|
||||
);
|
||||
const redacted = await worker.query<{
|
||||
metadata: unknown;
|
||||
recipient: string | null;
|
||||
linkId: string | null;
|
||||
rawExpiresAt: Date | null;
|
||||
}>(
|
||||
`SELECT "metadata", "recipient", "linkId", "rawExpiresAt"
|
||||
FROM "AdminEmailEvent"
|
||||
WHERE "id" = $1`,
|
||||
[EVENT_ID]
|
||||
);
|
||||
expect(redacted.rows).toEqual([
|
||||
{
|
||||
metadata: null,
|
||||
recipient: null,
|
||||
linkId: null,
|
||||
rawExpiresAt: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user