-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathtest_api.py
185 lines (164 loc) · 5.98 KB
/
test_api.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import unittest
from unittest import mock
from unittest.mock import MagicMock, call
from ctfcli.core.api import API
class MockConfigSection(dict):
def __init__(self, data):
super(MockConfigSection, self).__init__(data)
# this is a wrong implementation but all that's necessary for this test
def getboolean(self, key, default=None):
if key not in self:
return default
return self[key]
class TestAPI(unittest.TestCase):
def test_api_object_ensures_trailing_slash_on_prefix_url(self):
test_urls = [
"https://example.com/test/",
"https://example.com/test",
"https://example.com/test////",
]
for url in test_urls:
mock_config = {"config": MockConfigSection({"url": url, "access_token": "test"})}
with mock.patch("ctfcli.core.api.Config", return_value=mock_config):
api = API()
self.assertEqual(api.prefix_url, "https://example.com/test/")
@mock.patch(
"ctfcli.core.api.Config",
return_value=MockConfigSection(
{"config": MockConfigSection({"url": "https://example.com/test", "access_token": "test"})}
),
)
@mock.patch("ctfcli.core.api.Session.request")
def test_api_object_request_strips_preceding_slash_from_url_path(self, mock_request: MagicMock, *args, **kwargs):
api = API()
api.request("GET", "/path")
api.request("GET", "path")
mock_request.assert_has_calls(
[
call(
"GET",
"https://example.com/test/path",
headers={"Content-Type": "application/json"},
data=None,
files=None,
),
call(
"GET",
"https://example.com/test/path",
headers={"Content-Type": "application/json"},
data=None,
files=None,
),
]
)
@mock.patch(
"ctfcli.core.api.Config",
return_value={"config": MockConfigSection({"url": "https://example.com/test", "access_token": "test"})},
)
@mock.patch("ctfcli.core.api.Session.request")
def test_api_object_request_assigns_prefix_url(self, mock_request: MagicMock, *args, **kwargs):
api = API()
api.request("GET", "path")
mock_request.assert_called_once_with(
"GET", "https://example.com/test/path", headers={"Content-Type": "application/json"}, data=None, files=None
)
def test_api_object_assigns_ssl_verify(self, *args, **kwargs):
with mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/test",
"access_token": "test",
}
)
},
):
api = API()
# expect the default to be true
self.assertTrue(api.verify)
with mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/test",
"access_token": "test",
"ssl_verify": True,
}
)
},
):
api = API()
self.assertTrue(api.verify)
with mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/test",
"access_token": "test",
"ssl_verify": False,
}
)
},
):
api = API()
self.assertFalse(api.verify)
@mock.patch("ctfcli.core.api.Config")
def test_api_expects_value_error(self, mock_config_constructor: MagicMock):
mock_config_constructor.return_value = {
"config": MagicMock(
getboolean=MagicMock(side_effect=ValueError("Invalid boolean value")),
get=MagicMock(return_value="/tmp/certificate"),
)
}
api = API()
self.assertEqual("/tmp/certificate", api.verify)
@mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/test",
"access_token": "test",
}
)
},
)
def test_api_object_assigns_headers(self, *args, **kwargs):
api = API()
self.assertIn("Authorization", api.headers)
self.assertEqual("Token test", api.headers["Authorization"])
@mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/test",
"access_token": "test",
}
),
"cookies": MockConfigSection({"test-cookie": "test-value"}),
},
)
def test_api_object_assigns_cookies(self, *args, **kwargs):
api = API()
self.assertIn("test-cookie", api.cookies)
self.assertEqual(api.cookies["test-cookie"], "test-value")
@mock.patch(
"ctfcli.core.api.Config",
return_value={
"config": MockConfigSection(
{
"url": "https://example.com/",
"access_token": "test",
}
),
},
)
@mock.patch("ctfcli.core.api.Session.request")
def test_request_does_not_override_form_data_content_type(self, mock_request: MagicMock, *args, **kwargs):
api = API()
api.request("GET", "/test", data="some-file")
mock_request.assert_called_once_with("GET", "https://example.com/test", data="some-file", files=None)