async def test_zap_successful_authentication_after_invalid_request(event_loop): class MyZAPAuthenticator(BaseZAPAuthenticator): async def on_request(self, *args, **kwargs): return 'bob', {b'foo': b'bar'} async with azmq.Context() as context: async with MyZAPAuthenticator(context=context): async with ZAPClient(context=context) as zap_client: async with context.socket(azmq.DEALER) as socket: socket.connect(ZAP_INPROC_ENDPOINT) await socket.send_multipart([b'invalid', b'data']) username, metadata = await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], ), 1, ) assert username == 'bob' assert metadata == {b'foo': b'bar'}
async def test_zap_unknown_request(event_loop): class MyZAPAuthenticator(BaseZAPAuthenticator): async def on_request(self, *args, **kwargs): return 'bob', {b'foo': b'bar'} async with azmq.Context() as context: async with ZAPClient(context=context) as zap_client: async with context.socket(azmq.ROUTER) as socket: socket.bind(ZAP_INPROC_ENDPOINT) task = asyncio.ensure_future( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], )) identity, *_ = await asyncio.wait_for( socket.recv_multipart(), 1, ) await socket.send_multipart([ identity, b'', b'1.0', b'my_request', b'200', b'all good', b'userid', b'', ]) assert not task.done() task.cancel() async with MyZAPAuthenticator(context=context): username, metadata = await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], ), 1, ) assert username == 'bob' assert metadata == {b'foo': b'bar'}
async def test_zap_default_authenticator_authentication_success(event_loop): async with azmq.Context() as context: async with ZAPAuthenticator(context=context) as authenticator: authenticator.deny('192.168.0.1') async with ZAPClient(context=context) as zap_client: username, metadata = await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'NULL', credentials=[], ), 1, ) assert username == '' assert metadata == {}
async def test_zap_default_authenticator_authentication_failure(event_loop): async with azmq.Context() as context: async with ZAPAuthenticator(context=context) as authenticator: authenticator.allow('192.168.0.1') async with ZAPClient(context=context) as zap_client: with pytest.raises(ZAPAuthenticationFailure) as error: await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], ), 1, ) assert error.value.code == 400
async def test_zap_internal_error(event_loop): class MyZAPAuthenticator(BaseZAPAuthenticator): async def on_request(self, *args, **kwargs): raise RuntimeError async with azmq.Context() as context: async with MyZAPAuthenticator(context=context): async with ZAPClient(context=context) as zap_client: with pytest.raises(ZAPInternalError) as error: await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], ), 1, ) assert error.value.code == 500
async def test_zap_successful_authentication(event_loop): class MyZAPAuthenticator(BaseZAPAuthenticator): async def on_request(self, *args, **kwargs): return 'bob', {b'foo': b'bar'} async with azmq.Context() as context: async with MyZAPAuthenticator(context=context): async with ZAPClient(context=context) as zap_client: username, metadata = await asyncio.wait_for( zap_client.authenticate( domain='domain', address='127.0.0.1', identity=b'bob', mechanism=b'CURVE', credentials=[b'mycred', b'value'], ), 1, ) assert username == 'bob' assert metadata == {b'foo': b'bar'}