Esempio n. 1
0
    def test_validate_api_key_if_api_key_does_not_match_profile_api_key(self):
        # type: () -> None
        with self.assertRaises(JsonableError):
            validate_api_key(HostRequestMock(), self.webhook_bot.email, 'not_32_length')

        with self.assertRaises(JsonableError):
            validate_api_key(HostRequestMock(), self.webhook_bot.email, self.default_bot.api_key)
Esempio n. 2
0
    def test_validate_api_key_if_api_key_does_not_match_profile_api_key(self):
        # type: () -> None
        with self.assertRaises(JsonableError):
            validate_api_key(HostRequestMock(), self.webhook_bot.email, 'not_32_length')

        with self.assertRaises(JsonableError):
            validate_api_key(HostRequestMock(), self.webhook_bot.email, self.default_bot.api_key)
Esempio n. 3
0
    def test_valid_api_key_if_user_is_on_wrong_subdomain(self):
        # type: () -> None
        with self.settings(RUNNING_INSIDE_TORNADO=False):
            with mock.patch('logging.warning') as mock_warning:
                with self.assertRaisesRegex(JsonableError,
                                            "Account is not associated with this subdomain"):
                    validate_api_key(HostRequestMock(host=settings.EXTERNAL_HOST),
                                     self.default_bot.email,
                                     self.default_bot.api_key)

                mock_warning.assert_called_with(
                    "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,
                                            "Account is not associated with this subdomain"):
                    validate_api_key(HostRequestMock(host='acme.' + settings.EXTERNAL_HOST),
                                     self.default_bot.email,
                                     self.default_bot.api_key)

                mock_warning.assert_called_with(
                    "User {} ({}) attempted to access API on wrong "
                    "subdomain ({})".format(self.default_bot.email, 'zulip', 'acme'))
Esempio n. 4
0
    def test_valid_api_key_if_user_is_on_wrong_subdomain(self):
        # type: () -> None
        with self.settings(RUNNING_INSIDE_TORNADO=False):
            with mock.patch('logging.warning') as mock_warning:
                with self.assertRaisesRegex(JsonableError,
                                            "Account is not associated with this subdomain"):
                    validate_api_key(HostRequestMock(host=settings.EXTERNAL_HOST),
                                     self.default_bot.email,
                                     self.default_bot.api_key)

                mock_warning.assert_called_with(
                    "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,
                                            "Account is not associated with this subdomain"):
                    validate_api_key(HostRequestMock(host='acme.' + settings.EXTERNAL_HOST),
                                     self.default_bot.email,
                                     self.default_bot.api_key)

                mock_warning.assert_called_with(
                    "User {} ({}) attempted to access API on wrong "
                    "subdomain ({})".format(self.default_bot.email, 'zulip', 'acme'))
Esempio n. 5
0
 def test_validate_api_key_if_profile_is_not_active(self):
     self._change_is_active_field(self.default_bot, False)
     with self.assertRaises(JsonableError):
         validate_api_key(self.default_bot.email, self.default_bot.api_key)
     self._change_is_active_field(self.default_bot, True)
Esempio n. 6
0
    def test_validate_api_key_if_api_key_does_not_match_profile_api_key(self):
        with self.assertRaises(JsonableError):
            validate_api_key(self.webhook_bot.email, 'not_32_length')

        with self.assertRaises(JsonableError):
            validate_api_key(self.webhook_bot.email, self.default_bot.api_key)
Esempio n. 7
0
 def test_validate_api_key_if_profile_does_not_exist(self):
     with self.assertRaises(JsonableError):
         validate_api_key('*****@*****.**', 'api_key')
Esempio n. 8
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_set(self):
     # type: () -> None
     profile = validate_api_key(HostRequestMock(host="zulip.testserver"),
                                self.webhook_bot.email, self.webhook_bot.api_key,
                                is_webhook=True)
     self.assertEqual(profile.id, self.webhook_bot.id)
Esempio n. 9
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_unset(self):
     # type: () -> None
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), self.webhook_bot.email, self.webhook_bot.api_key)
Esempio n. 10
0
 def test_validate_api_key_if_profile_does_not_exist(self):
     # type: () -> None
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), '*****@*****.**', 'api_key')
Esempio n. 11
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_set(self):
     # type: () -> None
     profile = validate_api_key(HostRequestMock(host="zulip.testserver"),
                                self.webhook_bot.email, self.webhook_bot.api_key,
                                is_webhook=True)
     self.assertEqual(profile.id, self.webhook_bot.id)
Esempio n. 12
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_unset(self):
     # type: () -> None
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), self.webhook_bot.email, self.webhook_bot.api_key)
Esempio n. 13
0
 def test_validate_api_key_if_profile_is_not_active(self):
     # type: () -> None
     self._change_is_active_field(self.default_bot, False)
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), self.default_bot.email, self.default_bot.api_key)
     self._change_is_active_field(self.default_bot, True)
Esempio n. 14
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_unset(self):
     with self.assertRaises(JsonableError):
         validate_api_key(self.webhook_bot.email, self.webhook_bot.api_key)
Esempio n. 15
0
 def test_validate_api_key_if_profile_is_not_active(self):
     # type: () -> None
     self._change_is_active_field(self.default_bot, False)
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), self.default_bot.email, self.default_bot.api_key)
     self._change_is_active_field(self.default_bot, True)
Esempio n. 16
0
 def test_validate_api_key_if_profile_is_incoming_webhook_and_is_webhook_is_set(self):
     profile = validate_api_key(self.webhook_bot.email, self.webhook_bot.api_key, is_webhook=True)
     self.assertEqual(profile.pk, self.webhook_bot.pk)
Esempio n. 17
0
 def test_validate_api_key_if_profile_does_not_exist(self):
     # type: () -> None
     with self.assertRaises(JsonableError):
         validate_api_key(HostRequestMock(), '*****@*****.**', 'api_key')