# Path: pytrustnfe/certificado.py
# class Certificado(object):
#     def __init__(self, pfx, password):
#         self.pfx = pfx
#         self.password = password
# 
#     def save_pfx(self):
#         pfx_temp = tempfile.mkstemp()[1]
#         arq_temp = open(pfx_temp, "wb")
#         arq_temp.write(self.pfx)
#         arq_temp.close()
#         return pfx_temp
# 
# Path: pytrustnfe/certificado.py
# def save_cert_key(cert, key):
#     cert_temp = tempfile.mkstemp()[1]
#     key_temp = tempfile.mkstemp()[1]
# 
#     arq_temp = open(cert_temp, "w")
#     arq_temp.write(cert)
#     arq_temp.close()
# 
#     arq_temp = open(key_temp, "w")
#     arq_temp.write(key)
#     arq_temp.close()
# 
#     return cert_temp, key_temp
# 
# Path: pytrustnfe/certificado.py
# def extract_cert_and_key_from_pfx(pfx, password):
#     pfx = crypto.load_pkcs12(pfx, password)
#     # PEM formatted private key
#     key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pfx.get_privatekey())
#     # PEM formatted certificate
#     cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pfx.get_certificate())
#     return cert.decode(), key.decode()


import unittest
import os
import os.path
from pytrustnfe.certificado import Certificado
from pytrustnfe.certificado import save_cert_key
from pytrustnfe.certificado import extract_cert_and_key_from_pfx
CERTIFICADO = (
    "-----BEGIN CERTIFICATE-----\n"
    "MIICMTCCAZqgAwIBAgIQfYOsIEVuAJ1FwwcTrY0t1DANBgkqhkiG9w0BAQUFADBX\n"
    "MVUwUwYDVQQDHkwAewA1ADkARgAxAEUANAA2ADEALQBEAEQARQA1AC0ANABEADIA\n"
    "RgAtAEEAMAAxAEEALQA4ADMAMwAyADIAQQA5AEUAQgA4ADMAOAB9MB4XDTE1MDYx\n"
    "NTA1NDc1N1oXDTE2MDYxNDExNDc1N1owVzFVMFMGA1UEAx5MAHsANQA5AEYAMQBF\n"
    "ADQANgAxAC0ARABEAEUANQAtADQARAAyAEYALQBBADAAMQBBAC0AOAAzADMAMgAy\n"
    "AEEAOQBFAEIAOAAzADgAfTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAk41G\n"
    "nqXXLaiOC/y0/cA4tbS+NZCqI+x4EsztgDFvPPlHstiVYcLRkni4i93gK9zoC6g0\n"
    "mh66HMVzAfE8vRNwW5b7m6nWS1SiHBon7/Mqsw4MIq3SC+J/fTbKpqwyfAuH2YZl\n"
    "AiQuQc85fyllAMLh2WrA7JgOLR/5tF3kLtpbHdECAwEAATANBgkqhkiG9w0BAQUF\n"
    "AAOBgQArdh+RyT6VxKGsXk1zhHsgwXfToe6GpTF4W8PHI1+T0WIsNForDhvst6nm\n"
    "QtgAhuZM9rxpOJuNKc+pM29EixpAiZZiRMCSWEItNyEVdUIi+YnKBcAHd88TwO86\n"
    "d126MWQ2O8cu5W1VoDp7hYBYKOnLbYi11/StO+0rzK+oPYAvIw==\n"
    "-----END CERTIFICATE-----\n"
)


class test_assinatura(unittest.TestCase):

    caminho = os.path.dirname(__file__)

    def test_preparar_pfx(self):
        dir_pfx = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()
        cert, key = extract_cert_and_key_from_pfx(dir_pfx, "123456")
        self.assertEqual(key, CHAVE, "Chave gerada inválida")
        self.assertEqual(cert, CERTIFICADO, "Certificado inválido")

    def test_save_pfx(self):
        pfx_source = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()

# Path: pytrustnfe/certificado.py
# class Certificado(object):
#     def __init__(self, pfx, password):
#         self.pfx = pfx
#         self.password = password
# 
#     def save_pfx(self):
#         pfx_temp = tempfile.mkstemp()[1]
#         arq_temp = open(pfx_temp, "wb")
#         arq_temp.write(self.pfx)
#         arq_temp.close()
#         return pfx_temp
# 
# Path: pytrustnfe/certificado.py
# def save_cert_key(cert, key):
#     cert_temp = tempfile.mkstemp()[1]
#     key_temp = tempfile.mkstemp()[1]
# 
#     arq_temp = open(cert_temp, "w")
#     arq_temp.write(cert)
#     arq_temp.close()
# 
#     arq_temp = open(key_temp, "w")
#     arq_temp.write(key)
#     arq_temp.close()
# 
#     return cert_temp, key_temp
# 
# Path: pytrustnfe/certificado.py
# def extract_cert_and_key_from_pfx(pfx, password):
#     pfx = crypto.load_pkcs12(pfx, password)
#     # PEM formatted private key
#     key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pfx.get_privatekey())
#     # PEM formatted certificate
#     cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pfx.get_certificate())
#     return cert.decode(), key.decode()


# Path: tests/test_certificado.py
import unittest
import os
import os.path
from pytrustnfe.certificado import Certificado
from pytrustnfe.certificado import save_cert_key
from pytrustnfe.certificado import extract_cert_and_key_from_pfx

CERTIFICADO = (
    "-----BEGIN CERTIFICATE-----\n"
    "MIICMTCCAZqgAwIBAgIQfYOsIEVuAJ1FwwcTrY0t1DANBgkqhkiG9w0BAQUFADBX\n"
    "MVUwUwYDVQQDHkwAewA1ADkARgAxAEUANAA2ADEALQBEAEQARQA1AC0ANABEADIA\n"
    "RgAtAEEAMAAxAEEALQA4ADMAMwAyADIAQQA5AEUAQgA4ADMAOAB9MB4XDTE1MDYx\n"
    "NTA1NDc1N1oXDTE2MDYxNDExNDc1N1owVzFVMFMGA1UEAx5MAHsANQA5AEYAMQBF\n"
    "ADQANgAxAC0ARABEAEUANQAtADQARAAyAEYALQBBADAAMQBBAC0AOAAzADMAMgAy\n"
    "AEEAOQBFAEIAOAAzADgAfTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAk41G\n"
    "nqXXLaiOC/y0/cA4tbS+NZCqI+x4EsztgDFvPPlHstiVYcLRkni4i93gK9zoC6g0\n"
    "mh66HMVzAfE8vRNwW5b7m6nWS1SiHBon7/Mqsw4MIq3SC+J/fTbKpqwyfAuH2YZl\n"
    "AiQuQc85fyllAMLh2WrA7JgOLR/5tF3kLtpbHdECAwEAATANBgkqhkiG9w0BAQUF\n"
    "AAOBgQArdh+RyT6VxKGsXk1zhHsgwXfToe6GpTF4W8PHI1+T0WIsNForDhvst6nm\n"
    "QtgAhuZM9rxpOJuNKc+pM29EixpAiZZiRMCSWEItNyEVdUIi+YnKBcAHd88TwO86\n"
    "d126MWQ2O8cu5W1VoDp7hYBYKOnLbYi11/StO+0rzK+oPYAvIw==\n"
    "-----END CERTIFICATE-----\n"
)


class test_assinatura(unittest.TestCase):

    caminho = os.path.dirname(__file__)

    def test_preparar_pfx(self):
        dir_pfx = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()
        cert, key = extract_cert_and_key_from_pfx(dir_pfx, "123456")
        self.assertEqual(key, CHAVE, "Chave gerada inválida")
        self.assertEqual(cert, CERTIFICADO, "Certificado inválido")

    def test_save_pfx(self):
        pfx_source = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()

        pfx = Certificado(pfx_source, "123")