-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
70 lines (54 loc) · 2.38 KB
/
test.py
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
import pytest
import os
import sys
import re
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from faapi import API
from dotenv import load_dotenv
from loguru import logger
load_dotenv()
EXCHANGERATES_KEY=os.getenv('EXCHANGERATES_KEY')
class Ifconfg:
def __init__(self):
self.api = API('https://ifconfig.io')
self.api2 = API('https://api.exchangeratesapi.io/v1/', logger=logger)
@pytest.fixture
def _cfg():
return Ifconfg()
def is_ipv4_or_ipv6(text):
# ipv4 regex match
ipv4_pattern = r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$'
ipv6_pattern = r'(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
return re.match(ipv4_pattern, text) or re.match(ipv6_pattern, text)
def test_ifconfig_io(_cfg):
ret = _cfg.api.get.ip()
assert is_ipv4_or_ipv6(ret)
def test_ifconfig_io_ua(_cfg):
ret = _cfg.api.get.ua()
assert ret == 'python-requests/2.28.2\n'
def test_ifconfig_io_lang(_cfg):
ret = _cfg.api.get.lang()
assert ret == '\n'
def test_ifconfig_io_forwarded(_cfg):
ret = _cfg.api.get.forwarded()
assert is_ipv4_or_ipv6(ret)
def test_ifconfig_io_all_json(_cfg):
ret = _cfg.api.get.all_json(convert_dash=True)
assert ret['ifconfig_hostname'] == 'ifconfig.io'
def test_ifconfig_io_404(_cfg):
ret = _cfg.api.get.foo()
assert ret == '404 page not found'
def test_all_json(_cfg):
ret = _cfg.api.get.all_json(underline2dot=True)
print(ret)
assert ret['ifconfig_hostname'] == 'ifconfig.io'
def test_exchangerares_key_ng(_cfg):
ret = _cfg.api2.get.latest()
print(ret)
assert ret['error']['code'] == 'missing_access_key'
def test_exchangerares_key_ok(_cfg):
data = {
'access_key': EXCHANGERATES_KEY
}
ret = _cfg.api2.get.latest(data=data)
assert 'success' in ret and ret['success']