|
| 1 | +""" certificates tests """ |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import random |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath('.')) |
| 8 | +sys.path.insert(0, os.path.abspath('..')) |
| 9 | +import CloudFlare |
| 10 | + |
| 11 | +# test /centificates - a weird call (will fail if you don't have a certtoken) |
| 12 | + |
| 13 | +cf = None |
| 14 | + |
| 15 | +def test_cloudflare(): |
| 16 | + global cf |
| 17 | + cf = CloudFlare.CloudFlare() |
| 18 | + assert isinstance(cf, CloudFlare.CloudFlare) |
| 19 | + |
| 20 | +zone_name = None |
| 21 | +zone_id = None |
| 22 | + |
| 23 | +def test_find_zone(domain_name=None): |
| 24 | + global zone_name, zone_id |
| 25 | + # grab a random zone identifier from the first 10 zones |
| 26 | + if domain_name: |
| 27 | + params = {'per_page':10, 'name':domain_name} |
| 28 | + else: |
| 29 | + params = {'per_page':10} |
| 30 | + zones = cf.zones.get(params=params) |
| 31 | + assert len(zones) > 0 and len(zones) <= 10 |
| 32 | + n = random.randrange(len(zones)) |
| 33 | + zone_name = zones[n]['name'] |
| 34 | + zone_id = zones[n]['id'] |
| 35 | + assert len(zone_id) == 32 |
| 36 | + print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr) |
| 37 | + |
| 38 | +def test_certificates(): |
| 39 | + params = {'zone_id':zone_id} |
| 40 | + try: |
| 41 | + certificates = cf.certificates(params=params) |
| 42 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 43 | + 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) |
| 44 | + exit(0) |
| 45 | + |
| 46 | + assert isinstance(certificates, list) |
| 47 | + if len(certificates) == 0: |
| 48 | + # no cert's for this domain - which is the norm |
| 49 | + print('no cert(s) returned', file=sys.stderr) |
| 50 | + return |
| 51 | + for c in certificates: |
| 52 | + assert isinstance(c, dict) |
| 53 | + assert 'id' in c |
| 54 | + assert 'expires_on' in c |
| 55 | + assert 'hostnames' in c |
| 56 | + assert 'certificate' in c |
| 57 | + print('%s: %48s %29s %s' % (zone_name, c['id'], c['expires_on'], c['hostnames']), file=sys.stderr) |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + test_cloudflare() |
| 61 | + if len(sys.argv) > 1: |
| 62 | + test_find_zone(sys.argv[1]) |
| 63 | + else: |
| 64 | + test_find_zone() |
| 65 | + test_certificates() |
0 commit comments