File: test_service.py

package info (click to toggle)
python-aioxmpp 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 6,244 kB
  • sloc: python: 97,761; xml: 215; makefile: 155; sh: 63
file content (280 lines) | stat: -rw-r--r-- 8,959 bytes parent folder | download | duplicates (3)
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
########################################################################
# File name: test_service.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 unittest
import unittest.mock

import aioxmpp.disco
import aioxmpp.mdr.service as mdr_service
import aioxmpp.mdr.xso as mdr_xso
import aioxmpp.service
import aioxmpp.tracking

from aioxmpp.testutils import (
    make_connected_client,
)


TEST_FROM = aioxmpp.JID.fromstr("juliet@capulet.example/balcony")
TEST_TO = aioxmpp.JID.fromstr("romeo@montague.example")


class TestDeliveryReceiptsService(unittest.TestCase):
    def setUp(self):
        self.cc = make_connected_client()
        self.disco_svr = aioxmpp.DiscoServer(self.cc)
        self.t = aioxmpp.tracking.MessageTracker()
        self.s = mdr_service.DeliveryReceiptsService(self.cc, dependencies={
            aioxmpp.DiscoServer: self.disco_svr,
        })
        self.msg = unittest.mock.Mock(spec=aioxmpp.Message)
        self.msg.xep0184_received = None
        self.msg.xep0184_request_receipt = False
        self.msg.to = TEST_TO
        self.msg.id_ = "foo"

    def tearDown(self):
        del self.s
        del self.cc

    def test_is_service(self):
        self.assertIsInstance(
            self.s,
            aioxmpp.service.Service,
        )

    def test_registers_inbound_message_filter(self):
        self.assertTrue(
            aioxmpp.service.is_inbound_message_filter(
                mdr_service.DeliveryReceiptsService._inbound_message_filter,
            )
        )

    def test_inbound_message_filter_returns_random_stanza(self):
        stanza = unittest.mock.Mock(spec=aioxmpp.Message)
        stanza.xep0184_received = None
        self.assertIs(
            self.s._inbound_message_filter(stanza),
            stanza,
        )

    def test_declares_disco_feature(self):
        self.assertIsInstance(
            mdr_service.DeliveryReceiptsService.disco_feature,
            aioxmpp.disco.register_feature,
        )
        self.assertEqual(
            mdr_service.DeliveryReceiptsService.disco_feature.feature,
            "urn:xmpp:receipts",
        )

    def test_attach_tracker_sets_xep0184_request(self):
        self.s.attach_tracker(self.msg, self.t)
        self.assertTrue(
            self.msg.xep0184_request_receipt,
        )

    def test_attach_tracker_autosets_id(self):
        self.s.attach_tracker(self.msg, self.t)
        self.msg.autoset_id.assert_called_once_with()

    def test_attach_tracker_returns_passed_tracker(self):
        t = self.s.attach_tracker(self.msg, self.t)
        self.assertIs(t, self.t)

    def test_attach_tracker_rejects_receipts(self):
        self.msg.xep0184_received = mdr_xso.Received("foo")
        with self.assertRaisesRegex(
                ValueError,
                r"requesting delivery receipts for delivery receipts is not "
                r"allowed"):
            self.s.attach_tracker(self.msg, self.t)

    def test_attach_tracker_rejects_errors(self):
        self.msg.type_ = aioxmpp.MessageType.ERROR
        with self.assertRaisesRegex(
                ValueError,
                r"requesting delivery receipts for errors is not supported"):
            self.s.attach_tracker(self.msg, self.t)

    def test_attach_tracker_creates_new_tracker_if_none_passed(self):
        with unittest.mock.patch(
                "aioxmpp.tracking.MessageTracker") as MessageTracker:
            t = self.s.attach_tracker(self.msg)

        MessageTracker.assert_called_once_with()
        self.assertEqual(t, MessageTracker())

    def test_set_tracker_state_to_DTR_on_ack_for_full_match(self):
        self.msg.to = self.msg.to.replace(resource="foo")
        self.s.attach_tracker(self.msg, self.t)

        ack = aioxmpp.Message(
            type_=aioxmpp.MessageType.CHAT,
            from_=TEST_TO.replace(resource="foo")
        )
        ack.xep0184_received = mdr_xso.Received(self.msg.id_)

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.IN_TRANSIT,
        )
        self.assertIsNone(
            self.s._inbound_message_filter(ack)
        )

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.DELIVERED_TO_RECIPIENT,
        )

    def test_handle_state_exception_from_tracker(self):
        self.msg.to = self.msg.to.replace(resource="foo")
        self.s.attach_tracker(self.msg, self.t)

        ack = aioxmpp.Message(
            type_=aioxmpp.MessageType.CHAT,
            from_=TEST_TO.replace(resource="foo")
        )
        ack.xep0184_received = mdr_xso.Received(self.msg.id_)

        self.t._set_state(aioxmpp.tracking.MessageState.SEEN_BY_RECIPIENT)

        self.assertIsNone(
            self.s._inbound_message_filter(ack)
        )

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.SEEN_BY_RECIPIENT,
        )

    def test_do_not_modify_tracker_state_on_id_mismatch(self):
        self.msg.to = self.msg.to.replace(resource="foo")
        self.s.attach_tracker(self.msg, self.t)

        ack = aioxmpp.Message(
            type_=aioxmpp.MessageType.CHAT,
            from_=TEST_TO.replace(resource="foo")
        )
        ack.xep0184_received = mdr_xso.Received("fnord")

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.IN_TRANSIT,
        )
        self.assertIsNone(
            self.s._inbound_message_filter(ack)
        )

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.IN_TRANSIT,
        )

    def test_do_not_modify_tracker_state_on_bare_jid_mismatch(self):
        self.msg.to = self.msg.to.replace(resource="foo")
        self.s.attach_tracker(self.msg, self.t)

        ack = aioxmpp.Message(
            type_=aioxmpp.MessageType.CHAT,
            from_=self.msg.to.replace(localpart="nottheromeo")
        )
        ack.xep0184_received = mdr_xso.Received(self.msg.id_)

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.IN_TRANSIT,
        )
        self.assertIsNone(
            self.s._inbound_message_filter(ack)
        )

        self.assertEqual(
            self.t.state,
            aioxmpp.tracking.MessageState.IN_TRANSIT,
        )


class Testcompose_receipt(unittest.TestCase):
    def setUp(self):
        self.msg = aioxmpp.Message(
            type_=aioxmpp.MessageType.CHAT,
            from_=TEST_FROM,
            to=TEST_TO,
        )
        self.msg.body[None] = "foo"
        self.msg.xep0184_request_receipt = True
        self.msg.autoset_id()

    def test_sends_to_bare_jid(self):
        msg = mdr_service.compose_receipt(self.msg)
        self.assertEqual(
            msg.to,
            self.msg.from_.bare()
        )

    def test_preserves_type(self):
        msg = mdr_service.compose_receipt(self.msg)
        self.assertEqual(msg.type_, self.msg.type_)

    def test_raises_on_error(self):
        with self.assertRaisesRegex(
                ValueError,
                r"receipts cannot be generated for error messages"):
            mdr_service.compose_receipt(
                aioxmpp.Message(
                    type_=aioxmpp.MessageType.ERROR
                )
            )

    def test_raises_on_receipt(self):
        self.msg.xep0184_received = mdr_xso.Received("foo")

        with self.assertRaisesRegex(
                ValueError,
                r"receipts cannot be generated for receipts"):
            mdr_service.compose_receipt(self.msg)

    def test_if_id_is_unset(self):
        self.msg.id_ = None

        with self.assertRaisesRegex(
                ValueError,
                r"receipts cannot be generated for id-less messages"):
            mdr_service.compose_receipt(self.msg)

    def test_receipt_refers_to_message_id(self):
        msg = mdr_service.compose_receipt(self.msg)

        self.assertIsInstance(
            msg.xep0184_received,
            mdr_xso.Received,
        )

        self.assertEqual(
            msg.xep0184_received.message_id,
            self.msg.id_,
        )

    def test_strips_body(self):
        msg = mdr_service.compose_receipt(self.msg)
        self.assertFalse(msg.body)