Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
all tests now similar structure, callable from command line, debug on…
Browse files Browse the repository at this point in the history
… by default for command line calls
  • Loading branch information
mahtin committed Dec 31, 2023
1 parent b2c3eaa commit 5558e19
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 64 deletions.
25 changes: 16 additions & 9 deletions tests/test_add.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" dump api tests """
""" add to api tests """

import os
import sys
Expand All @@ -11,46 +11,53 @@

cf = None

def test_cloudflare():
def test_cloudflare(debug=False):
global cf
cf = CloudFlare.CloudFlare()
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_app_invalid():
"""add API commands"""
cf.add('OPEN', 'invalid')
try:
results = cf.invalid()
print('error - should not reach here')
print('error - should not reach here', file=sys.stderr)
assert False
except CloudFlare.exceptions.CloudFlareAPIError as e:
# error 7000 No route for that URI
assert int(e) == 7000
assert str(e) == 'No route for that URI'
print(int(e), str(e))
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)

def test_app_invalid_with_underscore():
"""add API commands"""
cf.add('OPEN', 'in_valid')
try:
results = cf.in_valid()
print('error - should not reach here')
print('error - should not reach here', file=sys.stderr)
assert False
except CloudFlare.exceptions.CloudFlareAPIError as e:
# error 7000 No route for that URI
assert int(e) == 7000
assert str(e) == 'No route for that URI'
print(int(e), str(e))
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)

def test_app_invalid_with_dash():
"""add API commands"""
cf.add('OPEN', 'in-val-id')
try:
results = cf.in_val_id()
print('error - should not reach here')
print('error - should not reach here', file=sys.stderr)
assert False
except CloudFlare.exceptions.CloudFlareAPIError as e:
# error 7000 No route for that URI
assert int(e) == 7000
assert str(e) == 'No route for that URI'
print(int(e), str(e))
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)

if __name__ == '__main__':
test_cloudflare(debug=True)
test_app_invalid()
test_app_invalid_with_underscore()
test_app_invalid_with_dash()

12 changes: 9 additions & 3 deletions tests/test_api_dump.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
""" dump api tests """
""" test dump calls """

import os
import sys

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

Expand All @@ -12,9 +13,9 @@

OPENAPI_URL = "https://github.com/cloudflare/api-schemas/raw/main/openapi.json"

def test_cloudflare():
def test_cloudflare(debug=False):
global cf
cf = CloudFlare.CloudFlare()
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_dump_commands():
Expand All @@ -26,3 +27,8 @@ def test_dump_commands_from_web():
"""dump a tree of all the known API commands - from web"""
w = cf.api_from_openapi(OPENAPI_URL)
assert len(w) > 0

if __name__ == '__main__':
test_cloudflare(debug=True)
test_dump_commands()
test_dump_commands_from_web()
14 changes: 9 additions & 5 deletions tests/test_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

cf = None

def test_cloudflare():
def test_cloudflare(debug=False):
global cf
cf = CloudFlare.CloudFlare()
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)

zone_name = None
Expand All @@ -24,10 +24,14 @@ def test_find_zone(domain_name=None):
global zone_name, zone_id
# grab a random zone identifier from the first 10 zones
if domain_name:
params = {'per_page':10, 'name':domain_name}
params = {'per_page':1, 'name':domain_name}
else:
params = {'per_page':10}
zones = cf.zones.get(params=params)
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)
exit(0)
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
Expand Down Expand Up @@ -57,7 +61,7 @@ def test_certificates():
print('%s: %48s %29s %s' % (zone_name, c['id'], c['expires_on'], c['hostnames']), file=sys.stderr)

if __name__ == '__main__':
test_cloudflare()
test_cloudflare(debug=True)
if len(sys.argv) > 1:
test_find_zone(sys.argv[1])
else:
Expand Down
63 changes: 52 additions & 11 deletions tests/test_cloudflare.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
""" test global_request_timeout and max_request_retries """

import os
import sys
sys.path.insert(0, os.path.abspath('..'))

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

class TestCloudflare:
def test_creating_default_client(self):
cf = CloudFlare.CloudFlare()
assert isinstance(cf, CloudFlare.CloudFlare)
global cf

def test_cloudflare(debug=False):
global cf
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_ips1():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0

def test_cloudflare_with_global_request_timeout(debug=False):
global cf
cf = CloudFlare.CloudFlare(debug=debug, global_request_timeout=10)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_ips2():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0

def test_cloudflare_with_max_request_retries(debug=False):
global cf
cf = CloudFlare.CloudFlare(debug=debug, max_request_retries=2)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_ips3():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0

def test_cloudflare_with_global_request_timeout_and__max_request_retries(debug=False):
global cf
cf = CloudFlare.CloudFlare(debug=debug, global_request_timeout=10, max_request_retries=2)
assert isinstance(cf, CloudFlare.CloudFlare)

def test_with_global_request_timeout(self):
cf = CloudFlare.CloudFlare({'global_request_timeout': 10})
assert isinstance(cf, CloudFlare.CloudFlare)
def test_ips4():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0

def test_with_max_request_retries(self):
cf = CloudFlare.CloudFlare({'max_request_retries': 2})
assert isinstance(cf, CloudFlare.CloudFlare)
if __name__ == '__main__':
test_cloudflare(debug=True)
test_ips1()
test_cloudflare_with_global_request_timeout(debug=True)
test_ips2()
test_cloudflare_with_max_request_retries(debug=True)
test_ips3()
test_cloudflare_with_global_request_timeout_and__max_request_retries(debug=True)
test_ips4()
16 changes: 12 additions & 4 deletions tests/test_cloudflare_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

Expand All @@ -20,7 +21,6 @@

def test_cloudflare():
global cf
cf = None
cf = CloudFlare.CloudFlare()
assert isinstance(cf, CloudFlare.CloudFlare)

Expand All @@ -31,7 +31,6 @@ def test_ips1():

def test_cloudflare_debug():
global cf
cf = None
cf = CloudFlare.CloudFlare(debug=True)
assert isinstance(cf, CloudFlare.CloudFlare)

Expand All @@ -42,7 +41,6 @@ def test_ips2():

def test_cloudflare_raw():
global cf
cf = None
cf = CloudFlare.CloudFlare(raw=False)
assert isinstance(cf, CloudFlare.CloudFlare)

Expand All @@ -53,7 +51,6 @@ def test_ips3():

def test_cloudflare_no_sessions():
global cf
cf = None
cf = CloudFlare.CloudFlare(use_sessions=False)
assert isinstance(cf, CloudFlare.CloudFlare)

Expand All @@ -66,3 +63,14 @@ def test_ips5():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0

if __name__ == '__main__':
test_cloudflare()
test_ips1()
test_cloudflare_debug()
test_ips2()
test_cloudflare_raw()
test_ips3()
test_cloudflare_no_sessions()
test_ips4()
test_ips5()
23 changes: 15 additions & 8 deletions tests/test_dns_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import random
import tempfile

fp = open('/dev/null', 'rb')
n = fp.read()

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

Expand All @@ -25,11 +23,18 @@ def test_cloudflare():
zone_name = None
zone_id = None

def test_find_zone():
def test_find_zone(domain_name=None):
global zone_name, zone_id
# grab a random zone identifier from the first 10 zones
params = {'per_page':10}
zones = cf.zones.get(params=params)
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)
exit(0)
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
Expand Down Expand Up @@ -66,7 +71,6 @@ def test_dns_export():

def test_cloudflare_with_debug():
global cf
cf = None
cf = CloudFlare.CloudFlare(debug=True)
assert isinstance(cf, CloudFlare.CloudFlare)

Expand Down Expand Up @@ -99,7 +103,10 @@ def test_dns_export_with_debug():

if __name__ == '__main__':
test_cloudflare()
test_find_zone()
if len(sys.argv) > 1:
test_find_zone(sys.argv[1])
else:
test_find_zone()
test_dns_import()
test_dns_export()
test_cloudflare_with_debug()
Expand Down
37 changes: 31 additions & 6 deletions tests/test_dns_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@
import uuid
import random

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

# test GET POST PUT PATCH & DELETE - but not in that order

cf = None

def test_cloudflare():
def test_cloudflare(debug=False):
global cf
cf = CloudFlare.CloudFlare()
cf = CloudFlare.CloudFlare(debug=debug)
assert isinstance(cf, CloudFlare.CloudFlare)

zone_name = None
zone_id = None

def test_find_zone():
def test_find_zone(domain_name=None):
global zone_name, zone_id
# grab a random zone identifier from the first 10 zones
params = {'per_page':10}
zones = cf.zones.get(params=params)
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)
exit(0)
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
Expand All @@ -38,7 +46,7 @@ def test_find_zone():
dns_content2 = None
dns_content3 = None

def test_dns_records():
def test_dns_records_create_values():
global dns_name, dns_type, dns_content1, dns_content2, dns_content3
dns_name = str(uuid.uuid1())
dns_type = 'TXT'
Expand Down Expand Up @@ -117,3 +125,20 @@ def test_dns_records_get5():
params = {'name':dns_name + '.' + zone_name, 'match':'all', 'type':dns_type}
dns_results = cf.zones.dns_records.get(zone_id, params=params)
assert len(dns_results) == 0

if __name__ == '__main__':
test_cloudflare(debug=True)
if len(sys.argv) > 1:
test_find_zone(sys.argv[1])
else:
test_find_zone()
test_dns_records_create_values()
test_dns_records_get1()
test_dns_records_post()
test_dns_records_get2()
test_dns_records_get3()
test_dns_records_patch()
test_dns_records_put()
test_dns_records_get4()
test_dns_records_delete()
test_dns_records_get5()
Loading

0 comments on commit 5558e19

Please sign in to comment.