Exemple #1
0
    def test_track_available_resources(self):
        st1 = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                              from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st1)

        self.assertDictEqual(
            {
                "foo": st1
            },
            self.s.get_peer_resources(TEST_PEER_JID1)
        )

        st2 = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st2)

        self.assertDictEqual(
            {
                "foo": st1,
                "bar": st2,
            },
            self.s.get_peer_resources(TEST_PEER_JID1)
        )

        st = stanza.Presence(type_=structs.PresenceType.UNAVAILABLE,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertDictEqual(
            {
                "bar": st2
            },
            self.s.get_peer_resources(TEST_PEER_JID1)
        )
Exemple #2
0
    def test_handle_presence_emits_available_signals_only_if_not_available(self):
        base = unittest.mock.Mock()
        base.bare.return_value = False
        base.full.return_value = False

        self.s.on_bare_available.connect(base.bare)
        self.s.on_available.connect(base.full)

        st1 = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                              from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st1)

        st2 = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st2)

        st3 = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st3)

        self.assertSequenceEqual(
            base.mock_calls,
            [
                unittest.mock.call.bare(st1),
                unittest.mock.call.full(st1.from_, st1),
                unittest.mock.call.full(st2.from_, st2),
            ]
        )
Exemple #3
0
    def test_handle_presence_emits_unavailable_signals(self):
        base = unittest.mock.Mock()
        base.bare.return_value = False
        base.full.return_value = False

        self.s.on_bare_unavailable.connect(base.bare)
        self.s.on_unavailable.connect(base.full)

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="foo")))

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="bar")))

        st2 = stanza.Presence(type_="unavailable",
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st2)

        st1 = stanza.Presence(type_="unavailable",
                              from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st1)

        self.assertSequenceEqual(base.mock_calls, [
            unittest.mock.call.full(st2.from_, st2),
            unittest.mock.call.full(st1.from_, st1),
            unittest.mock.call.bare(st1),
        ])
Exemple #4
0
    def test_handle_presence_emits_changed_signals(self):
        base = unittest.mock.Mock()
        base.bare.return_value = False
        base.full.return_value = False

        self.s.on_changed.connect(base.full)

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="foo")))

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="bar")))

        st1 = stanza.Presence(type_=None,
                              show="dnd",
                              from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st1)

        st2 = stanza.Presence(type_=None,
                              show="dnd",
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st2)

        st3 = stanza.Presence(type_=None,
                              show="chat",
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st3)

        self.assertSequenceEqual(base.mock_calls, [
            unittest.mock.call.full(st1.from_, st1),
            unittest.mock.call.full(st2.from_, st2),
            unittest.mock.call.full(st3.from_, st3),
        ])
Exemple #5
0
    def test_handle_presence_emits_unavailable_on_error(self):
        base = unittest.mock.Mock()
        base.bare.return_value = False
        base.full.return_value = False

        self.s.on_unavailable.connect(base.full)
        self.s.on_bare_unavailable.connect(base.bare)

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="foo")))

        self.s.handle_presence(
            stanza.Presence(type_=None,
                            from_=TEST_PEER_JID1.replace(resource="bar")))

        st = stanza.Presence(type_="error", from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIn(
            unittest.mock.call.full(st.from_.replace(resource="foo"), st),
            base.mock_calls)

        self.assertIn(
            unittest.mock.call.full(st.from_.replace(resource="bar"), st),
            base.mock_calls)

        self.assertIn(unittest.mock.call.bare(st), base.mock_calls)
Exemple #6
0
    def test_get_most_available_stanza(self):
        st = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(
            self.s.get_most_available_stanza(TEST_PEER_JID1),
            st
        )

        staway = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                                 show=structs.PresenceShow.AWAY,
                                 from_=TEST_PEER_JID1.replace(resource="baz"))
        self.s.handle_presence(staway)

        self.assertIs(
            self.s.get_most_available_stanza(TEST_PEER_JID1),
            st
        )

        stdnd = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                                show=structs.PresenceShow.DND,
                                from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(stdnd)

        self.assertEqual(
            len(self.s.get_peer_resources(TEST_PEER_JID1)),
            3
        )

        self.assertIs(
            self.s.get_most_available_stanza(TEST_PEER_JID1),
            stdnd
        )
Exemple #7
0
    def test_error_stanza_overrides_all_other_stanzas(self):
        st = stanza.Presence(type_=None,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)

        st = stanza.Presence(type_="error", from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_.replace(resource="foo")), st)
Exemple #8
0
    def test_from_stanza(self):
        stanza_obj = stanza.Presence(type_=structs.PresenceType.AVAILABLE)
        stanza_obj.show = structs.PresenceShow.XA
        self.assertEqual(
            structs.PresenceState(available=True,
                                  show=structs.PresenceShow.XA),
            structs.PresenceState.from_stanza(stanza_obj))

        stanza_obj = stanza.Presence(type_=structs.PresenceType.UNAVAILABLE, )
        self.assertEqual(structs.PresenceState(available=False),
                         structs.PresenceState.from_stanza(stanza_obj))
Exemple #9
0
    def test_error_stanza_overrides_all_other_stanzas(self):
        st = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)

        st = stanza.Presence(type_=structs.PresenceType.ERROR,
                             from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_.replace(resource="foo")), st)
Exemple #10
0
    def test_get_stanza_returns_original_stanza_as_received(self):
        st = stanza.Presence(type_=None,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)

        st = stanza.Presence(type_=None,
                             from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)
Exemple #11
0
    def test_from_stanza(self):
        stanza_obj = stanza.Presence(type_=None)
        stanza_obj.show = "xa"
        self.assertEqual(
            structs.PresenceState(available=True, show="xa"),
            structs.PresenceState.from_stanza(stanza_obj)
        )

        stanza_obj = stanza.Presence(type_="unavailable")
        self.assertEqual(
            structs.PresenceState(available=False),
            structs.PresenceState.from_stanza(stanza_obj)
        )
Exemple #12
0
    def test_handle_unsubscribe_emits_event(self):
        st = stanza.Presence(type_="unsubscribe", from_=TEST_JID)

        mock = unittest.mock.Mock()
        self.s.on_unsubscribe.connect(mock)
        self.s.handle_unsubscribe(st)
        self.assertSequenceEqual(mock.mock_calls, [unittest.mock.call(st)])
Exemple #13
0
    def test_apply_to_stanza(self):
        stanza_obj = stanza.Presence(type_="probe")
        self.assertEqual(
            "probe",
            stanza_obj.type_
        )
        self.assertIsNone(stanza_obj.show)

        ps = structs.PresenceState(available=True, show="dnd")
        ps.apply_to_stanza(stanza_obj)
        self.assertEqual(
            None,
            stanza_obj.type_
        )
        self.assertEqual(
            "dnd",
            stanza_obj.show
        )

        ps = structs.PresenceState()
        ps.apply_to_stanza(stanza_obj)
        self.assertEqual(
            "unavailable",
            stanza_obj.type_
        )
        self.assertIsNone(
            stanza_obj.show
        )
Exemple #14
0
    def test_apply_to_stanza(self):
        stanza_obj = stanza.Presence(type_=structs.PresenceType.PROBE)
        self.assertEqual(stanza_obj.show, structs.PresenceShow.NONE)

        ps = structs.PresenceState(available=True,
                                   show=structs.PresenceShow.DND)
        ps.apply_to_stanza(stanza_obj)
        self.assertEqual(
            structs.PresenceType.AVAILABLE,
            stanza_obj.type_
        )
        self.assertEqual(
            structs.PresenceShow.DND,
            stanza_obj.show
        )

        ps = structs.PresenceState()
        ps.apply_to_stanza(stanza_obj)
        self.assertEqual(
            structs.PresenceType.UNAVAILABLE,
            stanza_obj.type_
        )
        self.assertEqual(
            stanza_obj.show,
            structs.PresenceShow.NONE,
        )
Exemple #15
0
 def test_from_stanza_nonstrict_by_default(self):
     stanza_obj = stanza.Presence(type_="unavailable")
     stanza_obj.show = "dnd"
     self.assertEqual(
         structs.PresenceState(available=False),
         structs.PresenceState.from_stanza(stanza_obj)
     )
Exemple #16
0
 def test_from_stanza_strict_by_default(self):
     stanza_obj = stanza.Presence(
         type_=structs.PresenceType.UNAVAILABLE,
     )
     stanza_obj.show = structs.PresenceShow.DND
     with self.assertRaises(ValueError):
         structs.PresenceState.from_stanza(stanza_obj, strict=True)
Exemple #17
0
 def unsubscribe(self, peer_jid):
     """
     Unsubscribe from the presence of the given `peer_jid`.
     """
     self.client.enqueue(
         stanza.Presence(type_=structs.PresenceType.UNSUBSCRIBE,
                         to=peer_jid))
Exemple #18
0
    def test_get_stanza_returns_error_stanza_for_full_jid_as_received_for_bare_jid(
            self):
        st = stanza.Presence(type_=structs.PresenceType.ERROR,
                             from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_.replace(resource="foo")), st)
Exemple #19
0
    def test_handle_presence_does_not_emit_unavailable_if_already_unavailable(
            self):
        base = unittest.mock.Mock()
        base.bare.return_value = False
        base.full.return_value = False

        self.s.on_bare_unavailable.connect(base.bare)
        self.s.on_unavailable.connect(base.full)

        st2 = stanza.Presence(type_="unavailable",
                              from_=TEST_PEER_JID1.replace(resource="bar"))
        self.s.handle_presence(st2)

        st1 = stanza.Presence(type_="unavailable",
                              from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st1)

        self.assertSequenceEqual(base.mock_calls, [])
Exemple #20
0
    def test_get_any_non_error_stanza_erases_error_stanza(self):
        st = stanza.Presence(type_="error", from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)

        st = stanza.Presence(type_="unavailable",
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIsNone(self.s.get_stanza(st.from_.bare()))
        self.assertIsNone(self.s.get_stanza(st.from_))

        st = stanza.Presence(type_=None,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)
Exemple #21
0
 def test_from_stanza_nonstrict_by_default(self):
     stanza_obj = stanza.Presence(
         type_=structs.PresenceType.UNAVAILABLE
     )
     stanza_obj.show = structs.PresenceShow.AWAY
     self.assertEqual(
         structs.PresenceState(available=False),
         structs.PresenceState.from_stanza(stanza_obj)
     )
Exemple #22
0
    def test_get_any_non_error_stanza_erases_error_stanza(self):
        st = stanza.Presence(type_=structs.PresenceType.ERROR,
                             from_=TEST_PEER_JID1)
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)

        st = stanza.Presence(type_=structs.PresenceType.UNAVAILABLE,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIsNone(self.s.get_stanza(st.from_.bare()))
        self.assertIsNone(self.s.get_stanza(st.from_))

        st = stanza.Presence(type_=structs.PresenceType.AVAILABLE,
                             from_=TEST_PEER_JID1.replace(resource="foo"))
        self.s.handle_presence(st)

        self.assertIs(self.s.get_stanza(st.from_), st)
Exemple #23
0
 def test_init(self):
     s = stanza.Presence(from_=TEST_FROM,
                         type_=structs.PresenceType.PROBE,
                         show="away")
     self.assertEqual(TEST_FROM, s.from_)
     self.assertEqual(structs.PresenceType.PROBE, s.type_)
     self.assertEqual(
         "away",
         s.show,
     )
Exemple #24
0
    def subscribe(self, peer_jid):
        """
        Request presence subscription with the given `peer_jid`.

        This is deliberately not a coroutine; we don’t know whether the peer is
        online (usually) and they may defer the confirmation very long, if they
        confirm at all. Use :meth:`on_subscribed` to get notified when a peer
        accepted a subscription request.
        """
        self.client.stream.enqueue_stanza(
            stanza.Presence(type_="subscribe", to=peer_jid))