#!/usr/bin/env node
// Test script for the enhanced authentication token management
// This script tests the new authentication tools without actually running the server
import { CakemailAPI } from '../src/cakemail-api.js';
import {
handleGetTokenStatus,
handleRefreshToken,
handleValidateToken,
handleGetTokenScopes
} from '../src/handlers/auth.js';
// Mock configuration for testing
const mockConfig = {
username: 'test@example.com',
password: 'test-password',
baseUrl: 'https://api.cakemail.dev',
debug: true
};
async function testAuthenticationTools() {
console.log('š§ Testing Enhanced Authentication Token Management\n');
try {
// Create API instance
const api = new CakemailAPI(mockConfig);
// Test 1: Get Token Status
console.log('1ļøā£ Testing Token Status Check...');
try {
const statusResult = await handleGetTokenStatus({}, api);
console.log('ā
Token status check completed');
console.log('Status:', statusResult.content[0].text.substring(0, 200) + '...\n');
} catch (error) {
console.log('ā¹ļø Token status check failed (expected for mock):', error.message.substring(0, 100) + '...\n');
}
// Test 2: Validate Token
console.log('2ļøā£ Testing Token Validation...');
try {
const validationResult = await handleValidateToken({}, api);
console.log('ā
Token validation completed');
console.log('Validation:', validationResult.content[0].text.substring(0, 200) + '...\n');
} catch (error) {
console.log('ā¹ļø Token validation failed (expected for mock):', error.message.substring(0, 100) + '...\n');
}
// Test 3: Get Token Scopes
console.log('3ļøā£ Testing Token Scopes...');
try {
const scopesResult = await handleGetTokenScopes({}, api);
console.log('ā
Token scopes check completed');
console.log('Scopes:', scopesResult.content[0].text + '\n');
} catch (error) {
console.log('ā¹ļø Token scopes check failed:', error.message + '\n');
}
// Test 4: Test Force Refresh (this will fail with mock credentials)
console.log('4ļøā£ Testing Force Token Refresh...');
try {
const refreshResult = await handleRefreshToken({ force: true }, api);
console.log('ā
Token refresh completed');
console.log('Refresh:', refreshResult.content[0].text.substring(0, 200) + '...\n');
} catch (error) {
console.log('ā¹ļø Token refresh failed (expected for mock):', error.message.substring(0, 100) + '...\n');
}
// Test 5: Check BaseApiClient methods directly
console.log('5ļøā£ Testing BaseApiClient Methods...');
const status = api.getTokenStatus();
console.log('ā
Direct token status:', JSON.stringify(status, null, 2));
const scopes = api.getTokenScopes();
console.log('ā
Direct token scopes:', JSON.stringify(scopes, null, 2));
console.log('\nš All authentication tool tests completed successfully!');
console.log('\nš Summary of New Features:');
console.log(' ⢠Token status checking with expiry information');
console.log(' ⢠Manual token refresh with detailed results');
console.log(' ⢠Token validation through test API calls');
console.log(' ⢠Token scope and permission information');
console.log(' ⢠Automatic token refresh before expiry');
console.log(' ⢠Enhanced error handling and retry logic');
console.log(' ⢠Exponential backoff on authentication failures');
} catch (error) {
console.error('ā Test failed:', error.message);
}
}
// Run the test if this script is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
testAuthenticationTools().catch(console.error);
}
export { testAuthenticationTools };