Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__get_image_tries_next_if_image_bytes_not_available(self):
client = self._prep_client()
self.ap.prepare_client(client)
base = unittest.mock.Mock()
base.avatar1 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar1.get_image_bytes = CoroutineMock()
base.avatar2 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar2.get_image_bytes = CoroutineMock()
base.avatar2.get_image_bytes.side_effect = \
aioxmpp.errors.XMPPCancelError(("foo", "bar"))
base.avatar3 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar3.get_image_bytes = CoroutineMock()
base.avatar3.get_image_bytes.return_value = \
unittest.mock.sentinel.image_bytes
self.avatar.get_avatar_metadata.return_value = [
base.avatar2,
base.avatar3,
base.avatar1,
]
with unittest.mock.patch("jabbercat.Qt.QImage") as QImage:
def test_password_provider_asks_user_if_super_lookup_fails(self):
jid = object()
value = object()
password = object()
base = unittest.mock.Mock()
base.super_password_provider = CoroutineMock()
base.super_password_provider.return_value = value
base.prompt_password = CoroutineMock()
base.prompt_password.return_value = password
base.mock_calls.clear()
with contextlib.ExitStack() as stack:
stack.enter_context(unittest.mock.patch.object(
mlxc.client.AccountManager,
"password_provider",
new=base.super_password_provider
))
base.super_password_provider.side_effect = KeyError()
stack.enter_context(unittest.mock.patch.object(
self.manager,
"_prompt_password",
new=base.prompt_password
def test_query_info_cache_override(self):
to = structs.JID.fromstr("user@foo.example/res1")
with unittest.mock.patch.object(
self.s,
"send_and_decode_info_query",
new=CoroutineMock()) as send_and_decode:
response1 = {}
send_and_decode.return_value = response1
result1 = run_coroutine(
self.s.query_info(to, node="foobar")
)
response2 = {}
send_and_decode.return_value = response2
result2 = run_coroutine(
self.s.query_info(to, node="foobar", require_fresh=True)
)
self.assertIs(result1, response1)
def test__fetch_in_background_makes_lookup_in_background(self):
provider = unittest.mock.Mock(spec=avatar.XMPPAvatarProvider)
provider.fetch_avatar = CoroutineMock()
self.am._fetch_in_background(unittest.mock.sentinel.account,
provider,
unittest.mock.sentinel.address)
run_coroutine(asyncio.sleep(0.01))
provider.fetch_avatar.assert_called_once_with(
unittest.mock.sentinel.address,
)
self.listener.on_avatar_changed.assert_called_once_with(
unittest.mock.sentinel.account,
unittest.mock.sentinel.address,
)
def test_unblock_jids(self):
with contextlib.ExitStack() as stack:
stack.enter_context(
unittest.mock.patch.object(
self.s, "_check_for_blocking",
new=CoroutineMock()
)
)
stack.enter_context(
unittest.mock.patch.object(
self.cc, "send",
new=CoroutineMock()
)
)
run_coroutine(self.s.unblock_jids([TEST_JID2]))
self.assertSequenceEqual(
self.s._check_for_blocking.mock_calls,
[unittest.mock.call()]
)
self.assertEqual(len(self.cc.send.mock_calls), 1)
(_, (arg,), _), = self.cc.send.mock_calls
self.assertIsInstance(arg, aioxmpp.IQ)
self.assertEqual(arg.type_, aioxmpp.IQType.SET)
self.assertIsInstance(arg.payload, blocking_xso.UnblockCommand)
aioxmpp.DiscoClient: self.disco_client,
aioxmpp.DiscoServer: self.disco,
aioxmpp.PubSubClient: self.pubsub,
})
self.s = avatar_service.AvatarService(self.cc, dependencies={
aioxmpp.DiscoClient: self.disco_client,
aioxmpp.DiscoServer: self.disco,
aioxmpp.PubSubClient: self.pubsub,
aioxmpp.PEPClient: self.pep,
aioxmpp.PresenceClient: self.presence_client,
aioxmpp.PresenceServer: self.presence_server,
aioxmpp.vcard.VCardService: self.vcard
})
self.pep._check_for_pep = CoroutineMock()
self.pep.available = CoroutineMock()
self.pep.available.return_value = True
self.cc.mock_calls.clear()
def test_set_bookmarks(self):
bookmarks = aioxmpp.bookmarks.Storage()
bookmarks.bookmarks.append(
aioxmpp.bookmarks.Conference(
"Coven", aioxmpp.JID.fromstr("coven@example.com")),
)
bookmarks.bookmarks.append(
aioxmpp.bookmarks.URL(
"Interesting",
"http://example.com/"),
)
with unittest.mock.patch.object(
self.private_xml,
"set_private_xml",
new=CoroutineMock()) as set_private_xml_mock:
run_coroutine(self.s.set_bookmarks(bookmarks))
self.assertEqual(
len(set_private_xml_mock.mock_calls),
1
)
(_, (arg,), kwargs), = set_private_xml_mock.mock_calls
self.assertEqual(len(kwargs), 0)
self.assertIs(arg, bookmarks)
def test__fetch_avatar_and_emit_signal(self):
fetch_func = CoroutineMock()
fetch_func.return_value = unittest.mock.Mock(spec=Qt.QPicture)
def capture_future(*args, features_future=None, **kwargs):
nonlocal captured_features_future
captured_features_future = features_future
return base.protocol
features = nonza.StreamFeatures()
features_future = asyncio.Future()
features_future.set_result(
features
)
base = unittest.mock.Mock()
base.protocol.starttls = CoroutineMock()
base.create_starttls_connection = CoroutineMock()
base.create_starttls_connection.return_value = (
unittest.mock.sentinel.transport,
base.protocol,
)
base.metadata.tls_required = False
base.XMLStream.return_value = base.protocol
base.XMLStream.side_effect = capture_future
base.Future.return_value = features_future
with contextlib.ExitStack() as stack:
stack.enter_context(
unittest.mock.patch(
"asyncio.Future",
new=base.Future,
)
)
def test__get_image_tries_next_if_one_fails(self):
client = self._prep_client()
self.ap.prepare_client(client)
base = unittest.mock.Mock()
base.avatar1 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar1.get_image_bytes = CoroutineMock()
base.avatar2 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar2.get_image_bytes = CoroutineMock()
base.avatar2.get_image_bytes.side_effect = \
RuntimeError()
base.avatar3 = unittest.mock.Mock(
spec=aioxmpp.avatar.service.AbstractAvatarDescriptor)
base.avatar3.get_image_bytes = CoroutineMock()
base.avatar3.get_image_bytes.return_value = \
unittest.mock.sentinel.image_bytes
self.avatar.get_avatar_metadata.return_value = [
base.avatar2,
base.avatar3,
base.avatar1,
]
with unittest.mock.patch("jabbercat.Qt.QImage") as QImage: