|
7 | 7 | (ns navi.impl-test |
8 | 8 | (:require |
9 | 9 | [clojure.test :refer [deftest is testing]] |
10 | | - [navi.impl :as i]) |
| 10 | + [navi.core :as navi] |
| 11 | + [navi.impl :as i] |
| 12 | + [clojure.java.io :as io]) |
11 | 13 | (:import |
12 | 14 | [clojure.lang ExceptionInfo] |
13 | 15 | [io.swagger.v3.oas.models Operation PathItem] |
|
142 | 144 | (is (= {:get {:handler "a handler" |
143 | 145 | :parameters {:path [:map [:x int?]]}}} |
144 | 146 | (i/path-item->data path-item handlers)))))) |
| 147 | +(defn find-route [rts path method] |
| 148 | + (some (fn [[p r]] |
| 149 | + (when (= p path) |
| 150 | + (get r method))) |
| 151 | + rts)) |
| 152 | + |
| 153 | +(deftest security-requirements-test |
| 154 | + (testing "Verifying security requirements from security-users.yml" |
| 155 | + ;; A dummy map of operationId to handler (the actual function doesn't matter for this test). |
| 156 | + (let [handlers {"listUsers" (constantly :ok) |
| 157 | + "listUsersSingle" (constantly :ok) |
| 158 | + "listUsersNoScope" (constantly :ok) |
| 159 | + "listUsersNoSecurity" (constantly :ok)} |
| 160 | + api-spec (slurp (io/resource "security-users.yml")) |
| 161 | + routes (navi/routes-from api-spec handlers)] |
| 162 | + |
| 163 | + (testing "multiple security schemes" |
| 164 | + (let [route (find-route routes "/users" :get)] |
| 165 | + (is (some? route) "Should have found /users GET route") |
| 166 | + (is (= [["sessionCookieAuth" ["read:user"]] |
| 167 | + ["test" ["one:two"]]] |
| 168 | + (:security route))))) |
| 169 | + |
| 170 | + (testing "single security scheme with scopes" |
| 171 | + (let [route (find-route routes "/users-single-scheme" :get)] |
| 172 | + (is (some? route) "Should have found /users-single-scheme GET route") |
| 173 | + (is (= [["sessionCookieAuth" ["read:user"]]] |
| 174 | + (:security route))))) |
| 175 | + |
| 176 | + (testing "single security scheme without scopes" |
| 177 | + (let [route (find-route routes "/users-no-scope" :get)] |
| 178 | + (is (some? route) "Should have found /users-no-scope GET route") |
| 179 | + (is (= [["sessionCookieAuth" []]] |
| 180 | + (:security route)) |
| 181 | + "No scopes should yield an empty vector for that scheme"))) |
| 182 | + |
| 183 | + (testing "no security block" |
| 184 | + (let [route (find-route routes "/users-no-security" :get)] |
| 185 | + (is (some? route) "Should have found /users-no-security GET route") |
| 186 | + (is (nil? (:security route)) |
| 187 | + "Route with no security block should not have :security key")))))) |
0 commit comments