forked from connect-boiz/soroban-security-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_notification_service.js
More file actions
397 lines (342 loc) · 13.3 KB
/
Copy pathtest_notification_service.js
File metadata and controls
397 lines (342 loc) · 13.3 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env node
/**
* Comprehensive test suite for the notification service
* This test validates both JavaScript and Rust implementations
*/
const fs = require('fs');
const path = require('path');
// Test configuration
const TEST_CONFIG = {
email: {
smtp_host: 'smtp.gmail.com',
smtp_port: '587',
username: 'test@example.com',
password: 'test_password',
from_email: 'scanner@example.com',
from_name: 'Soroban Security Scanner'
},
sms: {
provider: 'twilio',
account_sid: 'test_account_sid',
auth_token: 'test_auth_token',
from_number: '+1234567890'
},
push: {
fcm_server_key: 'test_fcm_key',
apns_key_id: 'test_apns_key',
apns_team_id: 'test_team_id'
}
};
/**
* Test the JavaScript notification service implementation
*/
async function testJavaScriptImplementation() {
console.log('🧪 Testing JavaScript Notification Service...\n');
try {
// Load the notification service
const notificationServicePath = path.join(__dirname, 'src', 'notification-service');
if (!fs.existsSync(notificationServicePath)) {
throw new Error('JavaScript notification service not found');
}
const {
NotificationService,
NotificationChannel,
NotificationPriority,
Recipient,
NotificationPreferences,
NotificationTemplate,
TemplateVariable,
VariableType
} = require(path.join(notificationServicePath, 'index.js'));
console.log('✅ JavaScript notification service loaded successfully');
// Test 1: Create notification service
const service = new NotificationService();
console.log('✅ Notification service created successfully');
// Test 2: Create recipient
const recipient = new Recipient({
id: 'test_user_123',
email: 'test@example.com',
phone: '+1234567890',
device_tokens: ['device_token_abc'],
user_id: 'test_user_123',
preferences: new NotificationPreferences({
emailEnabled: true,
smsEnabled: false,
pushEnabled: true,
inAppEnabled: true
})
});
console.log('✅ Recipient created successfully');
// Test 3: Create template
const template = new NotificationTemplate({
id: 'test_vulnerability_alert',
name: 'Test Vulnerability Alert',
description: 'Test template for vulnerability notifications',
subjectTemplate: '🚨 {{severity}} Vulnerability in {{contract_name}}',
bodyTemplate: `Hello {{user_name}},
A {{severity}} vulnerability was found in {{contract_name}}.
Description: {{description}}
Risk Score: {{risk_score}}
{{#if critical}}
⚠️ This requires immediate attention!
{{/if}}
Report: {{report_url}}`,
supportedChannels: [NotificationChannel.EMAIL, NotificationChannel.IN_APP],
defaultPriority: NotificationPriority.HIGH,
variables: [
new TemplateVariable({
name: 'user_name',
description: 'User name',
required: true,
variableType: VariableType.STRING
}),
new TemplateVariable({
name: 'severity',
description: 'Severity level',
required: true,
variableType: VariableType.STRING
}),
new TemplateVariable({
name: 'contract_name',
description: 'Contract name',
required: true,
variableType: VariableType.STRING
}),
new TemplateVariable({
name: 'description',
description: 'Vulnerability description',
required: true,
variableType: VariableType.STRING
}),
new TemplateVariable({
name: 'risk_score',
description: 'Risk score',
required: true,
variableType: VariableType.NUMBER
}),
new TemplateVariable({
name: 'critical',
description: 'Whether this is critical',
required: false,
variableType: VariableType.BOOLEAN
}),
new TemplateVariable({
name: 'report_url',
description: 'Link to report',
required: true,
variableType: VariableType.URL
})
],
created_at: new Date(),
updated_at: new Date(),
version: 1,
active: true
});
await service.addTemplate(template);
console.log('✅ Template created and added successfully');
// Test 4: Send templated notification
const context = {
user_name: 'Test User',
severity: 'High',
contract_name: 'TestContract',
description: 'Reentrancy vulnerability detected',
risk_score: 85,
critical: true,
report_url: 'https://scanner.example.com/report/123'
};
const result = await service.sendTemplatedNotification(
'test_vulnerability_alert',
recipient,
context,
[NotificationChannel.IN_APP], // Only test in-app to avoid external dependencies
NotificationPriority.HIGH
);
if (result.success) {
console.log('✅ Templated notification sent successfully');
console.log(` Delivered to: ${result.deliveredChannels.join(', ')}`);
} else {
console.log('⚠️ Notification partially failed:', result.failedChannels);
}
// Test 5: Check provider health
const health = await service.healthCheck();
console.log('✅ Provider health check completed');
for (const [channel, healthy] of Object.entries(health)) {
console.log(` ${channel}: ${healthy ? '✅' : '❌'}`);
}
// Test 6: Get provider statistics
const stats = await service.getProviderStats();
console.log('✅ Provider statistics retrieved');
for (const [channel, channelStats] of Object.entries(stats)) {
console.log(` ${channel}: ${channelStats.totalSent} sent, ${channelStats.totalFailed} failed`);
}
// Test 7: List templates
const templates = await service.listTemplates();
console.log(`✅ Templates listed: ${templates.length} templates found`);
// Test 8: In-app notifications
const inAppNotifications = service.getInAppNotifications('test_user_123');
console.log(`✅ In-app notifications retrieved: ${inAppNotifications.length} notifications`);
console.log('\n🎉 All JavaScript tests passed!');
return true;
} catch (error) {
console.error('\n❌ JavaScript test failed:', error.message);
console.error(error.stack);
return false;
}
}
/**
* Validate Rust implementation structure
*/
function validateRustImplementation() {
console.log('\n🧪 Validating Rust Notification Service Structure...\n');
const rustServicePath = path.join(__dirname, 'src', 'notification_service');
try {
// Check required files exist
const requiredFiles = [
'mod.rs',
'types.rs',
'service.rs',
'providers.rs',
'templates.rs',
'tracking.rs',
'tests.rs'
];
for (const file of requiredFiles) {
const filePath = path.join(rustServicePath, file);
if (!fs.existsSync(filePath)) {
throw new Error(`Missing required file: ${file}`);
}
console.log(`✅ ${file} exists`);
}
// Check Cargo.toml exists
const cargoPath = path.join(__dirname, 'Cargo.toml');
if (!fs.existsSync(cargoPath)) {
throw new Error('Missing Cargo.toml file');
}
console.log('✅ Cargo.toml exists');
// Validate Cargo.toml content
const cargoContent = fs.readFileSync(cargoPath, 'utf8');
const requiredDependencies = [
'tokio',
'serde',
'uuid',
'chrono',
'async-trait',
'handlebars',
'lettre',
'reqwest',
'thiserror'
];
for (const dep of requiredDependencies) {
if (!cargoContent.includes(dep)) {
throw new Error(`Missing dependency in Cargo.toml: ${dep}`);
}
console.log(`✅ Dependency ${dep} found`);
}
// Validate module structure
const modContent = fs.readFileSync(path.join(rustServicePath, 'mod.rs'), 'utf8');
const requiredModules = [
'providers',
'templates',
'tracking',
'types',
'service'
];
for (const module of requiredModules) {
if (!modContent.includes(`pub mod ${module}`)) {
throw new Error(`Missing module export: ${module}`);
}
console.log(`✅ Module ${module} exported`);
}
console.log('\n🎉 Rust implementation structure validated successfully!');
return true;
} catch (error) {
console.error('\n❌ Rust validation failed:', error.message);
return false;
}
}
/**
* Test integration with security scanner
*/
function testSecurityScannerIntegration() {
console.log('\n🧪 Testing Security Scanner Integration...\n');
try {
// Check if notification service is integrated in main index.js
const indexPath = path.join(__dirname, 'src', 'index.js');
if (!fs.existsSync(indexPath)) {
throw new Error('Main index.js not found');
}
const indexContent = fs.readFileSync(indexPath, 'utf8');
// Check for notification service imports
if (!indexContent.includes('notification-service')) {
throw new Error('Notification service not imported in main index.js');
}
console.log('✅ Notification service imported in main index.js');
// Check for notification commands
if (!indexContent.includes('notifications')) {
throw new Error('Notification commands not found in main index.js');
}
console.log('✅ Notification commands found in main index.js');
// Check for notification options in scan command
if (!indexContent.includes('--notify')) {
throw new Error('Notification options not found in scan command');
}
console.log('✅ Notification options found in scan command');
// Check lib.rs exports
const libPath = path.join(__dirname, 'src', 'lib.rs');
if (fs.existsSync(libPath)) {
const libContent = fs.readFileSync(libPath, 'utf8');
if (!libContent.includes('notification_service')) {
throw new Error('Notification service not exported in lib.rs');
}
console.log('✅ Notification service exported in lib.rs');
}
console.log('\n🎉 Security scanner integration validated successfully!');
return true;
} catch (error) {
console.error('\n❌ Integration test failed:', error.message);
return false;
}
}
/**
* Main test runner
*/
async function runTests() {
console.log('🚀 Starting Comprehensive Notification Service Tests\n');
const results = {
javascript: false,
rust: false,
integration: false
};
// Run JavaScript tests
results.javascript = await testJavaScriptImplementation();
// Validate Rust implementation
results.rust = validateRustImplementation();
// Test integration
results.integration = testSecurityScannerIntegration();
// Summary
console.log('\n📊 Test Results Summary:');
console.log(` JavaScript Implementation: ${results.javascript ? '✅ PASS' : '❌ FAIL'}`);
console.log(` Rust Implementation: ${results.rust ? '✅ PASS' : '❌ FAIL'}`);
console.log(` Security Scanner Integration: ${results.integration ? '✅ PASS' : '❌ FAIL'}`);
const allPassed = Object.values(results).every(result => result);
if (allPassed) {
console.log('\n🎉 All tests passed! Notification service is ready for production.');
process.exit(0);
} else {
console.log('\n❌ Some tests failed. Please review the implementation.');
process.exit(1);
}
}
// Run tests if this file is executed directly
if (require.main === module) {
runTests().catch(error => {
console.error('Test runner failed:', error);
process.exit(1);
});
}
module.exports = {
testJavaScriptImplementation,
validateRustImplementation,
testSecurityScannerIntegration,
runTests
};