swindon 0.2.1

An HTTP edge (frontend) server with smart websockets support
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import pytest
import aiohttp
import json
import asyncio

from unittest import mock
from async_timeout import timeout
from aiohttp import web
from aiohttp import WSMsgType
from aiohttp.web import json_response


async def test_simple_userinfo(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        assert req.headers['Content-Type'] == 'application/json'
        assert 'Authorization' not in req.headers
        expected = [
            {'connection_id': '0'},
            [],
            {'http_cookie': None,
             'http_authorization': None,
             'url_querystring': '',
             }]
        assert await req.json() == expected

        fut.set_result(
            web.Response(text='{"user_id": "user:1", "username": "John"}'))
        ws = await call.websocket
        msg = await ws.receive_json()
        assert msg == ['hello', {}, {'user_id': 'user:1', 'username': 'John'}]


async def test_ws_close_timeout(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    with timeout(1):
        async with proxy_server.swindon_chat(url) as call:
            req, fut = await call.request()
            assert req.path == '/tangle/authorize_connection'
            fut.set_result(
                web.Response(text='{"user_id": "user:1"}'))
            ws = await call.websocket
            msg = await ws.receive_json()
            assert msg == [
                'hello', {}, {'user_id': 'user:1'}]


@pytest.mark.parametrize('status_code', [
    400, 401, 404, 410, 500, 503])
async def test_error_codes(proxy_server, swindon, loop, status_code):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        fut.set_result(
            web.Response(status=status_code, body=b'Custom Error'))
        ws = await call.websocket
        msg = await ws.receive()
        assert msg.type == WSMsgType.CLOSE
        assert msg.data == 4000 + status_code
        assert msg.extra == 'backend_error'
        assert ws.closed
        assert ws.close_code == 4000 + status_code


@pytest.mark.parametrize('status_code', [
    100, 101,
    201, 204,
    300, 301, 302, 304,
    402, 405,
    501, 502, 504,  # these codes are not exposed to end-user.
    ])
@pytest.mark.parametrize('body', [b'no body', b'{"user_id": "user:1"}'])
async def test_unexpected_responses(
        proxy_server, swindon, loop, status_code, body):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        fut.set_result(
            web.Response(status=status_code, body=body))
        ws = await call.websocket
        msg = await ws.receive()
        assert msg.type == WSMsgType.CLOSE
        assert msg.data == 4500
        assert msg.extra == 'backend_error'
        assert ws.closed
        assert ws.close_code == 4500


@pytest.mark.parametrize('auth_resp', [
    'invalid json',
    '["user_id", "user:1"]',  # list instead of dict
    '"user:123"',
    '{}',
    '{"user_id": null}',
    '{"user_id": 123.1}',
    '{"user_id": "123",}',  # trailing comma
    ])
async def test_invalid_auth_response(proxy_server, swindon, auth_resp):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'

        fut.set_result(
            web.Response(text=auth_resp, content_type='application/json'))
        ws = await call.websocket
        msg = await ws.receive()
        assert msg.type == WSMsgType.CLOSE
        assert msg.data == 4500
        assert msg.extra == 'backend_error'
        assert ws.closed
        assert ws.close_code == 4500


async def test_auth_request__cookies(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    h = {"Cookie": "valid=cookie; next=value"}
    call = proxy_server.swindon_chat
    async with call(url, headers=h, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        assert req.headers['Content-Type'] == 'application/json'
        assert 'Authorization' not in req.headers
        expected = [
            {'connection_id': mock.ANY},
            [],
            {'http_cookie': "valid=cookie; next=value",
             'http_authorization': None,
             'url_querystring': '',
             }]
        assert await req.json() == expected

        fut.set_result(
            json_response({"user_id": "user:1", "username": "John"}))
        ws = await call.websocket
        msg = await ws.receive_json()
        assert msg == ['hello', {}, {'user_id': 'user:1', 'username': 'John'}]


async def test_auth_request__querystring(proxy_server, swindon):
    url = (swindon.url / 'swindon-chat').with_query(
        'query=param1&query=param2')
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        assert req.headers['Content-Type'] == 'application/json'
        assert 'Authorization' not in req.headers
        expected = [
            {'connection_id': mock.ANY},
            [],
            {'http_cookie': None,
             'http_authorization': None,
             'url_querystring': 'query=param1&query=param2',
             }]
        assert await req.json() == expected

        fut.set_result(
            json_response({"user_id": "user:1", "username": "John"}))
        ws = await call.websocket
        msg = await ws.receive_json()
        assert msg == ['hello', {}, {'user_id': 'user:1', 'username': 'John'}]


async def test_auth_request__authorization(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    h = {"Authorization": "digest abcdef"}
    async with proxy_server.swindon_chat(url, headers=h, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        assert req.headers['Content-Type'] == 'application/json'
        assert 'Authorization' not in req.headers
        expected = [
            {'connection_id': mock.ANY},
            [],
            {'http_cookie': None,
             'http_authorization': "digest abcdef",
             'url_querystring': '',
             }]
        assert await req.json() == expected

        fut.set_result(json_response({
            "user_id": "user:1", "username": "John"}))
        ws = await call.websocket
        msg = await ws.receive_json()
        assert msg == ['hello', {}, {'user_id': 'user:1', 'username': 'John'}]


async def test_auth_request__all(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    url = url.with_query("foo=bar")
    h = {"Cookie": "valid=cookie", "Authorization": "digest abcdef"}
    async with proxy_server.swindon_chat(url, headers=h, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        assert req.headers['Content-Type'] == 'application/json'
        assert 'Authorization' not in req.headers
        expected = [
            {'connection_id': mock.ANY},
            [],
            {'http_cookie': "valid=cookie",
             'http_authorization': "digest abcdef",
             'url_querystring': 'foo=bar',
             }]
        assert await req.json() == expected

        fut.set_result(
            json_response({"user_id": "user:1", "username": "John"}))
        ws = await call.websocket
        msg = await ws.receive_json()
        assert msg == ['hello', {}, {'user_id': 'user:1', 'username': 'John'}]


async def test_echo_messages(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        fut.set_result(json_response({
            "user_id": "user:2", "username": "Jack"}))
        ws = await call.websocket
        hello = await ws.receive_json()
        assert hello == [
            'hello', {}, {'user_id': 'user:2', 'username': 'Jack'}]

        ws.send_json(['chat.echo_message', {'request_id': '1'},
                      ['some message'], {}])
        req, fut = await call.request()
        assert req.path == '/chat/echo_message'
        assert await req.json() == [
            {'request_id': '1', 'connection_id': mock.ANY},
            ['some message'],
            {},
        ]
        auth_data = 'Tangle eyJ1c2VyX2lkIjoidXNlcjoyIn0='
        assert req.headers['Authorization'] == auth_data

        fut.set_result(json_response({
            'echo': "some message",
            }))

        echo = await ws.receive_json()
        assert echo == [
            'result', {'request_id': '1'},
            {'echo': "some message"},
            ]


async def test_prefix_routes(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        fut.set_result(json_response({
            "user_id": "user:2", "username": "Jack"}))
        ws = await call.websocket
        hello = await ws.receive_json()
        assert hello == [
            'hello', {}, {'user_id': 'user:2', 'username': 'Jack'}]

        ws.send_json(['prefixed.echo_message', {'request_id': '1'},
                      ['some message'], {}])
        req, fut = await call.request()
        assert req.path == '/with-prefix/prefixed/echo_message'
        assert await req.json() == [
            {'request_id': '1', 'connection_id': mock.ANY},
            ['some message'],
            {},
        ]
        fut.set_result(json_response({
            'echo': "some message",
            }))

        echo = await ws.receive_json()
        assert echo == [
            'result', {'request_id': '1'},
            {'echo': "some message"},
            ]


async def test_topic_subscribe_publish(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        meta, args, kwargs = await req.json()
        assert 'connection_id' in meta
        assert not args
        assert kwargs

        cid = meta['connection_id']

        async with aiohttp.ClientSession() as s:
            sub_url = swindon.api / 'v1/connection' / cid / 'subscriptions'
            sub_url = sub_url / 'some/topic'
            async with s.put(sub_url) as resp:
                assert resp.status == 204

            publish_url = swindon.api / 'v1/publish' / 'some/topic'
            data = b'{"Test": "message"}'
            async with s.post(publish_url, data=data) as resp:
                assert resp.status == 204

        fut.set_result(json_response({
            "user_id": "topic-user:1", "username": "Jack"}))
        ws = await call.websocket
        hello = await ws.receive_json()
        assert hello == [
            'hello', {}, {'user_id': 'topic-user:1', 'username': 'Jack'}]
        msg = await ws.receive_json()
        assert msg == ['message', {'topic': 'some.topic'}, {'Test': 'message'}]

        async with aiohttp.ClientSession() as s:
            publish_url = swindon.api / 'v1/publish' / 'some/topic'
            data = b'"other message"'
            async with s.post(publish_url, data=data) as resp:
                assert resp.status == 204
        msg = await ws.receive_json()
        assert msg == ['message', {'topic': 'some.topic'}, 'other message']


async def test_lattice_subscribe_update(proxy_server, swindon):
    url = swindon.url / 'swindon-chat'
    async with proxy_server.swindon_chat(url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        meta, args, kwargs = await req.json()
        assert 'connection_id' in meta
        assert not args
        assert kwargs
        cid = meta['connection_id']

        async with aiohttp.ClientSession() as s:
            u = swindon.api / 'v1/connection' / cid / 'lattices'
            u = u / 'lattice/namespace'
            data = json.dumps({
                'shared': {
                    'room1': {'last_message_counter': 123},
                },
                'private': {
                    'lattice-user:1': {
                        'room1': {'last_seen_counter': 120},
                    }
                },
            })
            async with s.put(u, data=data) as resp:
                assert resp.status == 204

        fut.set_result(json_response({
            "user_id": "lattice-user:1", "username": "Jim"}))
        ws = await call.websocket
        hello = await ws.receive_json()
        assert hello == [
            'hello', {}, {'user_id': 'lattice-user:1', 'username': 'Jim'}]
        up = await ws.receive_json()
        assert up == [
            'lattice',
            {'namespace': 'lattice.namespace'},
            {'room1': {
                'last_message_counter': 123,
                'last_seen_counter': 120,
                }},
        ]


async def test_inactivity(proxy_server, swindon):
    chat_url = swindon.url / 'swindon-chat-w-timeouts'
    async with proxy_server.swindon_chat(chat_url, timeout=1) as call:
        req, fut = await call.request()
        assert req.path == '/tangle/authorize_connection'
        fut.set_result(json_response({
            "user_id": "user:1", "username": "Jim"}))

        ws = await call.websocket
        hello = await ws.receive_json()
        assert hello == [
            'hello', {}, {'user_id': 'user:1', 'username': 'Jim'}]

        req, fut = await asyncio.wait_for(call.request(), timeout=1.2)
        assert req.path == '/tangle/session_inactive'
        assert req.headers.getall('Authorization') == [
            'Tangle eyJ1c2VyX2lkIjoidXNlcjoxIn0='
            ]
        assert await req.json() == [{}, [], {}]
        fut.set_result(web.Response(status=200))

        ws.send_json([
            'whatever', {'request_id': '1', 'active': 2}, [], {}])
        req, fut = await call.request()
        assert req.path == '/whatever'
        assert await req.json() == [
            {'request_id': '1', 'active': 2, 'connection_id': mock.ANY},
            [], {}]
        fut.set_result(web.Response(status=200))

        req, fut = await asyncio.wait_for(call.request(), timeout=3.2)
        assert req.path == '/tangle/session_inactive'
        assert req.headers.getall('Authorization') == [
            'Tangle eyJ1c2VyX2lkIjoidXNlcjoxIn0='
            ]
        assert await req.json() == [{}, [], {}]
        fut.set_result(web.Response(status=200))