def test_receive_register_response_after_query_time_passes_calls_callback_with_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # sleep so that it times out query
        time.sleep(constant.DEFAULT_TIMEOUT_INTERVAL + 1)

        polling_machine._on_disconnect_completed_error()

        assert mock_request_response_provider.publish.call_count == 1
        assert mock_callback.call_count == 1
        print(mock_callback.call_args)
        assert mock_callback.call_args[0][1].args[
            0] == "Time is up for query timer"
    def test_receive_query_response_failure_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for first query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query

        fake_register_topic = fake_success_response_topic + "$rid={}".format(
            fake_request_id)
        fake_register_payload_result = ('{"operationId":"' +
                                        fake_operation_id + '","status":"' +
                                        fake_assigning_status + '"}')

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        # Response for register to transition to waiting and polling
        mock_request_response_provider.receive_response(
            fake_register_topic, fake_register_payload_result.encode("utf-8"))

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        fake_query_topic_1 = fake_failure_response_topic + "$rid={}".format(
            fake_request_id_query)
        fake_query_payload_result = "HelloHogwarts"

        # Response for query
        mock_request_response_provider.receive_response(
            fake_query_topic_1, fake_query_payload_result.encode("utf-8"))

        polling_machine._on_disconnect_completed_error()

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[0][1], ValueError)
        assert mock_callback.call_args[0][1].args[
            0] == "Incoming message failure"
Ejemplo n.º 3
0
    def test_receive_register_response_assigned_completes_registration(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)

        polling_machine._request_response_provider = mock_request_response_provider
        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]
        key_value_dict["retry-after"] = [fake_retry_after, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_registration_state = ('{"registrationId":"' +
                                   fake_registration_id + '","assignedHub":"' +
                                   fake_assigned_hub + '","deviceId":"' +
                                   fake_device_id + '","substatus":"' +
                                   fake_sub_status + '"}')

        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_assigned_status +
                               '","registrationState":' +
                               fake_registration_state + "}")

        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict, fake_payload_result)

        polling_machine._on_disconnect_completed_register()

        assert state_based_mqtt.send_request.call_count == 1
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_payload"] == " "

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[1]["result"],
                          RegistrationResult)
        registration_result = mock_callback.call_args[1]["result"]

        registration_result.request_id == fake_request_id
        registration_result.operation_id == fake_operation_id
        registration_result.status == fake_assigned_status
        registration_result.registration_state.device_id == fake_device_id
        registration_result.registration_state.sub_status == fake_sub_status
    def test_receive_register_response_assigned_completes_registration(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)

        polling_machine._request_response_provider = mock_request_response_provider
        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_topic = fake_success_response_topic + "$rid={}&retry-after={}".format(
            fake_request_id, fake_retry_after)

        fake_registration_state = ('{"registrationId":"' +
                                   fake_registration_id + '","assignedHub":"' +
                                   fake_assigned_hub + '","deviceId":"' +
                                   fake_device_id + '","substatus":"' +
                                   fake_sub_status + '"}')

        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_assigned_status +
                               '","registrationState":' +
                               fake_registration_state + "}")

        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        polling_machine._on_disconnect_completed_register()

        assert mock_request_response_provider.publish.call_count == 1
        # assert polling_machine.on_registration_complete.call_count == 1
        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[0][0], RegistrationResult)
        registration_result = mock_callback.call_args[0][0]

        registration_result.request_id == fake_request_id
        registration_result.operation_id == fake_operation_id
        registration_result.status == fake_assigned_status
        registration_result.registration_state.device_id == fake_device_id
        registration_result.registration_state.sub_status == fake_sub_status
def mock_polling_machine(mocker, mock_request_response_provider):
    state_based_mqtt = MagicMock()
    mock_init_request_response_provider = mocker.patch(
        "azure.iot.device.provisioning.internal.polling_machine.RequestResponseProvider"
    )
    mock_init_request_response_provider.return_value = mock_request_response_provider
    mock_polling_machine = PollingMachine(state_based_mqtt)
    return mock_polling_machine
 def __init__(self, provisioning_pipeline):
     """
     Initializer for the Provisioning Client.
     NOTE : This initializer should not be called directly.
     Instead, the class method `create_from_security_client` should be used to create a client object.
     :param provisioning_pipeline: The protocol pipeline for provisioning. As of now this only supports MQTT.
     """
     super(ProvisioningDeviceClient, self).__init__(provisioning_pipeline)
     self._polling_machine = PollingMachine(provisioning_pipeline)
Ejemplo n.º 7
0
    def test_receive_register_response_assigning_does_query_with_operation_id(self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)

        polling_machine._request_response_provider = mock_request_response_provider
        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]
        key_value_dict["retry-after"] = [fake_retry_after, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query

        fake_payload_result = (
            '{"operationId":"' + fake_operation_id + '","status":"' + fake_assigning_status + '"}'
        )

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer"
        )

        # Complete string pre-fixed by a b is the one that works for all versions of python
        # or a encode on a string works for all versions of python
        # For only python 3 , bytes(JsonString, "utf-8") can be done
        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict, fake_payload_result
        )

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert state_based_mqtt.send_request.call_count == 2
        assert state_based_mqtt.send_request.call_args_list[0][1]["request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1]["request_payload"] == " "

        assert (
            state_based_mqtt.send_request.call_args_list[1][1]["request_id"]
            == fake_request_id_query
        )
        assert (
            state_based_mqtt.send_request.call_args_list[1][1]["operation_id"] == fake_operation_id
        )
        assert state_based_mqtt.send_request.call_args_list[1][1]["request_payload"] == " "
Ejemplo n.º 8
0
    def __init__(self, provisioning_pipeline):
        """
        Initializer for the Provisioning Client.

        NOTE: This initializer should not be called directly.
        Instead, the class methods that start with `create_from_` should be used to create a
        client object.

        :param provisioning_pipeline: The protocol pipeline for provisioning.
        :type provisioning_pipeline: :class:`azure.iot.device.provisioning.pipeline.ProvisioningPipeline`
        """
        super(ProvisioningDeviceClient, self).__init__(provisioning_pipeline)
        self._polling_machine = PollingMachine(provisioning_pipeline)
Ejemplo n.º 9
0
    def test_receive_register_response_greater_than_429_does_register_again(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]
        key_value_dict["retry-after"] = [fake_retry_after, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for second time register
        mock_init_uuid.reset_mock()
        fake_request_id_2 = "Request4567"
        mock_init_uuid.return_value = fake_request_id_2

        fake_payload_result = "HelloHogwarts"

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        mock_request_response_provider.receive_response(
            fake_request_id, "430", key_value_dict, fake_payload_result)

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert state_based_mqtt.send_request.call_count == 2
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_payload"] == " "

        assert state_based_mqtt.send_request.call_args_list[1][1][
            "request_id"] == fake_request_id_2
        assert state_based_mqtt.send_request.call_args_list[1][1][
            "request_payload"] == " "
Ejemplo n.º 10
0
    def test_receive_register_response_failure_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_payload_result = "HelloHogwarts"
        mock_request_response_provider.receive_response(
            fake_request_id, "400", key_value_dict, fake_payload_result)

        polling_machine._on_disconnect_completed_error()

        assert state_based_mqtt.send_request.call_count == 1
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_payload"] == " "

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[1]["error"], ValueError)
        assert mock_callback.call_args[1]["error"].args[
            0] == "Incoming message failure"
Ejemplo n.º 11
0
    def test_receive_register_response_some_unknown_status_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_unknown_status = "disabled"
        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_unknown_status + '"}')

        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict, fake_payload_result)

        polling_machine._on_disconnect_completed_error()

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[1]["error"], ValueError)
        assert (mock_callback.call_args[1]["error"].args[0] ==
                "Other types of failure have occurred.")
        assert mock_callback.call_args[1]["error"].args[
            1] == fake_payload_result
    def test_receive_register_response_assigning_does_query_with_operation_id(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)

        polling_machine._request_response_provider = mock_request_response_provider
        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query

        fake_topic = fake_success_response_topic + "$rid={}&retry-after={}".format(
            fake_request_id, fake_retry_after)
        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_assigning_status + '"}')

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        # Complete string pre-fixed by a b is the one that works for all versions of python
        # or a encode on a string works for all versions of python
        # For only python 3 , bytes(JsonString, "utf-8") can be done
        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert mock_request_response_provider.publish.call_count == 2
        mock_request_response_provider.publish.assert_any_call(
            topic=constant.PUBLISH_TOPIC_QUERYING.format(
                fake_request_id_query, fake_operation_id),
            request=" ",
        )
    def test_receive_register_response_some_unknown_status_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_unknown_status = "disabled"
        fake_topic = fake_success_response_topic + "$rid={}".format(
            fake_request_id)
        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_unknown_status + '"}')

        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        polling_machine._on_disconnect_completed_error()

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[0][1], ValueError)
        assert mock_callback.call_args[0][1].args[
            0] == "Other types of failure have occurred."
        assert mock_callback.call_args[0][1].args[1] == fake_payload_result
    def test_receive_register_response_failure_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        fake_topic = fake_failure_response_topic + "$rid={}".format(
            fake_request_id)

        fake_payload_result = "HelloHogwarts"
        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        polling_machine._on_disconnect_completed_error()

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[0][1], ValueError)
        assert mock_callback.call_args[0][1].args[
            0] == "Incoming message failure"
    def test_receive_register_response_greater_than_429_does_register_again(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider
        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for second time register
        mock_init_uuid.reset_mock()
        fake_request_id_2 = "Request4567"
        mock_init_uuid.return_value = fake_request_id_2

        fake_topic = fake_greater_429_response_topic + "$rid={}&retry-after={}".format(
            fake_request_id, fake_retry_after)

        fake_payload_result = "HelloHogwarts"

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert mock_request_response_provider.publish.call_count == 2
        mock_request_response_provider.publish.assert_any_call(
            topic=constant.PUBLISH_TOPIC_REGISTRATION.format(
                fake_request_id_2),
            request=" ")
    def test_register_and_cancel_clears_timers_and_disconnects(self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query

        fake_topic = fake_success_response_topic + "$rid={}&retry-after={}".format(
            fake_request_id, fake_retry_after)
        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_assigning_status + '"}')

        mock_request_response_provider.receive_response(
            fake_topic, fake_payload_result.encode("utf-8"))

        polling_timer = polling_machine._polling_timer
        query_timer = polling_machine._query_timer
        poling_timer_cancel = mocker.patch.object(polling_timer, "cancel")
        query_timer_cancel = mocker.patch.object(query_timer, "cancel")

        mock_cancel_callback = MagicMock()
        polling_machine.cancel(mock_cancel_callback)

        assert poling_timer_cancel.call_count == 1
        assert query_timer_cancel.call_count == 1

        mock_request_response_provider.disconnect.assert_called_once_with(
            callback=polling_machine._on_disconnect_completed_cancel)
        polling_machine._on_disconnect_completed_cancel()

        assert mock_cancel_callback.call_count == 1
    def test_receive_query_response_greater_than_429_does_query_again_with_same_operation_id(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = TestRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "subscribe")
        mocker.patch.object(mock_request_response_provider, "publish")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for first query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query

        fake_register_topic = fake_success_response_topic + "$rid={}".format(
            fake_request_id)
        fake_register_payload_result = ('{"operationId":"' +
                                        fake_operation_id + '","status":"' +
                                        fake_assigning_status + '"}')

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        # Response for register to transition to waiting polling
        mock_request_response_provider.receive_response(
            fake_register_topic, fake_register_payload_result.encode("utf-8"))

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        # reset mock to generate different request id for second query
        mock_init_uuid.reset_mock()
        fake_request_id_query_2 = "Request7890"
        mock_init_uuid.return_value = fake_request_id_query_2

        fake_query_topic_1 = fake_greater_429_response_topic + "$rid={}".format(
            fake_request_id_query)
        fake_query_payload_result = "HelloHogwarts"

        mock_init_polling_timer.reset_mock()

        # Response for query
        mock_request_response_provider.receive_response(
            fake_query_topic_1, fake_query_payload_result.encode("utf-8"))

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert mock_request_response_provider.publish.call_count == 3
        mock_request_response_provider.publish.assert_any_call(
            topic=constant.PUBLISH_TOPIC_QUERYING.format(
                fake_request_id_query_2, fake_operation_id),
            request=" ",
        )
Ejemplo n.º 18
0
    def test_receive_query_response_failure_calls_callback_of_register_error(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        mock_callback = MagicMock()
        polling_machine.register(callback=mock_callback)

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for first query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query
        key_value_dict_2 = {}
        key_value_dict_2["request_id"] = [fake_request_id_query, " "]

        fake_register_payload_result = ('{"operationId":"' +
                                        fake_operation_id + '","status":"' +
                                        fake_assigning_status + '"}')

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        # Response for register to transition to waiting and polling
        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict,
            fake_register_payload_result)

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        fake_query_payload_result = "HelloHogwarts"

        # Response for query
        mock_request_response_provider.receive_response(
            fake_request_id_query, "400", key_value_dict_2,
            fake_query_payload_result)

        polling_machine._on_disconnect_completed_error()

        assert state_based_mqtt.send_request.call_count == 2
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_payload"] == " "

        assert (state_based_mqtt.send_request.call_args_list[1][1]
                ["request_id"] == fake_request_id_query)
        assert (state_based_mqtt.send_request.call_args_list[1][1]
                ["operation_id"] == fake_operation_id)
        assert state_based_mqtt.send_request.call_args_list[1][1][
            "request_payload"] == " "

        assert mock_callback.call_count == 1
        assert isinstance(mock_callback.call_args[1]["error"], ValueError)
        assert mock_callback.call_args[1]["error"].args[
            0] == "Incoming message failure"
Ejemplo n.º 19
0
    def test_receive_query_response_greater_than_429_does_query_again_with_same_operation_id(
            self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for first query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query
        key_value_dict_2 = {}
        key_value_dict_2["request_id"] = [fake_request_id_query, " "]

        # fake_register_topic = fake_success_response_topic + "$rid={}".format(fake_request_id)
        fake_register_payload_result = ('{"operationId":"' +
                                        fake_operation_id + '","status":"' +
                                        fake_assigning_status + '"}')

        mock_init_polling_timer = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.Timer")

        # Response for register to transition to waiting polling
        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict,
            fake_register_payload_result)

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        # reset mock to generate different request id for second query
        mock_init_uuid.reset_mock()
        fake_request_id_query_2 = "Request7890"
        mock_init_uuid.return_value = fake_request_id_query_2

        fake_query_payload_result = "HelloHogwarts"

        mock_init_polling_timer.reset_mock()

        # Response for query
        mock_request_response_provider.receive_response(
            fake_request_id_query, "430", key_value_dict_2,
            fake_query_payload_result)

        # call polling timer's time up call to simulate polling
        time_up_call = mock_init_polling_timer.call_args[0][1]
        time_up_call()

        assert state_based_mqtt.send_request.call_count == 3
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_id"] == fake_request_id
        assert state_based_mqtt.send_request.call_args_list[0][1][
            "request_payload"] == " "

        assert (state_based_mqtt.send_request.call_args_list[1][1]
                ["request_id"] == fake_request_id_query)
        assert (state_based_mqtt.send_request.call_args_list[1][1]
                ["operation_id"] == fake_operation_id)
        assert state_based_mqtt.send_request.call_args_list[1][1][
            "request_payload"] == " "

        assert (state_based_mqtt.send_request.call_args_list[2][1]
                ["request_id"] == fake_request_id_query_2)
        assert (state_based_mqtt.send_request.call_args_list[2][1]
                ["operation_id"] == fake_operation_id)
        assert state_based_mqtt.send_request.call_args_list[2][1][
            "request_payload"] == " "
Ejemplo n.º 20
0
    def test_register_and_cancel_clears_timers_and_disconnects(self, mocker):
        state_based_mqtt = MagicMock()
        mock_request_response_provider = SomeRequestResponseProvider(
            state_based_mqtt)
        polling_machine = PollingMachine(state_based_mqtt)
        polling_machine._request_response_provider = mock_request_response_provider

        mocker.patch.object(mock_request_response_provider, "enable_responses")
        mocker.patch.object(state_based_mqtt, "send_request")
        mocker.patch.object(mock_request_response_provider, "disconnect")

        # to transition into initializing
        polling_machine.register(callback=MagicMock())

        mock_init_uuid = mocker.patch(
            "azure.iot.device.provisioning.internal.polling_machine.uuid.uuid4"
        )
        mock_init_uuid.return_value = fake_request_id
        key_value_dict = {}
        key_value_dict["request_id"] = [fake_request_id, " "]

        # to transition into registering
        polling_machine._on_subscribe_completed()

        # reset mock to generate different request id for query
        mock_init_uuid.reset_mock()
        fake_request_id_query = "Request4567"
        mock_init_uuid.return_value = fake_request_id_query
        key_value_dict_2 = {}
        key_value_dict_2["request_id"] = [fake_request_id_query, " "]

        fake_payload_result = ('{"operationId":"' + fake_operation_id +
                               '","status":"' + fake_assigning_status + '"}')

        mock_request_response_provider.receive_response(
            fake_request_id, "200", key_value_dict, fake_payload_result)

        polling_timer = polling_machine._polling_timer
        query_timer = polling_machine._query_timer
        poling_timer_cancel = mocker.patch.object(polling_timer, "cancel")
        query_timer_cancel = mocker.patch.object(query_timer, "cancel")

        mock_cancel_callback = MagicMock()
        polling_machine.cancel(mock_cancel_callback)

        assert poling_timer_cancel.call_count == 1
        assert query_timer_cancel.call_count == 1

        assert mock_request_response_provider.disconnect.call_count == 1
        polling_machine._on_disconnect_completed_cancel()

        assert mock_cancel_callback.call_count == 1