namecheap/test/config.ts
2025-04-02 15:19:18 +00:00

103 lines
3.0 KiB
TypeScript

import { NamecheapClient } from '../ts/index.js';
import * as qenv from '@push.rocks/qenv';
// Define the auth interface to match what the client expects
interface INamecheapAuth {
apiUser: string;
apiKey: string;
userName: string; // Make userName required to match the client's expectation
clientIp: string;
}
// Test domains - use these in your tests
export const TEST_DOMAINS = {
// Use this domain for availability checks (should be available in sandbox)
CHECK: 'test-domain-availability-check.com',
// Use this domain for domain info tests (should exist in your sandbox account)
INFO: 'bleu.de',
// Use this domain for DNS tests (should exist in your sandbox account)
DNS: 'bleu.de',
// Use this for domain registration tests
REGISTER: 'test-domain-registration.net'
};
// Load environment variables from .nogit directory
const testQenv = new qenv.Qenv('./', '.nogit/');
// Initialize sandbox configuration with default values
let sandboxConfig: INamecheapAuth = {
apiUser: '',
apiKey: '',
userName: '',
clientIp: '127.0.0.1'
};
// Load sandbox configuration asynchronously
async function loadSandboxConfig() {
try {
const username = await testQenv.getEnvVarOnDemand('SANDBOX_USERNAME');
const apiKey = await testQenv.getEnvVarOnDemand('SANDBOX_APIKEY');
const clientIp = await testQenv.getEnvVarOnDemand('CLIENT_IP') || '127.0.0.1';
sandboxConfig = {
apiUser: username,
apiKey: apiKey,
userName: username,
clientIp: clientIp
};
console.log('Sandbox configuration loaded successfully');
} catch (error) {
console.warn('Failed to load sandbox configuration:', error);
}
}
// Try to load sandbox configuration immediately
loadSandboxConfig().catch(console.error);
// Default mock configuration (for tests that don't need real API access)
const mockConfig: INamecheapAuth = {
apiUser: 'testuser',
apiKey: 'testapikey',
userName: 'testuser',
clientIp: '127.0.0.1'
};
/**
* Get API configuration for tests
* @param useMock If true, returns mock config even if real credentials are available
* @returns Namecheap API configuration
*/
export function getApiConfig(useMock: boolean = false): INamecheapAuth {
// If mock config is requested or no real credentials are available, use mock config
if (useMock || !hasRealCredentials()) {
return mockConfig;
}
// Otherwise use sandbox configuration
return sandboxConfig;
}
/**
* Check if we have real API credentials
* @returns True if real credentials are available
*/
export function hasRealCredentials(): boolean {
return !!(sandboxConfig.apiUser &&
sandboxConfig.apiKey &&
sandboxConfig.clientIp);
}
/**
* Create a Namecheap client for testing
* @param useMock If true, uses mock config instead of real credentials
* @returns Configured Namecheap client
*/
export function createTestClient(useMock: boolean = false): NamecheapClient {
const config = getApiConfig(useMock);
return new NamecheapClient(config, true); // Always use sandbox mode for tests
}