Skip to content

Commit

Permalink
[#3889] Log outgoing requests, not log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Apr 4, 2024
1 parent 6a71420 commit a8ad94f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 51 deletions.
20 changes: 0 additions & 20 deletions src/openforms/logging/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from typing import Any

from django.contrib import admin
from django.http import Http404
from django.http.request import HttpRequest
from django.http.response import HttpResponse

from import_export import resources
from import_export.admin import ExportActionModelAdmin
Expand All @@ -12,8 +7,6 @@

from openforms.logging.models import AVGTimelineLogProxy, TimelineLogProxy

from .logevent import timelinelog_details_view_admin


class TimelineLogProxyResource(resources.ModelResource):
user = Field(attribute="user")
Expand Down Expand Up @@ -75,19 +68,6 @@ def has_change_permission(self, request, obj=None):
def has_delete_permission(self, request, obj=None):
return False

def change_view(
self,
request: HttpRequest,
object_id: str,
form_url: str = "",
extra_context: dict[str, Any] | None = None,
) -> HttpResponse:
timelinelog = self.get_object(request, object_id)
if timelinelog is None:
raise Http404(f"No {self.model._meta.object_name} matches the given query.")
timelinelog_details_view_admin(timelinelog, request.user)
return super().change_view(request, object_id, form_url, extra_context)


@admin.register(AVGTimelineLogProxy)
class AVGTimelineLogProxyAdmin(TimelineLogProxyAdmin):
Expand Down
10 changes: 7 additions & 3 deletions src/openforms/logging/logevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from openforms.typing import JSONObject

if TYPE_CHECKING:
from log_outgoing_requests.models import OutgoingRequestsLog

from openforms.payments.models import SubmissionPayment
from openforms.submissions.models import Submission, SubmissionStep
from stuf.models import StufService
Expand Down Expand Up @@ -71,10 +73,12 @@ def _create_log(
return log_entry


def timelinelog_details_view_admin(timelinelog_proxy: TimelineLogProxy, user: User):
def outgoing_request_log_details_view_admin(
outgoing_request_log: OutgoingRequestsLog, user: User
) -> None:
_create_log(
timelinelog_proxy,
"timelinelog_details_view_admin",
outgoing_request_log,
"outgoing_request_log_details_view_admin",
tags=[TimelineLogTags.AVG],
user=user,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n %}
{% blocktrans trimmed with lead=log.fmt_lead user=log.fmt_user timelinelog=log.pk %}
{{ lead }}: User {{ user }} viewed timelinelog {{ timelinelog }} in the admin
{{ lead }}: User {{ user }} viewed outgoing request log {{ timelinelog }} in the admin
{% endblocktrans %}
28 changes: 1 addition & 27 deletions src/openforms/logging/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
from django_webtest import WebTest
from maykin_2fa.test import disable_admin_mfa

from openforms.accounts.tests.factories import (
StaffUserFactory,
SuperUserFactory,
UserFactory,
)
from openforms.accounts.tests.factories import StaffUserFactory, SuperUserFactory
from openforms.logging import logevent
from openforms.logging.models import TimelineLogProxy
from openforms.logging.tests.factories import TimelineLogProxyFactory
Expand Down Expand Up @@ -102,28 +98,6 @@ def test_deleted_submission_doesnt_crash_logs(self):
self.assertEqual(200, response.status_code)


@disable_admin_mfa()
class TimelineLogAdminTests(WebTest):
def test_viewing_timelinelog_details_in_admin_creates_log(self):
user = UserFactory.create(is_superuser=True, is_staff=True)
timeline_log = TimelineLogProxyFactory.create(user=user)

self.app.get(
reverse(
"admin:logging_timelinelogproxy_change",
kwargs={"object_id": timeline_log.id},
),
user=user,
)

self.assertEqual(
TimelineLogProxy.objects.filter(
template="logging/events/timelinelog_details_view_admin.txt"
).count(),
1,
)


class TimelineLogExportsTest(TestCase):
def test_bare_timelinelog_export(self):
user = StaffUserFactory.create()
Expand Down
28 changes: 28 additions & 0 deletions src/openforms/utils/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from typing import Any

from django.conf import settings
from django.contrib import admin
from django.db import models
from django.http import Http404
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from cookie_consent.admin import LogItemAdmin as CookieLogAdmin
from cookie_consent.models import LogItem as CookieLog
from cspreports.admin import CSPReportAdmin
from cspreports.models import CSPReport
from log_outgoing_requests.admin import OutgoingRequestsLogAdmin
from log_outgoing_requests.models import OutgoingRequestsLog

from openforms.logging.logevent import outgoing_request_log_details_view_admin


class SubmitActions(models.TextChoices):
Expand Down Expand Up @@ -49,3 +58,22 @@ def json_as_html(self, instance):

admin.site.unregister(CSPReport)
admin.site.register(CSPReport, CSPReportOverrideAdmin)


class OutgoingRequestsLogOverrideAdmin(OutgoingRequestsLogAdmin):
def change_view(
self,
request: HttpRequest,
object_id: str,
form_url: str = "",
extra_context: dict[str, Any] | None = None,
) -> HttpResponse:
outgoing_request_log = self.get_object(request, object_id)
if outgoing_request_log is None:
raise Http404(f"No {self.model._meta.object_name} matches the given query.")

Check warning on line 73 in src/openforms/utils/admin.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/utils/admin.py#L73

Added line #L73 was not covered by tests
outgoing_request_log_details_view_admin(outgoing_request_log, request.user)
return super().change_view(request, object_id, form_url, extra_context)


admin.site.unregister(OutgoingRequestsLog)
admin.site.register(OutgoingRequestsLog, OutgoingRequestsLogOverrideAdmin)
33 changes: 33 additions & 0 deletions src/openforms/utils/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.urls import reverse
from django.utils import timezone

from django_webtest import WebTest
from log_outgoing_requests.models import OutgoingRequestsLog
from maykin_2fa.test import disable_admin_mfa

from openforms.accounts.tests.factories import UserFactory
from openforms.logging.models import TimelineLogProxy


@disable_admin_mfa()
class OutgoingRequestLogAdminTests(WebTest):
def test_viewing_outgoing_request_log_details_in_admin_creates_log(self):
user = UserFactory.create(is_superuser=True, is_staff=True)
outgoing_request_log = OutgoingRequestsLog.objects.create(
url="https://example.com", timestamp=timezone.now()
)

self.app.get(
reverse(
"admin:log_outgoing_requests_outgoingrequestslog_change",
kwargs={"object_id": outgoing_request_log.id},
),
user=user,
)

self.assertEqual(
TimelineLogProxy.objects.filter(
template="logging/events/outgoing_request_log_details_view_admin.txt"
).count(),
1,
)

0 comments on commit a8ad94f

Please sign in to comment.