| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
"""Tests for the ssl brain.""" |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from astroid import bases, nodes, parse |
| 10 |
from astroid.const import PY312_PLUS |
| 11 |
|
| 12 |
|
| 13 |
def test_ssl_brain() -> None: |
| 14 |
"""Test ssl brain transform.""" |
| 15 |
module = parse(""" |
| 16 |
import ssl |
| 17 |
ssl.PROTOCOL_TLSv1 |
| 18 |
ssl.VerifyMode |
| 19 |
ssl.TLSVersion |
| 20 |
ssl.VerifyMode.CERT_REQUIRED |
| 21 |
""") |
| 22 |
inferred_protocol = next(module.body[1].value.infer()) |
| 23 |
assert isinstance(inferred_protocol, nodes.Const) |
| 24 |
|
| 25 |
inferred_verifymode = next(module.body[2].value.infer()) |
| 26 |
assert isinstance(inferred_verifymode, nodes.ClassDef) |
| 27 |
assert inferred_verifymode.name == "VerifyMode" |
| 28 |
assert len(inferred_verifymode.bases) == 1 |
| 29 |
|
| 30 |
# Check that VerifyMode correctly inherits from enum.IntEnum |
| 31 |
int_enum = next(inferred_verifymode.bases[0].infer()) |
| 32 |
assert isinstance(int_enum, nodes.ClassDef) |
| 33 |
assert int_enum.name == "IntEnum" |
| 34 |
assert int_enum.parent.name == "enum" |
| 35 |
|
| 36 |
# TLSVersion is inferred from the main module, not from the brain |
| 37 |
inferred_tlsversion = next(module.body[3].value.infer()) |
| 38 |
assert isinstance(inferred_tlsversion, nodes.ClassDef) |
| 39 |
assert inferred_tlsversion.name == "TLSVersion" |
| 40 |
|
| 41 |
# TLSVersion is inferred from the main module, not from the brain |
| 42 |
inferred_cert_required = next(module.body[4].value.infer()) |
| 43 |
assert isinstance(inferred_cert_required, bases.Instance) |
| 44 |
assert inferred_cert_required._proxied.name == "CERT_REQUIRED" |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.skipif(not PY312_PLUS, reason="Uses new 3.12 constant") |
| 48 |
def test_ssl_brain_py312() -> None: |
| 49 |
"""Test ssl brain transform.""" |
| 50 |
module = parse(""" |
| 51 |
import ssl |
| 52 |
ssl.OP_LEGACY_SERVER_CONNECT |
| 53 |
ssl.Options.OP_LEGACY_SERVER_CONNECT |
| 54 |
""") |
| 55 |
|
| 56 |
inferred_constant = next(module.body[1].value.infer()) |
| 57 |
assert isinstance(inferred_constant, nodes.Const) |
| 58 |
|
| 59 |
inferred_instance = next(module.body[2].value.infer()) |
| 60 |
assert isinstance(inferred_instance, bases.Instance) |