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
|
""" certificates tests """
import os
import sys
import random
sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare
# test /centificates - a weird call (will fail if you don't have a certtoken)
cf = None
def test_cloudflare(debug=False):
""" test_cloudflare """
global cf
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)
zone_name = None
zone_id = None
def test_find_zone(domain_name=None):
""" test_find_zone """
global zone_name, zone_id
# grab a random zone identifier from the first 10 zones
if domain_name:
params = {'per_page':1, 'name':domain_name}
else:
params = {'per_page':10}
try:
zones = cf.zones.get(params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
print('%s: Error %d=%s' % (domain_name, int(e), str(e)), file=sys.stderr)
assert False
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
zone_id = zones[n]['id']
assert len(zone_id) == 32
print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr)
def test_certificates():
""" test_certificates """
params = {'zone_id':zone_id}
try:
certificates = cf.certificates(params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
print('%s: Error %d=%s - can not run this test on this domain - no worries - skipping' % (zone_name, int(e), str(e)), file=sys.stderr)
return
assert isinstance(certificates, list)
if len(certificates) == 0:
# no cert's for this domain - which is the norm
print('no cert(s) returned', file=sys.stderr)
return
for c in certificates:
assert isinstance(c, dict)
assert 'id' in c
assert 'expires_on' in c
assert 'hostnames' in c
assert 'certificate' in c
print('%s: %48s %29s %s' % (zone_name, c['id'], c['expires_on'], c['hostnames']), file=sys.stderr)
if __name__ == '__main__':
test_cloudflare(debug=True)
if len(sys.argv) > 1:
test_find_zone(sys.argv[1])
else:
test_find_zone()
test_certificates()
|