Files
bunq/test/test.oauth.caching.ts

105 lines
3.0 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as bunq from '../ts/index.js';
tap.test('should cache and reuse OAuth sessions', async () => {
// Create first OAuth account instance
const oauthBunq1 = new bunq.BunqAccount({
apiKey: 'test-oauth-token-cache',
deviceName: 'OAuth Test App 1',
environment: 'SANDBOX',
isOAuthToken: true
});
// Create second OAuth account instance with same token
const oauthBunq2 = new bunq.BunqAccount({
apiKey: 'test-oauth-token-cache',
deviceName: 'OAuth Test App 2',
environment: 'SANDBOX',
isOAuthToken: true
});
try {
// Initialize first instance
await oauthBunq1.init();
console.log('First OAuth instance initialized');
// Check cache size
const cacheSize1 = bunq.BunqAccount.getOAuthCacheSize();
console.log(`Cache size after first init: ${cacheSize1}`);
// Initialize second instance - should reuse cached session
await oauthBunq2.init();
console.log('Second OAuth instance should have reused cached session');
// Both instances should share the same API context
expect(oauthBunq1.apiContext).toEqual(oauthBunq2.apiContext);
// Cache size should still be 1
const cacheSize2 = bunq.BunqAccount.getOAuthCacheSize();
expect(cacheSize2).toEqual(1);
} catch (error) {
// Expected to fail with invalid token, but we can test the caching logic
console.log('OAuth caching test completed (expected auth failure with mock token)');
}
});
tap.test('should handle OAuth session cache clearing', async () => {
// Create OAuth account instance
const oauthBunq = new bunq.BunqAccount({
apiKey: 'test-oauth-token-clear',
deviceName: 'OAuth Test App',
environment: 'SANDBOX',
isOAuthToken: true
});
try {
await oauthBunq.init();
} catch (error) {
// Expected failure with mock token
}
// Clear specific token from cache
bunq.BunqAccount.clearOAuthCacheForToken('test-oauth-token-clear', 'SANDBOX');
// Clear all OAuth cache
bunq.BunqAccount.clearOAuthCache();
// Cache should be empty
const cacheSize = bunq.BunqAccount.getOAuthCacheSize();
expect(cacheSize).toEqual(0);
console.log('OAuth cache clearing test passed');
});
tap.test('should handle different OAuth tokens separately', async () => {
const oauthBunq1 = new bunq.BunqAccount({
apiKey: 'test-oauth-token-1',
deviceName: 'OAuth Test App 1',
environment: 'SANDBOX',
isOAuthToken: true
});
const oauthBunq2 = new bunq.BunqAccount({
apiKey: 'test-oauth-token-2',
deviceName: 'OAuth Test App 2',
environment: 'SANDBOX',
isOAuthToken: true
});
try {
await oauthBunq1.init();
await oauthBunq2.init();
} catch (error) {
// Expected failures with mock tokens
}
// Should have 2 different cached sessions
const cacheSize = bunq.BunqAccount.getOAuthCacheSize();
console.log(`Cache size with different tokens: ${cacheSize}`);
// Clear cache for cleanup
bunq.BunqAccount.clearOAuthCache();
});
tap.start();