Beispiel #1
0
def test_storage_push_succesful():
    """Bytes are properly pushed to the Storage."""
    test_monitor = MultipartEncoderMonitor(
        MultipartEncoder(fields={"binary": ("filename", "somefile", "application/octet-stream")})
    )

    with patch("requests.Session") as mock:
        _storage_push(test_monitor, "http://test.url:0000")
    cm_session_mock = mock().__enter__()

    # check request was properly called
    url = "http://test.url:0000/unscanned-upload/"
    headers = {
        "Content-Type": test_monitor.content_type,
        "Accept": "application/json",
        "User-Agent": build_user_agent(),
    }
    cm_session_mock.post.assert_called_once_with(url, headers=headers, data=test_monitor)

    # check the retries were properly setup
    (protocol, adapter), _ = cm_session_mock.mount.call_args
    assert protocol == "https://"
    assert isinstance(adapter, HTTPAdapter)
    assert adapter.max_retries.backoff_factor == 2
    assert adapter.max_retries.total == 5
    assert adapter.max_retries.status_forcelist == [500, 502, 503, 504]
def test_storage_push_network_error():
    """A generic network error happened."""
    test_monitor = MultipartEncoderMonitor(MultipartEncoder(
        fields={"binary": ("filename", 'somefile', "application/octet-stream")}))

    with patch('requests.Session.post') as mock:
        mock.side_effect = RequestException("naughty error")
        with pytest.raises(CommandError) as cm:
            _storage_push(test_monitor)
        expected = "Network error when pushing file: RequestException('naughty error')"
        assert str(cm.value) == expected