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

199 lines
5.6 KiB
TypeScript

/**
* Test file for Namecheap domain registration operations
*
* IMPORTANT: These tests can incur charges even in sandbox mode!
* Only run these tests if you understand the implications.
*/
import { expect, tap } from '@push.rocks/tapbundle';
import { createTestClient, TEST_DOMAINS, hasRealCredentials } from './config.js';
// Skip live API tests if no real credentials or if ENABLE_REGISTRATION_TESTS is not set
const skipLiveTests = !hasRealCredentials() || process.env.ENABLE_REGISTRATION_TESTS !== 'true';
// Always run this test to ensure the file doesn't fail when all other tests are skipped
tap.test('Registration - Basic Client Test', async () => {
// Create a client with mock credentials
const client = createTestClient(true);
// Verify the client has the expected methods
expect(client.domains.create).toBeTypeOf('function');
expect(client.domains.renew).toBeTypeOf('function');
expect(client.domains.reactivate).toBeTypeOf('function');
expect(client.domains.getTldList).toBeTypeOf('function');
// This test always passes
expect(true).toBeTrue();
});
// Test TLD list retrieval
if (skipLiveTests) {
tap.skip.test('Registration - Check TLD List', async () => {
console.log('Skipping TLD list test - no credentials or registration tests disabled');
});
} else {
tap.test('Registration - Check TLD List', async (tools) => {
// Set a timeout for the test
tools.timeout(15000); // 15 seconds timeout
// Create a new client instance
const client = createTestClient();
// Get list of available TLDs
const tlds = await client.domains.getTldList();
// Validate the result
expect(Array.isArray(tlds)).toBeTrue();
expect(tlds.length).toBeGreaterThan(0);
// Log some of the available TLDs with colored output
console.log(await tools.coloredString(
`Available TLDs: ${tlds.slice(0, 10).join(', ')}...`,
'cyan'
));
});
}
// Test domain registration
// This test is always skipped by default because it costs money
tap.skip.test('Registration - Register Domain', async (tools) => {
// Set a timeout for the test
tools.timeout(60000); // 60 seconds timeout
// Create a new client instance
const client = createTestClient();
// Sample contact information (required for registration)
const contacts = {
registrant: {
FirstName: 'Test',
LastName: 'User',
Address1: '123 Test St',
City: 'Test City',
StateProvince: 'CA',
PostalCode: '12345',
Country: 'US',
Phone: '+1.5555555555',
EmailAddress: 'test@example.com'
},
tech: {
FirstName: 'Test',
LastName: 'User',
Address1: '123 Test St',
City: 'Test City',
StateProvince: 'CA',
PostalCode: '12345',
Country: 'US',
Phone: '+1.5555555555',
EmailAddress: 'test@example.com'
},
admin: {
FirstName: 'Test',
LastName: 'User',
Address1: '123 Test St',
City: 'Test City',
StateProvince: 'CA',
PostalCode: '12345',
Country: 'US',
Phone: '+1.5555555555',
EmailAddress: 'test@example.com'
},
auxBilling: {
FirstName: 'Test',
LastName: 'User',
Address1: '123 Test St',
City: 'Test City',
StateProvince: 'CA',
PostalCode: '12345',
Country: 'US',
Phone: '+1.5555555555',
EmailAddress: 'test@example.com'
}
};
console.log(await tools.coloredString(
`Registering domain: ${TEST_DOMAINS.REGISTER}`,
'cyan'
));
// Register a new domain
const result = await client.domains.create({
domainName: TEST_DOMAINS.REGISTER,
years: 1,
contacts,
nameservers: ['dns1.namecheaphosting.com', 'dns2.namecheaphosting.com'],
addFreeWhoisguard: true,
whoisguardPrivacy: true
});
// Validate the result
expect(result).toBeTypeOf('object');
expect(result.registered).toBeTrue();
expect(result.domain).toEqual(TEST_DOMAINS.REGISTER);
// Log the registration result with colored output
console.log(await tools.coloredString(
`Domain ${result.domain} registered successfully`,
'green'
));
console.log(await tools.coloredString(
`Transaction ID: ${result.transactionId}`,
'green'
));
console.log(await tools.coloredString(
`Order ID: ${result.orderId}`,
'green'
));
console.log(await tools.coloredString(
`Charged Amount: ${result.chargedAmount}`,
'green'
));
});
// Test domain renewal
// This test is always skipped by default because it costs money
tap.skip.test('Registration - Renew Domain', async (tools) => {
// Set a timeout for the test
tools.timeout(60000); // 60 seconds timeout
// Create a new client instance
const client = createTestClient();
console.log(await tools.coloredString(
`Renewing domain: ${TEST_DOMAINS.INFO}`,
'cyan'
));
// Renew a domain
const result = await client.domains.renew(TEST_DOMAINS.INFO, 1);
// Validate the result
expect(result).toBeTypeOf('object');
expect(result.renewed).toBeTrue();
expect(result.domainName).toEqual(TEST_DOMAINS.INFO);
// Log the renewal result with colored output
console.log(await tools.coloredString(
`Domain ${result.domainName} renewed successfully`,
'green'
));
console.log(await tools.coloredString(
`Transaction ID: ${result.transactionId}`,
'green'
));
console.log(await tools.coloredString(
`Order ID: ${result.orderId}`,
'green'
));
console.log(await tools.coloredString(
`Charged Amount: ${result.chargedAmount}`,
'green'
));
console.log(await tools.coloredString(
`New Expiry Date: ${result.expireDate}`,
'green'
));
});
// Start the tests
tap.start();