|
| 1 | +""" radar returning CSV test """ |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import json |
| 6 | +import random |
| 7 | +import datetime |
| 8 | + |
| 9 | +sys.path.insert(0, os.path.abspath('.')) |
| 10 | +sys.path.insert(0, os.path.abspath('..')) |
| 11 | +import CloudFlare |
| 12 | + |
| 13 | +# test /accounts/:id/images/v2/direct_upload - this uses forms in port |
| 14 | + |
| 15 | +cf = None |
| 16 | + |
| 17 | +def test_cloudflare(debug=False): |
| 18 | + """ test_cloudflare """ |
| 19 | + global cf |
| 20 | + cf = CloudFlare.CloudFlare(debug=debug) |
| 21 | + assert isinstance(cf, CloudFlare.CloudFlare) |
| 22 | + |
| 23 | +account_name = None |
| 24 | +account_id = None |
| 25 | + |
| 26 | +def test_find_account(find_name=None): |
| 27 | + """ test_find_account """ |
| 28 | + global account_name, account_id |
| 29 | + # grab a random account identifier from the first 10 accounts |
| 30 | + if find_name: |
| 31 | + params = {'per_page':1, 'name':find_name} |
| 32 | + else: |
| 33 | + params = {'per_page':10} |
| 34 | + try: |
| 35 | + accounts = cf.accounts.get(params=params) |
| 36 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 37 | + print('%s: Error %d=%s' % (find_name, int(e), str(e)), file=sys.stderr) |
| 38 | + assert False |
| 39 | + assert len(accounts) > 0 and len(accounts) <= 10 |
| 40 | + # n = random.randrange(len(accounts)) |
| 41 | + # stop using a random account - use the primary account (i.e. the zero'th one) |
| 42 | + n = 0 |
| 43 | + account_name = accounts[n]['name'] |
| 44 | + account_id = accounts[n]['id'] |
| 45 | + assert len(account_id) == 32 |
| 46 | + print('account: %s %s' % (account_id, account_name), file=sys.stderr) |
| 47 | + |
| 48 | +# simple metadata in json string form |
| 49 | +metadata_values = json.dumps({ |
| 50 | + 'item1': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ...', |
| 51 | + 'item2': 'Ignore item number one', |
| 52 | +}) |
| 53 | + |
| 54 | +# format future time in RFC3339 format (and do it UTC time) |
| 55 | +time_plus_one_hour_in_iso = (datetime.datetime.utcnow().replace(microsecond=0) + datetime.timedelta(hours=1)).isoformat() + 'Z' |
| 56 | + |
| 57 | +def test_images_v2_direct_upload(): |
| 58 | + """ test_images_v2_direct_upload """ |
| 59 | + |
| 60 | + try: |
| 61 | + r = cf.accounts.images.v2.direct_upload.post(account_id) |
| 62 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 63 | + print('Error unexpected: %d %s' % (int(e), str(e)), file=sys.stderr) |
| 64 | + assert False |
| 65 | + |
| 66 | + assert isinstance(r, dict) |
| 67 | + assert len(r['id']) > 0 |
| 68 | + assert len(r['uploadURL']) > 0 |
| 69 | + |
| 70 | + image_id = r['id'] |
| 71 | + image_url = r['uploadURL'] |
| 72 | + print('%s %s' % (image_id, image_url), file=sys.stderr) |
| 73 | + |
| 74 | +def test_images_v2_direct_upload_data(): |
| 75 | + """ test_images_v2_direct_upload """ |
| 76 | + |
| 77 | + data = { |
| 78 | + 'metadata': metadata_values, |
| 79 | + 'expiry': time_plus_one_hour_in_iso, |
| 80 | + } |
| 81 | + try: |
| 82 | + r = cf.accounts.images.v2.direct_upload.post(account_id, data=data) |
| 83 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 84 | + print('Error unexpected: %d %s' % (int(e), str(e)), file=sys.stderr) |
| 85 | + assert False |
| 86 | + |
| 87 | + assert isinstance(r, dict) |
| 88 | + assert len(r['id']) > 0 |
| 89 | + assert len(r['uploadURL']) > 0 |
| 90 | + |
| 91 | + image_id = r['id'] |
| 92 | + image_url = r['uploadURL'] |
| 93 | + print('%s %s' % (image_id, image_url), file=sys.stderr) |
| 94 | + |
| 95 | +def test_images_v2_direct_upload_files(): |
| 96 | + """ test_images_v2_direct_upload """ |
| 97 | + |
| 98 | + files = { |
| 99 | + ('metadata', (None, metadata_values)), |
| 100 | + ('expiry', (None, time_plus_one_hour_in_iso)) |
| 101 | + } |
| 102 | + try: |
| 103 | + r = cf.accounts.images.v2.direct_upload.post(account_id, files=files) |
| 104 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 105 | + print('Error unexpected: %d %s' % (int(e), str(e)), file=sys.stderr) |
| 106 | + assert False |
| 107 | + |
| 108 | + assert isinstance(r, dict) |
| 109 | + assert len(r['id']) > 0 |
| 110 | + assert len(r['uploadURL']) > 0 |
| 111 | + |
| 112 | + image_id = r['id'] |
| 113 | + image_url = r['uploadURL'] |
| 114 | + print('%s %s' % (image_id, image_url), file=sys.stderr) |
| 115 | + |
| 116 | +def test_images_v2_direct_upload_data_and_files(): |
| 117 | + """ test_images_v2_direct_upload """ |
| 118 | + |
| 119 | + data = { |
| 120 | + 'metadata': metadata_values, |
| 121 | + 'expiry': time_plus_one_hour_in_iso, |
| 122 | + } |
| 123 | + files = { |
| 124 | + ('metadata', (None, metadata_values)), |
| 125 | + ('expiry', (None, time_plus_one_hour_in_iso)) |
| 126 | + } |
| 127 | + try: |
| 128 | + r = cf.accounts.images.v2.direct_upload.post(account_id, data=data, files=files) |
| 129 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 130 | + print('Error unexpected: %d %s' % (int(e), str(e)), file=sys.stderr) |
| 131 | + assert False |
| 132 | + |
| 133 | + assert isinstance(r, dict) |
| 134 | + assert len(r['id']) > 0 |
| 135 | + assert len(r['uploadURL']) > 0 |
| 136 | + |
| 137 | + image_id = r['id'] |
| 138 | + image_url = r['uploadURL'] |
| 139 | + print('%s %s' % (image_id, image_url), file=sys.stderr) |
| 140 | + |
| 141 | +def test_images_v2_direct_upload_files_len_zero(): |
| 142 | + """ test_images_v2_direct_upload """ |
| 143 | + |
| 144 | + files = set() # zero length set |
| 145 | + try: |
| 146 | + r = cf.accounts.images.v2.direct_upload.post(account_id, files=files) |
| 147 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 148 | + # this can trigger an error from the Cloudflare API backend - which is wrong; however, should be coded around. |
| 149 | + print('Error unexpected: %d %s' % (int(e), str(e)), file=sys.stderr) |
| 150 | + assert False |
| 151 | + |
| 152 | + assert isinstance(r, dict) |
| 153 | + assert len(r['id']) > 0 |
| 154 | + assert len(r['uploadURL']) > 0 |
| 155 | + |
| 156 | + image_id = r['id'] |
| 157 | + image_url = r['uploadURL'] |
| 158 | + print('%s %s' % (image_id, image_url), file=sys.stderr) |
| 159 | + |
| 160 | +if __name__ == '__main__': |
| 161 | + test_cloudflare(debug=True) |
| 162 | + if len(sys.argv) > 1: |
| 163 | + test_find_account(sys.argv[1]) |
| 164 | + else: |
| 165 | + test_find_account() |
| 166 | + test_images_v2_direct_upload() |
| 167 | + test_images_v2_direct_upload_data() |
| 168 | + test_images_v2_direct_upload_files() |
| 169 | + test_images_v2_direct_upload_data_and_files() |
| 170 | + test_images_v2_direct_upload_files_len_zero() |
0 commit comments