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

Commit cbf250f

Browse files
committed
/accounts/:id/urlscanner/scan/:id/screenshot returns a binary object - a png file
1 parent 231b7a7 commit cbf250f

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
""" urlscanner tests - PNG data retured """
2+
3+
import os
4+
import sys
5+
import time
6+
import random
7+
import tempfile
8+
9+
sys.path.insert(0, os.path.abspath('.'))
10+
sys.path.insert(0, os.path.abspath('..'))
11+
import CloudFlare
12+
13+
cf = None
14+
15+
def test_cloudflare(debug=False):
16+
global cf
17+
cf = CloudFlare.CloudFlare(debug=debug)
18+
assert isinstance(cf, CloudFlare.CloudFlare)
19+
20+
account_name = None
21+
account_id = None
22+
23+
def test_find_account(find_name=None):
24+
global account_name, account_id
25+
# grab a random account identifier from the first 10 accounts
26+
if find_name:
27+
params = {'per_page':1, 'name':find_name}
28+
else:
29+
params = {'per_page':10}
30+
try:
31+
accounts = cf.accounts.get(params=params)
32+
except CloudFlare.exceptions.CloudFlareAPIError as e:
33+
print('%s: Error %d=%s' % (find_name, int(e), str(e)), file=sys.stderr)
34+
exit(0)
35+
assert len(accounts) > 0 and len(accounts) <= 10
36+
# n = random.randrange(len(accounts))
37+
# stop using a random account - use the primary account (i.e. the zero'th one)
38+
n = 0
39+
account_name = accounts[n]['name']
40+
account_id = accounts[n]['id']
41+
assert len(account_id) == 32
42+
print('account: %s %s' % (account_id, account_name), file=sys.stderr)
43+
44+
scan_uuid = None
45+
scan_url = None
46+
47+
def test_urlscanner():
48+
global scan_uuid, scan_url
49+
scans = cf.accounts.urlscanner.scan.get(account_id, params={'limit':10})
50+
assert isinstance(scans, dict)
51+
assert 'tasks' in scans
52+
assert isinstance(scans['tasks'], list)
53+
tasks = scans['tasks']
54+
for task in tasks:
55+
assert isinstance(task, dict)
56+
assert 'success' in task
57+
assert 'time' in task
58+
assert 'uuid' in task
59+
assert 'url' in task
60+
print('%s: %s %s %s' % (task['uuid'], task['success'], task['time'], task['url']), file=sys.stderr)
61+
n = random.randrange(len(tasks))
62+
scan_uuid = tasks[n]['uuid']
63+
scan_url = tasks[n]['url']
64+
65+
def test_urlscanner_scan():
66+
scan = cf.accounts.urlscanner.scan.get(account_id, scan_uuid)
67+
assert isinstance(scan, dict)
68+
assert 'scan' in scan
69+
assert isinstance(scan['scan'], dict)
70+
assert 'task' in scan['scan']
71+
task = scan['scan']['task']
72+
assert 'success' in task
73+
assert 'time' in task
74+
assert 'url' in task
75+
assert 'uuid' in task
76+
assert 'visibility' in task
77+
print('%s: %s %s %s' % (task['uuid'], task['success'], task['time'], task['url']), file=sys.stderr)
78+
79+
# https://www.w3.org/TR/png/#5PNG-file-signature
80+
# PNG signature 89 50 4E 47 0D 0A 1A 0A
81+
def ispng(s):
82+
if b'\x89PNG\x0d\x0a\x1a\x0a' == s[0:8]:
83+
return True
84+
return False
85+
86+
# we don't write out the image - we have no interest in doing this
87+
def write_png_file(s):
88+
hostname = scan_url.split('/')[2].replace('.', '_')
89+
with tempfile.NamedTemporaryFile(mode='wb', prefix='screenshot-' + hostname + '-', suffix='.png', delete=False) as fp:
90+
fp.write(s)
91+
print('%s' % (fp.name), file=sys.stderr)
92+
93+
def test_urlscanner_scan_screenshot():
94+
# the real test - returning bytes as this is an image
95+
png_content = cf.accounts.urlscanner.scan.screenshot.get(account_id, scan_uuid)
96+
assert isinstance(png_content, bytes)
97+
print('%s: %s png_content: len=%d sig="%s"' % (scan_uuid, scan_url, len(png_content), png_content[0:8]), file=sys.stderr)
98+
assert ispng(png_content)
99+
# write_png_file(png_content)
100+
101+
if __name__ == '__main__':
102+
test_cloudflare(debug=True)
103+
if len(sys.argv) > 1:
104+
test_find_account(sys.argv[1])
105+
else:
106+
test_find_account()
107+
test_urlscanner()
108+
test_urlscanner_scan()
109+
test_urlscanner_scan_screenshot()

0 commit comments

Comments
 (0)