33from datetime import datetime
44
55import pytest
6+ from cryptography import x509
7+ from cryptography .hazmat .primitives .serialization import load_pem_private_key
8+ from cryptography .x509 .oid import NameOID
69from factory .django import FileField
710from freezegun import freeze_time
8- from OpenSSL import crypto
11+ from OpenSSL . crypto import X509
912
1013from django_afip import factories
1114
@@ -15,12 +18,13 @@ def test_key_generation() -> None:
1518 taxpayer = factories .TaxPayerFactory .build (key = None )
1619 taxpayer .generate_key ()
1720
18- key = taxpayer .key .file .read ().decode ()
19- assert key .splitlines ()[0 ] == "-----BEGIN PRIVATE KEY-----"
20- assert key .splitlines ()[- 1 ] == "-----END PRIVATE KEY-----"
21+ key = taxpayer .key .file .read ()
22+ key_str = key .decode ()
23+ assert key_str .splitlines ()[0 ] == "-----BEGIN PRIVATE KEY-----"
24+ assert key_str .splitlines ()[- 1 ] == "-----END PRIVATE KEY-----"
2125
22- loaded_key = crypto . load_privatekey ( crypto . FILETYPE_PEM , key )
23- assert isinstance ( loaded_key , crypto . PKey )
26+ loaded_key = load_pem_private_key ( key , password = None )
27+ assert loaded_key is not None
2428
2529
2630def test_dont_overwrite_keys () -> None :
@@ -39,14 +43,15 @@ def test_overwrite_keys_force() -> None:
3943 taxpayer = factories .TaxPayerFactory .build (key__data = text )
4044
4145 taxpayer .generate_key (force = True )
42- key = taxpayer .key .file .read ().decode ()
46+ key = taxpayer .key .file .read ()
47+ key_str = key .decode ()
4348
4449 assert text != key
45- assert key .splitlines ()[0 ] == "-----BEGIN PRIVATE KEY-----"
46- assert key .splitlines ()[- 1 ] == "-----END PRIVATE KEY-----"
50+ assert key_str .splitlines ()[0 ] == "-----BEGIN PRIVATE KEY-----"
51+ assert key_str .splitlines ()[- 1 ] == "-----END PRIVATE KEY-----"
4752
48- loaded_key = crypto . load_privatekey ( crypto . FILETYPE_PEM , key )
49- assert isinstance ( loaded_key , crypto . PKey )
53+ loaded_key = load_pem_private_key ( key , password = None )
54+ assert loaded_key is not None
5055
5156
5257@freeze_time (datetime .fromtimestamp (1489537017 ))
@@ -56,29 +61,36 @@ def test_csr_generation() -> None:
5661 taxpayer .generate_key ()
5762
5863 csr_file = taxpayer .generate_csr ()
59- csr = csr_file .read ().decode ()
64+ csr = csr_file .read ()
65+ csr_str = csr .decode ()
6066
61- assert csr .splitlines ()[0 ] == "-----BEGIN CERTIFICATE REQUEST-----"
67+ assert csr_str .splitlines ()[0 ] == "-----BEGIN CERTIFICATE REQUEST-----"
6268
63- assert csr .splitlines ()[- 1 ] == "-----END CERTIFICATE REQUEST-----"
69+ assert csr_str .splitlines ()[- 1 ] == "-----END CERTIFICATE REQUEST-----"
6470
65- loaded_csr = crypto . load_certificate_request ( crypto . FILETYPE_PEM , csr )
66- assert isinstance (loaded_csr , crypto . X509Req )
71+ loaded_csr = x509 . load_pem_x509_csr ( csr )
72+ assert isinstance (loaded_csr , x509 . CertificateSigningRequest )
6773
68- expected_components = [
69- (b"O" , b"John Smith" ),
70- (b"CN" , b"djangoafip1489537017" ),
71- (b"serialNumber" , b"CUIT 20329642330" ),
72- ]
73-
74- assert expected_components == loaded_csr .get_subject ().get_components ()
74+ subject = loaded_csr .subject
75+ assert (
76+ subject .get_attributes_for_oid (NameOID .ORGANIZATION_NAME )[0 ].value
77+ == "John Smith"
78+ )
79+ assert (
80+ subject .get_attributes_for_oid (NameOID .COMMON_NAME )[0 ].value
81+ == "djangoafip1489537017"
82+ )
83+ assert (
84+ subject .get_attributes_for_oid (NameOID .SERIAL_NUMBER )[0 ].value
85+ == "CUIT 20329642330"
86+ )
7587
7688
7789def test_certificate_object () -> None :
7890 taxpayer = factories .TaxPayerFactory .build ()
7991 cert = taxpayer .certificate_object
8092
81- assert isinstance (cert , crypto . X509 )
93+ assert isinstance (cert , X509 )
8294
8395
8496def test_null_certificate_object () -> None :
0 commit comments