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

156 lines
5.1 KiB
TypeScript

/**
* Test file for Namecheap domain information retrieval
*/
import { expect, tap } from '@push.rocks/tapbundle';
import { createTestClient, TEST_DOMAINS, hasRealCredentials } from './config.js';
// Skip live API tests if no real credentials
const skipLiveTests = !hasRealCredentials();
// Always run this test to ensure the file doesn't fail when all other tests are skipped
tap.test('Info - Basic Client Test', async () => {
// Create a client with mock credentials
const client = createTestClient(true);
// Verify the client has the expected methods
expect(client.domains.getList).toBeTypeOf('function');
expect(client.domains.getInfo).toBeTypeOf('function');
expect(client.domains.getContacts).toBeTypeOf('function');
// This test always passes
expect(true).toBeTrue();
});
// Test domain list retrieval
if (skipLiveTests) {
tap.skip.test('Domain Info - Get Domain List', async () => {
console.log('Skipping domain list test - no credentials');
});
} else {
tap.test('Domain Info - Get Domain 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 domains
const result = await client.domains.getList();
// Validate the result
expect(result).toBeTypeOf('object');
expect(Array.isArray(result.domains)).toBeTrue();
expect(result.paging).toBeTypeOf('object');
// Log the results with colored output
console.log(await tools.coloredString(
`Found ${result.domains.length} domains in your account`,
'cyan'
));
console.log(await tools.coloredString(
`Page ${result.paging.currentPage} of ${Math.ceil(result.paging.totalItems / result.paging.pageSize)}`,
'cyan'
));
// Display the first few domains
for (const domain of result.domains.slice(0, 3)) {
console.log(await tools.coloredString(
`- ${domain.Name} (expires: ${domain.Expires})`,
'green'
));
}
});
}
// Test domain info retrieval
if (skipLiveTests) {
tap.skip.test('Domain Info - Get Domain Info', async () => {
console.log('Skipping domain info test - no credentials');
});
} else {
tap.test('Domain Info - Get Domain Info', async (tools) => {
// Set a timeout for the test
tools.timeout(15000); // 15 seconds timeout
// Create a new client instance
const client = createTestClient();
// Get information about a specific domain
const domainInfo = await client.domains.getInfo(TEST_DOMAINS.INFO);
// Validate the result
expect(domainInfo).toBeTypeOf('object');
expect(domainInfo.domainName).toEqual(TEST_DOMAINS.INFO);
expect(domainInfo.status).toBeTypeOf('string');
expect(domainInfo.createdDate).toBeTypeOf('string');
expect(domainInfo.expiredDate).toBeTypeOf('string');
// Log the domain information with colored output
console.log(await tools.coloredString(`Domain: ${domainInfo.domainName}`, 'cyan'));
console.log(await tools.coloredString(`Status: ${domainInfo.status}`, 'cyan'));
console.log(await tools.coloredString(`Created: ${domainInfo.createdDate}`, 'cyan'));
console.log(await tools.coloredString(`Expires: ${domainInfo.expiredDate}`, 'cyan'));
console.log(await tools.coloredString(
`WhoisGuard: ${domainInfo.whoisGuard.enabled ? 'Enabled' : 'Disabled'}`,
domainInfo.whoisGuard.enabled ? 'green' : 'blue'
));
});
}
// Test domain contacts retrieval
if (skipLiveTests) {
tap.skip.test('Domain Info - Get Domain Contacts', async () => {
console.log('Skipping domain contacts test - no credentials');
});
} else {
tap.test('Domain Info - Get Domain Contacts', async (tools) => {
// Set a timeout for the test
tools.timeout(15000); // 15 seconds timeout
// Create a new client instance
const client = createTestClient();
// Get contact information for a domain
const contacts = await client.domains.getContacts(TEST_DOMAINS.INFO);
// Validate the result
expect(contacts).toBeTypeOf('object');
expect(contacts.registrant).toBeTypeOf('object');
// Log the registrant contact information with colored output
console.log(await tools.coloredString('Registrant Contact Information:', 'cyan'));
if (contacts.registrant.FirstName && contacts.registrant.LastName) {
console.log(await tools.coloredString(
`Name: ${contacts.registrant.FirstName} ${contacts.registrant.LastName}`,
'green'
));
}
if (contacts.registrant.EmailAddress) {
console.log(await tools.coloredString(
`Email: ${contacts.registrant.EmailAddress}`,
'green'
));
}
if (contacts.registrant.Phone) {
console.log(await tools.coloredString(
`Phone: ${contacts.registrant.Phone}`,
'green'
));
}
if (contacts.registrant.Address1 && contacts.registrant.City) {
console.log(await tools.coloredString(
`Address: ${contacts.registrant.Address1}, ${contacts.registrant.City}, ${contacts.registrant.StateProvince || ''}, ${contacts.registrant.PostalCode || ''}, ${contacts.registrant.Country}`,
'green'
));
}
});
}
// Start the tests
tap.start();