From 4291c802598759128ecf8a628b23c60728e37764 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 15 Jun 2019 18:46:57 +0200 Subject: [PATCH] test(backend): fix image create/upload test --- backend/src/uxbox/api/images.clj | 37 ++++++++++++++---------- backend/test/uxbox/tests/test_images.clj | 32 +++++++++++--------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/backend/src/uxbox/api/images.clj b/backend/src/uxbox/api/images.clj index cb93fe920f..8b3d037fde 100644 --- a/backend/src/uxbox/api/images.clj +++ b/backend/src/uxbox/api/images.clj @@ -90,26 +90,31 @@ ;; :opt-un [::us/id ::collection])) (defn create-image - [{user :identity data :data}] - #_(let [{:keys [file id width height - mimetype collection]} (us/conform ::create-image data) - id (or id (uuid/random)) - filename (fs/name file) + {:parameters {:multipart {:upload [st/required] + :id [st/uuid-str] + :width [st/required st/integer-str] + :height [st/required st/integer-str] + :mimetype [st/required st/string] + :collection [st/uuid-str]}}} + [{:keys [user parameters] :as ctx}] + (prn "create-image" (:body-params ctx) (:multipart-params ctx)) + (let [params (get parameters :multipart) + upload (get params :upload) + filename (fs/name (:filename upload)) + tempfile (:tempfile upload) storage media/images-storage] (letfn [(persist-image-entry [path] - (sv/novelty {:id id - :type :create-image - :user user - :width width - :height height - :mimetype mimetype - :collection collection - :name filename - :path (str path)})) + (let [message (select-keys params [:id :width :height :collection :mimetype])] + (sv/novelty (assoc message + :id (or (:id params) (uuid/random)) + :type :create-image + :name filename + :path (str path) + :user user)))) (create-response [entry] (let [loc (str "/api/library/images/" (:id entry))] - (http/created loc (rsp entry))))] - (->> (st/save storage filename file) + (http/created loc entry)))] + (->> (ds/save storage filename tempfile) (p/mapcat persist-image-entry) (p/map populate-thumbnails) (p/map populate-urls) diff --git a/backend/test/uxbox/tests/test_images.clj b/backend/test/uxbox/tests/test_images.clj index 9f84850942..a25f44f372 100644 --- a/backend/test/uxbox/tests/test_images.clj +++ b/backend/test/uxbox/tests/test_images.clj @@ -72,20 +72,24 @@ 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)] -;; ;; (with-server {:handler (uft/routes)} -;; ;; (let [uri (str th/+base-url+ "/api/library/images") -;; ;; params [{:name "sample.jpg" -;; ;; :part-name "file" -;; ;; :content (io/input-stream -;; ;; (io/resource "uxbox/tests/_files/sample.jpg"))}] -;; ;; [status data] (th/http-multipart user uri params)] -;; ;; ;; (println "RESPONSE:" status data) -;; ;; (t/is (= 201 status)) -;; ;; (t/is (= (:user data) (:id user))) -;; ;; (t/is (= (:name data) "sample.jpg"))))))) +(t/deftest test-http-create-image + (with-open [conn (db/connection)] + (let [user (th/create-user conn 1)] + (th/with-server {:handler uapi/app} + (let [uri (str th/+base-url+ "/api/library/images") + parts [{:name "sample.jpg" + :part-name "upload" + :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)]