decorator: Improve wrong-subdomain logging output.

Often, it's helpful to see both values in errors.
This commit is contained in:
Tim Abbott 2017-10-02 13:12:04 -07:00
parent 6b9755c854
commit 5a27a4cf1c
2 changed files with 18 additions and 18 deletions

View File

@ -215,8 +215,8 @@ def validate_account_and_subdomain(request, user_profile):
not (settings.RUNNING_INSIDE_TORNADO and
request.META["SERVER_NAME"] == "127.0.0.1" and
request.META["REMOTE_ADDR"] == "127.0.0.1")):
logging.warning("User %s attempted to access API on wrong subdomain %s" % (
user_profile.email, get_subdomain(request)))
logging.warning("User %s (%s) attempted to access API on wrong subdomain (%s)" % (
user_profile.email, user_profile.realm.subdomain, get_subdomain(request)))
raise JsonableError(_("Account is not associated with this subdomain"))
def access_user_by_api_key(request, api_key, email=None):

View File

@ -245,8 +245,8 @@ class DecoratorTestCase(TestCase):
api_result = my_webhook(request)
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(webhook_bot_email, ''))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(webhook_bot_email, 'zulip', ''))
with mock.patch('logging.warning') as mock_warning:
with self.assertRaisesRegex(JsonableError,
@ -255,8 +255,8 @@ class DecoratorTestCase(TestCase):
api_result = my_webhook(request)
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(webhook_bot_email, 'acme'))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(webhook_bot_email, 'zulip', 'acme'))
request.host = "zulip.testserver"
# Test when content_type is application/json and request.body
@ -931,8 +931,8 @@ class TestValidateApiKey(ZulipTestCase):
self.default_bot.api_key)
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(self.default_bot.email, ''))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(self.default_bot.email, 'zulip', ''))
with mock.patch('logging.warning') as mock_warning:
with self.assertRaisesRegex(JsonableError,
@ -942,8 +942,8 @@ class TestValidateApiKey(ZulipTestCase):
self.default_bot.api_key)
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(self.default_bot.email, 'acme'))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(self.default_bot.email, 'zulip', 'acme'))
def _change_is_active_field(self, profile, value):
# type: (UserProfile, bool) -> None
@ -1073,8 +1073,8 @@ class TestAuthenticatedJsonPostViewDecorator(ZulipTestCase):
"Account is not associated with this "
"subdomain")
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(user_email, ''))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(user_email, 'zulip', ''))
with mock.patch('logging.warning') as mock_warning, \
mock.patch('zerver.decorator.get_subdomain', return_value='acme'):
@ -1082,8 +1082,8 @@ class TestAuthenticatedJsonPostViewDecorator(ZulipTestCase):
"Account is not associated with this "
"subdomain")
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(user_email, 'acme'))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(user_email, 'zulip', 'acme'))
def test_authenticated_json_post_view_if_user_is_incoming_webhook(self):
# type: () -> None
@ -1142,8 +1142,8 @@ class TestAuthenticatedJsonViewDecorator(ZulipTestCase):
"Account is not associated with this "
"subdomain")
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(user_email, ''))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(user_email, 'zulip', ''))
with mock.patch('logging.warning') as mock_warning, \
mock.patch('zerver.decorator.get_subdomain', return_value='acme'):
@ -1151,8 +1151,8 @@ class TestAuthenticatedJsonViewDecorator(ZulipTestCase):
"Account is not associated with this "
"subdomain")
mock_warning.assert_called_with(
"User {} attempted to access API on wrong "
"subdomain {}".format(user_email, 'acme'))
"User {} ({}) attempted to access API on wrong "
"subdomain ({})".format(user_email, 'zulip', 'acme'))
def _do_test(self, user_email):
# type: (str) -> HttpResponse