mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 10:43:39 +02:00
Display api errors as notifications
This commit is contained in:
parent
7fd5e6189d
commit
aa04e68545
9 changed files with 118 additions and 61 deletions
|
|
@ -1,6 +1,7 @@
|
|||
(ns airsonic-ui.events-test
|
||||
(:require [cljs.test :refer [deftest testing is]]
|
||||
[clojure.string :as str]
|
||||
[airsonic-ui.fixtures :refer [responses]]
|
||||
[airsonic-ui.events :as events]))
|
||||
|
||||
(enable-console-print!)
|
||||
|
|
@ -16,8 +17,14 @@
|
|||
(testing "saves the given server location"
|
||||
(is (= server (get-in fx [:db :credentials :server]))))
|
||||
(testing "invokes correct success callback"
|
||||
(is (= ::events/credentials-verified (first (:on-success request)))))))
|
||||
(testing "On succesfull response"
|
||||
(is (= ::events/verify-auth-response (first (:on-success request)))))))
|
||||
(testing "Auth response verification"
|
||||
(is (= :notification/show
|
||||
(first (:dispatch (events/verify-auth-response {} [:_ "user" "pass" (:error responses)]))))
|
||||
"shows an error when we have an error response")
|
||||
(let [event (:dispatch (events/verify-auth-response {} [:_ "username" "password" (:auth-success responses)]))]
|
||||
(is (= [::events/credentials-verified "username" "password"] event))))
|
||||
(testing "On succesful response"
|
||||
(let [creds-before {:server "https://localhost"}
|
||||
fx (events/credentials-verified {:db {:credentials creds-before}}
|
||||
[:_ "user" "pass"])
|
||||
|
|
@ -47,6 +54,11 @@
|
|||
(defn- first-notification [db]
|
||||
(-> (:notifications db) vals first))
|
||||
|
||||
(deftest api-interaction
|
||||
(testing "Should show an error notification when airsonic responds with an error"
|
||||
(let [fx (events/good-api-response {} [:_ (:error responses)])]
|
||||
(is (= :error (-> fx :dispatch second))))))
|
||||
|
||||
(deftest user-notifications
|
||||
(testing "Should be able to display a message with an assigned level"
|
||||
(is (= :error (:level (first-notification (events/show-notification {} [:_ :error "foo"])))))
|
||||
|
|
|
|||
14
test/cljs/airsonic_ui/fixtures.cljs
Normal file
14
test/cljs/airsonic_ui/fixtures.cljs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(ns airsonic-ui.fixtures)
|
||||
|
||||
(def responses {:error {:subsonic-response
|
||||
{:error {:code 40
|
||||
:message "Wrong username or password"}
|
||||
:status "failed"
|
||||
:version "1.15.0"}}
|
||||
:ok {:subsonic-response
|
||||
{:scanStatus {:count 10326
|
||||
:scanning false}
|
||||
:status "ok"
|
||||
:version "1.15.0"}}
|
||||
:auth-success {:subsonic-response {:status "ok"
|
||||
:version "1.15.0"}}})
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
(ns airsonic-ui.utils.api-test
|
||||
(:require [cljs.test :refer [deftest testing is]]
|
||||
[clojure.string :as str]
|
||||
[airsonic-ui.fixtures :refer [responses]]
|
||||
[airsonic-ui.utils.api :as api]))
|
||||
|
||||
(defn- url
|
||||
|
|
@ -9,17 +10,7 @@
|
|||
(api/url server endpoint {}))
|
||||
|
||||
(def fixtures
|
||||
{:default-url (url "http://localhost:8080" "ping")
|
||||
:responses {:error {:subsonic-response
|
||||
{:error {:code 40
|
||||
:message "Wrong username or password"}
|
||||
:status "failed"
|
||||
:version "1.15.0"}}}
|
||||
:ok {:subsonic-response
|
||||
{:scanStatus {:count 10326
|
||||
:scanning false}
|
||||
:status "ok"
|
||||
:version "1.15.0"}}})
|
||||
{:default-url (url "http://localhost:8080" "ping")})
|
||||
|
||||
(deftest general-url-construction
|
||||
(testing "Handles missing slashes"
|
||||
|
|
@ -43,14 +34,14 @@
|
|||
|
||||
(deftest response-handling
|
||||
(testing "Should unwrap responses"
|
||||
(let [response (get-in fixtures [:responses :ok])]
|
||||
(let [response (:ok responses)]
|
||||
(is (= (get-in response [:subsonic-response :scanStatus])
|
||||
(api/unwrap-response response)))))
|
||||
(testing "Should detect errors"
|
||||
(is (true? (api/is-error? (get-in fixtures [:responses :error]))))
|
||||
(is (false? (api/is-error? (get-in fixtures [:responses :ok])))))
|
||||
(is (true? (api/is-error? (:error responses))))
|
||||
(is (false? (api/is-error? (:ok responses)))))
|
||||
(testing "Should throw an informative error when trying to unwrap an erroneous response"
|
||||
(let [error-response (get-in fixtures [:responses :error])]
|
||||
(let [error-response (:error responses)]
|
||||
(is (thrown? ExceptionInfo (api/unwrap-response error-response)))
|
||||
(try
|
||||
(api/unwrap-response error-response)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue