Skip to content

Commit

Permalink
more test cases and moved pytest to pytest with coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Dec 25, 2023
1 parent 08da722 commit 945afcc
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ install: build
sudo rm -rf ${NAME}.egg-info

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

cli4test: all
$(PYTHON) -m cli4 /ips > /dev/null
Expand Down
28 changes: 28 additions & 0 deletions tests/test_api_dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
""" dump api tests """

import os
import sys

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

# test API list fetches from Cloudflare website

cf = None

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

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

def test_dump_commands():
"""dump a tree of all the known API commands"""
w = cf.api_list()
assert len(w) > 0

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
64 changes: 64 additions & 0 deletions tests/test_cloudflare_calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
""" class calling tests """

import os
import sys

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

# test Cloudflare init param (ie. debug, raw, etc)
#
# cf = CloudFlare.CloudFlare(
# email=None, key=None, token=None, certtoken=None,
# debug=False, raw=False, use_sessions=True,
# profile=None,
# base_url=None,
# global_request_timeout=None, max_request_retries=None
# )

cf = None

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

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

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

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

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

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

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

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

def test_ips():
ips = cf.ips()
assert isinstance(ips, dict)
assert len(ips) > 0
51 changes: 51 additions & 0 deletions tests/test_dns_import_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" dns import/export test """

import os
import sys
import uuid

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

# test IMPORT EXPORT

cf = None
zone_name = None
zone_id = None

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

dns_name = None
dns_id = None

def test_find_zone():
global zone_name, zone_id
# grab the first zone identifier
params = {'per_page':1}
zones = cf.zones.get(params=params)
assert len(zones) == 1
zone_name = zones[0]['name']
zone_id = zones[0]['id']
assert len(zone_id) == 32
print('zone: %s %s' % (zone_id, zone_name))

def test_dns_import():
# IMPORT
fd = open('/dev/null', 'rb')
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fd})
# {"recs_added": 0, "recs_added_by_type": {}, "total_records_parsed": 0}
assert len(results) > 0
assert results['recs_added'] == 0
assert len(results['recs_added_by_type']) == 0
assert results['total_records_parsed'] == 0

def test_dns_export():
# EXPORT
dns_records = cf.zones.dns_records.export.get(zone_id)
assert len(dns_records) > 0
assert isinstance(dns_records, str)
assert 'SOA' in dns_records
assert 'NS' in dns_records
116 changes: 116 additions & 0 deletions tests/test_dns_records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
""" get/post/delete/etc dns based tests """

import os
import sys
import uuid

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

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

cf = None
zone_name = None
zone_id = None

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

def test_find_zone():
global zone_name, zone_id
# grab the first zone identifier
params = {'per_page':1}
zones = cf.zones.get(params=params)
assert len(zones) == 1
zone_name = zones[0]['name']
zone_id = zones[0]['id']
assert len(zone_id) == 32
print('zone: %s %s' % (zone_id, zone_name))

dns_name = None
dns_type = None
dns_content1 = None
dns_content2 = None
dns_content3 = None

def test_dns_records():
global dns_name, dns_type, dns_content1, dns_content2, dns_content3
dns_name = str(uuid.uuid1())
dns_type = 'TXT'
dns_content1 = 'temp pytest element 1'
dns_content2 = 'temp pytest element 2'
dns_content3 = 'temp pytest element 3'
print('dns_record: %s' % (dns_name))

def test_dns_records_get():
# GET
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

dns_id = None

def test_dns_records_post():
global dns_id
# POST
dns_record = {'name':dns_name, 'type':dns_type, 'content':dns_content1}
dns_result = cf.zones.dns_records.post(zone_id, data=dns_record)
assert dns_result['name'] == dns_name + '.' + zone_name
assert dns_result['type'] == dns_type
assert dns_result['content'] == dns_content1

dns_id = dns_result['id']
assert len(dns_id) == 32
print('dns_record: %s %s' % (dns_name, dns_id))

def test_dns_records_get():
# GET
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) == 1
assert dns_results[0]['name'] == dns_name + '.' + zone_name
assert dns_results[0]['type'] == dns_type
assert dns_results[0]['content'] == dns_content1

def test_dns_records_get():
# GET
dns_result = cf.zones.dns_records.get(zone_id, dns_id)
assert dns_result['name'] == dns_name + '.' + zone_name
assert dns_result['type'] == dns_type
assert dns_result['content'] == dns_content1

def test_dns_records_patch():
# PATCH
dns_record = {'content':dns_content2}
dns_result = cf.zones.dns_records.patch(zone_id, dns_id, data=dns_record)
assert dns_result['name'] == dns_name + '.' + zone_name
assert dns_result['type'] == dns_type
assert dns_result['content'] == dns_content2

def test_dns_records_put():
# PUT
dns_record = {'name':dns_name, 'type':dns_type, 'content':dns_content3}
dns_result = cf.zones.dns_records.put(zone_id, dns_id, data=dns_record)
assert dns_result['name'] == dns_name + '.' + zone_name
assert dns_result['type'] == dns_type
assert dns_result['content'] == dns_content3

def test_dns_records_get():
# GET
dns_result = cf.zones.dns_records.get(zone_id, dns_id)
assert dns_result['name'] == dns_name + '.' + zone_name
assert dns_result['type'] == dns_type
assert dns_result['content'] == dns_content3

def test_dns_records_delete():
# DELETE
dns_result = cf.zones.dns_records.delete(zone_id, dns_id)
assert dns_result['id'] == dns_id

def test_dns_records_get():
# GET
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

0 comments on commit 945afcc

Please sign in to comment.