Skip to content

Commit

Permalink
Merge pull request #13 from guivaloz:guivaloz/upload-test
Browse files Browse the repository at this point in the history
Funciona test de upload
  • Loading branch information
guivaloz authored May 13, 2024
2 parents a44b61f + 7f2a23f commit f60eb40
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*.py[cod]
*.log
*.sql
*.odt
*.pdf

*.egg-info/
__pycache__/
Expand Down
5 changes: 3 additions & 2 deletions carina/v4/exh_exhortos_archivos/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ async def detalle_exh_exhorto_archivo(
async def upload_exh_exhorto_archivo(
current_user: Annotated[UsuarioInDB, Depends(get_current_active_user)],
database: Annotated[Session, Depends(get_db)],
file: UploadFile = Depends(ExhExhortoArchivoFileIn),
exhortoOrigenId: str,
archivo: UploadFile,
):
"""Entregar un archivo"""
if current_user.permissions.get("EXH EXHORTOS ARCHIVOS", 0) < Permiso.CREAR:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
return {"message": file.filename}
return {"message": archivo.filename}
1 change: 0 additions & 1 deletion carina/v4/exh_exhortos_archivos/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ExhExhortoArchivoFileIn(BaseModel):
"""Exquema para recibir archivos Content-Disposition, form-data, file"""

exhortoOrigenId: str | None = None
archivo: bytes | None = None


class ExhExhortoArchivoFileDataArchivoOut(BaseModel):
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sqlalchemy = "^2.0.25"
sqlalchemy-utils = "^0.41.1"
unidecode = "^1.3.8"
uvicorn = "^0.25.0"
python-multipart = "^0.0.9"


[tool.poetry.group.dev.dependencies]
Expand All @@ -34,6 +35,7 @@ pylint = "^3.0.3"
pylint-sqlalchemy = "^0.3.0"
pytest = "^7.4.4"
pre-commit = "^3.6.0"
faker = "^25.2.0"

[build-system]
requires = ["poetry-core"]
Expand Down
64 changes: 55 additions & 9 deletions tests/test_exh_exhortos.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import uuid
import requests
from faker import Faker
from tests.load_env import config


Expand Down Expand Up @@ -34,8 +35,38 @@ def test_get_exh_exhorto_by_id(self):

def test_post_exh_exhorto(self):
"""Test POST method for exh_exhorto"""

# Generar exhortoOrigenId
random_uuid = uuid.uuid4()
exhorto_origen_id = str(random_uuid)

# Inicializar el generardo de nombres aleatorios
faker = Faker(locale="es_MX")

# Generar el nombre del juez exhortante
nombre_juez_exhortante = faker.name()

# Generar la parte Actor(1) con nombres aleatorios
if faker.random_element(elements=("M", "F")) == "F":
nombre_actor = faker.first_name_female()
genero_actor = "F"
else:
nombre_actor = faker.first_name_male()
genero_actor = "M"
apellido_paterno_actor = faker.last_name()
apellido_materno_actor = faker.last_name()

# Generar la parte Demandado(2) con nombres aleatorios
if faker.random_element(elements=("M", "F")) == "F":
nombre_demandado = faker.first_name_female()
genero_demandado = "F"
else:
nombre_demandado = faker.first_name_male()
genero_demandado = "M"
apellido_paterno_demandado = faker.last_name()
apellido_materno_demandado = faker.last_name()

# Mandar Exhorto
datos_nuevo_exhorto = {
"exhortoOrigenId": exhorto_origen_id,
"municipioDestinoId": 30,
Expand All @@ -47,22 +78,22 @@ def test_post_exh_exhorto(self):
"numeroExpedienteOrigen": "123/2024",
"numeroOficioOrigen": "3001/2024",
"tipoJuicioAsuntoDelitos": "DIVORCIO",
"juezExhortante": "PEDRO INFANTE",
"juezExhortante": nombre_juez_exhortante,
"partes": [
{
"nombre": "MARIA",
"apellidoPaterno": "FELIX",
"apellidoMaterno": "ALCALA",
"genero": "F",
"nombre": nombre_actor,
"apellidoPaterno": apellido_paterno_actor,
"apellidoMaterno": apellido_materno_actor,
"genero": genero_actor,
"esPersonaMoral": False,
"tipoParte": 1,
"tipoParteNombre": "",
},
{
"nombre": "EULALIO",
"apellidoPaterno": "GONZALEZ",
"apellidoMaterno": "PIPORRO",
"genero": "M",
"nombre": nombre_demandado,
"apellidoPaterno": apellido_paterno_demandado,
"apellidoMaterno": apellido_materno_demandado,
"genero": genero_demandado,
"esPersonaMoral": False,
"tipoParte": 2,
"tipoParteNombre": "",
Expand All @@ -86,6 +117,21 @@ def test_post_exh_exhorto(self):
self.assertEqual(data["success"], True)
self.assertEqual(data["data"]["exhortoOrigenId"], exhorto_origen_id)

# Mandar un archivo multipart/form-data
archivo_prueba_nombre = "prueba.pdf"
with open(f"tests/{archivo_prueba_nombre}", "rb") as archivo_prueba:
response = requests.post(
f"{config['api_base_url']}/exh_exhortos_archivos/upload",
headers={"X-Api-Key": config["api_key"]},
timeout=config["timeout"],
params={"exhortoOrigenId": exhorto_origen_id},
files={"archivo": (archivo_prueba_nombre, archivo_prueba, "application/pdf")},
)
self.assertEqual(response.status_code, 200)
data = response.json()
# self.assertEqual(data["success"], True)
self.assertEqual(data["message"], archivo_prueba_nombre)


if __name__ == "__main__":
unittest.main()

0 comments on commit f60eb40

Please sign in to comment.