summaryrefslogtreecommitdiff
blob: 405eb5325ad9191fb80d53cb2255ace06f6afe3e (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -11,9 +11,15 @@ from time import sleep
 from uuid import uuid4
 
 import pytest
-import trustme
-from cryptography.hazmat.backends import default_backend
-from cryptography.hazmat.primitives import serialization
+
+gentoo_got_rust_deps = True
+
+try:
+    import trustme
+    from cryptography.hazmat.backends import default_backend
+    from cryptography.hazmat.primitives import serialization
+except ImportError:
+    gentoo_got_rust_deps = False
 
 from uvicorn.config import LOGGING_CONFIG
 
@@ -28,72 +34,72 @@ from uvicorn.config import LOGGING_CONFIG
 # See also: https://github.com/pytest-dev/pytest/issues/3697
 LOGGING_CONFIG["loggers"]["uvicorn"]["propagate"] = True
 
-
-@pytest.fixture
-def tls_certificate_authority() -> trustme.CA:
-    return trustme.CA()
+if gentoo_got_rust_deps:
+    @pytest.fixture
+    def tls_certificate_authority() -> trustme.CA:
+        return trustme.CA()
 
 
-@pytest.fixture
-def tls_certificate(tls_certificate_authority: trustme.CA) -> trustme.LeafCert:
-    return tls_certificate_authority.issue_cert(
-        "localhost",
-        "127.0.0.1",
-        "::1",
-    )
+    @pytest.fixture
+    def tls_certificate(tls_certificate_authority: trustme.CA) -> trustme.LeafCert:
+        return tls_certificate_authority.issue_cert(
+            "localhost",
+            "127.0.0.1",
+            "::1",
+        )
 
 
-@pytest.fixture
-def tls_ca_certificate_pem_path(tls_certificate_authority: trustme.CA):
-    with tls_certificate_authority.cert_pem.tempfile() as ca_cert_pem:
-        yield ca_cert_pem
+    @pytest.fixture
+    def tls_ca_certificate_pem_path(tls_certificate_authority: trustme.CA):
+        with tls_certificate_authority.cert_pem.tempfile() as ca_cert_pem:
+            yield ca_cert_pem
 
 
-@pytest.fixture
-def tls_ca_certificate_private_key_path(tls_certificate_authority: trustme.CA):
-    with tls_certificate_authority.private_key_pem.tempfile() as private_key:
-        yield private_key
+    @pytest.fixture
+    def tls_ca_certificate_private_key_path(tls_certificate_authority: trustme.CA):
+        with tls_certificate_authority.private_key_pem.tempfile() as private_key:
+            yield private_key
 
 
-@pytest.fixture
-def tls_certificate_private_key_encrypted_path(tls_certificate):
-    private_key = serialization.load_pem_private_key(
-        tls_certificate.private_key_pem.bytes(),
-        password=None,
-        backend=default_backend(),
-    )
-    encrypted_key = private_key.private_bytes(
-        serialization.Encoding.PEM,
-        serialization.PrivateFormat.TraditionalOpenSSL,
-        serialization.BestAvailableEncryption(b"uvicorn password for the win"),
-    )
-    with trustme.Blob(encrypted_key).tempfile() as private_encrypted_key:
-        yield private_encrypted_key
+    @pytest.fixture
+    def tls_certificate_private_key_encrypted_path(tls_certificate):
+        private_key = serialization.load_pem_private_key(
+            tls_certificate.private_key_pem.bytes(),
+            password=None,
+            backend=default_backend(),
+        )
+        encrypted_key = private_key.private_bytes(
+            serialization.Encoding.PEM,
+            serialization.PrivateFormat.TraditionalOpenSSL,
+            serialization.BestAvailableEncryption(b"uvicorn password for the win"),
+        )
+        with trustme.Blob(encrypted_key).tempfile() as private_encrypted_key:
+            yield private_encrypted_key
 
 
-@pytest.fixture
-def tls_certificate_private_key_path(tls_certificate: trustme.CA):
-    with tls_certificate.private_key_pem.tempfile() as private_key:
-        yield private_key
+    @pytest.fixture
+    def tls_certificate_private_key_path(tls_certificate: trustme.CA):
+        with tls_certificate.private_key_pem.tempfile() as private_key:
+            yield private_key
 
 
-@pytest.fixture
-def tls_certificate_key_and_chain_path(tls_certificate: trustme.LeafCert):
-    with tls_certificate.private_key_and_cert_chain_pem.tempfile() as cert_pem:
-        yield cert_pem
+    @pytest.fixture
+    def tls_certificate_key_and_chain_path(tls_certificate: trustme.LeafCert):
+        with tls_certificate.private_key_and_cert_chain_pem.tempfile() as cert_pem:
+            yield cert_pem
 
 
-@pytest.fixture
-def tls_certificate_server_cert_path(tls_certificate: trustme.LeafCert):
-    with tls_certificate.cert_chain_pems[0].tempfile() as cert_pem:
-        yield cert_pem
+    @pytest.fixture
+    def tls_certificate_server_cert_path(tls_certificate: trustme.LeafCert):
+        with tls_certificate.cert_chain_pems[0].tempfile() as cert_pem:
+            yield cert_pem
 
 
-@pytest.fixture
-def tls_ca_ssl_context(tls_certificate_authority: trustme.CA) -> ssl.SSLContext:
-    ssl_ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
-    tls_certificate_authority.configure_trust(ssl_ctx)
-    return ssl_ctx
+    @pytest.fixture
+    def tls_ca_ssl_context(tls_certificate_authority: trustme.CA) -> ssl.SSLContext:
+        ssl_ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
+        tls_certificate_authority.configure_trust(ssl_ctx)
+        return ssl_ctx
 
 
 @pytest.fixture(scope="package")