-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_script.py
More file actions
152 lines (137 loc) · 5.58 KB
/
Copy pathtest_script.py
File metadata and controls
152 lines (137 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import unittest
from script import app, check_whois, check_dns
import json
import os
class TestDomainChecker(unittest.TestCase):
def setUp(self):
"""Set up test environment"""
# Ensure rate limiting is disabled for tests
os.environ['ENABLE_RATE_LIMITS'] = 'false'
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
"""Clean up after tests"""
# Clean up environment variables
if 'ENABLE_RATE_LIMITS' in os.environ:
del os.environ['ENABLE_RATE_LIMITS']
def test_health_check(self):
"""Test health check endpoint"""
response = self.app.get('/health')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertEqual(data['status'], 'healthy')
def test_metrics(self):
"""Test metrics endpoint"""
response = self.app.get('/metrics')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertIn('cache_stats', data)
self.assertIn('uptime', data)
def test_known_domains(self):
"""Test checking known domains (google.com and facebook.com)"""
# Test google.com
response = self.app.get('/?domain=google')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertEqual(data['status'], 'taken')
self.assertEqual(data['domain'], 'google.com')
self.assertEqual(data['whois']['status'], 'taken')
self.assertEqual(data['dns']['status'], 'taken')
# Test facebook.com
response = self.app.get('/?domain=facebook')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertEqual(data['status'], 'taken')
self.assertEqual(data['domain'], 'facebook.com')
self.assertEqual(data['whois']['status'], 'taken')
self.assertEqual(data['dns']['status'], 'taken')
def test_invalid_domain(self):
"""Test invalid domain input"""
invalid_domains = [
'inv@lid',
'-invalid',
'invalid-',
'inv--alid',
'a' * 64, # Too long
'', # Empty
' ', # Just whitespace
'domain.with.dots'
]
for domain in invalid_domains:
response = self.app.get(f'/?domain={domain}')
self.assertEqual(response.status_code, 400,
f"Expected 400 for domain: {domain}")
data = json.loads(response.data)
self.assertIn('error', data)
def test_invalid_tld(self):
"""Test invalid TLD input"""
invalid_tlds = [
'123', # Numbers not allowed
'a', # Too short
'com@', # Invalid characters
'', # Empty
' ', # Just whitespace
'com.', # Trailing dot
'co.uk' # Compound TLD not supported
]
for tld in invalid_tlds:
response = self.app.get(f'/?domain=example&tld={tld}')
self.assertEqual(response.status_code, 400,
f"Expected 400 for TLD: {tld}")
data = json.loads(response.data)
response = self.app.get('/?domain=inv@lid')
self.assertEqual(response.status_code, 400)
data = json.loads(response.data)
self.assertIn('error', data)
def test_missing_domain(self):
"""Test missing domain parameter"""
response = self.app.get('/')
self.assertEqual(response.status_code, 400)
data = json.loads(response.data)
self.assertIn('error', data)
def test_bulk_check(self):
"""Test bulk domain checking"""
domains = {
"domains": [
{"domain": "google", "tld": "com"},
{"domain": "facebook", "tld": "com"}
]
}
response = self.app.post('/bulk',
json=domains,
content_type='application/json')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertIn('results', data)
self.assertIn('google.com', data['results'])
self.assertIn('facebook.com', data['results'])
self.assertEqual(data['results']['google.com']['status'], 'taken')
self.assertEqual(data['results']['facebook.com']['status'], 'taken')
def test_custom_tld(self):
"""Test custom TLD parameter"""
response = self.app.get('/?domain=google&tld=org')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertEqual(data['domain'], 'google.org')
self.assertEqual(data['tld'], 'org')
def test_bulk_check_with_errors(self):
"""Test bulk domain checking with invalid entries"""
domains = {
"domains": [
{"domain": "google", "tld": "com"},
{"domain": "inv@lid", "tld": "com"},
{"domain": "example", "tld": "123"},
{"domain": "", "tld": "com"}
]
}
response = self.app.post('/bulk',
json=domains,
content_type='application/json')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertIn('results', data)
self.assertIn('errors', data)
self.assertIn('google.com', data['results'])
self.assertEqual(len(data['errors']), 3) # Should have 3 error messages
if __name__ == '__main__':
unittest.main()