5
5
from cryptography .x509 .oid import ExtensionOID , AuthorityInformationAccessOID , NameOID
6
6
import pytest
7
7
8
+
9
+ try :
10
+ from contextlib import nullcontext as does_not_raise
11
+ except ImportError :
12
+ # Python 2 fallback
13
+ class nullcontext (object ):
14
+ def __init__ (self , enter_result = None ):
15
+ self .enter_result = enter_result
16
+
17
+ def __enter__ (self ):
18
+ return self .enter_result
19
+
20
+ def __exit__ (self , * excinfo ):
21
+ pass
22
+
23
+ does_not_raise = nullcontext
24
+
8
25
try :
9
26
unicode # type: ignore
10
27
except NameError :
@@ -29,6 +46,16 @@ def test_certcontainer_x509_helper_props(cert):
29
46
assert fixture ["ca_issuer_access_location" ] == c .ca_issuer_access_location
30
47
31
48
49
+ def test_cert_constructor_requires_x509 ():
50
+ with pytest .raises (TypeError , match = "Argument must be a x509" ):
51
+ Cert ("not a x509 obj" )
52
+
53
+
54
+ def test_cert__eq__raises (mocker ):
55
+ with pytest .raises (TypeError ):
56
+ Cert (mocker .Mock (spec = x509 .Certificate )).__eq__ ("Not a Cert" )
57
+
58
+
32
59
@pytest .mark .parametrize (
33
60
"_subject" ,
34
61
["CA - XD 9001" , pytest .param ("CN=github.com,O=GitHub" , marks = [pytest .mark .xfail ])],
@@ -137,6 +164,28 @@ def test_missing_cert_properties_raise(mocker, prop, cert_prop, cert_value):
137
164
getattr (c , prop )
138
165
139
166
167
+ @pytest .mark .parametrize (
168
+ "value,expectation" ,
169
+ [
170
+ (b"Common name" , does_not_raise ()),
171
+ (unicode ("Common name" ), does_not_raise ()),
172
+ (["unexpected type" ], pytest .raises (ValueError )),
173
+ ],
174
+ )
175
+ def test_common_name_handles_unicode_or_bytes (mocker , value , expectation ):
176
+ m = mocker .Mock (
177
+ spec = x509 .Certificate ,
178
+ subject = mocker .Mock (
179
+ get_attributes_for_oid = mocker .Mock (
180
+ return_value = [mocker .Mock (spec = type (value ), value = value )]
181
+ )
182
+ ),
183
+ )
184
+ with expectation :
185
+ c = Cert (m )
186
+ assert c .common_name == "Common name"
187
+
188
+
140
189
def test_repr ():
141
190
class CertOverride (Cert ):
142
191
subject = "Subject"
0 commit comments