Example #1
0
def test_connection_is_aborted_when_not_authorised(router):
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['wampcra'],
        'authid': 'not-an-expected-user',
    }

    client = Client(
        url=router.url, roles=roles, name="unauthenticated-client-one")

    with pytest.raises(WampyError) as exc_info:
        client.start()

    client.stop()

    exception = exc_info.value

    message = str(exception)

    assert (
        "no principal with authid \"not-an-expected-user\" exists"
        in message
    )
    assert "wamp.error.not_authorized" in message
Example #2
0
def test_connection_is_ticket_challenged(router):
    os.environ['WAMPYSECRET'] = "wEx9TPFtHdRr2Zg7rtRE"
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['ticket'],
        'authid': 'martin',
    }

    client = Client(
        url=router.url,
        roles=roles,
        message_handler_cls=CollectingMessageHandler,
        name="unauthenticated-client"
    )

    client.start()
    messages = wait_for_messages(client, 2)

    # expect a Challenge and Welcome message
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE

    client.stop()

    # now also expect a Goodbye message
    assert len(messages) == 3
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE
    assert messages[2][0] == Goodbye.WAMP_CODE
Example #3
0
def test_connection_is_ticket_challenged(router):
    os.environ['WAMPYSECRET'] = "wEx9TPFtHdRr2Zg7rtRE"
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['ticket'],
        'authid': 'martin',
    }

    client = Client(url=router.url,
                    roles=roles,
                    message_handler_cls=CollectingMessageHandler,
                    name="unauthenticated-client")

    client.start()
    messages = wait_for_messages(client, 2)

    # expect a Challenge and Welcome message
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE

    client.stop()

    # now also expect a Goodbye message
    assert len(messages) == 3
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE
    assert messages[2][0] == Goodbye.WAMP_CODE
Example #4
0
def test_connection_is_aborted_when_not_authorised(router):
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['wampcra'],
        'authid': 'not-an-expected-user',
    }

    client = Client(router=router,
                    roles=roles,
                    name="unauthenticated-client-one")

    with pytest.raises(WelcomeAbortedError) as exc_info:
        client.start()

    client.stop()

    exception = exc_info.value

    message = str(exception)

    assert ("no principal with authid \"not-an-expected-user\" exists"
            in message)
    assert "wamp.error.not_authorized" in message
Example #5
0
def test_connection_is_challenged(router):
    os.environ['WAMPYSECRET'] = "prq7+YkJ1/KlW1X0YczMHw=="
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['wampcra'],
        'authid': 'peter',
    }

    message_handler = CollectingMessageHandler()
    client = Client(router=router,
                    roles=roles,
                    message_handler=message_handler,
                    name="unauthenticated-client")

    client.start()
    messages = wait_for_messages(client, 2)

    # expect a Challenge and Welcome message
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE

    client.stop()

    # now also expect a Goodbye message
    assert len(messages) == 3
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE
    assert messages[2][0] == Goodbye.WAMP_CODE
Example #6
0
def test_pinging(router):
    with patch('wampy.transports.websocket.connection.heartbeat', 1):
        with patch('wampy.transports.websocket.connection.heartbeat_timeout',
                   2):
            client = Client(router.url)
            client.start()
            wait_for_session(client)

            assert client.is_pinging

            ws = client.session.connection
            assert ws.missed_pongs == 0

            async_adapter.sleep(10)

            assert ws.missed_pongs == 0

    client.stop()
Example #7
0
class WampClientProxy(Extension):

    def setup(self):
        self.config_path = self.container.config[
            WAMP_CONFIG_KEY]['config_path']
        self.router = Router(config_path=self.config_path)
        self.client = Client(
            router=self.router, name="nameko-wamp client proxy"
        )

    def start(self):
        self.client.start()

    def stop(self):
        try:
            self.client.stop()
        except AttributeError:
            pass
Example #8
0
def test_pinging(router):
    with patch('wampy.transports.websocket.connection.heartbeat', 1):
        with patch(
            'wampy.transports.websocket.connection.heartbeat_timeout', 2
        ):
            client = Client(router.url)
            client.start()
            wait_for_session(client)

            assert client.is_pinging

            ws = client.session.connection
            assert ws.missed_pongs == 0

            async_adapter.sleep(10)

            assert ws.missed_pongs == 0

    client.stop()
def main():
    port = config('PORT')
    url = f'ws://*****:*****@example.com', 'blebs')
    if isinstance(x, Error):
        print('Error:', x.message)
    else:
        print(x)

    x = client.call('authenticate', '*****@*****.**', 'blebs')
    if isinstance(x, Error):
        print('Error:', x.message)
    else:
        print(x)

    client.stop()
Example #10
0
def test_publish_kwargs_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    client.start()
    client.publish(topic="foo", message="foobar")

    def check_call_count():
        assert foo_subscriber.call_count == 1

    assert_stops_raising(check_call_count)

    client.publish(topic="foo", message="foobar")
    client.publish(topic="foo", message="spam")
    client.publish(topic="foo", message="ham")

    def check_call_count():
        assert foo_subscriber.call_count == 4

    assert_stops_raising(check_call_count)

    client.stop()
Example #11
0
def test_publish_kwargs_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    client.start()
    client.publish(topic="foo", message="foobar")

    def check_call_count():
        assert foo_subscriber.call_count == 1

    assert_stops_raising(check_call_count)

    client.publish(topic="foo", message="foobar")
    client.publish(topic="foo", message="spam")
    client.publish(topic="foo", message="ham")

    def check_call_count():
        assert foo_subscriber.call_count == 4

    assert_stops_raising(check_call_count)

    client.stop()
Example #12
0
def test_peter_cannot_call_get_foo(router, foo_service):
    # `get_foo` can be registered but not called over the wampy role
    os.environ['WAMPYSECRET'] = "prq7+YkJ1/KlW1X0YczMHw=="
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['wampcra'],
        'authid': 'peter',
    }

    message_handler = CollectingMessageHandler()
    client = Client(
        router=router,
        roles=roles,
        message_handler=message_handler,
        name="unauthenticated-client-three",
    )

    client.start()

    with pytest.raises(NotAuthorisedError):
        client.rpc.get_foo()

    client.stop()

    messages = wait_for_messages(client, 4)

    # now also expect a Goodbye message
    assert len(messages) == 4
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE
    assert messages[2][0] == Error.WAMP_CODE
    assert messages[3][0] == Goodbye.WAMP_CODE
Example #13
0
def test_peter_cannot_call_get_foo(router, foo_service):
    # `get_foo` can be registered but not called over the wampy role
    os.environ['WAMPYSECRET'] = "prq7+YkJ1/KlW1X0YczMHw=="
    roles = {
        'roles': {
            'subscriber': {},
            'publisher': {},
            'callee': {},
            'caller': {},
        },
        'authmethods': ['wampcra'],
        'authid': 'peter',
    }

    client = Client(
        url=router.url,
        roles=roles,
        message_handler_cls=CollectingMessageHandler,
        name="unauthenticated-client-three",
    )

    client.start()

    with pytest.raises(WampyError):
        client.rpc.get_foo()

    client.stop()

    messages = wait_for_messages(client, 4)

    # now also expect a Goodbye message
    assert len(messages) == 4
    assert messages[0][0] == Challenge.WAMP_CODE
    assert messages[1][0] == Welcome.WAMP_CODE
    assert messages[2][0] == Error.WAMP_CODE
    assert messages[3][0] == Goodbye.WAMP_CODE
Example #14
0
class TestDaemon(object):
    @classmethod
    def setup_class(self):
        intents_folder = "tests/daemon_test/intents"
        dialogs_folder = "tests/daemon_test/dialogs"
        workdir = "tests/daemon_test/workdir"
        self.fake_daemon = FakeDaemon("fake_daemon", workdir, intents_folder,
                                      dialogs_folder)
        self.fake_daemon.settings.delete("/config/global")
        self.fake_daemon.settings.delete()

        self.wamp_client = Client(realm="tuxeatpi")
        self.wamp_client.start()

    @classmethod
    def teardown_class(self):
        self.fake_daemon.settings.delete("/config/global")
        self.fake_daemon.settings.delete()
        self.fake_daemon.shutdown()

        self.wamp_client.stop()

    @pytest.mark.run_loop
    def test_daemon(self, capsys):
        # Start
        self.fake_daemon._run_main_loop = False
        t = threading.Thread(target=self.fake_daemon.start)
        t = t.start()

        # Wait for start
        time.sleep(2)
        # FIRST CONFIGURATION (at init time)
        # Set global config
        self.fake_daemon.settings.save(
            {
                "language": "en_US",
                "nlu_engine": "nlu_test"
            }, "global")
        self.fake_daemon.settings.save({"param1": "value1"})
        time.sleep(2)
        # check first config
        assert self.fake_daemon.settings.language == "en_US"
        assert self.fake_daemon.settings.nlu_engine == "nlu_test"

        # Get dialog test
        dialog_rendered = self.fake_daemon.get_dialog("render_test",
                                                      test="mytest")
        assert dialog_rendered == "This is a rendering test mytest"

        # Publish Messages
        message = Message("fake_daemon.help",
                          {"arguments": {
                              "help_arg": "test1"
                          }})

        # TODO: waiting for https://github.com/noisyboiler/wampy/pull/42
        self.fake_daemon.publish(message)
        # Then remove me
        # Publish using an other wamp client
        self.wamp_client.publish(topic="fake_daemon.help",
                                 message=message.payload)

        time.sleep(2)
        assert self.fake_daemon.help_arg == "test1"
        # Publish Messages override
        message = Message("fake_daemon.help",
                          {"arguments": {
                              "help_arg": "test2"
                          }})

        # TODO: waiting for https://github.com/noisyboiler/wampy/pull/42
        self.fake_daemon.publish(message, override_topic="fake_daemon.help")
        # Then remove me
        # Publish using an other wamp client
        self.wamp_client.publish(topic="fake_daemon.help",
                                 message=message.payload)

        time.sleep(1)
        assert self.fake_daemon.help_arg == "test2"

        # Set component config
        # checkcomponent config
        assert self.fake_daemon.args1 == "value1"

        time.sleep(4)
        # SECOND CONFIGURATION (at run time)
        self.fake_daemon.settings.save({"param1": "value2"})
        self.fake_daemon.settings.save(
            {
                "language": "fr_FR",
                "nlu_engine": "nlu_test2"
            }, "global")
        time.sleep(1)
        assert self.fake_daemon.settings.language == "fr_FR"
        assert self.fake_daemon.settings.nlu_engine == "nlu_test2"
        assert self.fake_daemon.args1 == "value2"
        assert self.fake_daemon.settings.params == {"param1": "value2"}

        # TODO capture logging output
        self.fake_daemon.reload()