Ejemplo n.º 1
0
    def test_connect(self):
        # Test checking connection handshake
        frame_writer_cls_mock = Mock()
        on_open_mock = Mock()
        frame_writer_mock = frame_writer_cls_mock()
        conn = Connection(frame_writer=frame_writer_cls_mock,
                          on_open=on_open_mock)

        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)
            on_open_mock.assert_called_once_with(conn)
            security_mechanism = sasl.AMQPLAIN('guest',
                                               'guest').start(conn).decode(
                                                   'utf-8', 'surrogatepass')

            # Expected responses from client
            frame_writer_mock.assert_has_calls([
                call(
                    1,
                    0,
                    spec.Connection.StartOk,
                    # Due Table type, we cannot compare bytestream directly
                    DataComparator('FsSs', (CLIENT_CAPABILITIES, 'AMQPLAIN',
                                            security_mechanism, 'en_US')),
                    None),
                call(
                    1, 0, spec.Connection.TuneOk,
                    dumps('BlB',
                          (conn.channel_max, conn.frame_max, conn.heartbeat)),
                    None),
                call(1, 0, spec.Connection.Open,
                     dumps('ssb', (conn.virtual_host, '', False)), None)
            ])
Ejemplo n.º 2
0
    def test_connect_no_capabilities(self):
        # Test checking connection handshake with broker
        # not supporting capabilities
        frame_writer_cls_mock = Mock()
        on_open_mock = Mock()
        frame_writer_mock = frame_writer_cls_mock()
        conn = Connection(
            frame_writer=frame_writer_cls_mock, on_open=on_open_mock
        )

        with patch.object(conn, 'Transport') as transport_mock:
            server_properties = dict(SERVER_PROPERTIES)
            del server_properties['capabilities']
            client_properties = dict(CLIENT_PROPERTIES)
            del client_properties['capabilities']

            handshake(
                conn, transport_mock, server_properties=server_properties
            )
            on_open_mock.assert_called_once_with(conn)
            security_mechanism = sasl.AMQPLAIN(
                'guest', 'guest'
            ).start(conn).decode('utf-8', 'surrogatepass')

            # Expected responses from client
            frame_writer_mock.assert_has_calls(
                [
                    call(
                        1, 0, spec.Connection.StartOk,
                        # Due Table type, we cannot compare bytestream directly
                        DataComparator(
                            'FsSs',
                            (
                                client_properties, 'AMQPLAIN',
                                security_mechanism,
                                'en_US'
                            )
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.TuneOk,
                        dumps(
                            'BlB',
                            (conn.channel_max, conn.frame_max, conn.heartbeat)
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.Open,
                        dumps('ssb', (conn.virtual_host, '', False)),
                        None
                    )
                ]
            )
            assert conn.client_properties == client_properties
Ejemplo n.º 3
0
 def test_amqplain(self):
     username, password = '******', 'bar'
     mech = sasl.AMQPLAIN(username, password)
     response = mech.start(None)
     assert isinstance(response, bytes)
     login_response = BytesIO()
     _write_table({b'LOGIN': username, b'PASSWORD': password},
                  login_response.write, [])
     expected_response = login_response.getvalue()[4:]
     assert response == expected_response
Ejemplo n.º 4
0
    def test_connect_missing_capabilities(self):
        # Test checking connection handshake with broker
        # supporting subset of capabilities
        frame_writer_cls_mock = Mock()
        on_open_mock = Mock()
        frame_writer_mock = frame_writer_cls_mock()
        conn = Connection(frame_writer=frame_writer_cls_mock,
                          on_open=on_open_mock)

        with patch.object(conn, 'Transport') as transport_mock:
            server_properties = dict(SERVER_PROPERTIES)
            server_properties['capabilities'] = {
                # This capability is not supported by client
                'basic.nack': True,
                'consumer_cancel_notify': True,
                'connection.blocked': False,
                # server does not support 'authentication_failure_close'
                # which is supported by client
            }

            client_properties = dict(CLIENT_PROPERTIES)
            client_properties['capabilities'] = {
                'consumer_cancel_notify': True,
            }

            handshake(conn,
                      transport_mock,
                      server_properties=server_properties)
            on_open_mock.assert_called_once_with(conn)
            security_mechanism = sasl.AMQPLAIN('guest',
                                               'guest').start(conn).decode(
                                                   'utf-8', 'surrogatepass')

            # Expected responses from client
            frame_writer_mock.assert_has_calls([
                call(
                    1,
                    0,
                    spec.Connection.StartOk,
                    # Due Table type, we cannot compare bytestream directly
                    DataComparator('FsSs', (client_properties, 'AMQPLAIN',
                                            security_mechanism, 'en_US')),
                    None),
                call(
                    1, 0, spec.Connection.TuneOk,
                    dumps('BlB',
                          (conn.channel_max, conn.frame_max, conn.heartbeat)),
                    None),
                call(1, 0, spec.Connection.Open,
                     dumps('ssb', (conn.virtual_host, '', False)), None)
            ])
            assert conn.client_properties == client_properties
Ejemplo n.º 5
0
 def test_amqplain_no_password(self):
     username, password = '******', None
     mech = sasl.AMQPLAIN(username, password)
     response = mech.start(None)
     assert response == NotImplemented