| 1 |
import os |
| 2 |
import sys |
| 3 |
try: |
| 4 |
from unittest import mock |
| 5 |
except ImportError: |
| 6 |
import mock |
| 7 |
import pytest |
| 8 |
import tempfile |
| 9 |
import httplib2 |
| 10 |
|
| 11 |
|
| 12 |
CA_CERTS_BUILTIN = os.path.join(os.path.dirname(httplib2.__file__), "cacerts.txt") |
| 13 |
CERTIFI_CERTS_FILE = "unittest_certifi_file" |
| 14 |
CUSTOM_CA_CERTS = "unittest_custom_ca_certs" |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture() |
| 18 |
def clean_env(): |
| 19 |
current_env_var = os.environ.get("HTTPLIB2_CA_CERTS") |
| 20 |
if current_env_var is not None: |
| 21 |
os.environ.pop("HTTPLIB2_CA_CERTS") |
| 22 |
yield |
| 23 |
if current_env_var is not None: |
| 24 |
os.environ["HTTPLIB2_CA_CERTS"] = current_env_var |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture() |
| 28 |
def ca_certs_tmpfile(clean_env): |
| 29 |
tmpfd, tmpfile = tempfile.mkstemp() |
| 30 |
open(tmpfile, 'a').close() |
| 31 |
yield tmpfile |
| 32 |
os.remove(tmpfile) |
| 33 |
|
| 34 |
|
| 35 |
@mock.patch("httplib2.certs.certifi_available", False) |
| 36 |
@mock.patch("httplib2.certs.custom_ca_locater_available", False) |
| 37 |
def test_certs_file_from_builtin(clean_env): |
| 38 |
assert httplib2.certs.where() == CA_CERTS_BUILTIN |
| 39 |
|
| 40 |
|
| 41 |
@mock.patch("httplib2.certs.certifi_available", False) |
| 42 |
@mock.patch("httplib2.certs.custom_ca_locater_available", False) |
| 43 |
def test_certs_file_from_environment(ca_certs_tmpfile): |
| 44 |
os.environ["HTTPLIB2_CA_CERTS"] = ca_certs_tmpfile |
| 45 |
assert httplib2.certs.where() == ca_certs_tmpfile |
| 46 |
os.environ["HTTPLIB2_CA_CERTS"] = "" |
| 47 |
with pytest.raises(RuntimeError): |
| 48 |
httplib2.certs.where() |
| 49 |
os.environ.pop("HTTPLIB2_CA_CERTS") |
| 50 |
assert httplib2.certs.where() == CA_CERTS_BUILTIN |
| 51 |
|
| 52 |
|
| 53 |
@mock.patch("httplib2.certs.certifi_where", mock.MagicMock(return_value=CERTIFI_CERTS_FILE)) |
| 54 |
@mock.patch("httplib2.certs.certifi_available", True) |
| 55 |
@mock.patch("httplib2.certs.custom_ca_locater_available", False) |
| 56 |
def test_certs_file_from_certifi(clean_env): |
| 57 |
assert httplib2.certs.where() == CERTIFI_CERTS_FILE |
| 58 |
|
| 59 |
|
| 60 |
@mock.patch("httplib2.certs.certifi_available", False) |
| 61 |
@mock.patch("httplib2.certs.custom_ca_locater_available", True) |
| 62 |
@mock.patch("httplib2.certs.custom_ca_locater_where", mock.MagicMock(return_value=CUSTOM_CA_CERTS)) |
| 63 |
def test_certs_file_from_custom_getter(clean_env): |
| 64 |
assert httplib2.certs.where() == CUSTOM_CA_CERTS |
| 65 |
|
| 66 |
|
| 67 |
@mock.patch("httplib2.certs.certifi_available", False) |
| 68 |
@mock.patch("httplib2.certs.custom_ca_locater_available", False) |
| 69 |
def test_with_certifi_removed_from_modules(ca_certs_tmpfile): |
| 70 |
if "certifi" in sys.modules: |
| 71 |
del sys.modules["certifi"] |
| 72 |
os.environ["HTTPLIB2_CA_CERTS"] = ca_certs_tmpfile |
| 73 |
assert httplib2.certs.where() == ca_certs_tmpfile |
| 74 |
os.environ.pop("HTTPLIB2_CA_CERTS") |
| 75 |
assert httplib2.certs.where() == CA_CERTS_BUILTIN |