def test_auth_before_start(self):
        client = Client(endpoint=endpoint, appkey=appkey)

        auth_event = threading.Event()
        auth_delegate = auth.RoleSecretAuthDelegate(role, secret)
        mailbox = []

        def auth_callback(auth_result):
            if type(auth_result) == auth.Done:
                mailbox.append('Auth success')
                auth_event.set()
            else:
                mailbox.append('Auth failure: {0}'.format(auth_result))
                auth_event.set()

        client.authenticate(auth_delegate, auth_callback)

        client.start()

        if not auth_event.wait(60):
            raise RuntimeError('Auth never finished')

        self.assertEqual(mailbox, ['Auth success'])

        client.stop()
        client.dispose()
    def test_reauth(self):
        client = Client(endpoint=endpoint, appkey=appkey, reconnect_interval=0)
        auth_delegate = auth.RoleSecretAuthDelegate(role, secret)
        auth_event = threading.Event()
        mailbox = []

        co = ClientObserver()
        client.observer = co
        client.start()

        co.wait_connected()

        def auth_callback(auth_result):
            if type(auth_result) == auth.Done:
                mailbox.append('Auth success')
                auth_event.set()
            else:
                mailbox.append('Auth failure: {0}'.format(
                    auth_result.message))
                auth_event.set()
        client.authenticate(auth_delegate, auth_callback)

        if not auth_event.wait(30):
            raise RuntimeError("Auth timeout")

        self.assertEqual(mailbox, ['Auth success'])

        so = sync_subscribe(client, restricted_channel)

        message1 = make_channel_name('before disconnect')
        sync_publish(client, restricted_channel, message1)
        first_data = so.wait_for_channel_data()
        self.assertTrue(message1 in first_data['messages'])

        emulate_websocket_disconnect(client)

        co.wait_disconnected()
        co.wait_connected()

        message2 = make_channel_name('after reconnect')
        sync_publish(client, restricted_channel, message2)
        second_data = so.wait_for_channel_data()
        self.assertTrue(message2 in second_data['messages'])

        client.stop()
        client.dispose()
Beispiel #3
0
    def connect(self):
        config = self.config
        self.GPIO = GPIO
        self.GPIO.setmode(self.GPIO.BOARD)

        should_authenticate = 'role' in config[
            'satori'] and 'role_secret_key' in config['satori']
        auth_delegate = None if not should_authenticate else auth.RoleSecretAuthDelegate(
            config['satori']['role'], config['satori']['role_secret_key'])

        client = Client(
            endpoint=config['satori']['endpoint'],
            appkey=config['satori']['appkey'],
        )
        ready_event = threading.Event()

        class Observer:
            def on_enter_connected(self):
                ready_event.set()

            def on_enter_stopped(self):
                ready_event.set()

        client.observer = Observer()
        client.start()
        if not ready_event.wait(70):
            if client.last_connecting_error():
                client.dispose()
                raise RuntimeError(
                    "Client connection timeout, last connection error: {0}".
                    format(client.last_connecting_error()))
            else:
                raise RuntimeError("Client connection timeout")
        ready_event.clear()
        if not client.is_connected():
            client.dispose()
            raise RuntimeError("Client connection error: {0}".format(
                client.last_connecting_error()))

        auth_mailbox = []

        def auth_callback(auth_result):
            auth_mailbox.append(auth_result)
            ready_event.set()

        if auth_delegate:
            client.authenticate(auth_delegate, callback=auth_callback)

            if not ready_event.wait(20):
                client.dispose()
                print('[FAIL] Authentication process has timed out')
                raise Exception('Authentication process has timed out')

            auth_result = auth_mailbox[0]

            if type(auth_result) == auth.Error:
                raise Exception(auth_result.message)

            print('[OK] Auth success in make_client')

        print("[OK] Connected to Satori RTM")
        client.subscribe(config['channels']['in'], SubscriptionMode.SIMPLE,
                         self)

        self.client = client