diff --git a/sapl/parlamentares/tests/test_mesadiretora.py b/sapl/parlamentares/tests/test_mesadiretora.py index eb93a385d..a1cb445eb 100644 --- a/sapl/parlamentares/tests/test_mesadiretora.py +++ b/sapl/parlamentares/tests/test_mesadiretora.py @@ -1078,3 +1078,10 @@ def test_migration_0048_reversao_com_mesas_da_erro_claro(): baker.make(MesaDiretora) with pytest.raises(IrreversibleError, match='Restaure o backup'): migration.impede_reversao_com_mesas(apps, None) + + +@pytest.mark.django_db(transaction=False) +def test_composicaomesa_create_mesa_inexistente_retorna_404(admin_client): + response = admin_client.get(reverse( + 'sapl.parlamentares:composicaomesa_create', kwargs={'pk': 999999})) + assert response.status_code == 404 diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index ce71b278e..a29c99684 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -11,7 +11,7 @@ from django.db.models import F from django.db.models.aggregates import Count from django.http import Http404, JsonResponse from django.http.response import HttpResponseRedirect -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render from django.templatetags.static import static from django.urls import reverse, reverse_lazy from django.utils import timezone @@ -1102,7 +1102,8 @@ class ComposicaoMesaCrud(MasterDetailCrud): def get_initial(self): initial = super().get_initial() - initial['mesa_diretora'] = MesaDiretora.objects.get(pk=self.kwargs['pk']) + initial['mesa_diretora'] = get_object_or_404( + MesaDiretora, pk=self.kwargs['pk']) return initial