mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 14:35:27 +00:00
test_decorator: Add explicit subdomains in tests.
This commit is contained in:
@@ -273,12 +273,13 @@ class ZulipTestCase(TestCase):
|
|||||||
# type: () -> UserProfile
|
# type: () -> UserProfile
|
||||||
return get_user('notification-bot@zulip.com', get_realm('zulip'))
|
return get_user('notification-bot@zulip.com', get_realm('zulip'))
|
||||||
|
|
||||||
def login_with_return(self, email, password=None):
|
def login_with_return(self, email, password=None, **kwargs):
|
||||||
# type: (Text, Optional[Text]) -> HttpResponse
|
# type: (Text, Optional[Text], **Any) -> HttpResponse
|
||||||
if password is None:
|
if password is None:
|
||||||
password = initial_password(email)
|
password = initial_password(email)
|
||||||
return self.client_post('/accounts/login/',
|
return self.client_post('/accounts/login/',
|
||||||
{'username': email, 'password': password})
|
{'username': email, 'password': password},
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
def login(self, email, password=None, fails=False):
|
def login(self, email, password=None, fails=False):
|
||||||
# type: (Text, Optional[Text], bool) -> HttpResponse
|
# type: (Text, Optional[Text], bool) -> HttpResponse
|
||||||
|
|||||||
@@ -286,6 +286,9 @@ class HostRequestMock(object):
|
|||||||
self.path = ''
|
self.path = ''
|
||||||
self.user = user_profile
|
self.user = user_profile
|
||||||
self.method = ''
|
self.method = ''
|
||||||
|
self.body = ''
|
||||||
|
self.content_type = ''
|
||||||
|
self._email = ''
|
||||||
|
|
||||||
def get_host(self):
|
def get_host(self):
|
||||||
# type: () -> Text
|
# type: () -> Text
|
||||||
|
|||||||
@@ -219,22 +219,15 @@ class DecoratorTestCase(TestCase):
|
|||||||
# type: (HttpRequest, UserProfile) -> None
|
# type: (HttpRequest, UserProfile) -> None
|
||||||
raise Exception("raised by webhook function")
|
raise Exception("raised by webhook function")
|
||||||
|
|
||||||
class Request(HostRequestMock):
|
|
||||||
GET = {} # type: Dict[str, str]
|
|
||||||
POST = {} # type: Dict[str, str]
|
|
||||||
COOKIES = {} # type: Dict[str, str]
|
|
||||||
META = {'PATH_INFO': ''}
|
|
||||||
|
|
||||||
webhook_bot_email = 'webhook-bot@zulip.com'
|
webhook_bot_email = 'webhook-bot@zulip.com'
|
||||||
webhook_bot_realm = get_realm('zulip')
|
webhook_bot_realm = get_realm('zulip')
|
||||||
webhook_bot = get_user(webhook_bot_email, webhook_bot_realm)
|
webhook_bot = get_user(webhook_bot_email, webhook_bot_realm)
|
||||||
webhook_bot_api_key = webhook_bot.api_key
|
webhook_bot_api_key = webhook_bot.api_key
|
||||||
webhook_client_name = "ZulipClientNameWebhook"
|
webhook_client_name = "ZulipClientNameWebhook"
|
||||||
|
|
||||||
request = Request() # type: Any
|
request = HostRequestMock()
|
||||||
request.host = settings.EXTERNAL_HOST
|
|
||||||
|
|
||||||
request.POST['api_key'] = 'not_existing_api_key'
|
request.POST['api_key'] = 'not_existing_api_key'
|
||||||
|
|
||||||
with self.assertRaisesRegex(JsonableError, "Invalid API key"):
|
with self.assertRaisesRegex(JsonableError, "Invalid API key"):
|
||||||
my_webhook(request)
|
my_webhook(request)
|
||||||
|
|
||||||
@@ -261,6 +254,7 @@ class DecoratorTestCase(TestCase):
|
|||||||
"User {} attempted to access API on wrong "
|
"User {} attempted to access API on wrong "
|
||||||
"subdomain {}".format(webhook_bot_email, 'acme'))
|
"subdomain {}".format(webhook_bot_email, 'acme'))
|
||||||
|
|
||||||
|
request.host = "zulip.testserver"
|
||||||
# Test when content_type is application/json and request.body
|
# Test when content_type is application/json and request.body
|
||||||
# is valid JSON; exception raised in the webhook function
|
# is valid JSON; exception raised in the webhook function
|
||||||
# should be re-raised
|
# should be re-raised
|
||||||
@@ -671,7 +665,8 @@ class DeactivatedRealmTest(ZulipTestCase):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
do_deactivate_realm(get_realm("zulip"))
|
do_deactivate_realm(get_realm("zulip"))
|
||||||
result = self.login_with_return(self.example_email("hamlet"))
|
result = self.login_with_return(self.example_email("hamlet"),
|
||||||
|
subdomain="zulip")
|
||||||
self.assert_in_response("has been deactivated", result)
|
self.assert_in_response("has been deactivated", result)
|
||||||
|
|
||||||
def test_webhook_deactivated_realm(self):
|
def test_webhook_deactivated_realm(self):
|
||||||
@@ -823,7 +818,7 @@ class InactiveUserTest(ZulipTestCase):
|
|||||||
|
|
||||||
password = initial_password(user_profile.email)
|
password = initial_password(user_profile.email)
|
||||||
request = mock.MagicMock()
|
request = mock.MagicMock()
|
||||||
request.get_host.return_value = 'testserver'
|
request.get_host.return_value = 'zulip.testserver'
|
||||||
|
|
||||||
# Test a mirror-dummy active user.
|
# Test a mirror-dummy active user.
|
||||||
form = OurAuthenticationForm(request,
|
form = OurAuthenticationForm(request,
|
||||||
@@ -926,7 +921,9 @@ class TestValidateApiKey(ZulipTestCase):
|
|||||||
|
|
||||||
def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_set(self):
|
def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_set(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
profile = validate_api_key(HostRequestMock(), self.webhook_bot.email, self.webhook_bot.api_key, is_webhook=True)
|
profile = validate_api_key(HostRequestMock(host="zulip.testserver"),
|
||||||
|
self.webhook_bot.email, self.webhook_bot.api_key,
|
||||||
|
is_webhook=True)
|
||||||
self.assertEqual(profile.pk, self.webhook_bot.pk)
|
self.assertEqual(profile.pk, self.webhook_bot.pk)
|
||||||
|
|
||||||
def test_valid_api_key_if_user_is_on_wrong_subdomain(self):
|
def test_valid_api_key_if_user_is_on_wrong_subdomain(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user