Repository navigation
Conversation
Apply the same engagement permission checks to the template delete preview that the other questionnaire pages already use, and add a regression test. The delete itself is unchanged.
Maffooch
left a comment
There was a problem hiding this comment.
Adversarial review
Tier: full (backend authorization change to a server-rendered view). Model: Opus. Reviewed head 39553e83f5.
CI read: all checks green on the head commit: ruff, Migration Graph Check, both Rest Framework Unit Tests jobs (9247 tests run, OK), every User Interface Tests group, k8s, performance, docker builds. No migration, no API shape change.
Regression test: checked statically only; no Docker daemon was available to this pass, so the test was not run against dev. On dev the view runs NestedObjects.collect([survey]) unconditionally when DELETE_PREVIEW is on, and TextAnswer.__str__ returns the answer text, so HIDDEN_ANSWER is in the page and test_preview_hides_answers_of_engagements_the_user_cannot_view fails there. With the fix it passes (CI). test_preview_lists_answers_when_the_user_can_view_every_engagement runs with the same settings and proves the hidden case is not passing only because the preview is off.
What I drove: nothing in a browser. This is a classic Django template page and no stack was available. Instead I read the view, the template (rels|length > 1 -> unordered_list), the URL permission (delete_questionnaire -> config dojo.delete_engagement_survey), the OSS engagement.get_authorized_engagements filter (product authorized_users, with superuser and staff bypasses), and how the other questionnaire views check engagements (view_empty_survey and view_questionnaire check view, the delete views check edit).
Break attempts
- Partial visibility: a user who can view one of two engagements. The whole preview is hidden, the user's own answers included, and the new message renders in its place. That is all-or-nothing by design, and the test asserts it. The delete still cascades as before.
- Answers with no engagement (general or unassigned responses,
engagement=None): theengagement__isnull=Falsefilter leaves these out of the check, so their answers still show. That matchesview_empty_survey, which gates engagement-less responses only on the questionnaire config permission. No leak beyond what that page already shows. - Superuser, staff and anonymous: the OSS filter returns every engagement for superusers and staff, so the preview shows (the test covers superuser). An anonymous user gets
none(), so the preview hides, and the URL permission refuses anonymous users first anyway. A plugin that overrides the same registry key goes through the same call, so the check follows whatever RBAC is registered. - Other delete-preview surfaces for the same data: no
/api/v2viewset exposes a delete preview forEngagement_Survey(delete_previewlives only inapi_v2/mixins.pyand is not mixed into a questionnaire viewset).delete_engagement_survey,delete_empty_questionnaireanddelete_general_questionnairerender answers only after an engagement check, or render none. No site was left unpatched. - Scale: the check adds one
EXISTSover the template's answered surveys with aNOT IN (authorized engagements)subquery. The outer set is one template's responses (small), and the subquery scales with the number of engagements, not findings. That is acceptable on an infrequent admin delete page. The existingNestedObjectswalk is still the expensive part when the preview shows. - The tests themselves: they go through the real URL, login and permission middleware, not a mock. They use the real cascade on POST. The negative test checks only that both answers are absent, not that the page says why (R1).
Screenshots: none. Server-rendered page, no stack in this pass.
Findings
- R1
nitthis-prunittests/test_questionnaire_template_delete_preview.py:59: the hidden-preview test asserts only that both answers are absent, so it would also pass if the page dropped the relationship list for any other reason. Also assert that the new explanation ("The preview is hidden because some answers to this questionnaire belong to engagements you cannot view.") is in the page, which pins the user-visible behaviour this PR adds.
Recommendation
changes required: 1 open this-pr finding (0 blocker, 0 suggestion, 1 nit). The fix is correct, minimal, and consistent with the engagement checks on the other questionnaire views.
Checked only statically: whether the regression test fails on dev (reasoned from the code, not run), and the rendered page (no browser pass).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Makes the questionnaire template delete page use the same engagement permission checks as the other questionnaire pages. If a user cannot view some of the engagements tied to the template, the page now hides the relationship preview. Deleting the template still works the same way.
Adds a regression test.