Skip to content

Commit 5fb4695

Browse files
committed
Tests: fix MockRequestsBackend.get_api_call_arg edge cases
get_api_call_arg had incorrectly returned None if a kwarg was passed to the mocked function with a False-y value (e.g., [] or {}) get_api_call_json had only considered data param, ignoring json param requests added a while back
1 parent 2bf492c commit 5fb4695

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

tests/mock_requests_backend.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,16 @@ def assert_esp_called(self, url, method="POST"):
5151
# This assumes the last (or only) call to requests.Session.request is the API call of interest.
5252
if self.mock_request.call_args is None:
5353
raise AssertionError("No ESP API was called")
54-
(args, kwargs) = self.mock_request.call_args
55-
try:
56-
actual_method = kwargs.get('method', None) or args[1]
57-
actual_url = kwargs.get('url', None) or args[2]
58-
except IndexError:
59-
raise AssertionError("API was called without a method or url (?!)")
60-
if actual_method != method:
61-
raise AssertionError("API was not called using %s. (%s was used instead.)"
62-
% (method, actual_method))
63-
if not actual_url.endswith(url):
64-
raise AssertionError("API was not called at %s\n(It was called at %s)"
65-
% (url, actual_url))
66-
67-
def get_api_call_arg(self, kwarg, pos, required=True):
54+
if method is not None:
55+
actual_method = self.get_api_call_arg('method')
56+
if actual_method != method:
57+
self.fail("API was not called using %s. (%s was used instead.)" % (method, actual_method))
58+
if url is not None:
59+
actual_url = self.get_api_call_arg('url')
60+
if not actual_url.endswith(url):
61+
self.fail("API was not called at %s\n(It was called at %s)" % (url, actual_url))
62+
63+
def get_api_call_arg(self, kwarg, required=True):
6864
"""Returns an argument passed to the mock ESP API.
6965
7066
Fails test if API wasn't called.
@@ -73,36 +69,51 @@ def get_api_call_arg(self, kwarg, pos, required=True):
7369
raise AssertionError("API was not called")
7470
(args, kwargs) = self.mock_request.call_args
7571
try:
76-
return kwargs.get(kwarg, None) or args[pos]
77-
except IndexError:
78-
if required:
79-
raise AssertionError("API was called without required %s" % kwarg)
80-
else:
81-
return None
72+
return kwargs[kwarg]
73+
except KeyError:
74+
pass
75+
76+
try:
77+
# positional arg? This is the order of requests.Session.request params:
78+
pos = ('method', 'url', 'params', 'data', 'headers', 'cookies', 'files', 'auth',
79+
'timeout', 'allow_redirects', 'proxies', 'hooks', 'stream', 'verify', 'cert', 'json',
80+
).index(kwarg)
81+
return args[pos]
82+
except (ValueError, IndexError):
83+
pass
84+
85+
if required:
86+
self.fail("API was called without required arg '%s'" % kwarg)
87+
return None
8288

8389
def get_api_call_params(self, required=True):
8490
"""Returns the query params sent to the mock ESP API."""
85-
return self.get_api_call_arg('params', 3, required)
91+
return self.get_api_call_arg('params', required)
8692

8793
def get_api_call_data(self, required=True):
8894
"""Returns the raw data sent to the mock ESP API."""
89-
return self.get_api_call_arg('data', 4, required)
95+
return self.get_api_call_arg('data', required)
9096

9197
def get_api_call_json(self, required=True):
9298
"""Returns the data sent to the mock ESP API, json-parsed"""
93-
return json.loads(self.get_api_call_data(required))
99+
# could be either the data param (as json str) or the json param (needing formatting)
100+
value = self.get_api_call_arg('data', required=False)
101+
if value is not None:
102+
return json.loads(value)
103+
else:
104+
return self.get_api_call_arg('json', required)
94105

95106
def get_api_call_headers(self, required=True):
96107
"""Returns the headers sent to the mock ESP API"""
97-
return self.get_api_call_arg('headers', 5, required)
108+
return self.get_api_call_arg('headers', required)
98109

99110
def get_api_call_files(self, required=True):
100111
"""Returns the files sent to the mock ESP API"""
101-
return self.get_api_call_arg('files', 7, required)
112+
return self.get_api_call_arg('files', required)
102113

103114
def get_api_call_auth(self, required=True):
104115
"""Returns the auth sent to the mock ESP API"""
105-
return self.get_api_call_arg('auth', 8, required)
116+
return self.get_api_call_arg('auth', required)
106117

107118
def assert_esp_not_called(self, msg=None):
108119
if self.mock_request.called:

tests/test_mailgun_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_html_message(self):
108108
self.assertEqual(data['html'], html_content)
109109
# Don't accidentally send the html part as an attachment:
110110
files = self.get_api_call_files(required=False)
111-
self.assertIsNone(files)
111+
self.assertFalse(files)
112112

113113
def test_html_only_message(self):
114114
html_content = '<p>This is an <strong>important</strong> message.</p>'

tests/test_sendgrid_v2_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_html_message(self):
146146
self.assertEqual(data['html'], html_content)
147147
# Don't accidentally send the html part as an attachment:
148148
files = self.get_api_call_files(required=False)
149-
self.assertIsNone(files)
149+
self.assertFalse(files)
150150

151151
def test_html_only_message(self):
152152
html_content = '<p>This is an <strong>important</strong> message.</p>'

0 commit comments

Comments
 (0)