Example #1
0
def test_frame_to_json():
    uuid = uuid4().hex
    frame_as_dict = {'name': 'test-frame', 'uuid': uuid, 'kind': Kind.EVENT}
    f = Frame('test-frame', uuid=uuid)
    fjson = f.to_json()
    fdict = json.loads(fjson)
    assert fdict == frame_as_dict
async def test_queue_transport():
    qt = QueueTransport()
    frame = Frame('test-frame')
    await qt.connect('test-endpoint', 'test-token')
    assert qt.connected is True
    await qt.send(frame)
    frame_sent = await qt.queue_send.get()
    assert frame == frame_sent
    frame_ = Frame('test-recv')
    await qt.queue_recv.put(frame_)
    frame_recv = await qt.recv()
    assert frame_ == frame_recv
    await qt.close()
    assert qt.connected is False
Example #3
0
def test_frame_creation_with_name():
    f = Frame('test-frame')
    assert f.name == 'test-frame'
    assert len(f.uuid) == 32
    assert f.kind == Kind.EVENT
    assert f.data == {}
    assert f.meta == {}
Example #4
0
def test_frame_from_dict():
    uuid = uuid4().hex
    frame_as_dict = {'name': 'test-frame', 'uuid': uuid, 'kind': Kind.EVENT}
    f = Frame.from_dict(frame_as_dict)
    assert f.name == 'test-frame'
    assert f.uuid == uuid
    assert f.kind == Kind.EVENT
Example #5
0
def test_frame_from_json():
    uuid = uuid4().hex
    frame_as_dict = {'name': 'test-frame', 'uuid': uuid, 'kind': Kind.EVENT}
    frame_as_json = json.dumps(frame_as_dict)
    f = Frame.from_json(frame_as_json)
    assert f.name == 'test-frame'
    assert f.uuid == uuid
    assert f.kind == Kind.EVENT
Example #6
0
async def echo(websocket, path):
    async for message in websocket:
        frame = Frame.from_json(message)
        if frame.kind == Kind.COMMAND and frame.name == 'login':
            print(f'login-ok', frame.to_dict())
            await websocket.send(frame.reply('login-ok').to_json())
        else:
            print(frame.to_dict())
            await websocket.send(message)
 async def send(self, data):
     if not self._send_ok:
         raise ConnectionAbortedError()
     frame = Frame.from_json(data)
     if frame.name == 'login':
         if self._login_ok:
             self.frame = frame.reply('login-ok').to_json()
         else:
             self.frame = frame.reply('login-failed').to_json()
         return
     self.frame = data
async def test_websocket_transport(monkeypatch):
    monkeypatch.setattr(websocket, 'websockets', MockWebsockets())
    wt = WebsocketTransport()
    frame = Frame('test-frame')
    await wt.connect('ws://localhost:6789/', 'test-token')
    assert wt.connected is True
    await wt.send(frame)
    assert wt.connection.frame
    frame_recv = await wt.recv()
    assert frame_recv.name == 'test-frame'
    await wt.close()
    assert wt.connected is False
Example #9
0
async def test_datagram_transport(monkeypatch):
    monkeypatch.setattr(datagram, 'asyncio_dgram', MockDatagram())
    dt = datagram.DatagramTransport()
    frame = Frame('test-frame')
    await dt.connect('dgram://127.0.0.1:6789/', 'test-token')
    assert dt.connected is True
    await dt.send(frame)
    assert dt.connection.frame
    frame_recv = await dt.recv()
    assert frame_recv.name == 'test-frame'
    await dt.close()
    assert dt.connected is False
Example #10
0
async def echo(websocket, path):  # pragma: no cover
    async for message in websocket:
        frame = Frame.from_json(message)
        if frame.kind == Kind.COMMAND and frame.name == 'login' and frame.data.get(
                'token') == 'fail-token':
            print(frame.to_dict())
            print(f'login-fail')
            await websocket.send(frame.reply('login-fail').to_json())
        elif frame.kind == Kind.COMMAND and frame.name == 'login':
            print(frame.to_dict())
            print(f'login-ok')
            await websocket.send(frame.reply('login-ok').to_json())
        else:
            print(frame.to_dict())
            await websocket.send(message)
Example #11
0
    async def connect(self, endpoint, token):
        from zentropi import Kind
        from zentropi import Frame

        self.token = token
        self.endpoint = endpoint
        self._host, self._port = endpoint.replace('dgram://', '').split(':')
        self.connection = await asyncio_dgram.connect((self._host, self._port))
        await self.send(
            Frame('login', kind=Kind.COMMAND, data=dict(token=token)))
        auth_ack = await self.recv()
        if auth_ack.name == 'login-ok':
            self.connected = True
        else:
            raise PermissionError(
                f'Unable to connect to {endpoint}, got {auth_ack.to_dict()}')
Example #12
0
def test_frame_to_dict():
    uuid = uuid4().hex
    frame_as_dict = {'name': 'test-frame', 'uuid': uuid, 'kind': Kind.EVENT}
    f = Frame('test-frame', uuid=uuid)
    fdict = f.to_dict()
    assert fdict == frame_as_dict
Example #13
0
 async def recv(self):
     data, remote_addr = await self.connection.recv()
     frame = Frame.from_json(data.decode('utf-8'))
     return frame
Example #14
0
def test_frame_creation_with_data():
    data = {'test': 'item'}
    f = Frame('test-frame', data=data)
    assert f.name == 'test-frame'
    assert f.data == data
Example #15
0
def test_frame_creation_with_meta():
    meta = {'test': 'item'}
    f = Frame('test-frame', meta=meta)
    assert f.name == 'test-frame'
    assert f.meta == meta
Example #16
0
def test_frame_reply_with_name():
    f = Frame('test-frame')
    freply = f.reply('test-reply')
    assert freply.name == 'test-reply'
    assert freply.meta.get('reply_to') == f.uuid
Example #17
0
def test_frame_validate_name_must_be_string():
    Frame(42)
Example #18
0
def test_frame_validate_uuid_must_not_be_empty():
    Frame('test-frame', uuid='     ')
Example #19
0
def test_frame_validate_meta_must_be_dict():
    Frame('test-frame', meta='expect failure')
Example #20
0
def test_frame_creation_requires_name():
    Frame()
Example #21
0
def test_frame_reply_with_meta():
    meta = {'test': 'item'}
    f = Frame('test-frame')
    freply = f.reply(meta=meta)
    assert freply.meta.get('reply_to') == f.uuid
    assert freply.meta.get('test') == 'item'
Example #22
0
def test_frame_validate_kind_must_be_within_range():
    Frame('test-frame', -1)
Example #23
0
def test_frame_validate_kind_must_be_integer():
    Frame('test-frame', kind='event')
Example #24
0
def test_frame_validate_name_must_not_be_empty():
    Frame('     ')
async def test_websocket_transport_recv_fail(monkeypatch):
    monkeypatch.setattr(websocket, 'websockets', MockWebsockets(recv_ok=False))
    wt = WebsocketTransport()
    frame = Frame('test-frame')
    await wt.connect('ws://localhost:6789/', 'test-token')
Example #26
0
def test_frame_creation_with_kind():
    f = Frame('test-frame', kind=1)
    assert f.name == 'test-frame'
    assert f.kind == 1
Example #27
0
def test_frame_reply():
    f = Frame('test-frame')
    freply = f.reply()
    assert freply.meta.get('reply_to') == f.uuid
Example #28
0
def test_frame_creation_with_uuid():
    uuid = uuid4().hex
    f = Frame('test-frame', uuid=uuid)
    assert f.name == 'test-frame'
    assert f.uuid == uuid
Example #29
0
def test_frame_validate_uuid_must_be_string():
    Frame('test-frame', uuid=42)
Example #30
0
def test_frame_reply_for_request():
    f = Frame('test-frame', kind=Kind.REQUEST)
    freply = f.reply()
    assert freply.meta.get('reply_to') == f.uuid
    assert freply.kind == Kind.RESPONSE