File: test_api_dump.py

package info (click to toggle)
python-cloudflare 2.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 6,932; makefile: 138; sh: 76
file content (70 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""" test dump calls """

import os
import sys
import re

sys.path.insert(0, os.path.abspath('.'))
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(debug=False):
    """ test_cloudflare """
    global cf
    cf = CloudFlare.CloudFlare(debug=debug)
    assert isinstance(cf, CloudFlare.CloudFlare)

verb_only = re.compile('^[a-zA-Z0-9][-a-zA-Z0-9_]*[a-zA-Z0-9]$')

def check_cmd_syntax(cmd):
    """ check_cmd_syntax """
    assert '/' == cmd[0]
    for verb in cmd[1:].split('/'):
        if verb[0] == '@':
            # don't want to check the rest of the api - it's an AI one
            break
        if verb[0] == ':':
            # :id or equiv
            assert bool(verb_only.match(verb[1:]))
        else:
            # just a verb
            assert bool(verb_only.match(verb))

def check_method_syntax(method):
    """ check_method_syntax """
    assert method in ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']

def test_api_list():
    """dump a tree of all the known API commands"""
    api_list = cf.api_list()
    assert len(api_list) > 0
    for api in api_list:
        check_cmd_syntax(api)

def test_api_from_openapi():
    """dump a tree of all the known API commands - from web"""
    api_list = cf.api_from_openapi()
    assert len(api_list) > 0
    for api in api_list:
        # {'action': 'GET', 'cmd': '/accounts', 'deprecated': ...
        assert 'action' in api
        assert 'cmd' in api
        check_method_syntax(api['action'])
        check_cmd_syntax(api['cmd'])

def test_api_from_openapi_with_url():
    """dump a tree of all the known API commands - from web"""
    api_list = cf.api_from_openapi(OPENAPI_URL)
    assert len(api_list) > 0

if __name__ == '__main__':
    test_cloudflare(debug=True)
    test_api_list()
    test_api_from_openapi()
    test_api_from_openapi_with_url()