def assert_exceptions(patchSleep, patchReqGet):
     with patchSleep, patchReqGet as mockReqRequest:
         Messenger.request_device(mock_self, "sample-tenant")
         mockReqRequest.assert_any_call(
             "http://sample-url/device",
             headers={'authorization': "Bearer super-secret-token"})
         mock_self.emit.assert_not_called()
def test_messenger_request_device():
    mock_self = Mock(
        config=Mock(device_manager={
            "url": "http://sample-url",
            "connection_retries": 3,
            "timeout_sleep": 10
        },
                    dojot={"subjects": {
                        "devices": "devices-subject"
                    }}),
        auth=Mock(get_access_token=Mock(return_value="super-secret-token")),
        emit=Mock())
    mockResponse = Mock(
        json=Mock(return_value={
            "devices": ["device-1", "device-2"],
            "pagination": {
                "has_next": False
            }
        }),
        text="there was an error, but I will not tell you.",
        status_code=200)

    def reset_scenario():
        mock_self.emit.reset_mock()

    patchReqGet = patch("requests.get", return_value=mockResponse)
    patchSleep = patch("time.sleep")

    with patchSleep, patchReqGet as mockReqRequest:
        Messenger.request_device(mock_self, "sample-tenant")
        mockReqRequest.assert_called_with(
            "http://sample-url/device",
            headers={'authorization': "Bearer super-secret-token"})

        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-1\"}")
        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-2\"}")

    reset_scenario()

    mockResponse.status_code = 301
    patchReqGet = patch("requests.get", return_value=mockResponse)

    with patchSleep, patchReqGet as mockReqRequest:
        Messenger.request_device(mock_self, "sample-tenant")
        mockReqRequest.assert_called_with(
            "http://sample-url/device",
            headers={'authorization': "Bearer super-secret-token"})

        mock_self.emit.assert_not_called()

    reset_scenario()

    mockResponse.status_code = 200
    mockResponse.json.return_value = {
        "devices": ["device-1", "device-2"],
        "pagination": {
            "has_next": True,
            "next_page": 199
        }
    }
    mockResponse2 = Mock(
        json=Mock(return_value={
            "devices": ["device-3", "device-4"],
            "pagination": {
                "has_next": False
            }
        }),
        text="there was an error, but I will not tell you.",
        status_code=200)
    patchReqGet = patch("requests.get",
                        side_effect=[mockResponse, mockResponse2])

    with patchSleep, patchReqGet as mockReqRequest:
        Messenger.request_device(mock_self, "sample-tenant")
        mockReqRequest.assert_any_call(
            "http://sample-url/device",
            headers={'authorization': "Bearer super-secret-token"})
        mockReqRequest.assert_any_call(
            "http://sample-url/device?page_num=199",
            headers={'authorization': "Bearer super-secret-token"})
        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-1\"}")
        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-2\"}")
        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-3\"}")
        mock_self.emit.assert_any_call(
            "devices-subject", "sample-tenant", "message",
            "{\"event\": \"create\", \"data\": \"device-4\"}")

    def assert_exceptions(patchSleep, patchReqGet):
        with patchSleep, patchReqGet as mockReqRequest:
            Messenger.request_device(mock_self, "sample-tenant")
            mockReqRequest.assert_any_call(
                "http://sample-url/device",
                headers={'authorization': "Bearer super-secret-token"})
            mock_self.emit.assert_not_called()

    reset_scenario()
    patchReqGet = patch("requests.get", side_effect=ConnectionError)
    assert_exceptions(patchSleep, patchReqGet)

    reset_scenario()
    patchReqGet = patch("requests.get", side_effect=Timeout)
    assert_exceptions(patchSleep, patchReqGet)

    reset_scenario()
    patchReqGet = patch("requests.get", side_effect=TooManyRedirects)
    assert_exceptions(patchSleep, patchReqGet)

    reset_scenario()
    patchReqGet = patch("requests.get", side_effect=ValueError)
    assert_exceptions(patchSleep, patchReqGet)