mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
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
This commit is contained in:
@@ -51,20 +51,16 @@ class RequestsBackendMockAPITestCase(SimpleTestCase, AnymailTestMixin):
|
|||||||
# This assumes the last (or only) call to requests.Session.request is the API call of interest.
|
# This assumes the last (or only) call to requests.Session.request is the API call of interest.
|
||||||
if self.mock_request.call_args is None:
|
if self.mock_request.call_args is None:
|
||||||
raise AssertionError("No ESP API was called")
|
raise AssertionError("No ESP API was called")
|
||||||
(args, kwargs) = self.mock_request.call_args
|
if method is not None:
|
||||||
try:
|
actual_method = self.get_api_call_arg('method')
|
||||||
actual_method = kwargs.get('method', None) or args[1]
|
if actual_method != method:
|
||||||
actual_url = kwargs.get('url', None) or args[2]
|
self.fail("API was not called using %s. (%s was used instead.)" % (method, actual_method))
|
||||||
except IndexError:
|
if url is not None:
|
||||||
raise AssertionError("API was called without a method or url (?!)")
|
actual_url = self.get_api_call_arg('url')
|
||||||
if actual_method != method:
|
if not actual_url.endswith(url):
|
||||||
raise AssertionError("API was not called using %s. (%s was used instead.)"
|
self.fail("API was not called at %s\n(It was called at %s)" % (url, actual_url))
|
||||||
% (method, actual_method))
|
|
||||||
if not actual_url.endswith(url):
|
|
||||||
raise AssertionError("API was not called at %s\n(It was called at %s)"
|
|
||||||
% (url, actual_url))
|
|
||||||
|
|
||||||
def get_api_call_arg(self, kwarg, pos, required=True):
|
def get_api_call_arg(self, kwarg, required=True):
|
||||||
"""Returns an argument passed to the mock ESP API.
|
"""Returns an argument passed to the mock ESP API.
|
||||||
|
|
||||||
Fails test if API wasn't called.
|
Fails test if API wasn't called.
|
||||||
@@ -73,36 +69,51 @@ class RequestsBackendMockAPITestCase(SimpleTestCase, AnymailTestMixin):
|
|||||||
raise AssertionError("API was not called")
|
raise AssertionError("API was not called")
|
||||||
(args, kwargs) = self.mock_request.call_args
|
(args, kwargs) = self.mock_request.call_args
|
||||||
try:
|
try:
|
||||||
return kwargs.get(kwarg, None) or args[pos]
|
return kwargs[kwarg]
|
||||||
except IndexError:
|
except KeyError:
|
||||||
if required:
|
pass
|
||||||
raise AssertionError("API was called without required %s" % kwarg)
|
|
||||||
else:
|
try:
|
||||||
return None
|
# positional arg? This is the order of requests.Session.request params:
|
||||||
|
pos = ('method', 'url', 'params', 'data', 'headers', 'cookies', 'files', 'auth',
|
||||||
|
'timeout', 'allow_redirects', 'proxies', 'hooks', 'stream', 'verify', 'cert', 'json',
|
||||||
|
).index(kwarg)
|
||||||
|
return args[pos]
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if required:
|
||||||
|
self.fail("API was called without required arg '%s'" % kwarg)
|
||||||
|
return None
|
||||||
|
|
||||||
def get_api_call_params(self, required=True):
|
def get_api_call_params(self, required=True):
|
||||||
"""Returns the query params sent to the mock ESP API."""
|
"""Returns the query params sent to the mock ESP API."""
|
||||||
return self.get_api_call_arg('params', 3, required)
|
return self.get_api_call_arg('params', required)
|
||||||
|
|
||||||
def get_api_call_data(self, required=True):
|
def get_api_call_data(self, required=True):
|
||||||
"""Returns the raw data sent to the mock ESP API."""
|
"""Returns the raw data sent to the mock ESP API."""
|
||||||
return self.get_api_call_arg('data', 4, required)
|
return self.get_api_call_arg('data', required)
|
||||||
|
|
||||||
def get_api_call_json(self, required=True):
|
def get_api_call_json(self, required=True):
|
||||||
"""Returns the data sent to the mock ESP API, json-parsed"""
|
"""Returns the data sent to the mock ESP API, json-parsed"""
|
||||||
return json.loads(self.get_api_call_data(required))
|
# could be either the data param (as json str) or the json param (needing formatting)
|
||||||
|
value = self.get_api_call_arg('data', required=False)
|
||||||
|
if value is not None:
|
||||||
|
return json.loads(value)
|
||||||
|
else:
|
||||||
|
return self.get_api_call_arg('json', required)
|
||||||
|
|
||||||
def get_api_call_headers(self, required=True):
|
def get_api_call_headers(self, required=True):
|
||||||
"""Returns the headers sent to the mock ESP API"""
|
"""Returns the headers sent to the mock ESP API"""
|
||||||
return self.get_api_call_arg('headers', 5, required)
|
return self.get_api_call_arg('headers', required)
|
||||||
|
|
||||||
def get_api_call_files(self, required=True):
|
def get_api_call_files(self, required=True):
|
||||||
"""Returns the files sent to the mock ESP API"""
|
"""Returns the files sent to the mock ESP API"""
|
||||||
return self.get_api_call_arg('files', 7, required)
|
return self.get_api_call_arg('files', required)
|
||||||
|
|
||||||
def get_api_call_auth(self, required=True):
|
def get_api_call_auth(self, required=True):
|
||||||
"""Returns the auth sent to the mock ESP API"""
|
"""Returns the auth sent to the mock ESP API"""
|
||||||
return self.get_api_call_arg('auth', 8, required)
|
return self.get_api_call_arg('auth', required)
|
||||||
|
|
||||||
def assert_esp_not_called(self, msg=None):
|
def assert_esp_not_called(self, msg=None):
|
||||||
if self.mock_request.called:
|
if self.mock_request.called:
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase):
|
|||||||
self.assertEqual(data['html'], html_content)
|
self.assertEqual(data['html'], html_content)
|
||||||
# Don't accidentally send the html part as an attachment:
|
# Don't accidentally send the html part as an attachment:
|
||||||
files = self.get_api_call_files(required=False)
|
files = self.get_api_call_files(required=False)
|
||||||
self.assertIsNone(files)
|
self.assertFalse(files)
|
||||||
|
|
||||||
def test_html_only_message(self):
|
def test_html_only_message(self):
|
||||||
html_content = '<p>This is an <strong>important</strong> message.</p>'
|
html_content = '<p>This is an <strong>important</strong> message.</p>'
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase):
|
|||||||
self.assertEqual(data['html'], html_content)
|
self.assertEqual(data['html'], html_content)
|
||||||
# Don't accidentally send the html part as an attachment:
|
# Don't accidentally send the html part as an attachment:
|
||||||
files = self.get_api_call_files(required=False)
|
files = self.get_api_call_files(required=False)
|
||||||
self.assertIsNone(files)
|
self.assertFalse(files)
|
||||||
|
|
||||||
def test_html_only_message(self):
|
def test_html_only_message(self):
|
||||||
html_content = '<p>This is an <strong>important</strong> message.</p>'
|
html_content = '<p>This is an <strong>important</strong> message.</p>'
|
||||||
|
|||||||
Reference in New Issue
Block a user