Skip to content
Snippets Groups Projects
Commit 6bd580a7 authored by Cam Saul's avatar Cam Saul
Browse files

move the rest of the user stuff to metabase.test.data.users

parent d1f7c648
No related branches found
No related tags found
No related merge requests found
Showing with 155 additions and 128 deletions
......@@ -5,6 +5,7 @@
[metabase.db :refer :all]
(metabase.models [card :refer [Card]]
[common :as common])
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp]]
[metabase.test-data :refer :all]))
......
......@@ -10,6 +10,7 @@
[dashboard :refer [Dashboard]]
[dashboard-card :refer [DashboardCard]]
[user :refer [User]])
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp]]
[metabase.test-data :refer :all]))
......
......@@ -4,6 +4,7 @@
[korma.core :refer :all]
[metabase.db :refer :all]
[metabase.models.query-execution :refer [QueryExecution]]
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [match-$ expect-eval-actual-first]]
[metabase.test-data :refer :all]))
......
......@@ -5,7 +5,8 @@
[metabase.driver.mongo.test-data :as mongo-test-data]
(metabase.models [database :refer [Database]]
[table :refer [Table]])
[metabase.test.data.datasets :as datasets]
(metabase.test.data [datasets :as datasets]
[users :refer :all])
[metabase.test-data :refer :all]
[metabase.test.util :refer [match-$ random-name expect-eval-actual-first]]))
......
......@@ -4,6 +4,7 @@
(metabase.models [field :refer [Field]]
[field-values :refer [FieldValues]]
[table :refer [Table]])
[metabase.test.data.users :refer :all]
[metabase.test-data :refer :all]
[metabase.test.util :refer [match-$ expect-eval-actual-first]]))
......
......@@ -8,7 +8,8 @@
(metabase.models [field :refer [Field]]
[foreign-key :refer [ForeignKey]]
[table :refer [Table]])
[metabase.test.data.datasets :as datasets, :refer [*dataset* with-dataset-when-testing]]
(metabase.test.data [datasets :as datasets, :refer [*dataset* with-dataset-when-testing]]
[users :refer :all])
[metabase.test-data :refer :all]
[metabase.test-data.data :as data]
[metabase.test.util :refer [match-$ expect-eval-actual-first]]))
......
......@@ -7,7 +7,7 @@
[metabase.test-data :refer :all]
(metabase.models [session :refer [Session]]
[user :refer [User]])
[metabase.test.data.users :refer [create-user]]
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [random-name expect-eval-actual-first]]
[metabase.test-data :refer :all]))
......
......@@ -5,6 +5,7 @@
setting-exists?
test-setting-1
test-setting-2]])
[metabase.test.data.users :refer :all]
[metabase.test-data :refer :all]
[metabase.test.util :refer :all]))
......
......@@ -7,7 +7,7 @@
[metabase.middleware.auth :as auth]
(metabase.models [session :refer [Session]]
[user :refer [User]])
[metabase.test.data.users :refer [create-user]]
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [match-$ random-name expect-eval-actual-first]]
[metabase.test-data :refer :all]))
......
(ns metabase.middleware.auth-test
(:require [expectations :refer :all]
[korma.core :as korma]
[ring.mock.request :as mock]
[metabase.api.common :refer [*current-user-id* *current-user*]]
[metabase.middleware.auth :refer :all]
[metabase.models.session :refer [Session]]
[metabase.test.data.users :refer :all]
[metabase.test-data :refer :all]
[metabase.util :as util]
[ring.mock.request :as mock]))
[metabase.util :as util]))
;; =========================== TEST wrap-sessionid middleware ===========================
......
......@@ -2,6 +2,7 @@
(:require [expectations :refer :all]
[medley.core :as m]
[metabase.models.hydrate :refer :all]
[metabase.test.data.users :refer :all]
[metabase.test-data :refer :all]
[metabase.test.util :refer :all]))
......
......@@ -45,7 +45,8 @@
;; ## Loading / Deleting Test Datasets
(defn get-or-create-database!
"Create DBMS database associated with DATABASE-DEFINITION, create corresponding Metabase `Databases`/`Tables`/`Fields`, and sync the `Database`."
"Create DBMS database associated with DATABASE-DEFINITION, create corresponding Metabase `Databases`/`Tables`/`Fields`, and sync the `Database`.
DATASET-LOADER should be an object that implements `IDatasetLoader`."
[dataset-loader {:keys [database-name], :as ^DatabaseDefinition database-definition}]
(let [engine (engine dataset-loader)]
(or (metabase-instance database-definition engine)
......@@ -93,10 +94,14 @@
db)))))
(defn remove-database!
"Delete Metabase `Database`, `Fields` and `Tables` associated with DATABASE-DEFINITION, then remove the physical database from the associated DBMS."
"Delete Metabase `Database`, `Fields` and `Tables` associated with DATABASE-DEFINITION, then remove the physical database from the associated DBMS.
DATASET-LOADER should be an object that implements `IDatasetLoader`."
[dataset-loader ^DatabaseDefinition database-definition]
;; Delete the Metabase Database and associated objects
(cascade-delete (metabase-instance database-definition (engine dataset-loader)))
;; now delete the DBMS database
(drop-physical-db! dataset-loader database-definition))
;; ## Helper Functions for Writing Tests
(ns metabase.test.data.users
"Code related to creating / managing fake `Users` for testing purposes."
(:require [medley.core :as m]
[metabase.db :refer :all]
(metabase [db :refer :all]
[http-client :as http])
(metabase.models [user :refer [User]])
[metabase.test.util :refer [random-name]]))
[metabase.test.util :refer [random-name]])
(:import com.metabase.corvus.api.ApiException))
(declare fetch-or-create-user)
;; ## User definitions
;; ## Test Users
;;
;; These users have permissions for the Test. They are lazily created as needed.
;; Three test users are defined:
;; * rasta - an admin
;; * crowberto - an admin + superuser
;; * lucky
;; * trashbird
(def ^:private user->info
{:rasta {:email "rasta@metabase.com"
:first "Rasta"
:last "Toucan"
:password "blueberries"
:admin true}
:crowberto {:email "crowberto@metabase.com"
:first "Crowberto"
:last "Corv"
:password "blackjet"
:admin true
:superuser true}
:lucky {:email "lucky@metabase.com"
:first "Lucky"
:last "Pigeon"
:password "almonds"}
:trashbird {:email "trashbird@metabase.com"
:first "Trash"
:last "Bird"
:password "birdseed"
:active false}})
(def ^:private usernames
(set (keys user->info)))
;; ## Public functions for working with Users
(defn create-user
"Create a new `User` with random names + password."
......@@ -15,3 +57,88 @@
:password first-name}]
(->> (merge defaults kwargs)
(m/mapply ins User))))
(defn fetch-user
"Fetch the User object associated with USERNAME.
(fetch-user :rasta) -> {:id 100 :first_name \"Rasta\" ...}"
[username]
{:pre [(contains? usernames username)]}
(m/mapply fetch-or-create-user (user->info username)))
(def user->id
"Memoized fn that returns the ID of User associated with USERNAME.
(user->id :rasta) -> 4"
(memoize
(fn [username]
{:pre [(contains? usernames username)]}
(:id (fetch-user username)))))
(defn user->credentials
"Return a map with `:email` and `:password` for User with USERNAME.
(user->credentials :rasta) -> {:email \"rasta@metabase.com\", :password \"blueberries\"}"
[username]
{:pre [(contains? usernames username)]}
(-> (user->info username)
(select-keys [:email :password])))
(def id->user
"Reverse of `user->id`.
(id->user 4) -> :rasta"
(let [m (delay (zipmap (map user->id usernames) usernames))]
(fn [id]
(@m id))))
(def ^{:arglists '([username-kw])}
user->client
"Returns a `metabase.http-client/client` partially bound with the credentials for User with USERNAME.
In addition, it forces lazy creation of the User if needed.
((user->client) :get 200 \"meta/table\")"
(let [tokens (atom {})
user->token (fn [user]
(or (@tokens user)
(let [token (http/authenticate (user->credentials user))]
(when-not token
(throw (Exception. (format "Authentication failed for %s with credentials %s" user (user->credentials user)))))
(swap! tokens assoc user token)
token)))]
(fn [username]
;; Force lazy creation of User if need be
(user->id username)
(fn call-client [& args]
(try
(apply http/client (user->token username) args)
(catch ApiException e
(if-not (= (.getStatusCode e) 401) (throw e)
;; If we got a 401 unauthenticated clear the tokens cache + recur
(do (reset! tokens {})
(apply call-client args)))))))))
;; ## Implementation
(defn- fetch-or-create-user
"Create User if they don't already exist and return User."
[& {:keys [email first last password admin superuser active]
:or {admin false
superuser false
active true}}]
{:pre [(string? email)
(string? first)
(string? last)
(string? password)
(m/boolean? admin)
(m/boolean? superuser)]}
(or (sel :one User :email email)
(let [user (ins User
:email email
:first_name first
:last_name last
:password password
:is_superuser superuser
:is_active active)]
user)))
......@@ -3,21 +3,16 @@
(:require [cemerick.friend.credentials :as creds]
[korma.core :as k]
[medley.core :as medley]
(metabase [db :refer :all]
[http-client :as http])
(metabase [db :refer :all])
(metabase.models [field :refer [Field]]
[table :refer [Table]]
[user :refer [User]])
[metabase.test.data :refer :all]
(metabase.test.data [data :as data]
[h2 :as h2]))
(:import com.metabase.corvus.api.ApiException))
[h2 :as h2])))
(declare fetch-or-create-user
tables
table-fields
user->info
usernames)
(declare tables
table-fields)
;; # PUBLIC FUNCTIONS / VARS
......@@ -60,73 +55,11 @@
(sel :one Table :id (table->id table-name)))
;; ## Test Users
;;
;; These users have permissions for the Test. They are lazily created as needed.
;; Three test users are defined:
;; * rasta - an admin
;; * crowberto - an admin + superuser
;; * lucky
;; * trashbird
(defn fetch-user
"Fetch the User object associated with USERNAME.
(fetch-user :rasta) -> {:id 100 :first_name \"Rasta\" ...}"
[username]
{:pre [(contains? usernames username)]}
(medley/mapply fetch-or-create-user (user->info username)))
(defn user->credentials
"Return a map with `:email` and `:password` for User with USERNAME.
(user->credentials :rasta) -> {:email \"rasta@metabase.com\", :password \"blueberries\"}"
[username]
{:pre [(contains? usernames username)]}
(-> (user->info username)
(select-keys [:email :password])))
(def user->id
"Memoized fn that returns the ID of User associated with USERNAME.
(user->id :rasta) -> 4"
(memoize
(fn [username]
{:pre [(contains? usernames username)]}
(:id (fetch-user username)))))
(def id->user
"Reverse of `user->id`.
(id->user 4) -> :rasta"
(let [m (delay (zipmap (map user->id usernames) usernames))]
(fn [id]
(@m id))))
(let [tokens (atom {})
user->token (fn [user]
(or (@tokens user)
(let [token (http/authenticate (user->credentials user))]
(when-not token
(throw (Exception. (format "Authentication failed for %s with credentials %s" user (user->credentials user)))))
(swap! tokens assoc user token)
token)))]
(defn user->client
"Returns a `metabase.http-client/client` partially bound with the credentials for User with USERNAME.
In addition, it forces lazy creation of the User if needed.
((user->client) :get 200 \"meta/table\")"
[username]
;; Force lazy creation of User if need be
(user->id username)
(fn call-client [& args]
(try
(apply http/client (user->token username) args)
(catch ApiException e
(if-not (= (.getStatusCode e) 401) (throw e)
;; If we got a 401 unauthenticated clear the tokens cache + recur
(do (reset! tokens {})
(apply call-client args))))))))
;; ## Temporary Tables / Etc.
......@@ -211,50 +144,3 @@
(into {}))))))
;; ## Users
(def ^:private user->info
{:rasta {:email "rasta@metabase.com"
:first "Rasta"
:last "Toucan"
:password "blueberries"
:admin true}
:crowberto {:email "crowberto@metabase.com"
:first "Crowberto"
:last "Corv"
:password "blackjet"
:admin true
:superuser true}
:lucky {:email "lucky@metabase.com"
:first "Lucky"
:last "Pigeon"
:password "almonds"}
:trashbird {:email "trashbird@metabase.com"
:first "Trash"
:last "Bird"
:password "birdseed"
:active false}})
(def ^:private usernames
(set (keys user->info)))
(defn- fetch-or-create-user
"Create User if they don't already exist and return User."
[& {:keys [email first last password admin superuser active]
:or {admin false
superuser false
active true}}]
{:pre [(string? email)
(string? first)
(string? last)
(string? password)
(medley/boolean? admin)
(medley/boolean? superuser)]}
(or (sel :one User :email email)
(let [user (ins User
:email email
:first_name first
:last_name last
:password password
:is_superuser superuser
:is_active active)]
user)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment