penpot/backend/test/backend_tests/delete_object_test.clj
Andrey Antukh 90a06e982b
♻️ Rework the audit-log consumers and the jobs substrate (#12701)
* ♻️ Rework audit-log consumers and jobs substrate

Fold the implementation, follow-up fixes, migrations and tests into one

code commit.

AI-assisted-by: gpt-6-luna

* 📚 Consolidate 2.20.0 upgrade runbooks

Keep the operational upgrade procedure and its supporting runbooks in a

separate documentation commit.

AI-assisted-by: gpt-6-luna

* ♻️ Remove the audit-log flag and derive persistence at boot

Persistence now follows the consumer flags instead of a separate switch.
app.config computes cf/audit-persist? once at boot from :nexus and
:admin-console, and every write site reads that value, so request paths
no longer check flags per event. With no consumer enabled no full rows
are written; telemetry shadows and webhook deliveries keep their own
flags. The frontend wire signal stays :audit-log as a derived value.

Update the 2.20 runbooks, memories and the devenv flags to match, and
migrate the tests that used to toggle :audit-log directly.

AI-assisted-by: hy4-flash-free

* 🐛 Accept error-reporting batches and precompute sink flags

push-audit-events now accepts crash batches with :error-reporting alone, so
the frontend signal from get-environment-data matches a real destination.
The reporters receive unhandled-exception events without storing any row.

All per-event sink checks read booleans computed once at boot: webhooks,
telemetry and error reporting join audit-persist? in app.config. The test
fixture and with-audit-config rebind all four values together, and the
tests that toggled those flags directly were migrated.

Address F2 of the review of the previous commit. F1 stays deferred to a
separate plan.

AI-assisted-by: hy4-flash-free

* 💄 Match the layout cljfmt expects in the flags set

The first comment of the varia set sat on its own line after the opening
delimiter, and cljfmt joins such a comment to the delimiter line, so the
Common Tests lint step failed on common/src/app/common/flags.cljc. Keep
the comment on the opening line, the same shape the login and email sets
already use.

Note in mem:scripts/ci that the backend lint lints ../common/src but does
not format-check it, so common has to be passed to scripts/ci as well.

AI-assisted-by: deepseek-v4-flash
2026-11-20 11:12:00 +01:00

113 lines
5.5 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns backend-tests.delete-object-test
(:require
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.db :as db]
[backend-tests.helpers :as th]
[clojure.test :as t]
[mockery.core :refer [with-mocks]]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(defn- submitted-events
[audit-mock]
(->> (:call-args-list @audit-mock)
(map second)))
(defn- cascade-events
[audit-mock]
(filter #(= "delete-object-with-cascade" (:name %))
(submitted-events audit-mock)))
(t/deftest delete-object-team-cascade-emits-event
;; The cascade marks the team, its projects and their files; the event
;; carries every affected id so a projection can follow without reading
;; production tables.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [profile (th/create-profile* 1 {:is-active true})
team (th/create-team* 91 {:profile-id (:id profile)})
project (th/create-project* 91 {:profile-id (:id profile)
:team-id (:id team)})
file (th/create-file* 91 {:profile-id (:id profile)
:project-id (:id project)})
deleted-at (ct/now)]
(th/run-task! :delete-object {:object :team
:id (:id team)
:deleted-at deleted-at})
(t/is (some? (:deleted-at (th/db-get :team {:id (:id team)}
{::db/remove-deleted false}))))
(t/is (some? (:deleted-at (th/db-get :project {:id (:id project)}
{::db/remove-deleted false}))))
(t/is (some? (:deleted-at (th/db-get :file {:id (:id file)}
{::db/remove-deleted false}))))
(let [events (cascade-events audit-mock)]
(t/is (= 1 (count events)))
(t/is (= "action" (:type (first events))))
(let [props (:props (first events))]
(t/is (= "team" (name (:object props))))
(t/is (= (:id team) (:id props)))
(t/is (= deleted-at (:deleted-at props)))
;; The shape is the contract: only the team ids go flat, the
;; rest stays under `:cascade`.
(t/is (= #{:object :id :deleted-at :team-ids :cascade}
(set (keys props))))
;; Flat team ids for team-keyed consumers, next to the
;; per-entity cascade breakdown.
(t/is (= #{(:id team)} (set (:team-ids props))))
(t/is (= #{(:id team)} (set (get-in props [:cascade :team-ids]))))
(t/is (= #{(:default-project-id team) (:id project)}
(set (get-in props [:cascade :project-ids]))))
(t/is (= #{(:id file)} (set (get-in props [:cascade :file-ids]))))))))))
(t/deftest delete-object-file-cascade-emits-event
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [profile (th/create-profile* 1 {:is-active true})
team (th/create-team* 92 {:profile-id (:id profile)})
project (th/create-project* 92 {:profile-id (:id profile)
:team-id (:id team)})
file (th/create-file* 92 {:profile-id (:id profile)
:project-id (:id project)})
deleted-at (ct/now)]
(th/run-task! :delete-object {:object :file
:id (:id file)
:deleted-at deleted-at})
(let [events (cascade-events audit-mock)]
(t/is (= 1 (count events)))
(t/is (= "action" (:type (first events))))
(let [props (:props (first events))]
(t/is (= "file" (name (:object props))))
(t/is (= (:id file) (:id props)))
(t/is (= [] (get-in props [:cascade :team-ids])))
(t/is (= [] (get-in props [:cascade :project-ids])))
(t/is (= [(:id file)] (get-in props [:cascade :file-ids])))
(t/is (= #{(:id team)} (set (:team-ids props))))))))))
(t/deftest delete-object-missing-row-emits-nothing
;; A cascade over an unknown id marks nothing, so there is nothing to report.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(th/run-task! :delete-object {:object :team
:id (uuid/next)
:deleted-at (ct/now)})
(t/is (empty? (:call-args-list @audit-mock))))))
(t/deftest delete-object-snapshot-emits-nothing
;; Snapshots have no projection table; their cascade stays silent.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(th/run-task! :delete-object {:object :snapshot
:id (uuid/next)
:file-id (uuid/next)
:deleted-at (ct/now)})
(t/is (empty? (:call-args-list @audit-mock))))))