Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import aioxmpp.disco.service as disco_service
import aioxmpp.disco.xso as disco_xso
import aioxmpp.stanza as stanza
import aioxmpp.structs as structs
import aioxmpp.errors as errors
from aioxmpp.utils import namespaces
from aioxmpp.testutils import (
make_connected_client,
run_coroutine,
CoroutineMock,
)
TEST_JID = structs.JID.fromstr("foo@bar.example")
class TestNode(unittest.TestCase):
def test_init(self):
n = disco_service.Node()
self.assertSequenceEqual(
[],
list(n.iter_identities(unittest.mock.sentinel.stanza))
)
self.assertSetEqual(
{namespaces.xep0030_info},
set(n.iter_features(unittest.mock.sentinel.stanza))
)
self.assertSequenceEqual(
[],
list(n.iter_items(unittest.mock.sentinel.stanza))
def setUp(self):
self.client_jid = structs.JID.fromstr("foo@bar.example")
self.loop = asyncio.get_event_loop()
self.transport = unittest.mock.Mock()
self.transport.get_extra_info.return_value = object()
self.xmlstream = XMLStreamMock(self, loop=self.loop)
self.xmlstream.transport = self.transport
self.mechanisms = security_layer.SASLMechanisms()
self.features = nonza.StreamFeatures()
self.features[...] = self.mechanisms
self.post_sasl_features = nonza.StreamFeatures()
self.password_provider = unittest.mock.MagicMock()
@unittest.mock.patch.object(roster_service.Item, "update_from_xso_item")
def test_from_xso_item(self, update_from_xso_item):
xso_item = roster_xso.Item(
jid=structs.JID.fromstr("user@bar.example"),
subscription="from",
ask=None,
approved=True)
item = roster_service.Item.from_xso_item(xso_item)
self.assertEqual(xso_item.jid, item.jid)
self.assertSequenceEqual(
[
unittest.mock.call(xso_item)
],
update_from_xso_item.mock_calls
)
import aioxmpp.forms
import aioxmpp.service
import aioxmpp.stanza
import aioxmpp.structs
import aioxmpp.pubsub.service as pubsub_service
import aioxmpp.pubsub.xso as pubsub_xso
from aioxmpp.testutils import (
make_connected_client,
CoroutineMock,
run_coroutine,
)
TEST_FROM = aioxmpp.structs.JID.fromstr("foo@bar.example/baz")
TEST_JID1 = aioxmpp.structs.JID.fromstr("bar@bar.example/baz")
TEST_JID2 = aioxmpp.structs.JID.fromstr("baz@bar.example/baz")
TEST_JID3 = aioxmpp.structs.JID.fromstr("fnord@bar.example/baz")
TEST_TO = aioxmpp.structs.JID.fromstr("pubsub.example")
@pubsub_xso.as_payload_class
class SomePayload(aioxmpp.xso.XSO):
TAG = "aioxmpp.tests.pubsub.test_service", "foo"
class TestService(unittest.TestCase):
def test_is_service(self):
self.assertTrue(issubclass(
pubsub_service.PubSubClient,
aioxmpp.service.Service
))
def test_fromstr_domain(self):
self.assertEqual(
structs.JID(None, "example.test", None),
structs.JID.fromstr("example.test")
)
self.assertEqual(
structs.JID(None, "IX.test", None),
structs.JID.fromstr("ix.test")
)
import mlxc.roster
import mlxcqt.roster
import mlxcqt.Qt as Qt
TEST_ACCOUNT_JID = aioxmpp.structs.JID.fromstr(
"foo@a.example"
)
TEST_PEER_JID1 = aioxmpp.structs.JID.fromstr(
"foo@b.example"
)
TEST_PEER_JID2 = aioxmpp.structs.JID.fromstr(
"bar@b.example"
)
class TestViaView(unittest.TestCase):
def setUp(self):
self.item = aioxmpp.roster.Item(TEST_PEER_JID1)
self.via = mlxc.roster.Via(TEST_ACCOUNT_JID, self.item)
self.view = mlxcqt.roster.ViaView(self.via)
def test_data_returns_label_in_DisplayRole(self):
with unittest.mock.patch.object(
mlxc.roster.Via,
"label") as label:
self.assertEqual(
self.view.data(0, Qt.Qt.DisplayRole),
self.destroyed_rec.return_value = None
self.presence_sent_rec = unittest.mock.MagicMock()
self.presence_sent_rec.return_value = None
self.security_layer = object()
self.loop = asyncio.get_event_loop()
self.patches = [
unittest.mock.patch("aioxmpp.node.connect_secured_xmlstream",
self._connect_secured_xmlstream),
unittest.mock.patch("aioxmpp.stanza.StanzaBase.autoset_id",
self._autoset_id),
]
self.connect_secured_xmlstream, _ = (patch.start()
for patch in self.patches)
self._xmlstream = XMLStreamMock(self, loop=self.loop)
self.test_jid = structs.JID.fromstr("foo@bar.example/baz")
self.features = nonza.StreamFeatures()
self.features[...] = rfc6120.BindFeature()
self.client = node.PresenceManagedClient(
self.test_jid,
self.security_layer,
loop=self.loop)
self.client.on_failure.connect(self.failure_rec)
self.client.on_stream_destroyed.connect(self.destroyed_rec)
self.client.on_stream_established.connect(self.established_rec)
self.client.on_presence_sent.connect(self.presence_sent_rec)
self.resource_binding = [
XMLStreamMock.Send(
stanza.IQ(
payload=rfc6120.Bind(
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
def test_set_info_cache(self):
to = structs.JID.fromstr("user@foo.example/res1")
response = disco_xso.ItemsQuery()
self.s.set_info_cache(
to,
None,
response
)
other_response = disco_xso.InfoQuery()
self.cc.send.return_value = \
other_response
result = run_coroutine(self.s.query_info(to, node=None))
self.assertIs(result, response)
self.assertFalse(self.cc.stream.mock_calls)
def test_query_info_reraises_but_does_not_cache_exception(self):
to = structs.JID.fromstr("user@foo.example/res1")
self.cc.send.side_effect = \
errors.XMPPCancelError(
condition=errors.ErrorCondition.FEATURE_NOT_IMPLEMENTED,
)
with self.assertRaises(errors.XMPPCancelError):
run_coroutine(
self.s.query_items(to, node="foobar")
)
self.cc.send.side_effect = \
ConnectionError()
with self.assertRaises(ConnectionError):
run_coroutine(