Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

1.4 KB
Raw Blame
(ns metabase-enterprise.content-diagnostics.test-util
  "Shared fixtures for the content-diagnostics suites."
  (:require
   [metabase.test :as mt]
   [toucan2.core :as t2]))

(defn insert-finding!
  "Insert one finding, returning its id. A nil `invalidated-at` leaves the row active."
  [scan-id entity-id invalidated-at]
  (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding
                                   {:scan_id        scan-id
                                    :entity_type    :card
                                    :entity_id      entity-id
                                    :finding_type   :stale
                                    :invalidated_at invalidated-at})))

(defn with-authorized-reader!
  "`clojure.test` fixture giving `:rasta` the data-analyst role, so suites driving the finding-list
  endpoints as `:rasta` clear the audience gate. A superuser would bypass the collection filtering these
  suites assert on. The gate only accepts the role with `:advanced-permissions`, so each test's own
  `with-premium-features` must include it.

  The flag is not inert:
  - `collection/visible-collection-query` grants read on every `transforms`-namespace collection, so a
    test denying an unprivileged user sight of a transform needs its own `:monitoring` actor.
  - It writes the shared `:rasta` row, so the installing namespace must be `^:synchronized`."
  [thunk]
  (mt/with-data-analyst-role! (mt/user->id :rasta)
    (thunk)))