mirror of
https://github.com/metosin/reitit.git
synced 2025-12-20 09:31:11 +00:00
Polish
This commit is contained in:
parent
7cd4c62216
commit
0de9e1b3c6
1 changed files with 29 additions and 32 deletions
|
|
@ -122,38 +122,35 @@
|
||||||
respond (partial reset! result), raise ::not-called]
|
respond (partial reset! result), raise ::not-called]
|
||||||
(app {:uri "/api/users" :request-method :post} respond raise)
|
(app {:uri "/api/users" :request-method :post} respond raise)
|
||||||
(is (= {:status 200, :body [:api :users :post :ok :post :users :api]}
|
(is (= {:status 200, :body [:api :users :post :ok :post :users :api]}
|
||||||
@result))))))
|
@result)))))))
|
||||||
|
|
||||||
(testing "runtime extensions for meta-data"
|
(defn wrap-enforce-roles [handler]
|
||||||
(let [enforce-roles (fn [handler]
|
(fn [{:keys [::roles] :as request}]
|
||||||
(fn [{:keys [::roles] :as request}]
|
(let [required (some-> request (ring/get-match) :meta ::roles)]
|
||||||
(let [required (some-> request
|
(if (and (seq required) (not (set/intersection required roles)))
|
||||||
(ring/get-match)
|
{:status 403, :body "forbidden"}
|
||||||
:meta
|
(handler request)))))
|
||||||
::roles)]
|
(deftest enforcing-meta-data-rules-at-runtime-test
|
||||||
(if (or (not (seq required))
|
(let [handler (constantly {:status 200, :body "ok"})
|
||||||
(set/intersection required roles))
|
app (ring/ring-handler
|
||||||
(handler request)
|
(ring/router
|
||||||
{:status 403 :body "forbidden"}))))
|
[["/api"
|
||||||
router (ring/router
|
["/ping" handler]
|
||||||
[["/api"
|
["/admin" {::roles #{:admin}}
|
||||||
["/ping" handler]
|
["/ping" handler]]]]
|
||||||
["/admin" {::roles #{:admin}}
|
{:meta {:middleware [wrap-enforce-roles]}}))]
|
||||||
["/ping" handler]]]]
|
|
||||||
{:meta {:middleware [enforce-roles]}})
|
|
||||||
app (ring/ring-handler router)]
|
|
||||||
|
|
||||||
(testing "public handler"
|
(testing "public handler"
|
||||||
(is (= {:status 200, :body [:ok]}
|
(is (= {:status 200, :body "ok"}
|
||||||
(app {:uri "/api/ping" :request-method :get}))))
|
(app {:uri "/api/ping" :request-method :get}))))
|
||||||
|
|
||||||
(testing "runtime-enforced handler"
|
(testing "runtime-enforced handler"
|
||||||
(testing "without needed roles"
|
(testing "without needed roles"
|
||||||
(is (= {:status 403 :body "forbidden"}
|
(is (= {:status 403 :body "forbidden"}
|
||||||
(app {:uri "/api/admin/ping"
|
(app {:uri "/api/admin/ping"
|
||||||
:request-method :get}))))
|
:request-method :get}))))
|
||||||
(testing "with needed roles"
|
(testing "with needed roles"
|
||||||
(is (= {:status 200, :body [:ok]}
|
(is (= {:status 200, :body "ok"}
|
||||||
(app {:uri "/api/admin/ping"
|
(app {:uri "/api/admin/ping"
|
||||||
:request-method :get
|
:request-method :get
|
||||||
::roles #{:admin}}))))))))
|
::roles #{:admin}})))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue