Пример #1
0
    def test_that_when_one_authentication_task_is_successful_remaining_ones_are_cancelled(
            self, event_loop):
        with override_settings(
                CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
                CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
                SIGNING_SERVICE_PUBLIC_KEY=SIGNING_SERVICE_PUBLIC_KEY,
        ):

            async def mock_coro():
                await asyncio.sleep(3)

            some_bytes = b"some bytes"
            mocked_reader = prepare_mocked_reader(some_bytes)
            mocked_writer = prepare_mocked_writer()
            mocked_writer2 = prepare_mocked_writer()
            mock_authentication_task = event_loop.create_task(mock_coro())
            with mock.patch("middleman.middleman_server.is_authenticated",
                            new=async_stream_actor_mock(return_value=True)):
                self.middleman._ss_connection_candidates.append(
                    (mock_authentication_task, mocked_writer2))

                event_loop.run_until_complete(
                    self.middleman._authenticate_signing_service(
                        mocked_reader, mocked_writer))

                assert_that(mock_authentication_task.cancelled()).is_true()
                mocked_writer2.close.assert_called_once_with()
                assert_that(
                    self.middleman._ss_connection_candidates).is_empty()
                assert_that(self.middleman.
                            _is_signing_service_connection_active).is_true()
Пример #2
0
def _get_mocked_reader(message, request_id, sign_as, **kwargs):
    protocol_message = create_middleman_protocol_message(PayloadType.GOLEM_MESSAGE, message, request_id)
    data_to_send = append_frame_separator(
        escape_encode_raw_message(
            protocol_message.serialize(sign_as)
        )
    )
    mocked_reader = prepare_mocked_reader(data_to_send, **kwargs)
    return mocked_reader
Пример #3
0
    def test_that_when_signing_service_connection_is_active_subsequent_attempts_will_fail(
            self, event_loop):
        self.middleman._is_signing_service_connection_active = True
        mocked_reader = prepare_mocked_reader(b"some bytes")
        mocked_writer = prepare_mocked_writer()

        event_loop.run_until_complete(
            self.middleman._handle_service_connection(mocked_reader,
                                                      mocked_writer))

        mocked_writer.close.assert_called_once_with()
Пример #4
0
    async def test_that_when_client_terminates_connection_coroutine_ends(self, event_loop):
        with override_settings(
            CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
        ):
            some_bytes = b"some bytes"
            mocked_reader = prepare_mocked_reader(some_bytes, side_effect=IncompleteReadError(some_bytes, 777))

            producer_task = event_loop.create_task(
                request_producer(self.queue, self.response_queue, mocked_reader, self.connection_id)
            )
            await producer_task

            assert_that(self.queue.empty()).is_true()
Пример #5
0
    def test_that_when_authentication_is_unsuccessful_then_connection_ends(
            self, event_loop):
        self.middleman._is_signing_service_connection_active = False
        mocked_reader = prepare_mocked_reader(b"some bytes")
        mocked_writer = prepare_mocked_writer()

        with mock.patch.object(
                self.middleman,
                "_authenticate_signing_service",
                new=async_stream_actor_mock(return_value=False)):
            event_loop.run_until_complete(
                self.middleman._handle_service_connection(
                    mocked_reader, mocked_writer))

            mocked_writer.close.assert_called_once_with()
Пример #6
0
    async def test_that_if_signing_service_closes_connection_coroutine_ends(self, event_loop):
        with patch("middleman.asynchronous_operations.logger") as mocked_logger:
            with override_settings(
                CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
                CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
                SIGNING_SERVICE_PUBLIC_KEY=SIGNING_SERVICE_PUBLIC_KEY
            ):
                some_bytes = b"some bytes"
                mocked_reader = prepare_mocked_reader(some_bytes, side_effect=IncompleteReadError(some_bytes, 777))
                initial_message_tracker = OrderedDict(self.message_tracker)
                producer_task = event_loop.create_task(
                    response_producer(self.response_queue_pool, mocked_reader, self.message_tracker)
                )
                await producer_task

                assert_that(self.message_tracker).is_equal_to(initial_message_tracker)
                assert_that(self.response_queue_pool[self.connection_id_1].empty()).is_true()
                assert_that(self.response_queue_pool[self.connection_id_4].empty()).is_true()
                assert_that(mocked_logger.info.mock_calls[0][1][0]).contains(
                    "Signing Service has closed the connection"
                )
Пример #7
0
    def test_that_when_authentication_is_successful_connection_lasts_until_its_end(
            self, event_loop):
        with override_settings(
                CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
                CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
                SIGNING_SERVICE_PUBLIC_KEY=SIGNING_SERVICE_PUBLIC_KEY,
        ):
            self.middleman._is_signing_service_connection_active = False
            some_bytes = b"some bytes"
            mocked_reader = prepare_mocked_reader(
                some_bytes,
                side_effect=asyncio.IncompleteReadError(some_bytes, 123))
            mocked_writer = prepare_mocked_writer()

            with mock.patch.object(
                    self.middleman,
                    "_authenticate_signing_service",
                    new=async_stream_actor_mock(return_value=True)):
                event_loop.run_until_complete(
                    self.middleman._handle_service_connection(
                        mocked_reader, mocked_writer))

                self.crash_logger_mock.assert_not_called()
Пример #8
0
 def _prepare_mocked_reader(frame: AbstractFrame, private_key: bytes = SIGNING_SERVICE_PRIVATE_KEY):
     serialized = frame.serialize(private_key)
     data_to_send = append_frame_separator(escape_encode_raw_message(serialized))
     return prepare_mocked_reader(data_to_send)