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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
import pytest
from namecheap import Api, ApiError
api_key = '' # You create this on Namecheap site
username = ''
ip_address = '' # Your IP address that you whitelisted on the site
# If you prefer, you can put the above in credentials.py instead
try:
from credentials import api_key, username, ip_address
except:
pass
def random_domain_name():
import random
from time import gmtime, strftime
domain_name = "%s-%s.com" % (strftime("%Y%m%d-%H%M%S", gmtime()), random.randint(0, 10**16))
return domain_name
def test_domain_taken():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = "google.com"
assert api.domains_check(domain_name) == False
def test_domain_available():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = random_domain_name()
assert api.domains_check(domain_name)
def test_register_domain():
api = Api(username, api_key, username, ip_address, sandbox=True)
# Try registering a random domain. Fails if exception raised.
domain_name = random_domain_name()
api.domains_create(
DomainName=domain_name,
FirstName='Jack',
LastName='Trotter',
Address1='Ridiculously Big Mansion, Yellow Brick Road',
City='Tokushima',
StateProvince='Tokushima',
PostalCode='771-0144',
Country='Japan',
Phone='+81.123123123',
EmailAddress='jack.trotter@example.com'
)
return domain_name
def test_domains_getList():
api = Api(username, api_key, username, ip_address, sandbox=True)
iter(api.domains_getList())
def test_domains_dns_setDefault_on_nonexisting_domain():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = random_domain_name()
# This should fail because the domain does not exist
with pytest.raises(ApiError):
api.domains_dns_setDefault(domain_name)
def test_domains_dns_setDefault_on_existing_domain():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setDefault(domain_name)
def test_domains_getContacts():
# How would I test for this? This needs a known registered
# domain to get the contact info for, but in sandbox won't
# have any.
pass
def test_domains_dns_setHosts():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'URL',
'Address': 'http://news.ycombinator.com',
'MXPref': '10',
'TTL': '100'
}]
)
#
# I wasn't able to get this to work on any public name servers that I tried
# including the ones used in their own example:
# dns1.name-servers.com
# dns2.name-server.com
# Using my own Amazon Route53 name servers the test works fine but I didn't
# want to embed my own servers
# Adjust the name servers below to your own and uncomment the test to run
# def test_domains_dns_setCustom():
# api = Api(username, api_key, username, ip_address, sandbox=True)
# domain_name = test_register_domain()
# result = api.domains_dns_setCustom(
# domain_name, { 'Nameservers' : 'ns1.google.com,ns2.google.com' }
# )
def test_domains_dns_getHosts():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'URL',
'Address': 'http://news.ycombinator.com',
'MXPref': '10',
'TTL': '100'
}, {
'HostName': '*',
'RecordType': 'A',
'Address': '1.2.3.4',
'MXPref': '10',
'TTL': '1800'
}]
)
hosts = api.domains_dns_getHosts(domain_name)
# these might change
del hosts[0]['HostId']
del hosts[1]['HostId']
expected_result = [
{
'Name': '*',
'Address': '1.2.3.4',
'TTL': '1800',
'Type': 'A',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}, {
'Name': '@',
'Address': 'http://news.ycombinator.com',
'TTL': '100',
'Type': 'URL',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}
]
assert hosts == expected_result
def test_domains_dns_addHost():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'URL',
'Address': 'http://news.ycombinator.com'
}]
)
api.domains_dns_addHost(
domain_name,
{
'Name': 'test',
'Type': 'A',
'Address': '1.2.3.4',
'TTL': '100'
}
)
hosts = api.domains_dns_getHosts(domain_name)
# these might change
del hosts[0]['HostId']
del hosts[1]['HostId']
expected_result = [
{
'Name': 'test',
'Address': '1.2.3.4',
'TTL': '100',
'Type': 'A',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}, {
'Name': '@',
'Address': 'http://news.ycombinator.com',
'TTL': '1800',
'Type': 'URL',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}
]
assert hosts == expected_result
def test_domains_dns_bulkAddHosts():
api = Api(username, api_key, username, ip_address, sandbox=True)
api.payload_limit = 3
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'URL',
'Address': 'http://news.ycombinator.com'
}]
)
for i in range(1, 10):
api.domains_dns_addHost(
domain_name,
{'Name': "test" + str(i), 'Type': 'A', 'Address': '1.2.3.4', 'TTL': '60'}
)
hosts = api.domains_dns_getHosts(domain_name)
if len(hosts) == 10:
return True
return False
def test_domains_dns_delHost():
api = Api(username, api_key, username, ip_address, sandbox=True)
domain_name = test_register_domain()
api.domains_dns_setHosts(
domain_name,
[{
'HostName': '@',
'RecordType': 'URL',
'Address': 'http://news.ycombinator.com',
'TTL': '200'
}, {
'HostName': 'test',
'RecordType': 'A',
'Address': '1.2.3.4'
}]
)
api.domains_dns_delHost(
domain_name,
{
'Name': 'test',
'Type': 'A',
'Address': '1.2.3.4'
}
)
hosts = api.domains_dns_getHosts(domain_name)
# these might change
del hosts[0]['HostId']
expected_result = [
{
'Name': '@',
'Address': 'http://news.ycombinator.com',
'TTL': '200',
'Type': 'URL',
'MXPref': '10',
'AssociatedAppTitle': '',
'FriendlyName': '',
'IsActive': 'true',
'IsDDNSEnabled': 'false'
}
]
assert hosts == expected_result
def test_list_of_dictionaries_to_numbered_payload():
x = [
{'foo': 'bar', 'cat': 'purr'},
{'foo': 'buz'},
{'cat': 'meow'}
]
result = Api._list_of_dictionaries_to_numbered_payload(x)
expected_result = {
'foo1': 'bar',
'cat1': 'purr',
'foo2': 'buz',
'cat3': 'meow'
}
assert result == expected_result
|