The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.data-apps.serialization-test
(:require
[clojure.java.io :as io]
[clojure.test :refer :all]
[metabase-enterprise.data-apps.group-access :as group-access]
[metabase-enterprise.data-apps.test-util :as data-apps.tu]
[metabase-enterprise.serialization.core :as serialization]
[metabase-enterprise.serialization.test-util :as ts]
[metabase-enterprise.serialization.v2.extract :as extract]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.models.serialization :as serdes]
[metabase.test :as mt]
[metabase.util.yaml :as yaml]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(def ^:private app-bundle
"The bundle of the apps these tests export and import."
(.getBytes "console.log(1)" "UTF-8"))
(defn- export!
"Export the data apps, with the collection each owns (its serdes descendant) when `with-collections?`."
[dir & {:keys [with-collections?]}]
(serialization/store! (if with-collections?
(extract/extract {:targets (mapv (fn [id] ["DataApp" id])
(t2/select-pks-vec :model/DataApp))
:no-settings true
:no-data-model true})
(serdes/extract-all "DataApp" {}))
(serialization/file-writer dir)))
(defn- import! [dir]
(serialization/load-metabase! (serialization/ingest-yaml dir)))
(defn- bundle-text [app]
(String. ^bytes (t2/select-one-fn :bundle [:model/DataApp :bundle] :id (:id app)) "UTF-8"))
(defn- write-app-files!
"Write a serialized data app with `yaml` fields and the `files` next to it under `data_apps/<dir>/`."
[dump-dir dir yaml-fields files]
(let [app-dir (io/file dump-dir "data_apps" dir)]
(.mkdirs app-dir)
(spit (io/file app-dir "data_app.yaml") (yaml/generate-string yaml-fields))
(doseq [[path content] files
:let [f (io/file app-dir ^String path)]]
(io/make-parents f)
(spit f content))))
(defn- app-yaml [entity-id slug & {:as extra}]
(merge {:serdes/meta [{:model "DataApp" :id entity-id :label slug}]
:entity_id entity-id
:slug slug
:name "Sales"
:path "dist/index.js"}
extra))
(deftest export-writes-the-manifest-and-the-bundle-file-test
(ts/with-random-dump-dir [dump-dir "data-app-export-"]
(mt/with-temp [:model/DataApp app {:name "sales-ops" :display_name "Sales Ops" :description "Pipeline health" :bundle app-bundle
:allowed_hosts ["https://api.example.com"]}]
(export! dump-dir)
(testing "the manifest is a serdes YAML in the app's directory, keyed like a hand-written data_app.yaml"
(is (= {:serdes/meta [{:model "DataApp"}]
:entity_id (:entity_id app)
:slug "sales-ops"
:name "Sales Ops"
:description "Pipeline health"
:version 1
:path "dist/index.js"
:allowed_hosts ["https://api.example.com"]
:collection (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))}
(dissoc (yaml/from-file (io/file dump-dir "data_apps" "sales-ops" "data_app.yaml"))
:created_at))))
(testing "the bundle is a plain file at its path next to the manifest"
(is (= "console.log(1)"
(slurp (io/file dump-dir "data_apps" "sales-ops" "dist" "index.js"))))))))
(deftest round-trip-test
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection]
(ts/with-random-dump-dir [dump-dir "data-app-round-trip-"]
(mt/with-temp [:model/DataApp app {:name "sales-ops" :display_name "Sales Ops" :enabled false :bundle app-bundle
:allowed_hosts ["https://api.example.com"]}]
(let [collection-entity-id (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))]
(export! dump-dir :with-collections? true)
(testing "the app's collection is exported with it, like any collection"
(is (= [collection-entity-id]
(->> (file-seq (io/file dump-dir "collections"))
(filter #(.isFile ^java.io.File %))
(map (comp :entity_id yaml/from-file))))))
(t2/delete! :model/DataApp :id (:id app))
(import! dump-dir)
(let [imported (t2/select-one :model/DataApp :entity_id (:entity_id app))]
(is (=? {:name "sales-ops"
:display_name "Sales Ops"
:bundle_path "dist/index.js"
:bundle_hash (:bundle_hash app)
:allowed_hosts ["https://api.example.com"]}
imported))
(is (= "console.log(1)" (bundle-text imported)))
(testing "new app starts enabled, like one created through the API"
(is (true? (:enabled imported))))
(testing "the import links the collection the manifest names without importing group access"
(is (=? {:entity_id collection-entity-id :namespace :data-apps}
(t2/select-one :model/Collection :id (:resource_collection_id imported))))
(is (empty? (group-access/assigned-groups imported)))))))))))
(deftest import-refuses-a-manifest-that-names-another-collection-test
(mt/with-premium-features #{:data-apps}
(ts/with-random-dump-dir [dump-dir "data-app-switch-"]
(mt/with-temp [:model/Collection {other-entity-id :entity_id} {:namespace :data-apps}
:model/DataApp app {:name "sales-ops"}]
(write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :collection other-entity-id)
{"dist/index.js" "B"})
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed to load" (import! dump-dir)))
(is (= (:resource_collection_id app)
(t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))))))))
(deftest import-refuses-a-manifest-naming-a-collection-the-repository-lacks-test
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection]
(ts/with-random-dump-dir [dump-dir "data-app-no-collection-"]
(write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "x" :collection "nosuchcollection00001")
{"dist/index.js" "B"})
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Collection 'nosuchcollection00001' was not found"
(import! dump-dir)))
(is (not (t2/exists? :model/DataApp :entity_id "pZrj7PDuz3vSWYYi0QFhd")))))))
(deftest import-updates-in-place-and-keeps-local-state-test
(mt/with-premium-features #{:data-apps}
(ts/with-random-dump-dir [dump-dir "data-app-update-"]
(mt/with-temp [:model/DataApp app {:name "sales-ops" :enabled false} :model/PermissionsGroup group {}]
(group-access/add-groups! app [(:id group)])
(write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :name "Renamed"
:path "./dist/index.js")
{"dist/index.js" "console.log(2)"})
(import! dump-dir)
(let [updated (t2/select-one :model/DataApp :id (:id app))]
(is (=? {:display_name "Renamed"
:enabled false
:resource_collection_id (:resource_collection_id app)}
updated))
(is (= [(:id group)] (mapv :id (group-access/assigned-groups updated))))
(is (= "console.log(2)" (bundle-text updated)))
(is (not= (:bundle_hash app) (:bundle_hash updated))))))))
(deftest import-rejects-invalid-apps-test
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection]
(testing "a missing bundle file"
(ts/with-random-dump-dir [dump-dir "data-app-missing-"]
(write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "x") {})
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed to load"
(import! dump-dir)))))
(testing "a manifest field that fails validation"
(ts/with-random-dump-dir [dump-dir "data-app-invalid-"]
(write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "Not A Slug")
{"dist/index.js" "BUNDLE"})
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed to load"
(import! dump-dir)))))
(testing "a bundle path leaving the app's directory is never read"
(ts/with-random-dump-dir [dump-dir "data-app-traversal-"]
(write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "x" :path "../../secret.js") {})
(is (= "Invalid resource file path: ../../secret.js"
(try
(serialization/ingest-one (serialization/ingest-yaml dump-dir)
[{:model "DataApp" :id "pZrj7PDuz3vSWYYi0QFhd"}])
nil
(catch clojure.lang.ExceptionInfo e
(ex-message (ex-cause e))))))))
(is (not (t2/exists? :model/DataApp))))))
(deftest import-clears-an-omitted-description-test
(mt/with-premium-features #{:data-apps}
(ts/with-random-dump-dir [dump-dir "data-app-description-"]
(mt/with-temp [:model/DataApp app {:name "sales-ops" :description "Pipeline health"}]
(write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops") {"dist/index.js" "B"})
(import! dump-dir)
(is (nil? (t2/select-one-fn :description :model/DataApp :id (:id app))))))))
(deftest export-includes-data-apps-test
(mt/with-temp [:model/DataApp {app-entity-id :entity_id} {}]
(is (some #(= [{:model "DataApp" :id app-entity-id}] (map (fn [m] (dissoc m :label)) (:serdes/meta %)))
(into [] (extract/extract {:no-collections true :no-data-model true :no-settings true}))))))
(deftest round-trip-with-resources-test
(testing "an app travels with its collection and what that holds, written under collections/data_apps/"
(mt/with-premium-features #{:data-apps}
(data-apps.tu/with-published-tables! [(mt/id :venues)]
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/Card :model/Action]
(ts/with-random-dump-dir [dump-dir "data-app-resources-"]
(mt/with-temp [:model/DataApp {app-id :id app-entity-id :entity_id collection-id :resource_collection_id}
{:name "sales-ops"}
:model/Card {question-eid :entity_id}
{:name "Venues list"
:collection_id collection-id
:dataset_query (lib/query (mt/metadata-provider)
(lib.metadata/table (mt/metadata-provider) (mt/id :venues)))}
:model/Action {action-id :id action-eid :entity_id}
{:name "Rename venue" :type :implicit :collection_id collection-id}
:model/ImplicitAction _
{:action_id action-id :kind "row/update" :table_id (mt/id :venues)}]
(export! dump-dir :with-collections? true)
(testing "the files sit under the collection's directory of the data-apps namespace"
(doseq [path ["data_app__sales_ops.yaml" "data_app__sales_ops/venues_list.yaml"
"data_app__sales_ops/rename_venue.yaml"]]
(is (.exists (io/file dump-dir "collections" "data_apps" path)) path)))
(t2/delete! :model/DataApp :id app-id)
(is (not (t2/exists? :model/Card :entity_id question-eid)) "deleting the app deletes its collection's cards")
(import! dump-dir)
(let [imported (t2/select-one :model/DataApp :entity_id app-entity-id)
collection-id (:resource_collection_id imported)]
(is (pos-int? collection-id))
(is (= collection-id (t2/select-one-fn :collection_id :model/Card :entity_id question-eid)))
(is (= collection-id (t2/select-one-fn :collection_id :model/Action :entity_id action-eid)))))))))))