|
| 1 | +(ns parts.routes-test |
| 2 | + (:require |
| 3 | + [clojure.test :refer [deftest is testing use-fixtures]] |
| 4 | + [muuntaja.core :as m] |
| 5 | + [parts.api.auth :as auth] |
| 6 | + [parts.helpers.utils :refer [with-test-db register-test-user]] |
| 7 | + [parts.helpers.test-factory :as factory] |
| 8 | + [parts.entity.user :as user] |
| 9 | + [parts.routes :as routes] |
| 10 | + [reitit.ring :as ring] |
| 11 | + [ring.mock.request :as mock] |
| 12 | + [cognitect.transit :as transit]) |
| 13 | + (:import |
| 14 | + [java.io ByteArrayInputStream ByteArrayOutputStream])) |
| 15 | + |
| 16 | +(use-fixtures :once with-test-db) |
| 17 | + |
| 18 | +(defn- parse-transit [body] |
| 19 | + (let [in (ByteArrayInputStream. (.getBytes body)) |
| 20 | + reader (transit/reader in :json)] |
| 21 | + (transit/read reader))) |
| 22 | + |
| 23 | +(defn- create-app |
| 24 | + "Creates a test app with all middleware configured as in production" |
| 25 | + [] |
| 26 | + (ring/ring-handler |
| 27 | + (ring/router routes/routes))) |
| 28 | + |
| 29 | +(deftest test-login-handler |
| 30 | + (testing "login handler with middleware stack" |
| 31 | + (let [app (create-app) |
| 32 | + user-data (factory/create-test-user) |
| 33 | + {:keys [email password]} user-data] |
| 34 | + |
| 35 | + ;; First create the user |
| 36 | + (user/create! user-data) |
| 37 | + |
| 38 | + ;; Create login request with transit format |
| 39 | + (let [request (-> (mock/request :post "/api/auth/login") |
| 40 | + (mock/json-body {:email email :password password}) |
| 41 | + (mock/header "Content-Type" "application/transit+json") |
| 42 | + (mock/header "Accept" "application/transit+json")) |
| 43 | + response (app request) |
| 44 | + body (parse-transit (:body response))] |
| 45 | + |
| 46 | + ;; Test response status, headers and content |
| 47 | + (is (= 200 (:status response))) |
| 48 | + (is (= "application/transit+json; charset=utf-8" |
| 49 | + (get-in response [:headers "Content-Type"]))) |
| 50 | + (is (:access_token body)) |
| 51 | + (is (:refresh_token body)) |
| 52 | + (is (= "Bearer" (:token_type body))))))) |
| 53 | + |
| 54 | +(deftest test-unauthorized-access |
| 55 | + (testing "protected endpoints require authentication" |
| 56 | + (let [app (create-app) |
| 57 | + request (-> (mock/request :get "/api/account") |
| 58 | + (mock/header "Accept" "application/transit+json")) |
| 59 | + response (app request) |
| 60 | + body (parse-transit (:body response))] |
| 61 | + |
| 62 | + (is (= 401 (:status response))) |
| 63 | + (is (= "application/transit+json; charset=utf-8" |
| 64 | + (get-in response [:headers "Content-Type"]))) |
| 65 | + (is (= "Unauthorized" (:error body)))))) |
| 66 | + |
| 67 | +(deftest test-static-resource-content-type |
| 68 | + (testing "SVG files are served with correct content type" |
| 69 | + (let [app (create-app) |
| 70 | + request (mock/request :get "/images/parts-logo-horizontal.svg") |
| 71 | + response (app request)] |
| 72 | + |
| 73 | + (is (= 200 (:status response))) |
| 74 | + (is (= "image/svg+xml" |
| 75 | + (get-in response [:headers "Content-Type"])))))) |
0 commit comments