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

Commit 945afcc

Browse files
committed
more test cases and moved pytest to pytest with coverage
1 parent 08da722 commit 945afcc

5 files changed

Lines changed: 260 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ install: build
3737
sudo rm -rf ${NAME}.egg-info
3838

3939
test: all
40-
$(PYTEST) -vv
40+
if pip show pytest-cov > /dev/null; then $(PYTEST) --cov=CloudFlare ; else $(PYTEST) -vv ; fi
4141

4242
cli4test: all
4343
$(PYTHON) -m cli4 /ips > /dev/null

tests/test_api_dump.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
""" dump api tests """
2+
3+
import os
4+
import sys
5+
6+
sys.path.insert(0, os.path.abspath('..'))
7+
import CloudFlare
8+
9+
# test API list fetches from Cloudflare website
10+
11+
cf = None
12+
13+
OPENAPI_URL = "https://github.com/cloudflare/api-schemas/raw/main/openapi.json"
14+
15+
def test_cloudflare():
16+
global cf
17+
cf = CloudFlare.CloudFlare()
18+
assert isinstance(cf, CloudFlare.CloudFlare)
19+
20+
def test_dump_commands():
21+
"""dump a tree of all the known API commands"""
22+
w = cf.api_list()
23+
assert len(w) > 0
24+
25+
def test_dump_commands_from_web():
26+
"""dump a tree of all the known API commands - from web"""
27+
w = cf.api_from_openapi(OPENAPI_URL)
28+
assert len(w) > 0

tests/test_cloudflare_calls.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
""" class calling tests """
2+
3+
import os
4+
import sys
5+
6+
sys.path.insert(0, os.path.abspath('..'))
7+
import CloudFlare
8+
9+
# test Cloudflare init param (ie. debug, raw, etc)
10+
#
11+
# cf = CloudFlare.CloudFlare(
12+
# email=None, key=None, token=None, certtoken=None,
13+
# debug=False, raw=False, use_sessions=True,
14+
# profile=None,
15+
# base_url=None,
16+
# global_request_timeout=None, max_request_retries=None
17+
# )
18+
19+
cf = None
20+
21+
def test_cloudflare():
22+
global cf
23+
cf = CloudFlare.CloudFlare()
24+
assert isinstance(cf, CloudFlare.CloudFlare)
25+
26+
def test_ips():
27+
ips = cf.ips()
28+
assert isinstance(ips, dict)
29+
assert len(ips) > 0
30+
31+
def test_cloudflare_debug():
32+
global cf
33+
cf = CloudFlare.CloudFlare(debug=True)
34+
assert isinstance(cf, CloudFlare.CloudFlare)
35+
36+
def test_ips():
37+
ips = cf.ips()
38+
assert isinstance(ips, dict)
39+
assert len(ips) > 0
40+
41+
def test_cloudflare_raw():
42+
global cf
43+
cf = CloudFlare.CloudFlare(raw=False)
44+
assert isinstance(cf, CloudFlare.CloudFlare)
45+
46+
def test_ips():
47+
ips = cf.ips()
48+
assert isinstance(ips, dict)
49+
assert len(ips) > 0
50+
51+
def test_cloudflare_no_sessions():
52+
global cf
53+
cf = CloudFlare.CloudFlare(use_sessions=False)
54+
assert isinstance(cf, CloudFlare.CloudFlare)
55+
56+
def test_ips():
57+
ips = cf.ips()
58+
assert isinstance(ips, dict)
59+
assert len(ips) > 0
60+
61+
def test_ips():
62+
ips = cf.ips()
63+
assert isinstance(ips, dict)
64+
assert len(ips) > 0

tests/test_dns_import_export.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
""" dns import/export test """
2+
3+
import os
4+
import sys
5+
import uuid
6+
7+
sys.path.insert(0, os.path.abspath('..'))
8+
import CloudFlare
9+
10+
# test IMPORT EXPORT
11+
12+
cf = None
13+
zone_name = None
14+
zone_id = None
15+
16+
def test_cloudflare():
17+
global cf
18+
cf = CloudFlare.CloudFlare()
19+
assert isinstance(cf, CloudFlare.CloudFlare)
20+
21+
dns_name = None
22+
dns_id = None
23+
24+
def test_find_zone():
25+
global zone_name, zone_id
26+
# grab the first zone identifier
27+
params = {'per_page':1}
28+
zones = cf.zones.get(params=params)
29+
assert len(zones) == 1
30+
zone_name = zones[0]['name']
31+
zone_id = zones[0]['id']
32+
assert len(zone_id) == 32
33+
print('zone: %s %s' % (zone_id, zone_name))
34+
35+
def test_dns_import():
36+
# IMPORT
37+
fd = open('/dev/null', 'rb')
38+
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fd})
39+
# {"recs_added": 0, "recs_added_by_type": {}, "total_records_parsed": 0}
40+
assert len(results) > 0
41+
assert results['recs_added'] == 0
42+
assert len(results['recs_added_by_type']) == 0
43+
assert results['total_records_parsed'] == 0
44+
45+
def test_dns_export():
46+
# EXPORT
47+
dns_records = cf.zones.dns_records.export.get(zone_id)
48+
assert len(dns_records) > 0
49+
assert isinstance(dns_records, str)
50+
assert 'SOA' in dns_records
51+
assert 'NS' in dns_records

tests/test_dns_records.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
""" get/post/delete/etc dns based tests """
2+
3+
import os
4+
import sys
5+
import uuid
6+
7+
sys.path.insert(0, os.path.abspath('..'))
8+
import CloudFlare
9+
10+
# test GET POST PUT PATCH & DELETE - but not in that order
11+
12+
cf = None
13+
zone_name = None
14+
zone_id = None
15+
16+
def test_cloudflare():
17+
global cf
18+
cf = CloudFlare.CloudFlare()
19+
assert isinstance(cf, CloudFlare.CloudFlare)
20+
21+
def test_find_zone():
22+
global zone_name, zone_id
23+
# grab the first zone identifier
24+
params = {'per_page':1}
25+
zones = cf.zones.get(params=params)
26+
assert len(zones) == 1
27+
zone_name = zones[0]['name']
28+
zone_id = zones[0]['id']
29+
assert len(zone_id) == 32
30+
print('zone: %s %s' % (zone_id, zone_name))
31+
32+
dns_name = None
33+
dns_type = None
34+
dns_content1 = None
35+
dns_content2 = None
36+
dns_content3 = None
37+
38+
def test_dns_records():
39+
global dns_name, dns_type, dns_content1, dns_content2, dns_content3
40+
dns_name = str(uuid.uuid1())
41+
dns_type = 'TXT'
42+
dns_content1 = 'temp pytest element 1'
43+
dns_content2 = 'temp pytest element 2'
44+
dns_content3 = 'temp pytest element 3'
45+
print('dns_record: %s' % (dns_name))
46+
47+
def test_dns_records_get():
48+
# GET
49+
params = {'name':dns_name + '.' + zone_name, 'match':'all', 'type':dns_type}
50+
dns_results = cf.zones.dns_records.get(zone_id, params=params)
51+
assert len(dns_results) == 0
52+
53+
dns_id = None
54+
55+
def test_dns_records_post():
56+
global dns_id
57+
# POST
58+
dns_record = {'name':dns_name, 'type':dns_type, 'content':dns_content1}
59+
dns_result = cf.zones.dns_records.post(zone_id, data=dns_record)
60+
assert dns_result['name'] == dns_name + '.' + zone_name
61+
assert dns_result['type'] == dns_type
62+
assert dns_result['content'] == dns_content1
63+
64+
dns_id = dns_result['id']
65+
assert len(dns_id) == 32
66+
print('dns_record: %s %s' % (dns_name, dns_id))
67+
68+
def test_dns_records_get():
69+
# GET
70+
params = {'name':dns_name + '.' + zone_name, 'match':'all', 'type':dns_type}
71+
dns_results = cf.zones.dns_records.get(zone_id, params=params)
72+
assert len(dns_results) == 1
73+
assert dns_results[0]['name'] == dns_name + '.' + zone_name
74+
assert dns_results[0]['type'] == dns_type
75+
assert dns_results[0]['content'] == dns_content1
76+
77+
def test_dns_records_get():
78+
# GET
79+
dns_result = cf.zones.dns_records.get(zone_id, dns_id)
80+
assert dns_result['name'] == dns_name + '.' + zone_name
81+
assert dns_result['type'] == dns_type
82+
assert dns_result['content'] == dns_content1
83+
84+
def test_dns_records_patch():
85+
# PATCH
86+
dns_record = {'content':dns_content2}
87+
dns_result = cf.zones.dns_records.patch(zone_id, dns_id, data=dns_record)
88+
assert dns_result['name'] == dns_name + '.' + zone_name
89+
assert dns_result['type'] == dns_type
90+
assert dns_result['content'] == dns_content2
91+
92+
def test_dns_records_put():
93+
# PUT
94+
dns_record = {'name':dns_name, 'type':dns_type, 'content':dns_content3}
95+
dns_result = cf.zones.dns_records.put(zone_id, dns_id, data=dns_record)
96+
assert dns_result['name'] == dns_name + '.' + zone_name
97+
assert dns_result['type'] == dns_type
98+
assert dns_result['content'] == dns_content3
99+
100+
def test_dns_records_get():
101+
# GET
102+
dns_result = cf.zones.dns_records.get(zone_id, dns_id)
103+
assert dns_result['name'] == dns_name + '.' + zone_name
104+
assert dns_result['type'] == dns_type
105+
assert dns_result['content'] == dns_content3
106+
107+
def test_dns_records_delete():
108+
# DELETE
109+
dns_result = cf.zones.dns_records.delete(zone_id, dns_id)
110+
assert dns_result['id'] == dns_id
111+
112+
def test_dns_records_get():
113+
# GET
114+
params = {'name':dns_name + '.' + zone_name, 'match':'all', 'type':dns_type}
115+
dns_results = cf.zones.dns_records.get(zone_id, params=params)
116+
assert len(dns_results) == 0

0 commit comments

Comments
 (0)