예제 #1
0
 def setUp(self):
     self._fb = FakeBrain()
     self._sock = socket.create_connection(self._fb.addr)
     self._fb.work(1)
     self._sock.send(b'{"command": "switch-user", "user": "******"}\n')
     self._fb.work(1)
     self._fb.messages.pop(0)
예제 #2
0
    def setUp(self):
        self.sock = socket.socket(socket.AF_INET)
        self.sock.bind(('0.0.0.0', 0))
        self.addr = self.sock.getsockname()
        self.sock.listen()

        self.fb = FakeBrain()
        sock = socket.create_connection(self.fb.addr)
        self.fb.work(0.01)
        self.be = SocketChannel(sock, buffering='line')

        self.router = Router(self.sock, self.be)
예제 #3
0
class FakeBrainConnectTest(unittest.TestCase):
    def setUp(self):
        self._fb = FakeBrain()

    def tearDown(self):
        self._fb.shutdown()

    def test_connect(self):
        self.assertEqual(self._fb.addr[0], '127.0.0.1')
        sock = socket.create_connection(self._fb.addr)

    def test_shutdown(self):
        addr = self._fb.addr

        self._fb.shutdown()

        with self.assertRaises(ConnectionRefusedError):
            socket.create_connection(addr)
def before_scenario(context, _):
    context.dir = mkdtemp()
    context.add_cleanup(shutil.rmtree, context.dir)
    context.p = select.poll()
    context.b = FakeBrain()
    if 'fake' in context.tags:
        with open(os.path.join(context.dir, "config.yml"), "w") as cfg:
            cfg.write("""components:\n  brain:
    command: socat STDIO TCP:{}:{}
    buffering: line""".format(*context.b.addr))
    context.channels = {}
예제 #5
0
 def setUp(self):
     self._fb = FakeBrain()
예제 #6
0
class FakeBrainTest(unittest.TestCase):
    def setUp(self):
        self._fb = FakeBrain()
        self._sock = socket.create_connection(self._fb.addr)
        self._fb.work(1)
        self._sock.send(b'{"command": "switch-user", "user": "******"}\n')
        self._fb.work(1)
        self._fb.messages.pop(0)

    def tearDown(self):
        self._fb.shutdown()

    def _send_presence(self, user, channel):
        msg = {
            'event': 'presence',
            'from': {
                'user': '******',
                'channel': 'channel'
            }
        }
        self._sock.send(json.dumps(msg).encode() + b'\n')
        self._fb.work(1)
        return msg

    def test_presence_message(self):
        msg = self._send_presence('user', 'channel')

        self.assertEqual(self._fb.messages, [msg])
        with self.assertRaises(Exception), timeout(0.2):
            self._sock.recv(1024)

    def test_send_message_to(self):
        self._send_presence('user', 'channel')

        self._fb.send_message_to("test", 'user', 'channel')

        with timeout(0.2):
            data = self._sock.recv(1024)
        self.assertTrue(data.endswith(b'\n'))
        msg = json.loads(data.decode().strip())
        self.assertEqual(
            msg, {
                "message": "test",
                "from": {
                    "user": "******",
                    "channel": "brain"
                },
                "to": {
                    "user": "******",
                    "channel": 'channel'
                }
            })

    def test_echo(self):
        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": 'channel'
            },
            "to": "brain"
        }
        self._sock.send(json.dumps(msg).encode() + b'\n')
        self._fb.work(1)

        self.assertEquals(self._fb.messages, [msg])

        with timeout(0.2):
            data = self._sock.recv(1024)
        self.assertTrue(data.endswith(b'\n'))
        msg = json.loads(data.decode().strip())
        self.assertEqual(
            msg, {
                "message": "test",
                "from": "brain",
                "to": {
                    "user": "******",
                    "channel": 'channel'
                }
            })

    def test_fake_brain_is_line_buffered(self):
        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": 'channel'
            },
            "to": "brain"
        }
        self._sock.send(json.dumps(msg).encode() + b'\n')
        self._sock.send(json.dumps(msg).encode() + b'\n')
        self._fb.work(1)

        self.assertEquals(self._fb.messages, [msg, msg])

    def test_fake_brain_ignores_inactive_user(self):
        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": 'channel'
            },
            "to": "brain"
        }
        self._sock.send(json.dumps(msg).encode() + b'\n')
        self._fb.work(1)

        self.assertEqual(self._fb.messages, [msg])
        with self.assertRaises(Exception), timeout(0.2):
            self._sock.recv(1024)
예제 #7
0
class RouterTest(unittest.TestCase):
    def setUp(self):
        self.sock = socket.socket(socket.AF_INET)
        self.sock.bind(('0.0.0.0', 0))
        self.addr = self.sock.getsockname()
        self.sock.listen()

        self.fb = FakeBrain()
        sock = socket.create_connection(self.fb.addr)
        self.fb.work(0.01)
        self.be = SocketChannel(sock, buffering='line')

        self.router = Router(self.sock, self.be)

    def tearDown(self):
        self.fb.shutdown()
        self.be.close()
        self.sock.close()

    def _connect(self, channel_name="channel"):
        client = socket.create_connection(self.addr)
        self.addCleanup(client.close)
        self.router.tick(0.01)
        client.send(channel_name.encode() + b'\n')
        self.router.tick(0.01)
        return client

    def _feed(self, client, msg):
        client.send(json.dumps(msg).encode() + b'\n')
        self.router.tick(0.01)
        self.fb.work(0.01)
        self.router.tick(0.01)

    def test_route_to_brain(self):
        client = self._connect()

        msg = {"command": "nop", "to": {"user": "******", "channel": "brain"}}
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [msg])

    def test_discard_if_not_to_brain(self):
        client = self._connect()

        msg = {"command": "nop", "to": {"user": "******", "channel": "channel"}}
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [])

    def test_switch_user(self):
        client = self._connect()

        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [{
            "command": "switch-user",
            "user": "******"
        }, msg])

    def test_keep_same_user(self):
        client = self._connect()

        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [{
            "command": "switch-user",
            "user": "******"
        }, msg, msg])

    def test_route_from_brain(self):
        client1 = self._connect("channel1")
        client2 = self._connect("channel2")
        poll = select.poll()
        poll.register(client1, select.POLLIN)
        poll.register(client2, select.POLLIN)

        self.fb.send_message_to("hello", "user", "channel1:abcd")
        self.router.tick(0.01)

        res = poll.poll(0.01)
        self.assertEqual(res, [(client1.fileno(), select.POLLIN)])
        data = client1.recv(1024)
        msg = json.loads(data.decode())
        self.assertEqual(
            msg, {
                "message": "hello",
                "from": {
                    "user": "******",
                    "channel": "brain"
                },
                "to": {
                    "user": "******",
                    "channel": "channel1:abcd"
                }
            })

    def test_client_disconnect(self):
        client = self._connect()
        client.close()

        self.fb.send_message_to("hello", "user", "channel:abcd")
        self.router.tick(0.01)

        self.assertEqual(self.fb.messages, [])

    def test_client_gone(self):
        client = self._connect()
        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)
        self.assertEqual(self.fb.messages.pop(0), {
            "command": "switch-user",
            "user": "******"
        })
        self.assertEqual(self.fb.messages.pop(0), msg)
        self.assertEqual(self.fb.messages, [])

        client.close()
        self.router.tick(0.01)
        self.fb.work(0.01)

        self.assertEqual(self.fb.messages, [{
            "event": "gone",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }])

    def test_brain_gone(self):
        self.fb.shutdown()

        with self.assertRaises(BrainDisconnectedException):
            self.router.tick(0.01)