cfdi-trimbra/source/tests/tests.py

151 lines
4.3 KiB
Python
Raw Normal View History

2021-04-28 23:37:11 -05:00
#!/usr/bin/env python3
import unittest
2021-04-29 22:39:55 -05:00
import warnings
2021-04-28 23:37:11 -05:00
import httpx
URL_API = 'http://127.0.0.1:8000/api/{}'
PATH_CERT = 'certificados/comercio.{}'
2021-04-29 22:39:55 -05:00
CFDI_MINIMO = {
"comprobante": {
"TipoCambio": "1",
"Moneda": "MXN",
"TipoDeComprobante": "I",
"LugarExpedicion": "06850",
"SubTotal": "1000.00",
"Total": "1160.00",
"FormaPago": "03",
"MetodoPago": "PUE",
},
"emisor": {
"Rfc": "EKU9003173C9",
"RegimenFiscal": "601",
},
"receptor": {
"Rfc": "BASM740115RW0",
"UsoCFDI": "G01"
},
"conceptos": [
{
"ClaveProdServ": "60121001",
"Cantidad": "1.0",
"ClaveUnidad": "KGM",
"Descripcion": "Asesoría en desarrollo",
"ValorUnitario": "1000.00",
"Importe": "1000.00",
"impuestos": {
"traslados": [
{
"Base": "1000.00",
"Impuesto": "002",
"TipoFactor": "Tasa",
"TasaOCuota": "0.160000",
"Importe": "160.00"
}
]
}
}
],
"impuestos": {
"TotalImpuestosTrasladados": "160.00",
"traslados": [
{
"Impuesto": "002",
"TipoFactor": "Tasa",
"TasaOCuota": "0.160000",
"Importe": "160.00"
}
]
}
}
def ignore_warnings(test_func):
def do_test(self, *args, **kwargs):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
test_func(self, *args, **kwargs)
return do_test
2021-04-28 23:37:11 -05:00
class TestClients(unittest.TestCase):
def setUp(self):
2021-04-29 22:39:55 -05:00
print(f'In method: {self._testMethodName}')
2021-04-28 23:37:11 -05:00
self.url = URL_API.format('clients/')
def test_unauthorized_without_token(self):
expected = 401
result = httpx.post(self.url)
self.assertEqual(expected, result.status_code)
def test_unauthorized_with_token(self):
expected = 401
result = httpx.post(self.url, headers={'Token': '123'})
self.assertEqual(expected, result.status_code)
2021-04-29 22:39:55 -05:00
@ignore_warnings
2021-04-28 23:37:11 -05:00
def test_01_add_client(self):
expected = 201
headers = {'Token': '12345'}
data = {
'rfc': 'EKU9003173C9',
'password': '12345678a',
}
files = {
'cer': open(PATH_CERT.format('cer'), 'rb'),
'key': open(PATH_CERT.format('key'), 'rb'),
}
result = httpx.post(self.url, headers=headers, data=data, files=files)
self.assertEqual(expected, result.status_code)
def test_02_get_client(self):
expected = 200
headers = {'Token': '12345'}
params = {'rfc': 'EKU9003173C9'}
result = httpx.get(self.url, headers=headers, params=params)
self.assertEqual(expected, result.status_code)
def test_03_delete_client(self):
expected = 200
headers = {'Token': '12345'}
params = {'rfc': 'EKU9003173C9'}
result = httpx.delete(self.url, headers=headers, params=params)
self.assertEqual(expected, result.status_code)
2021-04-29 22:39:55 -05:00
def test_04_delete_client_not_exists(self):
expected = 202
headers = {'Token': '12345'}
params = {'rfc': 'EKU900317321'}
result = httpx.delete(self.url, headers=headers, params=params)
self.assertEqual(expected, result.status_code)
class TestCfdi(unittest.TestCase):
def setUp(self):
print(f'In method: {self._testMethodName}')
self.url = URL_API.format('cfdi/')
# ~ def test_stamp_cfdi_emisor_not_exists(self):
# ~ expected = 202
# ~ msg = 'Emisor no existe'
# ~ headers = {'Token': '12345'}
# ~ cfdi = CFDI_MINIMO.copy()
# ~ cfdi['emisor']['Rfc'] = 'No_exists'
# ~ result = httpx.post(self.url, headers=headers, json=cfdi)
# ~ self.assertEqual(result.text, msg)
# ~ self.assertEqual(expected, result.status_code)
def test_stamp_cfdi(self):
expected = 201
headers = {'Token': '12345'}
result = httpx.post(self.url, headers=headers, json=CFDI_MINIMO)
print(result.text)
self.assertEqual(expected, result.status_code)
2021-04-28 23:37:11 -05:00
if __name__ == '__main__':
unittest.main()