File: test_e2e.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 (184 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download | duplicates (2)
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
########################################################################
# File name: test_e2e.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 logging
import unittest
import uuid

import aioxmpp
import aioxmpp.pubsub
import aioxmpp.xso

from aioxmpp.utils import namespaces

from aioxmpp.e2etest import (
    TestCase,
    blocking,
    blocking_timed,
    require_feature_subset,
    require_pep,
    skip_with_quirk,
    Quirk,
)


@aioxmpp.pubsub.xso.as_payload_class
class Payload(aioxmpp.xso.XSO):
    TAG = (namespaces.aioxmpp_test, "foo")

    data = aioxmpp.xso.Text(default=None)


class TestOwnerUseCases(TestCase):
    @require_feature_subset(
        {
            namespaces.xep0060_features.PURGE_NODES.value,
            namespaces.xep0060_features.MODIFY_AFFILIATIONS.value,
            namespaces.xep0060_features.PUBLISH.value,
            namespaces.xep0060_features.PUBLISHER_AFFILIATION.value,
        },
        required_subset={
            namespaces.xep0060_features.CREATE_NODES.value,
            namespaces.xep0060_features.DELETE_NODES.value,
        }
    )
    @blocking
    async def setUp(self, pubsub_jid, supported_features):
        logging.info("using pubsub service %s, which provides %r",
                     pubsub_jid, supported_features)

        self.peer = pubsub_jid
        self.peer_features = supported_features

        self.owner = await self.provisioner.get_connected_client()
        self.pubsub = self.owner.summon(aioxmpp.PubSubClient)

        self.other = await self.provisioner.get_connected_client()

        self.node = str(uuid.uuid4())

        await self.pubsub.create(self.peer, self.node)

    @blocking
    async def tearDown(self):
        await self.pubsub.delete(self.peer, self.node)

    def _require_features(self, features):
        features = set(features)
        if features & self.peer_features != features:
            raise unittest.SkipTest(
                "selected PubSub provider ({!r}) does not provide {!r}".format(
                    self.peer,
                    features,
                )
            )

    @blocking_timed
    async def test_publish_and_purge(self):
        self._require_features({
            namespaces.xep0060_features.PURGE_NODES.value
        })

        payload = Payload()

        id_ = await self.pubsub.publish(
            self.peer,
            self.node,
            payload,
        )
        items = await self.pubsub.get_items(
            self.peer,
            self.node,
        )

        self.assertEqual(len(items.payload.items), 1)
        await self.pubsub.purge(self.peer, self.node)

        # using get items instead of get_items_by_id to work around ejabberd
        # #2288
        items = await self.pubsub.get_items(
            self.peer,
            self.node,
        )
        self.assertEqual(len(items.payload.items), 0)

    @blocking_timed
    @skip_with_quirk(Quirk.PUBSUB_GET_MULTIPLE_ITEMS_BY_ID_BROKEN)
    async def test_publish_multiple_and_get_by_id(self):
        # configure the node for multiple items
        config = await self.pubsub.get_node_config(
            self.peer,
            self.node,
        )
        form = aioxmpp.pubsub.xso.NodeConfigForm.from_xso(config)
        try:
            max_items = int(form.max_items.value)
        except ValueError:
            max_items = 0

        if max_items < 2:
            form.max_items.value = "10"
            await self.pubsub.set_node_config(
                self.peer,
                form.render_reply(),
                self.node,
            )

        payload1 = Payload()
        payload1.data = "foo"

        payload2 = Payload()
        payload2.data = "bar"

        id1 = await self.pubsub.publish(
            self.peer,
            self.node,
            payload1,
        )
        id2 = await self.pubsub.publish(
            self.peer,
            self.node,
            payload2,
        )
        items = await self.pubsub.get_items_by_id(
            self.peer,
            self.node,
            [id1, id2]
        )
        await self.pubsub.get_items(
            self.peer,
            self.node,
        )

        self.assertEqual(len(items.payload.items), 2)

        data_map = {
            item.id_: item.registered_payload.data
            for item in items.payload.items
        }

        self.assertDictEqual(
            {
                id1: "foo",
                id2: "bar",
            },
            data_map,
        )