2016-11-20 20:04:52 +01:00
|
|
|
(ns uxbox.tests.helpers
|
2019-11-18 11:52:57 +01:00
|
|
|
(:require
|
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[buddy.hashers :as hashers]
|
|
|
|
[promesa.core :as p]
|
|
|
|
[cuerdas.core :as str]
|
|
|
|
[mount.core :as mount]
|
|
|
|
[datoteka.storages :as st]
|
2019-12-01 16:48:41 +01:00
|
|
|
[uxbox.services.mutations.profiles :as profiles]
|
|
|
|
[uxbox.services.mutations.projects :as projects]
|
|
|
|
[uxbox.services.mutations.pages :as pages]
|
2019-11-18 11:52:57 +01:00
|
|
|
[uxbox.fixtures :as fixtures]
|
|
|
|
[uxbox.migrations]
|
|
|
|
[uxbox.media]
|
|
|
|
[uxbox.db :as db]
|
|
|
|
[uxbox.util.blob :as blob]
|
|
|
|
[uxbox.util.uuid :as uuid]
|
|
|
|
[uxbox.config :as cfg]))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(defn state-init
|
|
|
|
[next]
|
|
|
|
(let [config (cfg/read-test-config)]
|
|
|
|
(-> (mount/only #{#'uxbox.config/config
|
|
|
|
#'uxbox.config/secret
|
2019-11-18 11:52:57 +01:00
|
|
|
#'uxbox.core/system
|
|
|
|
#'uxbox.db/pool
|
2016-11-20 20:04:52 +01:00
|
|
|
#'uxbox.migrations/migrations
|
2017-01-22 15:50:00 +01:00
|
|
|
#'uxbox.media/assets-storage
|
2016-11-20 20:04:52 +01:00
|
|
|
#'uxbox.media/media-storage
|
|
|
|
#'uxbox.media/images-storage
|
|
|
|
#'uxbox.media/thumbnails-storage})
|
|
|
|
(mount/swap {#'uxbox.config/config config})
|
|
|
|
(mount/start))
|
|
|
|
(try
|
|
|
|
(next)
|
|
|
|
(finally
|
|
|
|
(mount/stop)))))
|
|
|
|
|
|
|
|
(defn database-reset
|
|
|
|
[next]
|
2019-11-18 11:52:57 +01:00
|
|
|
(let [sql (str "SELECT table_name "
|
|
|
|
" FROM information_schema.tables "
|
|
|
|
" WHERE table_schema = 'public' "
|
|
|
|
" AND table_name != 'migrations';")]
|
|
|
|
|
|
|
|
@(db/with-atomic [conn db/pool]
|
|
|
|
(-> (db/query conn sql)
|
|
|
|
(p/then #(map :table-name %))
|
|
|
|
(p/then (fn [result]
|
|
|
|
(db/query-one conn (str "TRUNCATE "
|
|
|
|
(apply str (interpose ", " result))
|
|
|
|
" CASCADE;")))))))
|
2019-06-15 18:20:36 +02:00
|
|
|
(try
|
|
|
|
(next)
|
|
|
|
(finally
|
|
|
|
(st/clear! uxbox.media/media-storage)
|
|
|
|
(st/clear! uxbox.media/assets-storage))))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(defn mk-uuid
|
|
|
|
[prefix & args]
|
|
|
|
(uuid/namespaced uuid/oid (apply str prefix args)))
|
|
|
|
|
|
|
|
;; --- Users creation
|
|
|
|
|
2016-11-20 20:04:52 +01:00
|
|
|
(defn create-user
|
|
|
|
[conn i]
|
2019-12-01 16:48:41 +01:00
|
|
|
(profiles/create-profile conn {:id (mk-uuid "user" i)
|
|
|
|
:fullname (str "User " i)
|
|
|
|
:username (str "user" i)
|
|
|
|
:email (str "user" i ".test@uxbox.io")
|
|
|
|
:password "123123"
|
|
|
|
:metadata {}}))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
|
|
|
(defn create-project
|
2019-12-01 16:48:41 +01:00
|
|
|
[conn user-id i]
|
|
|
|
(projects/create-project conn {:id (mk-uuid "project" i)
|
|
|
|
:user user-id
|
|
|
|
:name (str "sample project " i)}))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
|
|
|
(defn create-page
|
|
|
|
[conn uid pid i]
|
2019-12-01 16:48:41 +01:00
|
|
|
(pages/create-page conn {:id (mk-uuid "page" i)
|
|
|
|
:user uid
|
|
|
|
:project-id pid
|
|
|
|
:name (str "page" i)
|
|
|
|
:data {:shapes []}
|
|
|
|
:metadata {}}))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
|
|
|
(defn handle-error
|
|
|
|
[err]
|
|
|
|
(cond
|
|
|
|
(instance? clojure.lang.ExceptionInfo err)
|
|
|
|
(ex-data err)
|
|
|
|
|
|
|
|
(instance? java.util.concurrent.ExecutionException err)
|
|
|
|
(handle-error (.getCause err))
|
|
|
|
|
|
|
|
:else
|
|
|
|
[err nil]))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(defmacro try-on
|
2019-11-18 11:52:57 +01:00
|
|
|
[expr]
|
2016-11-20 20:04:52 +01:00
|
|
|
`(try
|
2019-11-18 11:52:57 +01:00
|
|
|
(let [result# (deref ~expr)]
|
2016-11-20 20:04:52 +01:00
|
|
|
[nil result#])
|
2019-11-18 11:52:57 +01:00
|
|
|
(catch Exception e#
|
|
|
|
[(handle-error e#) nil])))
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro try-on!
|
|
|
|
[expr]
|
|
|
|
`(try
|
|
|
|
(let [result# (deref ~expr)]
|
|
|
|
{:error nil
|
|
|
|
:result result#})
|
|
|
|
(catch Exception e#
|
|
|
|
{:error (handle-error e#)
|
|
|
|
:result nil})))
|
|
|
|
|
2019-11-25 15:56:49 +01:00
|
|
|
(defmacro try!
|
|
|
|
[expr]
|
|
|
|
`(try
|
|
|
|
{:error nil
|
|
|
|
:result ~expr}
|
|
|
|
(catch Exception e#
|
|
|
|
{:error (handle-error e#)
|
|
|
|
:result nil})))
|
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(defn print-result!
|
|
|
|
[{:keys [error result]}]
|
|
|
|
(if error
|
|
|
|
(do
|
|
|
|
(println "====> START ERROR")
|
|
|
|
(if (= :spec-validation (:code error))
|
2019-12-01 16:48:41 +01:00
|
|
|
(s/explain-out (:data error))
|
|
|
|
(prn error))
|
|
|
|
(println "====> END ERROR"))
|
2019-11-18 11:52:57 +01:00
|
|
|
(do
|
|
|
|
(println "====> START RESPONSE")
|
|
|
|
(prn result)
|
|
|
|
(println "====> END RESPONSE"))))
|
|
|
|
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(defn exception?
|
|
|
|
[v]
|
|
|
|
(instance? Throwable v))
|
|
|
|
|
|
|
|
(defn ex-info?
|
|
|
|
[v]
|
|
|
|
(instance? clojure.lang.ExceptionInfo v))
|
|
|
|
|
|
|
|
(defn ex-of-type?
|
|
|
|
[e type]
|
|
|
|
(let [data (ex-data e)]
|
|
|
|
(= type (:type data))))
|
|
|
|
|
|
|
|
(defn ex-with-code?
|
|
|
|
[e code]
|
|
|
|
(let [data (ex-data e)]
|
|
|
|
(= code (:code data))))
|