edtech / apps /api /src /tests /tenant-isolation.test.ts
CognxSafeTrack
feat: implement whatsapp templates management with security hardening, audit logs, and crm integration
0f2f80a
import { prisma } from '../services/prisma';
import { getOrganizationByPhoneNumberId } from '../services/organization';
/**
* Tenant Isolation Test Suite
*
* Verifies that the multi-tenant routing logic correctly identifies organizations
* based on the WhatsApp Phone Number ID.
*/
async function testTenantIsolation() {
console.log("🧪 Starting Tenant Isolation Tests...");
// 1. Setup Mock Organizations
const orgA = await prisma.organization.create({
data: {
name: "Test Org A",
mode: "EDTECH",
phoneNumbers: {
create: { id: "phone-id-a", displayPhone: "+221770000001" }
}
}
});
const orgB = await prisma.organization.create({
data: {
name: "Test Org B",
mode: "EDTECH",
phoneNumbers: {
create: { id: "phone-id-b", displayPhone: "+221770000002" }
}
}
});
try {
// 2. Test Routing for Org A
const detectedA = await getOrganizationByPhoneNumberId("phone-id-a");
if (detectedA === orgA.id) {
console.log("✅ Org A routed correctly.");
} else {
console.log(`❌ Org A routing failed. Expected ${orgA.id}, got ${detectedA}`);
}
// 3. Test Routing for Org B
const detectedB = await getOrganizationByPhoneNumberId("phone-id-b");
if (detectedB === orgB.id) {
console.log("✅ Org B routed correctly.");
} else {
console.log(`❌ Org B routing failed. Expected ${orgB.id}, got ${detectedB}`);
}
// 4. Test Unknown Routing (should return null for unregistered phone IDs)
const detectedUnknown = await getOrganizationByPhoneNumberId("unknown-id");
console.log(`ℹ️ Unknown ID result: ${detectedUnknown} (null = correctly rejected)`);
} finally {
// Cleanup
await prisma.organization.deleteMany({
where: { id: { in: [orgA.id, orgB.id] } }
});
console.log("🧹 Cleanup complete.");
}
}
// Simple execution if called directly
if (require.main === module) {
testTenantIsolation().catch(console.error);
}