Skip to content

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

5.0 KB
Raw Blame
(ns metabase-enterprise.library.api-test
  (:require
   [clojure.test :refer :all]
   [metabase.collections.models.collection :as collection]
   [metabase.collections.test-utils :refer [without-library]]
   [metabase.test :as mt]
   [toucan2.core :as t2]))

(deftest create-library-endpoint-test
  (mt/with-premium-features #{:library}
    (mt/with-discard-model-updates! [:model/Collection]
      (without-library
       (testing "non-data-analysts cannot create the library"
         (mt/user-http-request :rasta :post 403 "ee/library"))
       (testing "POST /ee/library creates the Library root + Data/Metrics subcollections"
         (let [response (mt/user-http-request :crowberto :post 200 "ee/library")]
           (is (= "Library" (:name response)))
           (is (some? (t2/select-one-pk :model/Collection :type collection/library-data-collection-type)))
           (is (some? (t2/select-one-pk :model/Collection :type collection/library-metrics-collection-type)))))
       (testing "a second call rejects with 400 'Semantic layer already exists'"
         (is (= "Semantic layer already exists"
                (mt/user-http-request :crowberto :post 400 "ee/library"))))))))

(deftest get-library-test
  (mt/with-premium-features #{:library}
    (mt/with-discard-model-updates! [:model/Collection]
      (without-library
       (testing "When there is no library, returns a message but still 200"
         (let [response (mt/user-http-request :crowberto :get 200 "ee/library")]
           (is (= {:data nil} response))))
       (let [_          (collection/create-library-collection!)
             data-id    (t2/select-one-pk :model/Collection :type collection/library-data-collection-type)
             metrics-id (t2/select-one-pk :model/Collection :type collection/library-metrics-collection-type)
             response   (mt/user-http-request :crowberto :get 200 "ee/library")]
         (testing "When library exists, but no content"
           (is (= "Library" (:name response)))
           (is (= ["collection"] (:here response)))
           (is (= [] (:below response))))
         (testing "With content in the library"
           (mt/with-temp [:model/Table _ {:display_name  "Table in Data"
                                          :collection_id data-id
                                          :is_published  true}
                          :model/Card _  {:name          "Card in Metrics"
                                          :collection_id metrics-id
                                          :type          :metric}
                          :model/Card _  {:name          "Other Card in Metrics"
                                          :collection_id metrics-id
                                          :type          :metric}]
             (let [response (mt/user-http-request :crowberto :get 200 "ee/library")]
               (is (= "Library" (:name response)))
               (is (= ["metric" "table"] (:below response)))
               (is (= ["collection"] (:here response))))))
         (testing "Dashboards and their questions show up as dashboards"
           (let [dashboards-id (t2/select-one-pk :model/Collection :type collection/library-dashboards-collection-type)]
             (mt/with-temp [:model/Dashboard dashboard {:collection_id dashboards-id}
                            :model/Card      _         {:collection_id dashboards-id
                                                        :dashboard_id  (:id dashboard)
                                                        :type          :question}]
               (is (= ["dashboard"] (:below (mt/user-http-request :crowberto :get 200 "ee/library"))))))))))))

(deftest disallow-cross-type-collection-move-via-api-test
  (mt/with-premium-features #{:library}
    (mt/with-temp [:model/Collection data-parent    {:name "Data Parent"    :type collection/library-data-collection-type}
                   :model/Collection metrics-parent {:name "Metrics Parent" :type collection/library-metrics-collection-type}
                   :model/Collection data-child     {:name "Data Child"     :type collection/library-data-collection-type
                                                     :location (str "/" (:id data-parent) "/")}
                   :model/Collection metrics-child  {:name "Metrics Child"  :type collection/library-metrics-collection-type
                                                     :location (str "/" (:id metrics-parent) "/")}]
      (testing "Moving a library-data collection into a library-metrics parent returns 400"
        (let [response (mt/user-http-request :crowberto :put 400 (str "collection/" (:id data-child))
                                             {:parent_id (:id metrics-parent)})]
          (is (some? response))))
      (testing "Moving a library-metrics collection into a library-data parent returns 400"
        (let [response (mt/user-http-request :crowberto :put 400 (str "collection/" (:id metrics-child))
                                             {:parent_id (:id data-parent)})]
          (is (some? response)))))))