diff options
| author | Yuval Adam <_@yuv.al> | 2025-07-18 15:43:32 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2025-07-18 15:43:32 +0200 |
| commit | d0c73a92f27a652eaf925d880625a05ae211211d (patch) | |
| tree | 7a82b603764f56690b7a8e3d0241e1d19934839c | |
| parent | 350eedaf53068b1822846fceee15c97c644aad8a (diff) | |
Add tests
| -rwxr-xr-x | test_apis.py | 29 | ||||
| -rw-r--r-- | tests/__init__.py | 1 | ||||
| -rwxr-xr-x | tests/run_tests.py | 61 | ||||
| -rwxr-xr-x | tests/test_cloudflare_api.py | 106 | ||||
| -rw-r--r-- | tests/test_connectivity.py | 58 | ||||
| -rwxr-xr-x | tests/test_namecheap_api.py | 88 |
6 files changed, 343 insertions, 0 deletions
diff --git a/test_apis.py b/test_apis.py new file mode 100755 index 0000000..24a5d80 --- /dev/null +++ b/test_apis.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +""" +API Integration Test Runner for Regflow +Run this to verify your API credentials are working correctly. +""" + +import os +import sys + +# Add the current directory to the Python path +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +# Import the test runner +from tests.run_tests import run_all_tests + +if __name__ == "__main__": + print("Regflow API Integration Test Suite") + print("=" * 40) + success = run_all_tests() + + if success: + print("\n๐ All tests passed! Your APIs are ready to use.") + print("\nYou can now run:") + print(" uv run regflow domain.com --dry-run") + print(" uv run regflow domain.com --setup-only") + else: + print("\nโ Some tests failed. Please fix the issues before using the tool.") + sys.exit(1)
\ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..9b534f0 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# Integration tests for regflow
\ No newline at end of file diff --git a/tests/run_tests.py b/tests/run_tests.py new file mode 100755 index 0000000..4933017 --- /dev/null +++ b/tests/run_tests.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from tests.test_namecheap_api import test_namecheap_api +from tests.test_cloudflare_api import test_cloudflare_api + +def run_all_tests(): + """Run all integration tests""" + print("๐งช REGFLOW INTEGRATION TEST SUITE") + print("="*60) + print("Testing API integrations with real endpoints") + print("(Non-mutating operations only)") + print("="*60) + + results = [] + + # Test Namecheap API + print("\n" + "="*60) + namecheap_success = test_namecheap_api() + results.append(("Namecheap API", namecheap_success)) + + # Test Cloudflare API + print("\n" + "="*60) + cloudflare_success = test_cloudflare_api() + results.append(("Cloudflare API", cloudflare_success)) + + # Summary + print("\n" + "="*60) + print("TEST RESULTS SUMMARY") + print("="*60) + + all_passed = True + for test_name, success in results: + status = "โ
PASSED" if success else "โ FAILED" + print(f"{test_name:20} {status}") + if not success: + all_passed = False + + print("="*60) + if all_passed: + print("๐ ALL TESTS PASSED! API integrations are working correctly.") + print("\nYour API credentials are valid and the system is ready for use.") + print("\nNext steps:") + print(" - Use --dry-run for safe testing") + print(" - Use --setup-only for existing domains") + print(" - Run without flags for live domain registration") + else: + print("โ SOME TESTS FAILED! Please check your API credentials and network connection.") + print("\nTroubleshooting:") + print(" - Verify API tokens are correct and complete") + print(" - Check that APIs are enabled for your accounts") + print(" - Ensure network connectivity to API endpoints") + + return all_passed + +if __name__ == "__main__": + success = run_all_tests() + sys.exit(0 if success else 1)
\ No newline at end of file diff --git a/tests/test_cloudflare_api.py b/tests/test_cloudflare_api.py new file mode 100755 index 0000000..97c19b3 --- /dev/null +++ b/tests/test_cloudflare_api.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 + +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from config import Config +from cloudflare_api import CloudflareAPI + +def test_cloudflare_api(): + """Test Cloudflare API integration with non-mutating calls""" + print("="*50) + print("CLOUDFLARE API INTEGRATION TESTS") + print("="*50) + + # Load configuration + config = Config.from_env() + + # Check if API credentials are available + if not config.cloudflare_api_token or config.cloudflare_api_token == "your_cloudflare_api_token": + print("โ Cloudflare API token not found. Please set CLOUDFLARE_API_TOKEN in .env file.") + return False + + print(f"โ API credentials loaded") + print(f" - API Token: {'*' * (len(config.cloudflare_api_token) - 8)}{config.cloudflare_api_token[-8:]}") + print(f" - Token length: {len(config.cloudflare_api_token)} chars") + + # Initialize API + api = CloudflareAPI(config) + + # Test 1: List existing zones (non-mutating) + print(f"\n1. Testing zones listing...") + try: + zones = api.list_zones() + print(f" โ Zone listing successful") + print(f" - Found {len(zones)} zones in account") + + if zones: + print(f" - Example zones:") + for i, zone in enumerate(zones[:3]): # Show first 3 zones + print(f" โข {zone.get('name', 'Unknown')} (ID: {zone.get('id', 'Unknown')[:8]}...)") + if i >= 2: # Limit to 3 zones + break + except Exception as e: + print(f" โ Zone listing failed: {e}") + return False + + # Test 2: Get zone info for an existing zone (if any) + if zones: + print(f"\n2. Testing zone info retrieval...") + try: + first_zone = zones[0] + zone_name = first_zone.get('name') + zone_info = api.get_zone_info(zone_name) + + if zone_info: + print(f" โ Zone info retrieved successfully") + print(f" - Zone: {zone_info.get('name')}") + print(f" - Status: {zone_info.get('status')}") + print(f" - ID: {zone_info.get('id')[:8]}...") + + # Test 3: Get nameservers for the zone + print(f"\n3. Testing nameserver retrieval...") + try: + nameservers = api.get_zone_nameservers(zone_info['id']) + print(f" โ Nameservers retrieved successfully") + print(f" - Nameservers for {zone_name}:") + for ns in nameservers: + print(f" โข {ns}") + except Exception as e: + print(f" โ Nameserver retrieval failed: {e}") + return False + + # Test 4: Get DNS records for the zone (non-mutating) + print(f"\n4. Testing DNS records retrieval...") + try: + dns_records = api.get_zone_dns_records(zone_info['id']) + print(f" โ DNS records retrieved successfully") + print(f" - Found {len(dns_records)} DNS records") + + if dns_records: + print(f" - Example records:") + for i, record in enumerate(dns_records[:3]): # Show first 3 records + print(f" โข {record.get('type', 'Unknown')} {record.get('name', 'Unknown')} -> {record.get('content', 'Unknown')}") + if i >= 2: # Limit to 3 records + break + except Exception as e: + print(f" โ DNS records retrieval failed: {e}") + return False + else: + print(f" โ Zone info retrieval failed: No zone info returned") + return False + except Exception as e: + print(f" โ Zone info retrieval failed: {e}") + return False + else: + print(f"\n2. Skipping zone-specific tests (no zones found)") + print(f"3. Skipping nameserver tests (no zones found)") + print(f"4. Skipping DNS records tests (no zones found)") + + print(f"\nโ
All Cloudflare API tests passed!") + return True + +if __name__ == "__main__": + success = test_cloudflare_api() + sys.exit(0 if success else 1)
\ No newline at end of file diff --git a/tests/test_connectivity.py b/tests/test_connectivity.py new file mode 100644 index 0000000..a344861 --- /dev/null +++ b/tests/test_connectivity.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import sys +import os +import requests +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from config import Config + +def test_api_connectivity(): + """Test basic API connectivity""" + print("="*50) + print("API CONNECTIVITY TEST") + print("="*50) + + config = Config.from_env() + + # Test Namecheap API connectivity + print("\n1. Testing Namecheap API connectivity...") + try: + response = requests.get("https://api.namecheap.com/xml.response", + params={"Command": "namecheap.domains.check", "DomainList": "test.com"}, + timeout=10) + print(f" โ Namecheap API reachable (Status: {response.status_code})") + except Exception as e: + print(f" โ Namecheap API unreachable: {e}") + + # Test Cloudflare API connectivity + print("\n2. Testing Cloudflare API connectivity...") + try: + headers = {"Authorization": f"Bearer {config.cloudflare_api_token}"} + response = requests.get("https://api.cloudflare.com/client/v4/user/tokens/verify", + headers=headers, timeout=10) + print(f" โ Cloudflare API reachable (Status: {response.status_code})") + + if response.status_code == 200: + result = response.json() + if result.get('success'): + print(f" โ Cloudflare API token is valid") + else: + print(f" โ Cloudflare API token is invalid: {result.get('errors', [])}") + else: + print(f" โ Cloudflare API token verification failed") + except Exception as e: + print(f" โ Cloudflare API unreachable: {e}") + + print("\n3. API Token Information:") + print(f" - Namecheap API Key: {len(config.namecheap_api_key)} chars") + print(f" - Cloudflare API Token: {len(config.cloudflare_api_token)} chars") + print(f" - Expected Cloudflare token length: 40 chars") + + if len(config.cloudflare_api_token) < 40: + print(f" โ Cloudflare token appears to be truncated") + print(f" - Current token: {config.cloudflare_api_token}") + print(f" - Please check if the token is complete in .env file") + +if __name__ == "__main__": + test_api_connectivity()
\ No newline at end of file diff --git a/tests/test_namecheap_api.py b/tests/test_namecheap_api.py new file mode 100755 index 0000000..c1df4c5 --- /dev/null +++ b/tests/test_namecheap_api.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 + +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from config import Config +from namecheap_api import NamecheapAPI + +def test_namecheap_api(): + """Test Namecheap API integration with non-mutating calls""" + print("="*50) + print("NAMECHEAP API INTEGRATION TESTS") + print("="*50) + + # Load configuration + config = Config.from_env() + + # Check if API credentials are available + if not all([ + config.namecheap_api_key, + config.namecheap_api_user, + config.namecheap_username, + config.namecheap_client_ip + ]): + print("โ Namecheap API credentials not found. Please set them in .env file.") + return False + + print(f"โ API credentials loaded") + print(f" - API User: {config.namecheap_api_user}") + print(f" - Username: {config.namecheap_username}") + print(f" - Client IP: {config.namecheap_client_ip}") + print(f" - API Key: {'*' * len(config.namecheap_api_key)}") + + # Initialize API + api = NamecheapAPI(config) + + # Test 1: Check domain availability (non-mutating) + print(f"\n1. Testing domain availability check...") + try: + test_domain = "test-domain-12345.com" + is_available = api.check_domain_availability(test_domain) + print(f" โ Domain availability check successful") + print(f" - {test_domain} is {'available' if is_available else 'not available'}") + except Exception as e: + print(f" โ Domain availability check failed: {e}") + return False + + # Test 2: Get account balance (non-mutating) + print(f"\n2. Testing account balance retrieval...") + try: + balance = api.get_account_balance() + print(f" โ Account balance retrieved successfully") + print(f" - Current balance: ${balance:.2f}") + except Exception as e: + print(f" โ Account balance retrieval failed: {e}") + return False + + # Test 3: Get domain pricing (non-mutating) + print(f"\n3. Testing domain pricing retrieval...") + try: + test_domain = "example.com" + pricing = api.get_domain_pricing(test_domain) + print(f" โ Domain pricing retrieved successfully") + print(f" - .com registration: ${pricing['register']:.2f}") + print(f" - .com renewal: ${pricing['renew']:.2f}") + except Exception as e: + print(f" โ Domain pricing retrieval failed: {e}") + return False + + # Test 4: Test with different TLD + print(f"\n4. Testing pricing for different TLD...") + try: + test_domain = "example.xyz" + pricing = api.get_domain_pricing(test_domain) + print(f" โ .xyz domain pricing retrieved successfully") + print(f" - .xyz registration: ${pricing['register']:.2f}") + print(f" - .xyz renewal: ${pricing['renew']:.2f}") + except Exception as e: + print(f" โ .xyz domain pricing retrieval failed: {e}") + return False + + print(f"\nโ
All Namecheap API tests passed!") + return True + +if __name__ == "__main__": + success = test_namecheap_api() + sys.exit(0 if success else 1)
\ No newline at end of file |
