File: test_xbr_schema_wamp_control.py

package info (click to toggle)
python-autobahn 22.7.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,404 kB
  • sloc: python: 38,356; javascript: 2,705; makefile: 905; ansic: 371; sh: 63
file content (224 lines) | stat: -rw-r--r-- 8,393 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
import os
import copy
import pkg_resources
import txaio
from unittest import skipIf

if 'USE_TWISTED' in os.environ and os.environ['USE_TWISTED']:
    from twisted.trial import unittest

    txaio.use_twisted()
else:
    import unittest

    txaio.use_asyncio()

from autobahn.xbr import HAS_XBR
from autobahn.wamp.exception import InvalidPayload

if HAS_XBR:
    from autobahn.xbr import FbsRepository


@skipIf(not HAS_XBR, 'package autobahn[xbr] not installed')
class TestFbsBase(unittest.TestCase):
    """
    FlatBuffers tests base class, loads test schemas.
    """

    def setUp(self):
        self.repo = FbsRepository('autobahn')
        self.archives = []
        for fbs_file in ['wamp-control.bfbs']:
            archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file))
            self.repo.load(archive)
            self.archives.append(archive)


class TestFbsValidatePermissionAllow(TestFbsBase):

    def test_validate_PermissionAllow_valid(self):
        tests = [
            {
                'call': True,
                'register': True,
                'publish': True,
                'subscribe': True
            },
            {
                'call': False,
                'register': False,
                'publish': False,
                'subscribe': False
            },
        ]
        for value in tests:
            try:
                self.repo.validate_obj('wamp.PermissionAllow', value)
            except Exception as exc:
                self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')

    def test_validate_PermissionAllow_invalid(self):
        tests = [
            (None, 'invalid type'),
            (666, 'invalid type'),
            (True, 'invalid type'),
            ({'some_unexpected_key': 666}, 'unexpected argument'),
            ({'call': True, 'register': True, 'publish': True}, 'missing argument'),
            ({'call': True, 'register': True, 'publish': True, 'subscribe': 666}, 'invalid type'),
            ({'call': True, 'register': True, 'publish': True, 'subscribe': None}, 'invalid type'),
            ({'call': True, 'register': True, 'publish': True, 'subscribe': True, 'some_unexpected_key': 666},
             'unexpected argument'),
        ]
        for value, expected_regex in tests:
            self.assertRaisesRegex(InvalidPayload, expected_regex,
                                   self.repo.validate_obj, 'wamp.PermissionAllow', value)


class TestFbsValidateRolePermission(TestFbsBase):

    def test_validate_RolePermission_valid(self):
        tests = [
            {},
            {
                'uri': 'com.example.',
                'match': 'prefix',
                'allow': {
                    'call': True,
                    'register': True,
                    'publish': True,
                    'subscribe': True
                },
                'disclose': {
                    'caller': True,
                    'publisher': True,
                },
                'cache': True
            },
        ]
        for value in tests:
            try:
                self.repo.validate_obj('wamp.RolePermission', value)
            except Exception as exc:
                self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')

    def test_validate_RolePermission_invalid(self):
        tests = [
            (None, 'invalid type'),
            ({'some_unexpected_key': True}, 'unexpected argument'),
            ({'uri': 'com.example.', 'allow': {'some_unexpected_key': True}}, 'unexpected argument'),
            ({'uri': 666}, 'invalid type'),
            ({'uri': 'com.example.', 'match': 'prefix', 'allow': {'call': 666}}, 'invalid type'),
            ({'uri': 666, 'match': 'prefix',
              'allow': {'call': True, 'register': True, 'publish': True, 'subscribe': True},
              'disclose': {'caller': True, 'publisher': True}, 'cache': True}, 'invalid type'),
        ]
        for value, expected_regex in tests:
            self.assertRaisesRegex(InvalidPayload, expected_regex,
                                   self.repo.validate_obj, 'wamp.RolePermission', value)


class TestFbsValidateRoleConfig(TestFbsBase):
    def setUp(self):
        super().setUp()
        self.role_config1 = {
            "name": "anonymous",
            "permissions": [{
                "uri": "",
                "match": "prefix",
                "allow": {
                    "call": True,
                    "register": True,
                    "publish": True,
                    "subscribe": True
                },
                "disclose": {
                    "caller": True,
                    "publisher": True
                },
                "cache": True
            }]
        }

    def test_RoleConfig_valid(self):
        try:
            self.repo.validate_obj('wamp.RoleConfig', self.role_config1)
        except Exception as exc:
            self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')

    def test_RoleConfig_invalid(self):
        config = copy.copy(self.role_config1)
        config['name'] = 666
        self.assertRaisesRegex(InvalidPayload, 'invalid type', self.repo.validate_obj,
                               'wamp.RoleConfig', config)

        # config = copy.copy(self.realm_config1)
        # del config['roles']
        # config['foobar'] = 666
        # self.assertRaisesRegex(InvalidPayload, 'missing positional argument', self.repo.validate_obj,
        #                        'wamp.RealmConfig', config)


class TestFbsValidateRealmConfig(TestFbsBase):
    def setUp(self):
        super().setUp()
        self.realm_config1 = {
            "name": "realm1",
            "roles": [{
                "name": "anonymous",
                "permissions": [{
                    "uri": "",
                    "match": "prefix",
                    "allow": {
                        "call": True,
                        "register": True,
                        "publish": True,
                        "subscribe": True
                    },
                    "disclose": {
                        "caller": True,
                        "publisher": True
                    },
                    "cache": True
                }]
            }]
        }

    def test_RealmConfig_valid(self):
        try:
            self.repo.validate_obj('wamp.RealmConfig', self.realm_config1)
        except Exception as exc:
            self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')

    def test_RealmConfig_invalid(self):
        config = copy.copy(self.realm_config1)
        config['name'] = 666
        self.assertRaisesRegex(InvalidPayload, 'invalid type', self.repo.validate_obj,
                               'wamp.RealmConfig', config)

    def test_start_router_realm_valid(self):
        valid_args = ['realm023', self.realm_config1]
        try:
            self.repo.validate('wamp.StartRealm', args=valid_args, kwargs={})
        except Exception as exc:
            self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')

    def test_start_router_realm_invalid(self):
        tests = [
            (None, None, 'missing positional argument'),
            (None, {}, 'missing positional argument'),
            (['realm023', {}], {'bogus': 666}, 'unexpected keyword arguments'),
            ([], None, 'missing positional argument'),
            (['realm023'], None, 'missing positional argument'),
            (['realm023', None], None, 'invalid type'),
            (['realm023', 666], None, 'invalid type'),
            (['realm023', {'name': 'realm1', 'bogus': []}], None, 'unexpected argument'),
            (['realm023', {'name': 666}], None, 'invalid type'),
            (['realm023', {'name': 'realm1', 'roles': 666}], None, 'invalid type'),
            (['realm023', {'name': 'realm1', 'roles': None}], None, 'invalid type'),
            (['realm023', {'name': 'realm1', 'roles': {}}], None, 'invalid type'),
            (['realm023', {'name': 'realm1', 'roles': [{'name': 666}]}], None, 'invalid type'),
        ]
        for args, kwargs, expected_regex in tests:
            self.assertRaisesRegex(InvalidPayload, expected_regex,
                                   self.repo.validate, 'wamp.StartRealm', args=args, kwargs=kwargs)