Beispiel #1
0
        def check_if_device_was_removed_from_azure():
            """Check if device was remove from Azure IoT HUb using azure-iot-manager service proxy."""
            if self.azure_iot_hub_mock:
                httpserver.expect_oneshot_request(
                    re.compile("^/devices"),
                    method="GET",
                    query_string="api-version=2021-04-12",
                ).respond_with_data(status=404)

            rsp = self.api_azure.with_auth(azure_user.token).call(
                "GET", iot.URL_DEVICE_STATE(dev.id))
            assert rsp.status_code == 404
            self.devices.remove(dev.id)
Beispiel #2
0
 def _check_if_device_status_is_set_to_value(self, azure_user: User,
                                             httpserver: HTTPServer,
                                             device_id: str, status: str):
     """Check if device status in IoT Hub is set to the desired value."""
     if self.azure_iot_hub_mock:
         httpserver.expect_oneshot_request(
             re.compile("^/devices"),
             method="GET",
             query_string="api-version=2021-04-12",
         ).respond_with_json(
             self._prepare_iot_hub_upsert_device_response(status=status))
     # device exists in iot-manager
     rsp = self.api_azure.with_auth(azure_user.token).call(
         "GET", iot.URL_DEVICE_STATE(device_id))
     assert rsp.status_code == 200
     # check the status of the device in IoT Hub
     device = get_azure_client().get_device(device_id)
     assert device.status == status
Beispiel #3
0
    def test_device_twin(
        self,
        azure_user: User,
        httpserver: HTTPServer,
        httpserver_ssl_context: ssl.SSLContext,
    ):
        """Test device state synchronization with IoT Hub Device Twin"""
        dev = self._prepare_device(azure_user, httpserver,
                                   httpserver_ssl_context)
        self._check_if_device_status_is_set_to_value(azure_user, httpserver,
                                                     dev.id, "enabled")

        # get the all device states (device twins)
        if self.azure_iot_hub_mock:
            httpserver.expect_oneshot_request(
                re.compile("^/devices"),
                method="GET",
                query_string="api-version=2021-04-12",
            ).respond_with_json(self._prepare_iot_hub_upsert_device_response())
        rsp = self.api_azure.with_auth(azure_user.token).call(
            "GET", iot.URL_DEVICE_STATE(dev.id))
        assert rsp.status_code == 200
        states = rsp.json()
        assert len(states.keys()) == 1
        integration_id = list(states.keys())[0]
        assert "desired" in states[integration_id]
        assert "reported" in states[integration_id]

        # set the device state (device twin)
        if self.azure_iot_hub_mock:
            httpserver.expect_oneshot_request(
                re.compile("^/twins"),
                method="GET",
                query_string="api-version=2021-04-12",
            ).respond_with_json(self._prepare_iot_hub_upsert_device_response())
            httpserver.expect_oneshot_request(
                re.compile("^/twins"),
                method="PUT",
                query_string="api-version=2021-04-12",
            ).respond_with_data(status=200)
        twin = {
            "desired": {
                "key": "value"
            },
        }
        rsp = (self.api_azure.with_auth(azure_user.token).with_header(
            "Content-Type", "application/json").call(
                "PUT",
                iot.URL_DEVICE_STATE(dev.id) + "/" + integration_id, twin))
        assert rsp.status_code == 200
        state = rsp.json()
        assert "desired" in state
        assert "reported" in states[integration_id]
        assert state["desired"]["key"] == "value"

        # get the device state (device twin)
        if self.azure_iot_hub_mock:
            httpserver.expect_oneshot_request(
                re.compile("^/twins"),
                method="GET",
                query_string="api-version=2021-04-12",
            ).respond_with_json(self._prepare_iot_hub_upsert_device_response())
        rsp = self.api_azure.with_auth(azure_user.token).call(
            "GET",
            iot.URL_DEVICE_STATE(dev.id) + "/" + integration_id)
        assert rsp.status_code == 200
        state = rsp.json()
        assert "desired" in state
        assert "reported" in states[integration_id]
        assert state["desired"]["key"] == "value"