|
| 1 | +const MandateService = require("../../src/services/mandate"); |
| 2 | +const jwt = require("jsonwebtoken"); |
| 3 | + |
| 4 | +describe("MandateService - Context Validation", () => { |
| 5 | + let mandateService; |
| 6 | + const signingKey = "test-secret"; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + mandateService = new MandateService({ signingKey }); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should validate amount against intent mandate budget", async () => { |
| 13 | + const mandate = await mandateService.issueIntentMandate({ |
| 14 | + userDid: "did:key:user", |
| 15 | + agentDid: "did:key:agent", |
| 16 | + maxBudget: 100, |
| 17 | + }); |
| 18 | + |
| 19 | + // Valid amount |
| 20 | + await expect( |
| 21 | + mandateService.verifyMandate(mandate, { amount: 50 }), |
| 22 | + ).resolves.toBeDefined(); |
| 23 | + |
| 24 | + // Invalid amount |
| 25 | + await expect( |
| 26 | + mandateService.verifyMandate(mandate, { amount: 150 }), |
| 27 | + ).rejects.toThrow( |
| 28 | + "Zero Trust Validation Failed: Amount 150 exceeds mandate budget of 100", |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should validate recipient against allowed_merchants whitelist", async () => { |
| 33 | + const mandate = await mandateService.issueIntentMandate({ |
| 34 | + userDid: "did:key:user", |
| 35 | + agentDid: "did:key:agent", |
| 36 | + maxBudget: 100, |
| 37 | + allowedMerchants: ["did:key:merchant-1"], |
| 38 | + }); |
| 39 | + |
| 40 | + // Authorized merchant |
| 41 | + await expect( |
| 42 | + mandateService.verifyMandate(mandate, { recipient: "did:key:merchant-1" }), |
| 43 | + ).resolves.toBeDefined(); |
| 44 | + |
| 45 | + // Unauthorized merchant |
| 46 | + await expect( |
| 47 | + mandateService.verifyMandate(mandate, { recipient: "did:key:merchant-2" }), |
| 48 | + ).rejects.toThrow( |
| 49 | + "Zero Trust Validation Failed: Merchant did:key:merchant-2 not authorized by mandate", |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should handle expired tokens with correct prefix", async () => { |
| 54 | + const mandate = jwt.sign( |
| 55 | + { |
| 56 | + exp: Math.floor(Date.now() / 1000) - 60, |
| 57 | + }, |
| 58 | + signingKey, |
| 59 | + ); |
| 60 | + |
| 61 | + await expect(mandateService.verifyMandate(mandate)).rejects.toThrow( |
| 62 | + "Zero Trust Validation Failed: Mandate has expired", |
| 63 | + ); |
| 64 | + }); |
| 65 | +}); |
0 commit comments