File: test_cacerts_from_env.py

package info (click to toggle)
python-httplib2 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,448 kB
  • sloc: python: 6,629; javascript: 3,563; makefile: 56
file content (75 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
try:
    from unittest import mock
except ImportError:
    import mock
import pytest
import tempfile
import httplib2


CA_CERTS_BUILTIN = "/etc/ssl/certs/ca-certificates.crt"
CERTIFI_CERTS_FILE = "unittest_certifi_file"
CUSTOM_CA_CERTS = "unittest_custom_ca_certs"


@pytest.fixture()
def clean_env():
    current_env_var = os.environ.get("HTTPLIB2_CA_CERTS")
    if current_env_var is not None:
        os.environ.pop("HTTPLIB2_CA_CERTS")
    yield
    if current_env_var is not None:
        os.environ["HTTPLIB2_CA_CERTS"] = current_env_var


@pytest.fixture()
def ca_certs_tmpfile(clean_env):
    tmpfd, tmpfile = tempfile.mkstemp()
    open(tmpfile, 'a').close()
    yield tmpfile
    os.remove(tmpfile)


@mock.patch("httplib2.certs.certifi_available", False)
@mock.patch("httplib2.certs.custom_ca_locater_available", False)
def test_certs_file_from_builtin(clean_env):
    assert httplib2.certs.where() == CA_CERTS_BUILTIN


@mock.patch("httplib2.certs.certifi_available", False)
@mock.patch("httplib2.certs.custom_ca_locater_available", False)
def test_certs_file_from_environment(ca_certs_tmpfile):
    os.environ["HTTPLIB2_CA_CERTS"] = ca_certs_tmpfile
    assert httplib2.certs.where() == ca_certs_tmpfile
    os.environ["HTTPLIB2_CA_CERTS"] = ""
    with pytest.raises(RuntimeError):
        httplib2.certs.where()
    os.environ.pop("HTTPLIB2_CA_CERTS")
    assert httplib2.certs.where() == CA_CERTS_BUILTIN


@mock.patch("httplib2.certs.certifi_where", mock.MagicMock(return_value=CERTIFI_CERTS_FILE))
@mock.patch("httplib2.certs.certifi_available", True)
@mock.patch("httplib2.certs.custom_ca_locater_available", False)
def test_certs_file_from_certifi(clean_env):
    assert httplib2.certs.where() == CERTIFI_CERTS_FILE


@mock.patch("httplib2.certs.certifi_available", False)
@mock.patch("httplib2.certs.custom_ca_locater_available", True)
@mock.patch("httplib2.certs.custom_ca_locater_where", mock.MagicMock(return_value=CUSTOM_CA_CERTS))
def test_certs_file_from_custom_getter(clean_env):
    assert httplib2.certs.where() == CUSTOM_CA_CERTS


@mock.patch("httplib2.certs.certifi_available", False)
@mock.patch("httplib2.certs.custom_ca_locater_available", False)
def test_with_certifi_removed_from_modules(ca_certs_tmpfile):
    if "certifi" in sys.modules:
        del sys.modules["certifi"]
    os.environ["HTTPLIB2_CA_CERTS"] = ca_certs_tmpfile
    assert httplib2.certs.where() == ca_certs_tmpfile
    os.environ.pop("HTTPLIB2_CA_CERTS")
    assert httplib2.certs.where() == CA_CERTS_BUILTIN