1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
########################################################################
# File name: test_errors.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import contextlib
import enum
import gettext
import unittest
import unittest.mock
import aioxmpp
import aioxmpp.errors as errors
import aioxmpp.i18n as i18n
import aioxmpp.structs as structs
import aioxmpp.xso as xso
from aioxmpp.utils import etree, namespaces
class Testformat_error_text(unittest.TestCase):
def test_only_condition(self):
self.assertEqual(
"{urn:ietf:params:xml:ns:xmpp-stanzas}internal-server-error",
errors.format_error_text(
condition=errors.ErrorCondition.INTERNAL_SERVER_ERROR
)
)
def test_with_text(self):
self.assertEqual(
"{urn:ietf:params:xml:ns:xmpp-stanzas}bad-request ('foobar')",
errors.format_error_text(
condition=errors.ErrorCondition.BAD_REQUEST,
text="foobar",
)
)
def test_with_application_defined(self):
class Appcond:
TAG = ("uri:bar", "value-error")
self.assertEqual(
"{urn:ietf:params:xml:ns:xmpp-stanzas}gone/{uri:bar}value-error",
errors.format_error_text(
condition=errors.ErrorCondition.GONE,
application_defined_condition=Appcond(),
)
)
def test_with_application_defined_and_text(self):
class Appcond:
TAG = ("uri:bar", "value-error")
self.assertEqual(
"{urn:ietf:params:xml:ns:xmpp-stanzas}undefined-condition/{uri:bar}value-error ('that’s not an integer')",
errors.format_error_text(
condition=errors.ErrorCondition.UNDEFINED_CONDITION,
application_defined_condition=Appcond(),
text="that’s not an integer"
)
)
class TestErrorCondition(unittest.TestCase):
def test_is_enum(self):
self.assertTrue(issubclass(errors.ErrorCondition, enum.Enum))
@unittest.skipIf(aioxmpp.version_info >= (1, 0, 0),
"does not apply to this version of aioxmpp")
def test_uses_compat_mixin(self):
self.assertTrue(
issubclass(
errors.ErrorCondition,
structs.CompatibilityMixin,
)
)
def test_uses_xso_enum_mixin(self):
self.assertTrue(issubclass(errors.ErrorCondition, xso.XSOEnumMixin))
def test_gone_new_address(self):
self.assertIsInstance(
errors.ErrorCondition.GONE.xso_class.new_address,
xso.Text,
)
self.assertIsInstance(
errors.ErrorCondition.GONE.xso_class.new_address.type_,
xso.String,
)
def test_gone_new_address(self):
self.assertIsInstance(
errors.ErrorCondition.REDIRECT.xso_class.new_address,
xso.Text,
)
self.assertIsInstance(
errors.ErrorCondition.REDIRECT.xso_class.new_address.type_,
xso.String,
)
class TestStreamError(unittest.TestCase):
def test_init_formats_condition(self):
with unittest.mock.patch("aioxmpp.errors.format_error_text") as format_:
errors.StreamError(
errors.StreamErrorCondition.INTERNAL_SERVER_ERROR,
text=unittest.mock.sentinel.text,
)
format_.assert_called_once_with(
errors.StreamErrorCondition.INTERNAL_SERVER_ERROR,
unittest.mock.sentinel.text,
)
@unittest.skipIf(aioxmpp.version_info >= (1, 0, 0),
"does not apply to this version of aioxmpp")
def test_init_converts_tuple_to_condition_and_warns(self):
with contextlib.ExitStack() as stack:
format_ = stack.enter_context(unittest.mock.patch(
"aioxmpp.errors.format_error_text"
))
ctx = stack.enter_context(self.assertWarnsRegex(
DeprecationWarning,
r"as of aioxmpp 1\.0, stream error conditions must be members "
r"of the aioxmpp\.errors\.StreamErrorCondition enumeration"
))
errors.StreamError(
(namespaces.streams, "not-authorized"),
text=unittest.mock.sentinel.text,
)
format_.assert_called_once_with(
errors.StreamErrorCondition.NOT_AUTHORIZED,
unittest.mock.sentinel.text,
)
self.assertTrue(
ctx.filename.endswith("test_errors.py"),
)
class TestXMPPError(unittest.TestCase):
def test_init_formats_condition(self):
with unittest.mock.patch("aioxmpp.errors.format_error_text") as format_:
errors.XMPPError(
errors.ErrorCondition.INTERNAL_SERVER_ERROR,
text=unittest.mock.sentinel.text,
application_defined_condition=unittest.mock.sentinel.appcond,
)
format_.assert_called_once_with(
errors.ErrorCondition.INTERNAL_SERVER_ERROR,
text=unittest.mock.sentinel.text,
application_defined_condition=unittest.mock.sentinel.appcond,
)
@unittest.skipIf(aioxmpp.version_info >= (1, 0, 0),
"does not apply to this version of aioxmpp")
def test_init_converts_tuple_to_condition_and_warns(self):
with contextlib.ExitStack() as stack:
format_ = stack.enter_context(unittest.mock.patch(
"aioxmpp.errors.format_error_text"
))
ctx = stack.enter_context(self.assertWarnsRegex(
DeprecationWarning,
r"as of aioxmpp 1\.0, error conditions must be members of the "
r"aioxmpp\.ErrorCondition enumeration"
))
errors.XMPPError(
(namespaces.stanzas, "bad-request"),
text=unittest.mock.sentinel.text,
application_defined_condition=unittest.mock.sentinel.appcond,
)
format_.assert_called_once_with(
errors.ErrorCondition.BAD_REQUEST,
text=unittest.mock.sentinel.text,
application_defined_condition=unittest.mock.sentinel.appcond,
)
self.assertTrue(
ctx.filename.endswith("test_errors.py"),
)
def test_init_sets_attributes_from_enum_member(self):
appcond = unittest.mock.Mock(["TAG"])
appcond.TAG = ("a", "b")
exc = errors.XMPPError(
errors.ErrorCondition.INTERNAL_SERVER_ERROR,
text=unittest.mock.sentinel.text,
application_defined_condition=appcond,
)
self.assertEqual(exc.condition,
errors.ErrorCondition.INTERNAL_SERVER_ERROR)
self.assertEqual(exc.text, unittest.mock.sentinel.text)
self.assertIsInstance(
exc.condition_obj,
errors.ErrorCondition.INTERNAL_SERVER_ERROR.xso_class
)
self.assertEqual(exc.application_defined_condition, appcond)
def test_init_sets_attributes_from_xso(self):
appcond = unittest.mock.Mock(["TAG"])
appcond.TAG = ("a", "b")
condition_obj = errors.ErrorCondition.GONE.to_xso()
condition_obj.new_address = "foo"
exc = errors.XMPPError(
condition_obj,
text=unittest.mock.sentinel.text,
application_defined_condition=appcond,
)
self.assertEqual(exc.condition,
errors.ErrorCondition.GONE)
self.assertEqual(exc.text, unittest.mock.sentinel.text)
self.assertIs(
exc.condition_obj,
condition_obj
)
self.assertEqual(exc.application_defined_condition, appcond)
class TestErroneousStanza(unittest.TestCase):
def test_is_exception(self):
self.assertTrue(issubclass(
errors.ErroneousStanza,
errors.StanzaError
))
def test_init(self):
obj = object()
exc = errors.ErroneousStanza(obj)
self.assertIs(exc.partial_obj, obj)
self.assertTrue(
str(exc),
"erroneous stanza received: {!r}".format(obj)
)
class TestMultiOSError(unittest.TestCase):
def test_message(self):
exc = errors.MultiOSError("foo",
[ValueError("bar"),
ValueError("baz")])
self.assertEqual(
"foo: multiple errors: bar, baz",
str(exc)
)
def test_flatten(self):
base_excs1 = [OSError(), OSError()]
base_excs2 = [OSError(), OSError()]
exc3 = OSError()
excs = [errors.MultiOSError("foo", base_excs1),
errors.MultiOSError("foo", base_excs2),
exc3]
exc = errors.MultiOSError("bar", excs)
self.assertSequenceEqual(
base_excs1+base_excs2+[exc3],
exc.exceptions
)
class TestUserError(unittest.TestCase):
def test_simple(self):
s = unittest.mock.Mock()
ue = errors.UserError(s)
self.assertSequenceEqual(
[
unittest.mock.call.localize(
errors.UserError.DEFAULT_FORMATTER,
errors.UserError.DEFAULT_TRANSLATIONS,
)
],
s.mock_calls
)
self.assertEqual(
str(s.localize()),
str(ue),
)
def test_format(self):
s = unittest.mock.Mock()
ue = errors.UserError(s, 10, abc="baz")
self.assertSequenceEqual(
[
unittest.mock.call.localize(
errors.UserError.DEFAULT_FORMATTER,
errors.UserError.DEFAULT_TRANSLATIONS,
10,
abc="baz"
),
],
s.mock_calls
)
self.assertEqual(
str(s.localize()),
str(ue),
)
def test_localize(self):
fmt = i18n.LocalizingFormatter()
tx = gettext.NullTranslations()
s = unittest.mock.Mock()
ue = errors.UserError(s, 10, abc="baz")
expected_result = s.localize()
s.reset_mock()
self.assertEqual(
expected_result,
ue.localize(fmt, tx)
)
self.assertSequenceEqual(
[
unittest.mock.call.localize(
fmt,
tx,
10,
abc="baz")
],
s.mock_calls
)
class TestUserValueError(unittest.TestCase):
def test_is_value_and_user_error(self):
self.assertTrue(issubclass(
errors.UserValueError,
errors.UserError,
))
self.assertTrue(issubclass(
errors.UserValueError,
ValueError
))
|