forked from TadaSoftware/PyNFe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_certificadoA1.py
82 lines (62 loc) · 2.78 KB
/
test_certificadoA1.py
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
#!/usr/bin/env python
# *-* encoding: utf8 *-*
import unittest
from pynfe.entidades.certificado import CertificadoA1
class CertificadoTestCase(unittest.TestCase):
def setUp(self):
self.certificado_correto = "./tests/certificado.pfx"
self.certificado_incorreto = "./tests/arquivo_erro.pfx"
self.senha_correto = bytes('123456', encoding='utf-8')
self.senha_incorreto = bytes('654321', encoding='utf-8')
def test_assinatura_com_caminho(self):
self.a1 = CertificadoA1(self.certificado_correto)
cert = self.a1.separar_arquivo(
senha=self.senha_correto, caminho=self.certificado_correto
)
self.assertTrue(cert[0].startswith('/tmp/'))
self.assertTrue(cert[1].startswith('/tmp/'))
self.a1.excluir()
def test_exception_certificado_filenotfounderror(self):
with self.assertRaises(Exception) as context:
self.a1 = CertificadoA1(self.certificado_incorreto)
self.a1.separar_arquivo(
senha=self.senha_incorreto,
caminho=self.certificado_incorreto
)
self.assertEqual(str(context.exception), "Falha ao abrir arquivo do certificado digital A1. Verifique local e permissoes do arquivo.")
def test_exception_certificado_senha_errada(self):
with self.assertRaises(Exception) as context:
self.a1 = CertificadoA1(self.certificado_correto)
self.a1.separar_arquivo(
senha=self.senha_incorreto,
caminho=self.certificado_correto
)
self.assertEqual(str(context.exception), "Falha ao carregar certificado digital A1. Verifique a senha do certificado.")
def test_assinatura_sem_caminho(self):
self.a1 = CertificadoA1(self.certificado_correto)
cert = self.a1.separar_arquivo(
senha=self.senha_correto,
caminho=None
)
texto_inicial_esperado = '-----BEGIN PRIVATE KEY-----'
texto_inicial_gerado = cert[0].decode('utf-8')
self.assertTrue(texto_inicial_gerado.startswith(texto_inicial_esperado))
self.a1.excluir()
def test_verificar_se_arquivos_temporarios_existem(self):
self.a1 = CertificadoA1(self.certificado_correto)
self.a1.separar_arquivo(
senha=self.senha_correto,
caminho=self.certificado_correto
)
self.assertTrue(len(self.a1.arquivos_temp) > 0)
self.a1.excluir()
def test_excluir_arquivos_temporarios(self):
self.a1 = CertificadoA1(self.certificado_correto)
self.a1.separar_arquivo(
senha=self.senha_correto,
caminho=self.certificado_correto
)
self.a1.excluir()
self.assertTrue(len(self.a1.arquivos_temp) == 0)
if __name__ == '__main__':
unittest.main()