|
| 1 | +""" graphql tests """ |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import time |
| 6 | +import random |
| 7 | +import datetime |
| 8 | +import pytz |
| 9 | +import json |
| 10 | + |
| 11 | +sys.path.insert(0, os.path.abspath('.')) |
| 12 | +sys.path.insert(0, os.path.abspath('..')) |
| 13 | +import CloudFlare |
| 14 | + |
| 15 | +# test /graphql |
| 16 | + |
| 17 | +cf = None |
| 18 | + |
| 19 | +def test_cloudflare(debug=False): |
| 20 | + global cf |
| 21 | + cf = CloudFlare.CloudFlare(debug=debug) |
| 22 | + assert isinstance(cf, CloudFlare.CloudFlare) |
| 23 | + |
| 24 | +zone_name = None |
| 25 | +zone_id = None |
| 26 | + |
| 27 | +def test_find_zone(domain_name=None): |
| 28 | + global zone_name, zone_id |
| 29 | + # grab a random zone identifier from the first 10 zones |
| 30 | + if domain_name: |
| 31 | + params = {'per_page':1, 'name':domain_name} |
| 32 | + else: |
| 33 | + params = {'per_page':10} |
| 34 | + try: |
| 35 | + zones = cf.zones.get(params=params) |
| 36 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 37 | + print('%s: Error %d=%s' % (domain_name, int(e), str(e)), file=sys.stderr) |
| 38 | + exit(0) |
| 39 | + assert len(zones) > 0 and len(zones) <= 10 |
| 40 | + n = random.randrange(len(zones)) |
| 41 | + zone_name = zones[n]['name'] |
| 42 | + zone_id = zones[n]['id'] |
| 43 | + assert len(zone_id) == 32 |
| 44 | + print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr) |
| 45 | + |
| 46 | +def now_iso8601_time(h_delta): |
| 47 | + """need a yyyy-mm-dd string""" |
| 48 | + t = time.time() - (h_delta * 3600) |
| 49 | + # only use yyyy-mm-dd part for httpRequests1dGroups below |
| 50 | + r = datetime.datetime.fromtimestamp(int(t), tz=pytz.timezone("UTC")).strftime('%Y-%m-%d') |
| 51 | + return r |
| 52 | + |
| 53 | +def test_graphql(): |
| 54 | + """ /graphql test """ |
| 55 | + date_before = now_iso8601_time(0) # now |
| 56 | + date_after = now_iso8601_time(3 * 24) # 3 days worth |
| 57 | + |
| 58 | + query = """ |
| 59 | + query { |
| 60 | + viewer { |
| 61 | + zones(filter: {zoneTag: "%s"} ) { |
| 62 | + httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) { |
| 63 | + sum { countryMap { bytes, requests, clientCountryName } } |
| 64 | + dimensions { date } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + """ % (zone_id, date_before, date_after) |
| 70 | + |
| 71 | + # remove whitespace from query - this isn't needed; but helps debug |
| 72 | + query = '\n'.join([s.strip() for s in query.splitlines()]).strip() |
| 73 | + |
| 74 | + # graphql query is always a post |
| 75 | + try: |
| 76 | + r = cf.graphql.post(data={'query':query}) |
| 77 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 78 | + exit('/graphql.post %d %s - api call failed' % (e, e)) |
| 79 | + |
| 80 | + # success - lets confirm it's graphql results as-per query above |
| 81 | + |
| 82 | + # basic graphql results |
| 83 | + assert 'data' in r |
| 84 | + assert 'errors' in r |
| 85 | + assert r['errors'] == None |
| 86 | + |
| 87 | + # viewer and zones from above |
| 88 | + assert 'viewer' in r['data'] |
| 89 | + assert 'zones' in r['data']['viewer'] |
| 90 | + |
| 91 | + # only one zone |
| 92 | + zones = r['data']['viewer']['zones'] |
| 93 | + assert len(zones) == 1 |
| 94 | + zone_info = zones[0] |
| 95 | + |
| 96 | + # the data |
| 97 | + assert 'httpRequests1dGroups' in zone_info |
| 98 | + httpRequests1dGroups = zone_info['httpRequests1dGroups'] |
| 99 | + assert isinstance(httpRequests1dGroups, list) |
| 100 | + for h in httpRequests1dGroups: |
| 101 | + assert 'dimensions' in h |
| 102 | + assert 'date' in h['dimensions'] |
| 103 | + result_date = h['dimensions']['date'] |
| 104 | + assert 'sum' in h |
| 105 | + result_sum = h['sum'] |
| 106 | + assert 'countryMap' in result_sum |
| 107 | + countryMap = h['sum']['countryMap'] |
| 108 | + for element in countryMap: |
| 109 | + assert 'bytes' in element |
| 110 | + assert 'requests' in element |
| 111 | + assert 'clientCountryName' in element |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + test_cloudflare(debug=True) |
| 115 | + if len(sys.argv) > 1: |
| 116 | + test_find_zone(sys.argv[1]) |
| 117 | + else: |
| 118 | + test_find_zone() |
| 119 | + test_graphql() |
0 commit comments