test-token-management.jsโข4.06 kB
#!/usr/bin/env node
/**
* Test OAuth Token Management System
* This script tests the secure token storage and refresh functionality
*/
import 'dotenv/config';
import { TokenManager } from './lib/tokenManager.js';
import { getOAuthAccessToken, hasValidTokens, getTokenInfo } from './lib/oauth-helper.js';
console.log('๐งช Testing OAuth Token Management System');
console.log('=========================================\n');
async function testTokenManagement() {
try {
console.log('1. ๐ Checking environment variables...');
const clientId = process.env.GOOGLE_APP_SCRIPT_API_CLIENT_ID;
const clientSecret = process.env.GOOGLE_APP_SCRIPT_API_CLIENT_SECRET;
console.log(' - CLIENT_ID exists:', !!clientId);
console.log(' - CLIENT_SECRET exists:', !!clientSecret);
if (!clientId || !clientSecret) {
console.error('\nโ Missing OAuth credentials in .env file');
console.log('๐ก Please update your .env file with:');
console.log(' - GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_client_id');
console.log(' - GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_client_secret');
process.exit(1);
}
console.log('\n2. ๐ Checking token storage...');
const tokenManager = new TokenManager();
const tokenInfo = tokenManager.getTokenInfo();
if (tokenInfo.hasTokens) {
console.log(' โ
Tokens found');
console.log(` ๐ Location: ${tokenInfo.location}`);
console.log(` ๐พ Saved at: ${tokenInfo.savedAt}`);
console.log(` โฐ Expires at: ${tokenInfo.expiresAt}`);
console.log(` ๐ Status: ${tokenInfo.status}`);
console.log(` ๐ Scope: ${tokenInfo.scope || 'Not specified'}`);
} else {
console.log(' โ No tokens found');
console.log(` ๐ Expected location: ${tokenInfo.location}`);
console.log('\n๐ก Run "node oauth-setup.js" to set up OAuth tokens');
process.exit(0);
}
console.log('\n3. ๐ Testing token validity...');
const hasTokens = hasValidTokens();
console.log(' - Has valid tokens:', hasTokens);
if (hasTokens) {
console.log('\n4. ๐ Testing access token retrieval...');
try {
const accessToken = await getOAuthAccessToken();
console.log(' โ
Access token obtained successfully');
console.log(' ๐ Token preview:', accessToken.substring(0, 20) + '...');
console.log('\n5. โ
All tests passed!');
console.log(' ๐ฏ Your OAuth token management is working correctly');
} catch (error) {
console.error('\nโ Failed to get access token:', error.message);
if (error.message.includes('Token refresh failed')) {
console.log('\n๐ก This might happen if:');
console.log(' - Your OAuth credentials have been revoked');
console.log(' - Your client secret has changed');
console.log(' - There are network connectivity issues');
console.log('\n๐ง Try running: node oauth-setup.js --force');
}
}
} else {
console.log('\nโ No valid tokens available');
console.log('๐ก Run "node oauth-setup.js" to set up OAuth tokens');
}
} catch (error) {
console.error('\n๐ฅ Test failed with error:', error.message);
process.exit(1);
}
}
// Command line help
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log('๐ OAuth Token Management Test');
console.log('\nUsage:');
console.log(' node test-token-management.js # Run all tests');
console.log(' node test-token-management.js --help # Show this help');
console.log('\nThis script tests:');
console.log(' - Environment variable configuration');
console.log(' - Token storage and retrieval');
console.log(' - Access token refresh functionality');
console.log(' - Overall OAuth system health');
process.exit(0);
}
testTokenManagement().catch((error) => {
console.error('๐ฅ Unexpected error:', error);
process.exit(1);
});