-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest-imports.mjs
More file actions
73 lines (59 loc) · 1.92 KB
/
test-imports.mjs
File metadata and controls
73 lines (59 loc) · 1.92 KB
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
70
71
72
73
// Test importing the package
import Mpesa, {
// Core exports
MpesaError,
InvalidPhoneNumberError,
ValidationError,
// Middleware
mpesaCallbackMiddleware,
c2bCallbackMiddleware,
verifyCallbackSignature,
// Token Storage
MemoryTokenStorage,
RedisTokenStorage,
FileTokenStorage,
MongoDBTokenStorage,
// Utils
formatPhoneNumber,
validateAmount,
sanitizeForLogging,
} from './dist/index.js';
console.log('✅ All imports successful!\n');
// Test basic functionality
console.log('Testing basic functionality...\n');
// 1. Test phone number formatting
const formattedPhone = formatPhoneNumber('0712345678');
console.log('1. Phone formatting:', formattedPhone, '✅');
// 2. Test amount validation
try {
validateAmount(100);
console.log('2. Amount validation: Valid amount ✅');
} catch (error) {
console.log('2. Amount validation: Failed ❌');
}
// 3. Test sanitization
const sanitized = sanitizeForLogging({
consumerKey: 'secret123',
amount: 100,
phone: '254712345678',
});
console.log('3. Sanitization:', sanitized.consumerKey === '***REDACTED***' ? '✅' : '❌');
// 4. Test token storage
const tokenStorage = new MemoryTokenStorage();
tokenStorage.set('test-token', 3600);
const retrievedToken = tokenStorage.get();
console.log('4. Token storage:', retrievedToken === 'test-token' ? '✅' : '❌');
// 5. Test error classes
const error = new InvalidPhoneNumberError('invalid');
console.log('5. Error classes:', error instanceof MpesaError ? '✅' : '❌');
// 6. Test callback signature verification
import crypto from 'crypto';
const payload = JSON.stringify({ test: 'data' });
const secretKey = 'my-secret-key';
const signature = crypto
.createHmac('sha256', secretKey)
.update(payload)
.digest('base64');
const isValid = verifyCallbackSignature(payload, signature, secretKey);
console.log('6. Signature verification:', isValid ? '✅' : '❌');
console.log('\n🎉 All basic tests passed!');