Advanced Testing Scenarios
Integration Testing
Payment Flow Testing
describe('Payment Integration Tests', () => {
const finfusion = new FinFusion({
apiKey: process.env.TEST_API_KEY,
environment: 'sandbox'
});
test('complete payment flow', async () => {
// Create payment intent
const intent = await finfusion.payments.createIntent({
amount: 1000,
currency: 'USD',
paymentMethod: 'card',
metadata: { orderId: 'test_123' }
});
expect(intent.status).toBe('requires_payment_method');
// Attach test card
const paymentMethod = await finfusion.paymentMethods.create({
type: 'card',
card: {
number: '4242424242424242',
expMonth: 12,
expYear: 2024,
cvc: '123'
}
});
// Confirm payment
const payment = await finfusion.payments.confirm(intent.id, {
paymentMethod: paymentMethod.id
});
expect(payment.status).toBe('succeeded');
expect(payment.amount).toBe(1000);
});
});
Error Scenario Testing
describe('Payment Error Scenarios', () => {
test('handles insufficient funds', async () => {
const intent = await finfusion.payments.createIntent({
amount: 1000,
currency: 'USD',
paymentMethod: 'card'
});
// Use test card that triggers insufficient funds
const error = await finfusion.payments.confirm(intent.id, {
paymentMethod: {
card: {
number: '4000000000009995', // Test card for insufficient funds
expMonth: 12,
expYear: 2024,
cvc: '123'
}
}
}).catch(e => e);
expect(error.code).toBe('card_declined');
expect(error.decline_code).toBe('insufficient_funds');
});
});
Performance Testing
Load Testing
import { check } from 'k6';
import http from 'k6/http';
export const options = {
stages: [
{ duration: '2m', target: 100 }, // Ramp up to 100 users
{ duration: '5m', target: 100 }, // Stay at 100 users
{ duration: '2m', target: 0 } // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests must complete below 500ms
http_req_failed: ['rate<0.01'] // Error rate must be below 1%
}
};
export default function() {
const payload = JSON.stringify({
amount: 1000,
currency: 'USD',
paymentMethod: 'card'
});
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${__ENV.API_KEY}`
}
};
const res = http.post(
'https://api.finfusion.cloud/v1/payments',
payload,
params
);
check(res, {
'is status 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500
});
}
Security Testing
Authentication Testing
describe('Security Tests', () => {
test('rejects invalid API keys', async () => {
const invalidClient = new FinFusion({
apiKey: 'invalid_key',
environment: 'sandbox'
});
const error = await invalidClient.payments.create({
amount: 1000,
currency: 'USD'
}).catch(e => e);
expect(error.code).toBe('authentication_failed');
});
test('validates webhook signatures', async () => {
const payload = {
type: 'payment.succeeded',
data: { id: 'pmt_123' }
};
const invalidSignature = 'invalid_signature';
expect(() => {
webhookHandler.verifySignature(payload, invalidSignature)
}).toThrow('Invalid webhook signature');
});
});
Monitoring & Observability
Key Metrics
Business Metrics
- Transaction Volume
- Success Rate
- Average Transaction Value
- Revenue
Technical Metrics
- API Latency
- Error Rate
- Request Volume
- Resource Usage
Alert Configuration
High Error Rate
Condition: error_rate > 5% for 5 minutes
Severity: critical
Elevated Latency
Condition: p95_latency > 500ms for 10 minutes
Severity: warning
class AlertManager {
private readonly alerting: AlertingService;
constructor() {
this.alerting = new AlertingService({
endpoint: process.env.ALERT_ENDPOINT
});
}
async checkThresholds(metrics: MetricsBatch) {
// Check error rate
if (metrics.errorRate > 0.05) {
await this.alerting.trigger({
name: 'high_error_rate',
severity: 'critical',
value: metrics.errorRate,
message: `Error rate of ${metrics.errorRate}% exceeds threshold`
});
}
// Check latency
if (metrics.p95Latency > 500) {
await this.alerting.trigger({
name: 'high_latency',
severity: 'warning',
value: metrics.p95Latency,
message: `P95 latency of ${metrics.p95Latency}ms exceeds threshold`
});
}
}
}