From 126015b2c140c01a216c2cc90e1aad025a0afc8d Mon Sep 17 00:00:00 2001 From: Edward Oliveira Date: Tue, 13 Oct 2026 16:39:01 -0300 Subject: [PATCH] =?UTF-8?q?Corrige=20redirecionamento=20do=20PDF=20de=20ac?= =?UTF-8?q?ess=C3=B3rios=20de=20documento=20administrativo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- sapl/protocoloadm/tests/test_docadm_acesso.py | 43 +++++++++++++++++-- sapl/protocoloadm/views.py | 15 ++++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/sapl/protocoloadm/tests/test_docadm_acesso.py b/sapl/protocoloadm/tests/test_docadm_acesso.py index 602da119c..c88d7a7d1 100644 --- a/sapl/protocoloadm/tests/test_docadm_acesso.py +++ b/sapl/protocoloadm/tests/test_docadm_acesso.py @@ -1,3 +1,6 @@ +from io import BytesIO + +from PyPDF4 import PdfFileWriter from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType @@ -8,7 +11,8 @@ from model_bakery import baker import pytest from sapl.base.models import AppConfig -from sapl.protocoloadm.models import DocumentoAdministrativo +from sapl.protocoloadm.models import (DocumentoAcessorioAdministrativo, + DocumentoAdministrativo) @pytest.fixture(autouse=True) @@ -84,23 +88,54 @@ def test_doc_texto_integral_acesso(client, media_tmp, comportamento, assert response.status_code == (200 if pode_ver else 404) +def _pdf(): + writer = PdfFileWriter() + writer.addBlankPage(72, 72) + data = BytesIO() + writer.write(data) + return data.getvalue() + + @pytest.mark.django_db(transaction=False) @pytest.mark.parametrize('comportamento,restrito,perfil,pode_ver', ACESSO) -def test_pdf_docacessorios_acesso(client, media_tmp, comportamento, - restrito, perfil, pode_ver): +def test_pdf_docacessorios_acesso(client, media_tmp, monkeypatch, + comportamento, restrito, perfil, pode_ver): + # create_pdf_docacessorios usa o MEDIA_ROOT importado de sapl.settings + monkeypatch.setattr('sapl.protocoloadm.views.MEDIA_ROOT', str(media_tmp)) _config(comportamento) documento = baker.make(DocumentoAdministrativo, restrito=restrito) + acessorio = baker.make(DocumentoAcessorioAdministrativo, + documento=documento) + acessorio.arquivo = SimpleUploadedFile( + 'acessorio.pdf', _pdf(), content_type='application/pdf') + acessorio.save() client = _client(client, perfil) response = client.get(reverse('sapl.protocoloadm:merge_docacessorios', kwargs={'pk': documento.pk})) if pode_ver: - assert response.status_code != 404 + assert response.status_code == 200 + assert response['Content-Type'] == 'application/pdf' + assert response.content.startswith(b'%PDF') else: assert response.status_code == 404 +@pytest.mark.django_db(transaction=False) +def test_pdf_docacessorios_sem_acessorios_volta_para_lista(client): + _config('O') + documento = baker.make(DocumentoAdministrativo, restrito=False) + + response = client.get(reverse('sapl.protocoloadm:merge_docacessorios', + kwargs={'pk': documento.pk})) + + assert response.status_code == 302 + assert response['Location'] == reverse( + 'sapl.protocoloadm:documentoacessorioadministrativo_list', + kwargs={'pk': documento.pk}) + + @pytest.mark.django_db(transaction=False) @pytest.mark.parametrize('url_name', ['doc_texto_integral', 'merge_docacessorios']) diff --git a/sapl/protocoloadm/views.py b/sapl/protocoloadm/views.py index 25c465438..10e6716b1 100755 --- a/sapl/protocoloadm/views.py +++ b/sapl/protocoloadm/views.py @@ -133,21 +133,24 @@ def get_pdf_docacessorios(request, pk): logger.error("user= {}.Não há arquivos cadastrados".format(username)) msg = _('Não há arquivos cadastrados nesses documentos acessórios.') messages.add_message(request, messages.ERROR, msg) - return redirect(reverse('sapl.materia:documentoacessorio_list', - kwargs={'pk': pk})) + return redirect(reverse( + 'sapl.protocoloadm:documentoacessorioadministrativo_list', + kwargs={'pk': pk})) except Exception as e: logger.error("user= {}.Um erro inesperado ocorreu na criação do pdf de documentos acessorios: {}" .format(username, str(e))) msg = _('Um erro inesperado ocorreu. Entre em contato com o suporte do SAPL.') messages.add_message(request, messages.ERROR, msg) - return redirect(reverse('sapl.materia:documentoacessorio_list', - kwargs={'pk': pk})) + return redirect(reverse( + 'sapl.protocoloadm:documentoacessorioadministrativo_list', + kwargs={'pk': pk})) if not data: msg = _('Não há nenhum documento acessório PDF cadastrado.') messages.add_message(request, messages.ERROR, msg) - return redirect(reverse('sapl.materia:documentoacessorio_list', - kwargs={'pk': pk})) + return redirect(reverse( + 'sapl.protocoloadm:documentoacessorioadministrativo_list', + kwargs={'pk': pk})) response = HttpResponse(data, content_type='application/pdf') response['Content-Disposition'] = ('attachment; filename="%s"'