💄 Rename 'test' directory to 'tests'.

This commit is contained in:
Andrey Antukh 2020-01-12 18:59:35 +00:00
parent fe2c3aa8ad
commit 19529408f4
17 changed files with 13 additions and 9 deletions

94
backend/tests/user.clj Normal file
View file

@ -0,0 +1,94 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
(ns user
(:require
[clojure.spec.alpha :as s]
[clojure.tools.namespace.repl :as repl]
[clojure.walk :refer [macroexpand-all]]
[clojure.pprint :refer [pprint]]
[clojure.test :as test]
[clojure.java.io :as io]
[clojure.repl :refer :all]
[criterium.core :refer [quick-bench bench with-progress-reporting]]
[expound.alpha :as expound]
[promesa.core :as p]
;; [sieppari.core :as sp]
;; [sieppari.context :as spx]
;; [buddy.core.codecs :as codecs]
;; [buddy.core.codecs.base64 :as b64]
;; [buddy.core.nonce :as nonce]
[mount.core :as mount]
[uxbox.main]))
(defmacro run-quick-bench
[& exprs]
`(with-progress-reporting (quick-bench (do ~@exprs) :verbose)))
(defmacro run-quick-bench'
[& exprs]
`(quick-bench (do ~@exprs)))
(defmacro run-bench
[& exprs]
`(with-progress-reporting (bench (do ~@exprs) :verbose)))
(defmacro run-bench'
[& exprs]
`(bench (do ~@exprs)))
(def stress-data
{:shapes [{:id #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :name "Canvas-1", :type :canvas, :page #uuid "1b7d4218-e3be-5254-9582-18f767d30501", :x1 200, :y1 200, :x2 1224, :y2 968} {:stroke-color "#000000", :name "Rect-1-copy-4-copy-1", :y1 334, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "c1966bac-b568-4f8f-9917-227cc37e5672", :x1 674, :proportion 1.353448275862069, :y2 450, :x2 831, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-7-copy-1", :y1 400, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "20f11fd1-e9a2-41b1-a539-1298194c6d07", :x1 836, :proportion 1.353448275862069, :y2 516, :x2 993, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-11", :y1 355, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "e1efb49a-03dd-4b87-b568-f35e3a85cf19", :x1 223, :proportion 1.353448275862069, :y2 471, :x2 380, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-10", :y1 404, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "4bf2440d-38d4-461b-af41-001cfb00be49", :x1 928, :proportion 1.353448275862069, :y2 520, :x2 1085, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-9", :y1 525, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "bd1cb612-3960-4428-b030-ea0e59c70b06", :x1 497, :proportion 1.353448275862069, :y2 641, :x2 654, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-8", :y1 393, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "78104154-3d6b-43dc-8444-873ca3285bca", :x1 395, :proportion 1.353448275862069, :y2 509, :x2 552, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-7", :y1 711, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "35540bc3-e1a9-4052-8cb7-a4b07da229dd", :x1 838, :proportion 1.353448275862069, :y2 827, :x2 995, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-6", :y1 502, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "a4d2ef30-1da9-4aba-98ed-15088aba36e0", :x1 893, :proportion 1.353448275862069, :y2 618, :x2 1050, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-5", :y1 479, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "4de4a87a-74f3-47ce-85e6-a0d5da9adb62", :x1 595, :proportion 1.353448275862069, :y2 595, :x2 752, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-4", :y1 645, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "c6c062d2-20fe-4e48-8035-63358db2df4c", :x1 676, :proportion 1.353448275862069, :y2 761, :x2 833, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-3", :y1 609, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "b3089c39-24f9-4574-8df1-d1e4596a6200", :x1 831, :proportion 1.353448275862069, :y2 725, :x2 988, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-2", :y1 404, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "da7c624b-807a-41b1-84a6-7e14ed4f150c", :x1 733, :proportion 1.353448275862069, :y2 520, :x2 890, :height 2} {:stroke-color "#000000", :name "Rect-1-copy-1", :y1 609, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "ec65a0a8-9de1-48d7-b245-71f98961dc7c", :x1 518, :proportion 1.353448275862069, :y2 725, :x2 675, :height 2} {:stroke-color "#000000", :name "Rect-1", :y1 566, :width 2, :type :rect, :page #uuid "2ef8e638-4018-5581-aa18-0887f126966c", :canvas #uuid "352cbd3c-1336-5793-80f7-31027d0acfe7", :proportion-lock false, :id #uuid "c7eafc25-214a-4d16-abbe-19aa0f2eb25b", :x1 306, :proportion 1.353448275862069, :y2 682, :x2 463, :height 2}]})
;; (def a1 (blob/encode stress-data))
;; (def b1 (blob/encode-with-json stress-data))
;; (def b2 (blob/encode-json-snappy stress-data))
;; --- Development Stuff
;; (defn- make-secret
;; []
;; (-> (nonce/random-bytes 64)
;; (b64/encode true)
;; (codecs/bytes->str)))
(defn- start
[]
(-> #_(mount/except #{#'uxbox.scheduled-jobs/scheduler})
(mount/start)))
(defn- stop
[]
(mount/stop))
(defn restart
[]
(stop)
(repl/refresh :after 'user/start))
;; (defn- start-minimal
;; []
;; (-> (mount/only #{#'uxbox.config/config
;; #'uxbox.db/datasource
;; #'uxbox.migrations/migrations})
;; (mount/start)))
(defn- run-test
([] (run-test #"^uxbox.tests.*"))
([o]
;; (repl/refresh)
(cond
(instance? java.util.regex.Pattern o)
(test/run-all-tests o)
(symbol? o)
(if-let [sns (namespace o)]
(do (require (symbol sns))
(test/test-vars [(resolve o)]))
(test/test-ns o)))))

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB

View file

@ -0,0 +1,20 @@
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" width="500" height="500" viewBox="0 0 500.00001 500.00001" id="svg2" version="1.1" sodipodi:docname="lock.svg">
<defs id="defs4"/>
<sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" showgrid="false" units="px"/>
<metadata id="metadata7">
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>
image/svg+xml
</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
</cc:Work>
</rdf:RDF>
</metadata>
<use id="use32130" xlink:href="#symbol16714" transform="matrix(0.48875855,0,0,0.48875855,266.5973,18.208584)" x="0" y="0" width="100%" height="100%"/>
<path id="path4498" d="m110.60416 499.33847c-22.177988-4.50776-39.962038-21.75589-46.405198-45.0068-1.977315-7.13539-2.016513-9.26603-2.016513-109.60819 0-98.645 0.06949-102.60306 1.929042-109.87383 4.423652-17.29632 16.524827-32.63809 31.819424-40.34039l7.825735-3.94103 0.60407-49.32264c0.65846-53.763135 0.62011-53.373185 6.83197-69.481055 8.21794-21.30983 30.46536-44.049766 55.16981-56.391163 10.20624-5.09865 24.27131-9.8702562 37.2909-12.6510232 12.29719-2.62647101 13.38097-2.69147701 45.36838-2.72123901 36.82119-0.03426 43.33148 0.691327 63.09496 7.03207001 22.66364 7.2712112 38.73778 16.8847932 54.33879 32.4988162 11.05888 11.068109 16.97809 19.704479 22.16356 32.337529 5.96869 14.54116 6.15544 16.63373 6.17191 69.157365 0.008 26.25671 0.33839 48.1887 0.73367 48.73773 0.39527 0.54903 4.20746 2.70218 8.47153 4.7848 15.26633 7.45624 27.42013 22.8142 31.8923 40.30023 1.85931 7.2698 1.92905 11.22683 1.92905 109.45314 0 98.71665-0.0614 102.15316-1.95844 109.64831-2.76597 10.92815-7.22084 19.12549-14.77309 27.18364-7.14127 7.61966-13.97588 12.19324-24.11727 16.13877l-6.5947 2.56569-137.92851 0.14728c-75.86068 0.081-139.6893-0.21059-141.84138-0.64801zm275.44488-42.36785c3.21442-1.69982 4.97898-3.57704 7.00136-7.44832l2.70384-5.17574 0-101.43074 0-101.43073-2.51453-5.046c-1.49217-2.99441-3.82602-5.85656-5.74021-7.03959-3.20494-1.98076-4.09399-1.99357-138.31459-1.99357l-135.08891 0-3.61136 3.17081c-7.68721 6.74946-7.17152-1.28062-7.19562 112.04559-0.019 89.01071 0.15867 101.46341 1.49253 104.6558 1.77442 4.24679 4.66753 7.5951 8.75726 10.13513 2.8251 1.7546 7.88585 1.82277 135.57367 1.82629l132.63902 0.004 4.29754-2.2726zM210.87134 414.7115c0-0.81959 3.52158-15.55592 7.82573-32.74738 4.30416-17.19147 7.82573-31.96021 7.82573-32.81943 0-0.85925-2.41047-3.76528-5.3566-6.45788-9.7665-8.92602-14.60162-22.54037-12.4537-35.0661 1.56659-9.13564 4.34066-14.72784 10.7286-21.62759 23.40741-25.28278 65.56256-12.69769 71.62135 21.382 2.17751 12.24808-2.24683 25.13545-11.97693 34.88693l-5.77232 5.785 8.32828 33.30344c4.58055 18.31689 8.11305 33.65168 7.84999 34.07733-0.26305 0.42563-18.0602 0.77387-39.54921 0.77387-34.34393 0-39.07092-0.18029-39.07092-1.49019zM354.66915 143.45686c0-49.238655-0.0768-50.006535-6.08212-60.826955-10.93104-19.69552-35.55506-34.687-65.08507-39.62477-14.60005-2.441306-62.54427-1.62974-74.64943 1.26361-30.35851 7.25623-52.8469 24.27954-61.66065 46.676-2.28452 5.80515-2.31336 6.34422-2.63922 49.320085-0.1812 23.89793-0.0718 44.47715 0.24301 45.73161l0.57244 2.28083 104.65052 0 104.65052 0 0-44.82041z"/>
</svg>

After

Width:  |  Height:  |  Size: 3.5 KiB

View file

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="500"
height="500"
viewBox="0 0 500 500.00001"
id="svg2"
version="1.1"
inkscape:version="0.91 r13725"
sodipodi:docname="play.svg">
<metadata
id="metadata10">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs8" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1920"
inkscape:window-height="1056"
id="namedview6"
showgrid="false"
inkscape:zoom="0.472"
inkscape:cx="250"
inkscape:cy="250"
inkscape:window-x="0"
inkscape:window-y="24"
inkscape:window-maximized="1"
inkscape:current-layer="svg2" />
<path
d="m 121.67073,70.753329 0,359.389241 c 0,12.19281 8.74009,22.00921 23.46146,21.00294 14.72137,-1.00628 22.01162,-5.33889 27.30623,-10.32034 71.43412,-52.70054 141.82816,-106.26647 212.71245,-159.42587 7.89079,-6.55446 17.48742,-12.31195 23.29965,-20.13673 5.24872,-9.68035 -1.12541,-20.67448 -11.37732,-26.63162 C 320.53204,173.95026 244.16396,113.11502 166.1641,53.583899 c -4.02348,-2.658979 -9.04841,-4.760077 -22.40044,-4.800328 -13.35284,-0.04025 -22.09293,9.776148 -22.09293,21.968958 z m 44.01841,39.302621 c 32.61935,25.56497 124.45828,97.72763 175.71703,139.98708 3.07839,5.93701 -5.40168,9.45575 -9.37686,13.25704 -54.54483,42.67403 -111.22136,83.65914 -166.34097,125.8727 z"
id="path4"
inkscape:connector-curvature="0" />
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -0,0 +1,180 @@
(ns uxbox.tests.helpers
(:require
[clojure.spec.alpha :as s]
[promesa.core :as p]
[cuerdas.core :as str]
[mount.core :as mount]
[datoteka.storages :as st]
[uxbox.services.mutations.users :as users]
[uxbox.services.mutations.projects :as projects]
[uxbox.services.mutations.project-files :as files]
[uxbox.services.mutations.project-pages :as pages]
[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]))
(defn state-init
[next]
(let [config (cfg/read-test-config)]
(-> (mount/only #{#'uxbox.config/config
#'uxbox.core/system
#'uxbox.db/pool
#'uxbox.services.init/query-services
#'uxbox.services.init/mutation-services
#'uxbox.migrations/migrations
#'uxbox.media/assets-storage
#'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]
(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;")))))))
(try
(next)
(finally
(st/clear! uxbox.media/media-storage)
(st/clear! uxbox.media/assets-storage))))
(defn mk-uuid
[prefix & args]
(uuid/namespaced uuid/oid (apply str prefix args)))
;; --- Users creation
(defn create-user
[conn i]
(users/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 {}}))
(defn create-project
[conn user-id i]
(projects/create-project conn {:id (mk-uuid "project" i)
:user user-id
:version 1
:name (str "sample project " i)}))
(defn create-project-file
[conn user-id project-id i]
(files/create-file conn {:id (mk-uuid "project-file" i)
:user user-id
:project-id project-id
:name (str "sample project file" i)}))
(defn create-project-page
[conn user-id file-id i]
(pages/create-page conn {:id (mk-uuid "page" i)
:user user-id
:file-id file-id
:name (str "page" i)
:ordering i
:data {:shapes []
:canvas []
:shapes-by-id {}}
:metadata {}}))
(defn handle-error
[err]
(if (instance? java.util.concurrent.ExecutionException err)
(handle-error (.getCause err))
err))
(defmacro try-on
[expr]
`(try
(let [result# (deref ~expr)]
[nil result#])
(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})))
(defmacro try!
[expr]
`(try
{:error nil
:result ~expr}
(catch Exception e#
{:error (handle-error e#)
:result nil})))
(defn print-error!
[error]
(let [data (ex-data error)]
(cond
(= :spec-validation (:code data))
(println (:explain data))
:else
(.printStackTrace error))))
(defn print-result!
[{:keys [error result]}]
(if error
(do
(println "====> START ERROR")
(print-error! error)
(println "====> END ERROR"))
(do
(println "====> START RESPONSE")
(prn result)
(println "====> END RESPONSE"))))
(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-of-code?
[e code]
(let [data (ex-data e)]
(= code (:code data))))
(defn ex-with-code?
[e code]
(let [data (ex-data e)]
(= code (:code data))))

View file

@ -0,0 +1,44 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.tests.test-emails
(:require
[clojure.test :as t]
[promesa.core :as p]
[mockery.core :refer [with-mock]]
[uxbox.db :as db]
[uxbox.emails :as emails]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest register-email-rendering
(let [result (emails/render emails/register {:to "example@uxbox.io" :name "foo"})]
(t/is (map? result))
(t/is (contains? result :subject))
(t/is (contains? result :body))
(t/is (contains? result :to))
(t/is (contains? result :reply-to))
(t/is (vector? (:body result)))))
;; (t/deftest email-sending-and-sendmail-job
;; (let [res @(emails/send! emails/register {:to "example@uxbox.io" :name "foo"})]
;; (t/is (nil? res)))
;; (with-mock mock
;; {:target 'uxbox.jobs.sendmail/impl-sendmail
;; :return (p/resolved nil)}
;; (let [res @(uxbox.jobs.sendmail/send-emails {})]
;; (t/is (= 1 res))
;; (t/is (:called? @mock))
;; (t/is (= 1 (:call-count @mock))))
;; (let [res @(uxbox.jobs.sendmail/send-emails {})]
;; (t/is (= 0 res))
;; (t/is (:called? @mock))
;; (t/is (= 1 (:call-count @mock))))))

View file

@ -0,0 +1,162 @@
(ns uxbox.tests.test-icons
#_(:require [clojure.test :as t]
[promesa.core :as p]
[suricatta.core :as sc]
[uxbox.db :as db]
[uxbox.sql :as sql]
[uxbox.http :as http]
[uxbox.services.icons :as icons]
[uxbox.services :as usv]
[uxbox.tests.helpers :as th]))
;; (t/use-fixtures :once th/state-init)
;; (t/use-fixtures :each th/database-reset)
;; (t/deftest test-http-list-icon-collections
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"}
;; coll (icons/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icon-collections")
;; [status data] (th/http-get user uri)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= 1 (count data))))))))
;; (t/deftest test-http-create-icon-collection
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icon-collections")
;; data {:user (:id user)
;; :name "coll1"}
;; params {:body data}
;; [status data] (th/http-post user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 201 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "coll1")))))))
;; (t/deftest test-http-update-icon-collection
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"}
;; coll (icons/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icon-collections/" (:id coll))
;; params {:body (assoc coll :name "coll2")}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "coll2")))))))
;; (t/deftest test-http-icon-collection-delete
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"
;; :data #{1}}
;; coll (icons/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icon-collections/" (:id coll))
;; [status data] (th/http-delete user uri)]
;; (t/is (= 204 status))
;; (let [sqlv (sql/get-icon-collections {:user (:id user)})
;; result (sc/fetch conn sqlv)]
;; (t/is (empty? result))))))))
;; (t/deftest test-http-create-icon
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icons")
;; data {:name "sample.jpg"
;; :content "<g></g>"
;; :metadata {:width 200
;; :height 200
;; :view-box [0 0 200 200]}
;; :collection nil}
;; params {:body data}
;; [status data] (th/http-post user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 201 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "sample.jpg"))
;; (t/is (= (:metadata data) {:width 200
;; :height 200
;; :view-box [0 0 200 200]})))))))
;; (t/deftest test-http-update-icon
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.svg"
;; :content "<g></g>"
;; :metadata {}
;; :collection nil}
;; icon (icons/create-icon conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icons/" (:id icon))
;; params {:body (assoc icon :name "my stuff")}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "my stuff")))))))
;; (t/deftest test-http-copy-icon
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.svg"
;; :content "<g></g>"
;; :metadata {}
;; :collection nil}
;; icon (icons/create-icon conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icons/" (:id icon) "/copy")
;; body {:collection nil}
;; params {:body body}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= status 200))
;; (let [sqlv (sql/get-icons {:user (:id user) :collection nil})
;; result (sc/fetch conn sqlv)]
;; (t/is (= 2 (count result)))))))))
;; (t/deftest test-http-delete-icon
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.svg"
;; :content "<g></g>"
;; :metadata {}
;; :collection nil}
;; icon (icons/create-icon conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icons/" (:id icon))
;; [status data] (th/http-delete user uri)]
;; (t/is (= 204 status))
;; (let [sqlv (sql/get-icons {:user (:id user) :collection nil})
;; result (sc/fetch conn sqlv)]
;; (t/is (empty? result))))))))
;; (t/deftest test-http-list-icons
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.png"
;; :content "<g></g>"
;; :metadata {}
;; :collection nil}
;; icon (icons/create-icon conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/icons")
;; [status data] (th/http-get user uri)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= 1 (count data))))))))

View file

@ -0,0 +1,176 @@
(ns uxbox.tests.test-images
#_(:require [clojure.test :as t]
[promesa.core :as p]
[suricatta.core :as sc]
[clojure.java.io :as io]
[datoteka.storages :as st]
[uxbox.db :as db]
[uxbox.sql :as sql]
[uxbox.media :as media]
[uxbox.http :as http]
[uxbox.services.images :as images]
[uxbox.services :as usv]
[uxbox.tests.helpers :as th]))
;; (t/use-fixtures :once th/state-init)
;; (t/use-fixtures :each th/database-reset)
;; (t/deftest test-http-list-image-collections
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"}
;; coll (images/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/image-collections")
;; [status data] (th/http-get user uri)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= 1 (count data))))))))
;; (t/deftest test-http-create-image-collection
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/image-collections")
;; data {:user (:id user)
;; :name "coll1"}
;; params {:body data}
;; [status data] (th/http-post user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 201 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "coll1")))))))
;; (t/deftest test-http-update-image-collection
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"}
;; coll (images/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/image-collections/" (:id coll))
;; params {:body (assoc coll :name "coll2")}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "coll2")))))))
;; (t/deftest test-http-image-collection-delete
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "coll1"
;; :data #{1}}
;; coll (images/create-collection conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/image-collections/" (:id coll))
;; [status data] (th/http-delete user uri)]
;; (t/is (= 204 status))
;; (let [sqlv (sql/get-image-collections {:user (:id user)})
;; result (sc/fetch conn sqlv)]
;; (t/is (empty? result))))))))
;; (t/deftest test-http-create-image
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/images")
;; parts [{:name "sample.jpg"
;; :part-name "file"
;; :content (io/input-stream
;; (io/resource "uxbox/tests/_files/sample.jpg"))}
;; {:part-name "user" :content (str (:id user))}
;; {:part-name "width" :content "100"}
;; {:part-name "height" :content "100"}
;; {:part-name "mimetype" :content "image/png"}]
;; [status data] (th/http-multipart user uri parts)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 201 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "sample.jpg")))))))
;; (t/deftest test-http-update-image
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.png"
;; :path "some/path"
;; :width 100
;; :height 100
;; :mimetype "image/png"
;; :collection nil}
;; img (images/create-image conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/images/" (:id img))
;; params {:body (assoc img :name "my stuff")}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= (:user data) (:id user)))
;; (t/is (= (:name data) "my stuff")))))))
;; (t/deftest test-http-copy-image
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; storage media/images-storage
;; filename "sample.jpg"
;; rcs (io/resource "uxbox/tests/_files/sample.jpg")
;; path @(st/save storage filename rcs)
;; data {:user (:id user)
;; :name filename
;; :path (str path)
;; :width 100
;; :height 100
;; :mimetype "image/jpg"
;; :collection nil}
;; img (images/create-image conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/images/" (:id img) "/copy")
;; body {:id (:id img)
;; :collection nil}
;; params {:body body}
;; [status data] (th/http-put user uri params)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (let [sqlv (sql/get-images {:user (:id user) :collection nil})
;; result (sc/fetch conn sqlv)]
;; (t/is (= 2 (count result)))))))))
;; (t/deftest test-http-delete-image
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.png"
;; :path "some/path"
;; :width 100
;; :height 100
;; :mimetype "image/png"
;; :collection nil}
;; img (images/create-image conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/images/" (:id img))
;; [status data] (th/http-delete user uri)]
;; (t/is (= 204 status))
;; (let [sqlv (sql/get-images {:user (:id user) :collection nil})
;; result (sc/fetch conn sqlv)]
;; (t/is (empty? result))))))))
;; (t/deftest test-http-list-images
;; (with-open [conn (db/connection)]
;; (let [user (th/create-user conn 1)
;; data {:user (:id user)
;; :name "test.png"
;; :path "some/path"
;; :width 100
;; :height 100
;; :mimetype "image/png"
;; :collection nil}
;; img (images/create-image conn data)]
;; (th/with-server {:handler @http/app}
;; (let [uri (str th/+base-url+ "/api/library/images")
;; [status data] (th/http-get user uri)]
;; ;; (println "RESPONSE:" status data)
;; (t/is (= 200 status))
;; (t/is (= 1 (count data))))))))

View file

@ -0,0 +1,46 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.tests.test-services-auth
(:require
[clojure.test :as t]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.services.mutations :as sm]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest failed-auth
(let [user @(th/create-user db/pool 1)
event {:username "user1"
::sm/type :login
:password "foobar"
:metadata "1"
:scope "foobar"}
out (th/try-on! (sm/handle event))]
;; (th/print-result! out)
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :service-error)))
(let [error (ex-cause (:error out))]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :validation))
(t/is (th/ex-of-code? error :uxbox.services.mutations.auth/wrong-credentials)))))
(t/deftest success-auth
(let [user @(th/create-user db/pool 1)
event {:username "user1"
::sm/type :login
:password "123123"
:metadata "1"
:scope "foobar"}
out (th/try-on! (sm/handle event))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (get-in out [:result :id]) (:id user)))))

View file

@ -0,0 +1,76 @@
(ns uxbox.tests.test-services-project-files
(:require
[clojure.test :as t]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.http :as http]
[uxbox.services.mutations :as sm]
[uxbox.services.queries :as sq]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest query-project-files
(let [user @(th/create-user db/pool 2)
proj @(th/create-project db/pool (:id user) 1)
pf @(th/create-project-file db/pool (:id user) (:id proj) 1)
pp @(th/create-project-page db/pool (:id user) (:id pf) 1)
data {::sq/type :project-files
:user (:id user)
:project-id (:id proj)}
out (th/try-on! (sq/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= 1 (count (:result out))))
(t/is (= (:id pf) (get-in out [:result 0 :id])))
(t/is (= (:id proj) (get-in out [:result 0 :project-id])))
(t/is (= (:name pf) (get-in out [:result 0 :name])))
(t/is (= [(:id pp)] (get-in out [:result 0 :pages])))))
(t/deftest mutation-create-project-file
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
data {::sm/type :create-project-file
:user (:id user)
:name "test file"
:project-id (:id proj)}
out (th/try-on! (sm/handle data))
]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (:name data) (get-in out [:result :name])))
#_(t/is (= (:project-id data) (get-in out [:result :project-id])))))
(t/deftest mutation-rename-project-file
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
pf @(th/create-project-file db/pool (:id user) (:id proj) 1)
data {::sm/type :rename-project-file
:id (:id pf)
:name "new file name"
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
;; TODO: check the result
(t/is (nil? (:error out)))
(t/is (nil? (:result out)))))
(t/deftest mutation-delete-project-file
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
pf @(th/create-project-file db/pool (:id user) (:id proj) 1)
data {::sm/type :delete-project-file
:id (:id pf)
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (nil? (:result out)))
(let [sql "select * from project_files
where project_id=$1 and deleted_at is null"
res @(db/query db/pool [sql (:id proj)])]
(t/is (empty? res)))))
;; ;; TODO: add permisions related tests

View file

@ -0,0 +1,168 @@
(ns uxbox.tests.test-services-project-pages
(:require
[clojure.spec.alpha :as s]
[clojure.test :as t]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.http :as http]
[uxbox.services.mutations :as sm]
[uxbox.services.queries :as sq]
[uxbox.util.uuid :as uuid]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest query-project-pages
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
data {::sq/type :project-pages
:file-id (:id file)
:user (:id user)}
out (th/try-on! (sq/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (vector? (:result out)))
(t/is (= 1 (count (:result out))))
(t/is (= "page1" (get-in out [:result 0 :name])))
(t/is (:id file) (get-in out [:result 0 :file-id]))))
(t/deftest mutation-create-project-page
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
pf @(th/create-project-file db/pool (:id user) (:id proj) 1)
data {::sm/type :create-project-page
:data {:canvas []
:shapes []
:shapes-by-id {}}
:metadata {}
:file-id (:id pf)
:ordering 1
:name "test page"
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (uuid? (get-in out [:result :id])))
(t/is (= (:user data) (get-in out [:result :user-id])))
(t/is (= (:name data) (get-in out [:result :name])))
(t/is (= (:data data) (get-in out [:result :data])))
(t/is (= (:metadata data) (get-in out [:result :metadata])))
(t/is (= 0 (get-in out [:result :version])))))
(t/deftest mutation-update-project-page-data
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
data {::sm/type :update-project-page-data
:id (:id page)
:data {:shapes [(uuid/next)]
:canvas []
:shapes-by-id {}}
:file-id (:id file)
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
;; TODO: check history creation
;; TODO: check correct page data update operation
(t/is (nil? (:error out)))
(t/is (= (:id data) (get-in out [:result :id])))
(t/is (= 1 (get-in out [:result :version])))))
(t/deftest mutation-update-project-page-1
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
data {::sm/type :update-project-page
:id (:id page)
:version 99
:user (:id user)
:operations []}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :service-error)))
(let [error (ex-cause (:error out))]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :validation))
(t/is (th/ex-of-code? error :version-conflict)))))
(t/deftest mutation-update-project-page-2
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
sid (uuid/next)
data {::sm/type :update-project-page
:id (:id page)
:version 0
:user (:id user)
:operations [[:add-shape sid {:id sid :type :rect}]]}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= 0 (count (:result out))))
;; (t/is (= 1 (count (:result out))))
;; (t/is (= (:id data) (get-in out [:result 0 :page-id])))
;; (t/is (= 1 (count (get-in out [:result 0 :operations]))))
;; (t/is (= :add-shape (get-in out [:result 0 :operations 0 0])))
;; (t/is (= sid (get-in out [:result 0 :operations 0 1])))
))
(t/deftest mutation-update-project-page-3
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
sid (uuid/next)
data {::sm/type :update-project-page
:id (:id page)
:version 0
:user (:id user)
:operations [[:add-shape sid {:id sid :type :rect}]]}
out1 (th/try-on! (sm/handle data))
out2 (th/try-on! (sm/handle data))]
;; (th/print-result! out1)
;; (th/print-result! out2)
(t/is (nil? (:error out1)))
(t/is (nil? (:error out2)))
(t/is (= 0 (count (:result out1))))
(t/is (= 1 (count (:result out2))))
(t/is (= (:id data) (get-in out2 [:result 0 :page-id])))
(t/is (= 1 (count (get-in out2 [:result 0 :operations]))))
(t/is (= :add-shape (get-in out2 [:result 0 :operations 0 0])))
(t/is (= sid (get-in out2 [:result 0 :operations 0 1])))
))
(t/deftest mutation-delete-project-page
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
file @(th/create-project-file db/pool (:id user) (:id proj) 1)
page @(th/create-project-page db/pool (:id user) (:id file) 1)
data {::sm/type :delete-project-page
:id (:id page)
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (nil? (:result out)))))

View file

@ -0,0 +1,65 @@
(ns uxbox.tests.test-services-projects
(:require
[clojure.test :as t]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.http :as http]
[uxbox.services.mutations :as sm]
[uxbox.services.queries :as sq]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest query-projects
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
data {::sq/type :projects
:user (:id user)}
out (th/try-on! (sq/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= 1 (count (:result out))))
(t/is (= (:id proj) (get-in out [:result 0 :id])))
(t/is (= (:name proj) (get-in out [:result 0 :name])))))
(t/deftest mutation-create-project
(let [user @(th/create-user db/pool 1)
data {::sm/type :create-project
:user (:id user)
:name "test project"}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (:name data) (get-in out [:result :name])))))
(t/deftest mutation-rename-project
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
data {::sm/type :rename-project
:id (:id proj)
:name "test project mod"
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (:id data) (get-in out [:result :id])))
(t/is (= (:user data) (get-in out [:result :user-id])))
(t/is (= (:name data) (get-in out [:result :name])))))
(t/deftest mutation-delete-project
(let [user @(th/create-user db/pool 1)
proj @(th/create-project db/pool (:id user) 1)
data {::sm/type :delete-project
:id (:id proj)
:user (:id user)}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (nil? (:result out)))
(let [sql "select * from projects where user_id=$1 and deleted_at is null"
res @(db/query db/pool [sql (:id user)])]
(t/is (empty? res)))))
;; TODO: add permisions related tests

View file

@ -0,0 +1,65 @@
(ns uxbox.tests.test-services-user-attrs
(:require
[clojure.spec.alpha :as s]
[clojure.test :as t]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.http :as http]
[uxbox.services.mutations :as sm]
[uxbox.services.queries :as sq]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest test-user-attrs
(let [{:keys [id] :as user} @(th/create-user db/pool 1)]
(let [out (th/try-on! (sq/handle {::sq/type :user-attr
:key "foobar"
:user id}))]
(t/is (nil? (:result out)))
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :service-error)))
(let [error (ex-cause (:error out))]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(let [out (th/try-on! (sm/handle {::sm/type :upsert-user-attr
:user id
:key "foobar"
:val {:some #{:value}}}))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (nil? (:result out))))
(let [out (th/try-on! (sq/handle {::sq/type :user-attr
:key "foobar"
:user id}))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= {:some #{:value}} (get-in out [:result :val])))
(t/is (= "foobar" (get-in out [:result :key]))))
(let [out (th/try-on! (sm/handle {::sm/type :delete-user-attr
:user id
:key "foobar"}))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (nil? (:result out))))
(let [out (th/try-on! (sq/handle {::sq/type :user-attr
:key "foobar"
:user id}))]
;; (th/print-result! out)
(t/is (nil? (:result out)))
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :service-error)))
(let [error (ex-cause (:error out))]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))))

View file

@ -0,0 +1,116 @@
(ns uxbox.tests.test-services-users
(:require
[clojure.test :as t]
[clojure.java.io :as io]
[promesa.core :as p]
[cuerdas.core :as str]
[datoteka.core :as fs]
[uxbox.db :as db]
[uxbox.services.mutations :as sm]
[uxbox.services.queries :as sq]
[uxbox.tests.helpers :as th]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest test-query-profile
(let [user @(th/create-user db/pool 1)
data {::sq/type :profile
:user (:id user)}
out (th/try-on! (sq/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= "User 1" (get-in out [:result :fullname])))
(t/is (= "user1" (get-in out [:result :username])))
(t/is (= "user1.test@uxbox.io" (get-in out [:result :email])))
(t/is (not (contains? (:result out) :password)))))
(t/deftest test-mutation-update-profile
(let [user @(th/create-user db/pool 1)
data (assoc user
::sm/type :update-profile
:fullname "Full Name"
:username "user222"
:metadata {:foo "bar"}
:email "user222@uxbox.io")
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (:fullname data) (get-in out [:result :fullname])))
(t/is (= (:username data) (get-in out [:result :username])))
(t/is (= (:email data) (get-in out [:result :email])))
(t/is (= (:metadata data) (get-in out [:result :metadata])))
(t/is (not (contains? (:result out) :password)))))
(t/deftest test-mutation-update-profile-photo
(let [user @(th/create-user db/pool 1)
data {::sm/type :update-profile-photo
:user (:id user)
:file {:name "sample.jpg"
:path (fs/path "test/uxbox/tests/_files/sample.jpg")
:size 123123
:mtype "image/jpeg"}}
out (th/try-on! (sm/handle data))]
;; (th/print-result! out)
(t/is (nil? (:error out)))
(t/is (= (:id user) (get-in out [:result :id])))
(t/is (str/starts-with? (get-in out [:result :photo]) "http"))))
;; (t/deftest test-mutation-register-profile
;; (let[data {:fullname "Full Name"
;; :username "user222"
;; :email "user222@uxbox.io"
;; :password "user222"
;; ::sv/type :register-profile}
;; [err rsp] (th/try-on (sm/handle data))]
;; (println "RESPONSE:" err rsp)))
;; ;; (t/deftest test-http-validate-recovery-token
;; ;; (with-open [conn (db/connection)]
;; ;; (let [user (th/create-user conn 1)]
;; ;; (with-server {:handler (uft/routes)}
;; ;; (let [token (#'usu/request-password-recovery conn "user1")
;; ;; uri1 (str th/+base-url+ "/api/auth/recovery/not-existing")
;; ;; uri2 (str th/+base-url+ "/api/auth/recovery/" token)
;; ;; [status1 data1] (th/http-get user uri1)
;; ;; [status2 data2] (th/http-get user uri2)]
;; ;; ;; (println "RESPONSE:" status1 data1)
;; ;; ;; (println "RESPONSE:" status2 data2)
;; ;; (t/is (= 404 status1))
;; ;; (t/is (= 204 status2)))))))
;; ;; (t/deftest test-http-request-password-recovery
;; ;; (with-open [conn (db/connection)]
;; ;; (let [user (th/create-user conn 1)
;; ;; sql "select * from user_pswd_recovery"
;; ;; res (sc/fetch-one conn sql)]
;; ;; ;; Initially no tokens exists
;; ;; (t/is (nil? res))
;; ;; (with-server {:handler (uft/routes)}
;; ;; (let [uri (str th/+base-url+ "/api/auth/recovery")
;; ;; data {:username "user1"}
;; ;; [status data] (th/http-post user uri {:body data})]
;; ;; ;; (println "RESPONSE:" status data)
;; ;; (t/is (= 204 status)))
;; ;; (let [res (sc/fetch-one conn sql)]
;; ;; (t/is (not (nil? res)))
;; ;; (t/is (= (:user res) (:id user))))))))
;; ;; (t/deftest test-http-validate-recovery-token
;; ;; (with-open [conn (db/connection)]
;; ;; (let [user (th/create-user conn 1)]
;; ;; (with-server {:handler (uft/routes)}
;; ;; (let [token (#'usu/request-password-recovery conn (:username user))
;; ;; uri (str th/+base-url+ "/api/auth/recovery")
;; ;; data {:token token :password "mytestpassword"}
;; ;; [status data] (th/http-put user uri {:body data})
;; ;; user' (usu/find-full-user-by-id conn (:id user))]
;; ;; (t/is (= status 204))
;; ;; (t/is (hashers/check "mytestpassword" (:password user'))))))))

View file

@ -0,0 +1,53 @@
(ns uxbox.tests.test-util-svg
(:require
[clojure.test :as t]
[clojure.java.io :as io]
[uxbox.http :as http]
[uxbox.util.svg :as svg]
[uxbox.tests.helpers :as th]))
(t/deftest parse-svg-1
(let [result (-> (io/resource "uxbox/tests/_files/sample1.svg")
(svg/parse))]
(t/is (contains? result :width))
(t/is (contains? result :height))
(t/is (contains? result :view-box))
(t/is (contains? result :name))
(t/is (contains? result :content))
(t/is (= 500.0 (:width result)))
(t/is (= 500.0 (:height result)))
(t/is (= [0.0 0.0 500.00001 500.00001] (:view-box result)))
(t/is (= "lock.svg" (:name result)))))
(t/deftest parse-svg-2
(let [result (-> (io/resource "uxbox/tests/_files/sample2.svg")
(svg/parse))]
(t/is (contains? result :width))
(t/is (contains? result :height))
(t/is (contains? result :view-box))
(t/is (contains? result :name))
(t/is (contains? result :content))
(t/is (= 500.0 (:width result)))
(t/is (= 500.0 (:height result)))
(t/is (= [0.0 0.0 500.0 500.00001] (:view-box result)))
(t/is (= "play.svg" (:name result)))))
(t/deftest parse-invalid-svg-1
(let [image (io/resource "uxbox/tests/_files/sample.jpg")
out (th/try! (svg/parse image))]
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-code? error ::svg/invalid-input)))))
(t/deftest parse-invalid-svg-2
(let [out (th/try! (svg/parse-string ""))]
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-code? error ::svg/invalid-input)))))
(t/deftest parse-invalid-svg-3
(let [out (th/try! (svg/parse-string "<svg></svg>"))]
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-code? error ::svg/invalid-result)))))