diff --git a/resources/system.edn b/resources/system.edn index ca1d929e7..4093135c9 100644 --- a/resources/system.edn +++ b/resources/system.edn @@ -59,7 +59,7 @@ :wrap-auth #ig/ref :drafter.middleware/wrap-auth} - :drafter.routes.draftsets-api/create-draftsets-handler {:drafter/backend #ig/ref :drafter/backend + :drafter.feature.draftset.create/handler {:drafter/backend #ig/ref :drafter/backend :wrap-auth #ig/ref :drafter.middleware/wrap-auth} :drafter.feature.middleware/wrap-as-draftset-owner {:drafter/backend #ig/ref :drafter/backend @@ -113,7 +113,7 @@ :drafter.routes/draftsets-api {:get-users-handler #ig/ref :drafter.feature.users.list/get-users-handler :get-draftsets-handler #ig/ref :drafter.feature.draftset.list/get-draftsets-handler - :create-draftsets-handler #ig/ref :drafter.routes.draftsets-api/create-draftsets-handler + :create-draftsets-handler #ig/ref :drafter.feature.draftset.create/handler :get-draftset-handler #ig/ref :drafter.routes.draftsets-api/get-draftset-handler :delete-draftset-handler #ig/ref :drafter.routes.draftsets-api/delete-draftset-handler :draftset-options-handler #ig/ref :drafter.routes.draftsets-api/draftset-options-handler diff --git a/src/drafter/feature/draftset/create.clj b/src/drafter/feature/draftset/create.clj index 3adad3928..ced257fb6 100644 --- a/src/drafter/feature/draftset/create.clj +++ b/src/drafter/feature/draftset/create.clj @@ -1,2 +1,27 @@ -(ns drafter.feature.draftset.create) +(ns drafter.feature.draftset.create + (:require [drafter.backend.draftset.operations :as dsops] + [drafter.feature.common :as feat-common] + [drafter.rdf.draftset-management.job-util :as jobutil] + [drafter.util :as util] + [ring.util.response :as ring] + [swirrl-server.responses :as response])) + +(defn create-draftsets-handler [{wrap-authenticated :wrap-auth backend :drafter/backend}] + (let [version "/v1"] + (wrap-authenticated + (fn [{{:keys [display-name description]} :params user :identity :as request}] + (feat-common/run-sync #(dsops/create-draftset! backend user display-name description util/create-uuid util/get-current-time) + (fn [result] + (if (jobutil/failed-job-result? result) + (response/api-response 500 result) + (ring/redirect-after-post (str version "/draftset/" + (get-in result [:details :id])))))))))) + +(s/def ::wrap-auth fn?) + +(defmethod ig/pre-init-spec ::handler [_] + (s/keys :req [:drafter/backend] :req-un [::wrap-auth])) + +(defmethod ig/init-key ::handler [_ opts] + (create-draftsets-handler opts)) diff --git a/src/drafter/routes/draftsets_api.clj b/src/drafter/routes/draftsets_api.clj index 6ae944bfd..616f2cbad 100644 --- a/src/drafter/routes/draftsets_api.clj +++ b/src/drafter/routes/draftsets_api.clj @@ -65,15 +65,6 @@ (ring/redirect-after-post (str version "/draftset/" (get-in result [:details :id])))))))))) -(s/def ::wrap-auth fn?) - -(defmethod ig/pre-init-spec ::create-draftsets-handler [_] - (s/keys :req [:drafter/backend] :req-un [::wrap-auth])) - -(defmethod ig/init-key ::create-draftsets-handler [_ opts] - (create-draftsets-handler opts)) - - (defn get-draftset-handler [{wrap-authenticated :wrap-auth backend :drafter/backend}] (wrap-authenticated (feat-middleware/existing-draftset-handler diff --git a/test/drafter/feature/draftset/create_test.clj b/test/drafter/feature/draftset/create_test.clj index 5469a81af..e9d34b5be 100644 --- a/test/drafter/feature/draftset/create_test.clj +++ b/test/drafter/feature/draftset/create_test.clj @@ -1,4 +1,71 @@ (ns drafter.feature.draftset.create-test (:require [drafter.feature.draftset.create :as sut] - [clojure.test :as t])) + [clojure.test :as t]) + (:require [clojure.java.io :as io] + [clojure.set :as set] + [clojure.test :refer :all :as t] + [drafter.middleware :as middleware] + [drafter.rdf.drafter-ontology + :refer + [drafter:DraftGraph drafter:modifiedAt]] + [drafter.rdf.draftset-management.job-util :as jobs] + [drafter.rdf.sparql :as sparql] + [drafter.swagger :as swagger] + [drafter.test-common :as tc] + [drafter.timeouts :as timeouts] + [drafter.user :as user] + [drafter.user-test :refer [test-editor test-manager test-password test-publisher]] + [drafter.user.memory-repository :as memrepo] + [drafter.util :as util] + [grafter.rdf :refer [add context statements]] + [grafter.rdf.protocols :refer [->Quad ->Triple map->Triple]] + [grafter.rdf4j.formats :as formats] + [grafter.rdf4j.io :refer [rdf-writer]] + [schema.core :as s] + [swirrl-server.async.jobs :refer [finished-jobs]])) +(def see-other-response-schema + (merge tc/ring-response-schema + {:status (s/eq 303) + :headers {(s/required-key "Location") s/Str}})) + + + +(defn assert-is-see-other-response [response] + (tc/assert-schema see-other-response-schema response)) + + + +(defn valid-swagger-response? + "Applies handler to request and validates the response against the + swagger spec for the requested route. + + Returns the response if valid, otherwise raises an error." + [handler request] + (let [swagger-spec (swagger/load-spec-and-resolve-refs)] + (swagger/validate-response-against-swagger-spec swagger-spec request (handler request)))) + +(defn create-draftset-request + "Build a HTTP request object that representing a request to create a + draftset." + ([] (create-draftset-request test-editor)) + ([user] (create-draftset-request user nil)) + ([user display-name] (create-draftset-request user display-name nil)) + ([user display-name description] + (tc/with-identity user {:uri "/v1/draftsets" :request-method :post :params {:display-name display-name :description description}}))) + +(tc/deftest-system create-draftset-without-title-or-description + [{handler :drafter.feature.draftset.create/handler} "test-system.edn"] + (let [request (tc/with-identity test-editor {:uri "/v1/draftsets" :request-method :post}) + response (valid-swagger-response? handler request)] + (assert-is-see-other-response response))) + +(tc/deftest-system create-draftset-with-title-and-without-description + [{handler :drafter.feature.draftset.create/handler} "test-system.edn"] + (let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test Title!"))] + (assert-is-see-other-response response))) + +(tc/deftest-system create-draftset-with-title-and-description + [{handler :drafter.feature.draftset.create/handler} "test-system.edn"] + (let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test title" "Test description"))] + (assert-is-see-other-response response))) diff --git a/test/drafter/routes/draftsets_api_test.clj b/test/drafter/routes/draftsets_api_test.clj index 8deda0c85..defd7a8b3 100644 --- a/test/drafter/routes/draftsets_api_test.clj +++ b/test/drafter/routes/draftsets_api_test.clj @@ -7,6 +7,7 @@ :refer [drafter:DraftGraph drafter:modifiedAt]] [drafter.rdf.draftset-management.job-util :as jobs] + [drafter.feature.draftset.create-test :as create-test] [drafter.rdf.sparql :as sparql] [drafter.routes.draftsets-api :as sut :refer :all] [drafter.swagger :as swagger] @@ -66,13 +67,6 @@ :body data-stream :headers {"content-type" content-type}})) -(defn- create-draftset-request - ([] (create-draftset-request test-editor)) - ([user] (create-draftset-request user nil)) - ([user display-name] (create-draftset-request user display-name nil)) - ([user display-name description] - (tc/with-identity user {:uri "/v1/draftsets" :request-method :post :params {:display-name display-name :description description}}))) - (defn- make-append-data-to-draftset-request [user draftset-location data-file-path] (with-open [fs (io/input-stream data-file-path)] (let [request (append-to-draftset-request user draftset-location fs "application/x-trig")] @@ -100,14 +94,6 @@ response (route request)] (tc/await-success finished-jobs (get-in response [:body :finished-job])))) -(def see-other-response-schema - (merge tc/ring-response-schema - {:status (s/eq 303) - :headers {(s/required-key "Location") s/Str}})) - -(defn assert-is-see-other-response [response] - (tc/assert-schema see-other-response-schema response)) - (def ^:private DraftsetWithoutTitleOrDescription {:id s/Str :changes {URI {:status (s/enum :created :updated :deleted)}} @@ -144,9 +130,9 @@ ([user] (create-draftset-through-api user nil)) ([user display-name] (create-draftset-through-api user display-name nil)) ([user display-name description] - (let [request (create-draftset-request user display-name description) + (let [request (create-test/create-draftset-request user display-name description) {:keys [headers] :as response} (route request)] - (assert-is-see-other-response response) + (create-test/assert-is-see-other-response response) (get headers "Location")))) (defn- get-draftset-quads-accept-request [draftset-location user accept union-with-live?-str] @@ -262,30 +248,9 @@ (tc/assert-is-ok-response response))) ;; define a local alternative to the route fixture wrapper -(defn valid-swagger-response? - "Applies handler to request and validates the response against the - swagger spec for the requested route. - - Returns the response if valid, otherwise raises an error." - [handler request] - (let [swagger-spec (swagger/load-spec-and-resolve-refs)] - (swagger/validate-response-against-swagger-spec swagger-spec request (handler request)))) - -(tc/deftest-system create-draftset-without-title-or-description - [{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"] - (let [request (tc/with-identity test-editor {:uri "/v1/draftsets" :request-method :post}) - response (valid-swagger-response? handler request)] - (assert-is-see-other-response response))) - -(tc/deftest-system create-draftset-with-title-and-without-description - [{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"] - (let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test Title!"))] - (assert-is-see-other-response response))) - -(tc/deftest-system create-draftset-with-title-and-description - [{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"] - (let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test title" "Test description"))] - (assert-is-see-other-response response))) + + + #_(defn get-draftsets-request [include user] (tc/with-identity user diff --git a/test/resources/web.edn b/test/resources/web.edn index 117663605..a2766e13a 100644 --- a/test/resources/web.edn +++ b/test/resources/web.edn @@ -29,7 +29,7 @@ :wrap-auth #ig/ref :drafter.middleware/wrap-auth} - :drafter.routes.draftsets-api/create-draftsets-handler {:drafter/backend #ig/ref :drafter/backend + :drafter.feature.draftset.create/handler {:drafter/backend #ig/ref :drafter/backend :wrap-auth #ig/ref :drafter.middleware/wrap-auth} :drafter.feature.middleware/wrap-as-draftset-owner {:drafter/backend #ig/ref :drafter/backend @@ -82,7 +82,7 @@ :drafter.routes/draftsets-api {:get-users-handler #ig/ref :drafter.feature.users.list/get-users-handler :get-draftsets-handler #ig/ref :drafter.feature.draftset.list/get-draftsets-handler - :create-draftsets-handler #ig/ref :drafter.routes.draftsets-api/create-draftsets-handler + :create-draftsets-handler #ig/ref :drafter.feature.draftset.create/handler :get-draftset-handler #ig/ref :drafter.routes.draftsets-api/get-draftset-handler :delete-draftset-handler #ig/ref :drafter.routes.draftsets-api/delete-draftset-handler :draftset-options-handler #ig/ref :drafter.routes.draftsets-api/draftset-options-handler