|
| 1 | +""" paging thru zones tests """ |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +sys.path.insert(0, os.path.abspath('.')) |
| 7 | +sys.path.insert(0, os.path.abspath('..')) |
| 8 | +import CloudFlare |
| 9 | + |
| 10 | +# test paging thru zones with raw option |
| 11 | + |
| 12 | +cf = None |
| 13 | + |
| 14 | +def test_cloudflare(debug=False): |
| 15 | + global cf |
| 16 | + cf = CloudFlare.CloudFlare(raw=True, debug=debug) |
| 17 | + assert isinstance(cf, CloudFlare.CloudFlare) |
| 18 | + |
| 19 | +def paging_thru_zones(name=None): |
| 20 | + |
| 21 | + count_received = 0 |
| 22 | + total_count = 0 # we want to confirm this total later |
| 23 | + page_number = 0 |
| 24 | + while True: |
| 25 | + page_number += 1 |
| 26 | + params = {'per_page':10,'page':page_number,'name':name} |
| 27 | + try: |
| 28 | + raw_results = cf.zones.get(params=params) |
| 29 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 30 | + exit('/zones.get %d %s - api call failed' % (e, e)) |
| 31 | + |
| 32 | + assert 'result_info' in raw_results |
| 33 | + assert 'result' in raw_results |
| 34 | + |
| 35 | + results_info = raw_results['result_info'] |
| 36 | + results = raw_results['result'] |
| 37 | + |
| 38 | + assert 'count' in results_info |
| 39 | + assert 'page' in results_info |
| 40 | + assert 'per_page' in results_info |
| 41 | + assert 'total_count' in results_info |
| 42 | + assert 'total_pages' in results_info |
| 43 | + |
| 44 | + count = results_info['count'] |
| 45 | + page = results_info['page'] |
| 46 | + per_page = results_info['per_page'] |
| 47 | + total_count = results_info['total_count'] |
| 48 | + total_pages = results_info['total_pages'] |
| 49 | + |
| 50 | + assert isinstance(count, int) |
| 51 | + assert isinstance(page, int) |
| 52 | + assert isinstance(per_page, int) |
| 53 | + assert isinstance(total_count, int) |
| 54 | + assert isinstance(total_pages, int) |
| 55 | + |
| 56 | + assert page_number == page |
| 57 | + |
| 58 | + assert len(results) == count |
| 59 | + assert isinstance(results, list) |
| 60 | + |
| 61 | + count_received += count |
| 62 | + |
| 63 | + domains = [] |
| 64 | + for zone in results: |
| 65 | + assert 'id' in zone |
| 66 | + assert 'name' in zone |
| 67 | + zone_id = zone['id'] |
| 68 | + zone_name = zone['name'] |
| 69 | + domains.append(zone_name) |
| 70 | + print("COUNT=%d PAGE=%d PER_PAGE=%d TOTAL_COUNT=%d TOTAL_PAGES=%d -- %s" % (count, page, per_page, total_count, total_pages, ','.join(domains)), file=sys.stderr) |
| 71 | + |
| 72 | + if count == 0 or page_number >= total_pages: |
| 73 | + # finished |
| 74 | + break |
| 75 | + |
| 76 | + # did we receive all the info? |
| 77 | + assert count_received == total_count |
| 78 | + |
| 79 | +def test_paging_thru_zones(): |
| 80 | + paging_thru_zones(None) |
| 81 | + |
| 82 | +def test_paging_thru_zones_match_com(): |
| 83 | + # we assume your account has one of these domains |
| 84 | + paging_thru_zones('ends_with:.com') |
| 85 | + |
| 86 | +def test_paging_thru_zones_match_nothing(): |
| 87 | + paging_thru_zones('QWERTYUIOOP') |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + test_cloudflare(debug=True) |
| 91 | + test_paging_thru_zones() |
| 92 | + test_paging_thru_zones_match_com() |
| 93 | + test_paging_thru_zones_match_nothing() |
0 commit comments