Ejemplo n.º 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]
Ejemplo n.º 2
0
def test_useragent_windows(monkeypatch):
    """Construct a user-agent as a patched Windows machine"""
    monkeypatch.setenv("TRAVIS_TESTING", "1")
    with patch('charmcraft.commands.store.client.__version__', '1.2.3'), \
            patch('platform.system', return_value='Windows'), \
            patch('platform.release', return_value='10'), \
            patch('platform.machine', return_value='AMD64'), \
            patch('platform.python_version', return_value='3.9.1'):
        ua = build_user_agent()
    assert ua == "charmcraft/1.2.3 (testing) Windows/10 (AMD64) python/3.9.1"
Ejemplo n.º 3
0
def test_useragent_linux(monkeypatch):
    """Construct a user-agent as a patched Linux machine"""
    monkeypatch.setenv("TRAVIS_TESTING", "1")
    with patch('charmcraft.commands.store.client.__version__', '1.2.3'), \
            patch('charmcraft.commands.store.client._get_os_platform',
                  return_value="Arch Linux/5.10.10-arch1-1 (x86_64)"), \
            patch('platform.system', return_value='Linux'), \
            patch('platform.machine', return_value='x86_64'), \
            patch('platform.python_version', return_value='3.9.1'):
        ua = build_user_agent()
    assert ua == "charmcraft/1.2.3 (testing) Arch Linux/5.10.10-arch1-1 (x86_64) python/3.9.1"
Ejemplo n.º 4
0
def test_useragent_windows(monkeypatch):
    """Construct a user-agent as a patched Windows machine"""
    monkeypatch.setenv("TRAVIS_TESTING", "1")
    with patch("charmcraft.commands.store.client.__version__", "1.2.3"), patch(
        "platform.system", return_value="Windows"
    ), patch("platform.release", return_value="10"), patch(
        "platform.machine", return_value="AMD64"
    ), patch(
        "platform.python_version", return_value="3.9.1"
    ):
        ua = build_user_agent()
    assert ua == "charmcraft/1.2.3 (testing) Windows/10 (AMD64) python/3.9.1"
Ejemplo n.º 5
0
def test_useragent_linux(monkeypatch):
    """Construct a user-agent as a patched Linux machine"""
    monkeypatch.setenv("TRAVIS_TESTING", "1")
    os_platform = OSPlatform(system="Arch Linux", release="5.10.10-arch1-1", machine="x86_64")
    with patch("charmcraft.commands.store.client.__version__", "1.2.3"), patch(
        "charmcraft.utils.get_os_platform", return_value=os_platform
    ), patch("platform.system", return_value="Linux"), patch(
        "platform.machine", return_value="x86_64"
    ), patch(
        "platform.python_version", return_value="3.9.1"
    ):
        ua = build_user_agent()
    assert ua == "charmcraft/1.2.3 (testing) Arch Linux/5.10.10-arch1-1 (x86_64) python/3.9.1"
Ejemplo n.º 6
0
    def fake_request(self, method, url, json, headers):
        # check it was properly called
        assert method == "testmethod"
        assert url == "testurl"
        assert json == "testbody"
        assert headers == {"User-Agent": build_user_agent()}

        # check credentials were loaded at this time
        assert auth_holder._cookiejar is not None

        # modify the credentials, to simulate that a re-auth happened while the request
        auth_holder._cookiejar.set_cookie(other_cookie)

        return "raw request response"
def test_useragent():
    with patch('charmcraft.commands.store.client.__version__', '1.2.3'):
        ua = build_user_agent()
    assert ua == "charmcraft/1.2.3"