File size: 2,210 Bytes
6248bf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f2f80a
6248bf4
0f2f80a
6248bf4
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

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);
}