-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_yamlsettings_requests.py
103 lines (81 loc) · 3.11 KB
/
test_yamlsettings_requests.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import yamlsettings
import pytest
import responses
from requests.auth import HTTPBasicAuth
@pytest.fixture
def resps():
"""Mock request fixture"""
with responses.RequestsMock() as rsps:
yield rsps
@pytest.mark.parametrize(
"url,obj",
[
('http://testing.com/foobar', {'test': 'win'}),
('http://testing.com:90/foobar', {'test': 'win'}),
('http://[email protected]/foobar', {'test': 'win'}),
('http://[email protected]:60/foobar', {'test': 'win'}),
('http://test:[email protected]/foobar', {'test': 'win'}),
('http://test:[email protected]:99/foobar', {'test': 'win'}),
('http://test:[email protected]:99/foobar', {'test': 'win'})
],
)
def test_user_pass_combos(resps, url, obj):
"""Test loading with u/p urls"""
resps.add(responses.GET, url,
json=obj, status=200)
config = yamlsettings.load(url)
assert config.test == obj['test']
def test_fail_first_load(resps):
url_bad = 'https://bad_times.com/not_found'
url_good = 'https://good_times.com/happy_page'
resps.add(responses.GET, url_bad,
body='Page Not Found', status=404)
resps.add(responses.GET, url_good,
body='{"happy": True}', status=200)
config = yamlsettings.load([url_bad, url_good])
assert config.happy is True
def test_auth_required(resps):
url = "https://verysecure.com/doc"
resps.add(responses.GET, url,
body='{"secure": true}', status=200)
config = yamlsettings.load(
url,
auth=HTTPBasicAuth('Aladdin', 'OpenSesame'),
)
auth_header = resps.calls[0].request.__dict__['headers']['Authorization']
assert auth_header == "Basic QWxhZGRpbjpPcGVuU2VzYW1l"
assert config.secure is True
def test_no_raise_with_unexpected(resps):
"""Test Runtime Error when excepted status code"""
url_1 = 'http://testing.com/one'
url_2 = 'http://testing.com/two'
expected = 202
raise_on = False
resps.add(responses.GET, url_1, json={'error': True}, status=500)
resps.add(responses.GET, url_2, json={'foo': 'bar'}, status=202)
config = yamlsettings.load([url_1, url_2],
expected_status_code=expected,
raise_on_status=raise_on)
assert config.foo == 'bar'
def test_raise_with_unexpected(resps):
"""Test Runtime Error when excepted status code"""
url = 'http://testing.com/one'
obj = {'error': True}
expected = 200
status = 500
raise_on = True
resps.add(responses.GET, url, json=obj, status=status)
# RuntimeError stops yamlsettings from trying the next url
with pytest.raises(RuntimeError):
yamlsettings.load(url,
expected_status_code=expected,
raise_on_status=raise_on)
def test_not_found_ok(resps):
"""Test 404 can return data when expected"""
url = 'http://missing.com/data'
obj = {'hidden': 'treasure'}
expected = 404
status = 404
resps.add(responses.GET, url, json=obj, status=status)
config = yamlsettings.load(url, expected_status_code=expected)
assert config.hidden == 'treasure'