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

Commit 5558e19

Browse files
committed
all tests now similar structure, callable from command line, debug on by default for command line calls
1 parent b2c3eaa commit 5558e19

10 files changed

Lines changed: 212 additions & 64 deletions

tests/test_add.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" dump api tests """
1+
""" add to api tests """
22

33
import os
44
import sys
@@ -11,46 +11,53 @@
1111

1212
cf = None
1313

14-
def test_cloudflare():
14+
def test_cloudflare(debug=False):
1515
global cf
16-
cf = CloudFlare.CloudFlare()
16+
cf = CloudFlare.CloudFlare(debug=debug)
1717
assert isinstance(cf, CloudFlare.CloudFlare)
1818

1919
def test_app_invalid():
2020
"""add API commands"""
2121
cf.add('OPEN', 'invalid')
2222
try:
2323
results = cf.invalid()
24-
print('error - should not reach here')
24+
print('error - should not reach here', file=sys.stderr)
2525
assert False
2626
except CloudFlare.exceptions.CloudFlareAPIError as e:
2727
# error 7000 No route for that URI
2828
assert int(e) == 7000
2929
assert str(e) == 'No route for that URI'
30-
print(int(e), str(e))
30+
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)
3131

3232
def test_app_invalid_with_underscore():
3333
"""add API commands"""
3434
cf.add('OPEN', 'in_valid')
3535
try:
3636
results = cf.in_valid()
37-
print('error - should not reach here')
37+
print('error - should not reach here', file=sys.stderr)
3838
assert False
3939
except CloudFlare.exceptions.CloudFlareAPIError as e:
4040
# error 7000 No route for that URI
4141
assert int(e) == 7000
4242
assert str(e) == 'No route for that URI'
43-
print(int(e), str(e))
43+
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)
4444

4545
def test_app_invalid_with_dash():
4646
"""add API commands"""
4747
cf.add('OPEN', 'in-val-id')
4848
try:
4949
results = cf.in_val_id()
50-
print('error - should not reach here')
50+
print('error - should not reach here', file=sys.stderr)
5151
assert False
5252
except CloudFlare.exceptions.CloudFlareAPIError as e:
5353
# error 7000 No route for that URI
5454
assert int(e) == 7000
5555
assert str(e) == 'No route for that URI'
56-
print(int(e), str(e))
56+
print('error: %d=%s' % (int(e), str(e)), file=sys.stderr)
57+
58+
if __name__ == '__main__':
59+
test_cloudflare(debug=True)
60+
test_app_invalid()
61+
test_app_invalid_with_underscore()
62+
test_app_invalid_with_dash()
63+

tests/test_api_dump.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
""" dump api tests """
1+
""" test dump calls """
22

33
import os
44
import sys
55

6+
sys.path.insert(0, os.path.abspath('.'))
67
sys.path.insert(0, os.path.abspath('..'))
78
import CloudFlare
89

@@ -12,9 +13,9 @@
1213

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

15-
def test_cloudflare():
16+
def test_cloudflare(debug=False):
1617
global cf
17-
cf = CloudFlare.CloudFlare()
18+
cf = CloudFlare.CloudFlare(debug=debug)
1819
assert isinstance(cf, CloudFlare.CloudFlare)
1920

2021
def test_dump_commands():
@@ -26,3 +27,8 @@ def test_dump_commands_from_web():
2627
"""dump a tree of all the known API commands - from web"""
2728
w = cf.api_from_openapi(OPENAPI_URL)
2829
assert len(w) > 0
30+
31+
if __name__ == '__main__':
32+
test_cloudflare(debug=True)
33+
test_dump_commands()
34+
test_dump_commands_from_web()

tests/test_certificates.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
cf = None
1414

15-
def test_cloudflare():
15+
def test_cloudflare(debug=False):
1616
global cf
17-
cf = CloudFlare.CloudFlare()
17+
cf = CloudFlare.CloudFlare(debug=debug)
1818
assert isinstance(cf, CloudFlare.CloudFlare)
1919

2020
zone_name = None
@@ -24,10 +24,14 @@ def test_find_zone(domain_name=None):
2424
global zone_name, zone_id
2525
# grab a random zone identifier from the first 10 zones
2626
if domain_name:
27-
params = {'per_page':10, 'name':domain_name}
27+
params = {'per_page':1, 'name':domain_name}
2828
else:
2929
params = {'per_page':10}
30-
zones = cf.zones.get(params=params)
30+
try:
31+
zones = cf.zones.get(params=params)
32+
except CloudFlare.exceptions.CloudFlareAPIError as e:
33+
print('%s: Error %d=%s' % (domain_name, int(e), str(e)), file=sys.stderr)
34+
exit(0)
3135
assert len(zones) > 0 and len(zones) <= 10
3236
n = random.randrange(len(zones))
3337
zone_name = zones[n]['name']
@@ -57,7 +61,7 @@ def test_certificates():
5761
print('%s: %48s %29s %s' % (zone_name, c['id'], c['expires_on'], c['hostnames']), file=sys.stderr)
5862

5963
if __name__ == '__main__':
60-
test_cloudflare()
64+
test_cloudflare(debug=True)
6165
if len(sys.argv) > 1:
6266
test_find_zone(sys.argv[1])
6367
else:

tests/test_cloudflare.py

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,60 @@
1+
""" test global_request_timeout and max_request_retries """
2+
13
import os
24
import sys
3-
sys.path.insert(0, os.path.abspath('..'))
45

6+
sys.path.insert(0, os.path.abspath('.'))
7+
sys.path.insert(0, os.path.abspath('..'))
58
import CloudFlare
69

7-
class TestCloudflare:
8-
def test_creating_default_client(self):
9-
cf = CloudFlare.CloudFlare()
10-
assert isinstance(cf, CloudFlare.CloudFlare)
10+
global cf
11+
12+
def test_cloudflare(debug=False):
13+
global cf
14+
cf = CloudFlare.CloudFlare(debug=debug)
15+
assert isinstance(cf, CloudFlare.CloudFlare)
16+
17+
def test_ips1():
18+
ips = cf.ips()
19+
assert isinstance(ips, dict)
20+
assert len(ips) > 0
21+
22+
def test_cloudflare_with_global_request_timeout(debug=False):
23+
global cf
24+
cf = CloudFlare.CloudFlare(debug=debug, global_request_timeout=10)
25+
assert isinstance(cf, CloudFlare.CloudFlare)
26+
27+
def test_ips2():
28+
ips = cf.ips()
29+
assert isinstance(ips, dict)
30+
assert len(ips) > 0
31+
32+
def test_cloudflare_with_max_request_retries(debug=False):
33+
global cf
34+
cf = CloudFlare.CloudFlare(debug=debug, max_request_retries=2)
35+
assert isinstance(cf, CloudFlare.CloudFlare)
36+
37+
def test_ips3():
38+
ips = cf.ips()
39+
assert isinstance(ips, dict)
40+
assert len(ips) > 0
1141

42+
def test_cloudflare_with_global_request_timeout_and__max_request_retries(debug=False):
43+
global cf
44+
cf = CloudFlare.CloudFlare(debug=debug, global_request_timeout=10, max_request_retries=2)
45+
assert isinstance(cf, CloudFlare.CloudFlare)
1246

13-
def test_with_global_request_timeout(self):
14-
cf = CloudFlare.CloudFlare({'global_request_timeout': 10})
15-
assert isinstance(cf, CloudFlare.CloudFlare)
47+
def test_ips4():
48+
ips = cf.ips()
49+
assert isinstance(ips, dict)
50+
assert len(ips) > 0
1651

17-
def test_with_max_request_retries(self):
18-
cf = CloudFlare.CloudFlare({'max_request_retries': 2})
19-
assert isinstance(cf, CloudFlare.CloudFlare)
52+
if __name__ == '__main__':
53+
test_cloudflare(debug=True)
54+
test_ips1()
55+
test_cloudflare_with_global_request_timeout(debug=True)
56+
test_ips2()
57+
test_cloudflare_with_max_request_retries(debug=True)
58+
test_ips3()
59+
test_cloudflare_with_global_request_timeout_and__max_request_retries(debug=True)
60+
test_ips4()

tests/test_cloudflare_calls.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55

6+
sys.path.insert(0, os.path.abspath('.'))
67
sys.path.insert(0, os.path.abspath('..'))
78
import CloudFlare
89

@@ -20,7 +21,6 @@
2021

2122
def test_cloudflare():
2223
global cf
23-
cf = None
2424
cf = CloudFlare.CloudFlare()
2525
assert isinstance(cf, CloudFlare.CloudFlare)
2626

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

3232
def test_cloudflare_debug():
3333
global cf
34-
cf = None
3534
cf = CloudFlare.CloudFlare(debug=True)
3635
assert isinstance(cf, CloudFlare.CloudFlare)
3736

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

4342
def test_cloudflare_raw():
4443
global cf
45-
cf = None
4644
cf = CloudFlare.CloudFlare(raw=False)
4745
assert isinstance(cf, CloudFlare.CloudFlare)
4846

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

5452
def test_cloudflare_no_sessions():
5553
global cf
56-
cf = None
5754
cf = CloudFlare.CloudFlare(use_sessions=False)
5855
assert isinstance(cf, CloudFlare.CloudFlare)
5956

@@ -66,3 +63,14 @@ def test_ips5():
6663
ips = cf.ips()
6764
assert isinstance(ips, dict)
6865
assert len(ips) > 0
66+
67+
if __name__ == '__main__':
68+
test_cloudflare()
69+
test_ips1()
70+
test_cloudflare_debug()
71+
test_ips2()
72+
test_cloudflare_raw()
73+
test_ips3()
74+
test_cloudflare_no_sessions()
75+
test_ips4()
76+
test_ips5()

tests/test_dns_import_export.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import random
88
import tempfile
99

10-
fp = open('/dev/null', 'rb')
11-
n = fp.read()
12-
10+
sys.path.insert(0, os.path.abspath('.'))
1311
sys.path.insert(0, os.path.abspath('..'))
1412
import CloudFlare
1513

@@ -25,11 +23,18 @@ def test_cloudflare():
2523
zone_name = None
2624
zone_id = None
2725

28-
def test_find_zone():
26+
def test_find_zone(domain_name=None):
2927
global zone_name, zone_id
3028
# grab a random zone identifier from the first 10 zones
31-
params = {'per_page':10}
32-
zones = cf.zones.get(params=params)
29+
if domain_name:
30+
params = {'per_page':1, 'name':domain_name}
31+
else:
32+
params = {'per_page':10}
33+
try:
34+
zones = cf.zones.get(params=params)
35+
except CloudFlare.exceptions.CloudFlareAPIError as e:
36+
print('%s: Error %d=%s' % (domain_name, int(e), str(e)), file=sys.stderr)
37+
exit(0)
3338
assert len(zones) > 0 and len(zones) <= 10
3439
n = random.randrange(len(zones))
3540
zone_name = zones[n]['name']
@@ -66,7 +71,6 @@ def test_dns_export():
6671

6772
def test_cloudflare_with_debug():
6873
global cf
69-
cf = None
7074
cf = CloudFlare.CloudFlare(debug=True)
7175
assert isinstance(cf, CloudFlare.CloudFlare)
7276

@@ -99,7 +103,10 @@ def test_dns_export_with_debug():
99103

100104
if __name__ == '__main__':
101105
test_cloudflare()
102-
test_find_zone()
106+
if len(sys.argv) > 1:
107+
test_find_zone(sys.argv[1])
108+
else:
109+
test_find_zone()
103110
test_dns_import()
104111
test_dns_export()
105112
test_cloudflare_with_debug()

tests/test_dns_records.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
import uuid
66
import random
77

8+
sys.path.insert(0, os.path.abspath('.'))
89
sys.path.insert(0, os.path.abspath('..'))
910
import CloudFlare
1011

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

1314
cf = None
1415

15-
def test_cloudflare():
16+
def test_cloudflare(debug=False):
1617
global cf
17-
cf = CloudFlare.CloudFlare()
18+
cf = CloudFlare.CloudFlare(debug=debug)
1819
assert isinstance(cf, CloudFlare.CloudFlare)
1920

2021
zone_name = None
2122
zone_id = None
2223

23-
def test_find_zone():
24+
def test_find_zone(domain_name=None):
2425
global zone_name, zone_id
2526
# grab a random zone identifier from the first 10 zones
26-
params = {'per_page':10}
27-
zones = cf.zones.get(params=params)
27+
if domain_name:
28+
params = {'per_page':1, 'name':domain_name}
29+
else:
30+
params = {'per_page':10}
31+
try:
32+
zones = cf.zones.get(params=params)
33+
except CloudFlare.exceptions.CloudFlareAPIError as e:
34+
print('%s: Error %d=%s' % (domain_name, int(e), str(e)), file=sys.stderr)
35+
exit(0)
2836
assert len(zones) > 0 and len(zones) <= 10
2937
n = random.randrange(len(zones))
3038
zone_name = zones[n]['name']
@@ -38,7 +46,7 @@ def test_find_zone():
3846
dns_content2 = None
3947
dns_content3 = None
4048

41-
def test_dns_records():
49+
def test_dns_records_create_values():
4250
global dns_name, dns_type, dns_content1, dns_content2, dns_content3
4351
dns_name = str(uuid.uuid1())
4452
dns_type = 'TXT'
@@ -117,3 +125,20 @@ def test_dns_records_get5():
117125
params = {'name':dns_name + '.' + zone_name, 'match':'all', 'type':dns_type}
118126
dns_results = cf.zones.dns_records.get(zone_id, params=params)
119127
assert len(dns_results) == 0
128+
129+
if __name__ == '__main__':
130+
test_cloudflare(debug=True)
131+
if len(sys.argv) > 1:
132+
test_find_zone(sys.argv[1])
133+
else:
134+
test_find_zone()
135+
test_dns_records_create_values()
136+
test_dns_records_get1()
137+
test_dns_records_post()
138+
test_dns_records_get2()
139+
test_dns_records_get3()
140+
test_dns_records_patch()
141+
test_dns_records_put()
142+
test_dns_records_get4()
143+
test_dns_records_delete()
144+
test_dns_records_get5()

0 commit comments

Comments
 (0)