Skip to content

Commit

Permalink
logging: Add test for logging middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Sep 10, 2024
1 parent e0006bf commit f660e87
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/apossiblespace/parts/api/middleware_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns apossiblespace.parts.api.middleware-test
(:require [clojure.test :refer :all]
[apossiblespace.parts.api.middleware :as middleware]
[com.brunobonacci.mulog :as mulog]
[com.brunobonacci.mulog.buffer :as rb]
[ring.mock.request :as mock]
[reitit.ring :as ring])
(:import (org.sqlite SQLiteException SQLiteErrorCode)))
Expand Down Expand Up @@ -32,3 +34,19 @@
response (app request)]
(is (= 409 (:status response)))
(is (= {:error "A resource with this unique identifier already exists"} (:body response))))))

(deftest test-logging-middleware
(let [logged-entries (atom [])
handler (middleware/logging (fn [_request] {:status 200}))
request {:identity {:sub "user-123"}
:uri "/test"
:request-method :get}]
(with-redefs [mulog/log (fn [event-type & data]
(swap! logged-entries conj {:event-type event-type :data data}))]
(handler request)
(is (= 1 (count @logged-entries)))
(let [{:keys [event-type data]} (first @logged-entries)]
(is (= :apossiblespace.parts.api.middleware/request event-type))
(is (= request (:request (apply hash-map data))))
(is (= true (:authenticated? (apply hash-map data))))
(is (= "user-123" (:user-id (apply hash-map data))))))))

0 comments on commit f660e87

Please sign in to comment.