|
| 1 | +const TokenizationService = require('../../src/services/tokenization'); |
| 2 | +const MandateService = require('../../src/services/mandate'); |
| 3 | +const jwt = require('jsonwebtoken'); |
| 4 | + |
| 5 | +describe('TokenizationService', () => { |
| 6 | + let tokenizationService; |
| 7 | + const signingKey = 'test-secret'; |
| 8 | + const apiKey = 'test-key'; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + tokenizationService = new TokenizationService({ |
| 12 | + apiKey, |
| 13 | + mandateConfig: { signingKey }, |
| 14 | + strictMandateMode: true |
| 15 | + }); |
| 16 | + process.env.NODE_ENV = 'test'; |
| 17 | + }); |
| 18 | + |
| 19 | + describe('signWithToken - Zero Trust Validation', () => { |
| 20 | + it('should throw error if mandate is required but not provided in strict mode', async () => { |
| 21 | + await expect(tokenizationService.signWithToken('token_123', 'data')) |
| 22 | + .rejects.toThrow('Zero Trust Validation Failed: Mandate required for signing in strict mode'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should throw error if amount exceeds mandate budget', async () => { |
| 26 | + const mandateService = new MandateService({ signingKey }); |
| 27 | + const mandate = await mandateService.issueIntentMandate({ |
| 28 | + userDid: 'did:key:user', |
| 29 | + agentDid: 'did:key:agent', |
| 30 | + maxBudget: 100 |
| 31 | + }); |
| 32 | + |
| 33 | + await expect(tokenizationService.signWithToken('token_123', 'data', mandate, { amount: 150 })) |
| 34 | + .rejects.toThrow('Zero Trust Validation Failed: Amount 150 exceeds mandate budget of 100'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should throw error if merchant is not authorized', async () => { |
| 38 | + const mandateService = new MandateService({ signingKey }); |
| 39 | + const mandate = await mandateService.issueIntentMandate({ |
| 40 | + userDid: 'did:key:user', |
| 41 | + agentDid: 'did:key:agent', |
| 42 | + maxBudget: 1000, |
| 43 | + allowedMerchants: ['did:key:merchant_a'] |
| 44 | + }); |
| 45 | + |
| 46 | + await expect(tokenizationService.signWithToken('token_123', 'data', mandate, { merchant: 'did:key:merchant_b' })) |
| 47 | + .rejects.toThrow('Zero Trust Validation Failed: Merchant did:key:merchant_b not authorized by mandate'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw error if mandate has expired', async () => { |
| 51 | + const mandateService = new MandateService({ signingKey }); |
| 52 | + const mandate = await mandateService.issueIntentMandate({ |
| 53 | + userDid: 'did:key:user', |
| 54 | + agentDid: 'did:key:agent', |
| 55 | + maxBudget: 1000, |
| 56 | + expiration: Math.floor(Date.now() / 1000) - 100 // Expired 100s ago |
| 57 | + }); |
| 58 | + |
| 59 | + await expect(tokenizationService.signWithToken('token_123', 'data', mandate)) |
| 60 | + .rejects.toThrow('Zero Trust Validation Failed: Mandate has expired'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should successfully sign if mandate is valid', async () => { |
| 64 | + const mandateService = new MandateService({ signingKey }); |
| 65 | + const mandate = await mandateService.issueIntentMandate({ |
| 66 | + userDid: 'did:key:user', |
| 67 | + agentDid: 'did:key:agent', |
| 68 | + maxBudget: 1000, |
| 69 | + allowedMerchants: ['did:key:merchant_a'] |
| 70 | + }); |
| 71 | + |
| 72 | + const signature = await tokenizationService.signWithToken( |
| 73 | + 'token_123', |
| 74 | + 'data', |
| 75 | + mandate, |
| 76 | + { amount: 500, merchant: 'did:key:merchant_a' } |
| 77 | + ); |
| 78 | + |
| 79 | + expect(signature).toContain('0x_mock_signature'); |
| 80 | + expect(signature).toContain('validated_by_mandate'); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments