mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-06 18:33:38 +02:00
Add tests for auth process
This commit is contained in:
parent
ca8972f8c3
commit
ed060e55b6
5 changed files with 54 additions and 23 deletions
|
|
@ -38,6 +38,8 @@ This project uses [karma](https://karma-runner.github.io/) for tests. Make sure
|
|||
$ npm test
|
||||
```
|
||||
|
||||
**Note:** If you want nice console output in your tests, make sure to `(enable-console-print!)`. You can call `println` afterwards like you're used to.
|
||||
|
||||
## Build artifacts
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,5 @@
|
|||
(:require [airsonic-ui.routes :as routes]))
|
||||
|
||||
(def default-db
|
||||
{:active-requests 0
|
||||
;; because navigate! executes asynchronously we force to display the login screen first
|
||||
{;; because navigate! executes asynchronously we force to display the login screen first
|
||||
:current-route [routes/default-route]})
|
||||
|
|
|
|||
|
|
@ -18,31 +18,33 @@
|
|||
(fn [_]
|
||||
db/default-db))
|
||||
|
||||
;; this is called with user and password to try and see if the credentials are
|
||||
;; correct; if yes, ::auth-success will be fired
|
||||
;; auth logic
|
||||
|
||||
(defn authenticate
|
||||
"Tries to authenticate a user by pinging the server with credentials, saving
|
||||
them when the request was succesful."
|
||||
[{:keys [db]} [_ user pass server]]
|
||||
{:db (assoc db :server server)
|
||||
:http-xhrio {:method :get
|
||||
:uri (api/url server "ping" {:u user :p pass})
|
||||
:response-format (ajax/json-response-format {:keywords? true})
|
||||
:on-success [::credentials-verified user pass]
|
||||
:on-failure [::api-failure]}})
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::authenticate
|
||||
(fn [{:keys [db]} [_ user pass server]]
|
||||
{:db (-> (update db :active-requests inc)
|
||||
(assoc :server server))
|
||||
:http-xhrio {:method :get
|
||||
:uri (api/url server "ping" {:u user :p pass})
|
||||
:response-format (ajax/json-response-format {:keywords? true})
|
||||
:on-success [::auth-success user pass]
|
||||
:on-failure [::api-failure]}}))
|
||||
::authenticate authenticate)
|
||||
|
||||
;; TODO: Test that credentials are associated
|
||||
(defn credentials-verified
|
||||
"Gets called after the server indicates that the credentials entered by a user
|
||||
are correct (see `authenticate`)."
|
||||
[{:keys [db]} [_ user pass response]]
|
||||
(let [login {:u user :p pass}]
|
||||
{:routes/set-credentials login
|
||||
:db (assoc db :login login)
|
||||
:dispatch [::logged-in]}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::auth-success
|
||||
(fn [{:keys [db]} [_ user pass response]]
|
||||
;; TODO: Handle failures differently
|
||||
(let [login {:u user :p pass}]
|
||||
{:routes/set-credentials login
|
||||
:db (-> (update db :active-requests #(max (dec %) 0))
|
||||
(assoc :login login))
|
||||
:dispatch [::logged-in]})))
|
||||
::credentials-verified credentials-verified)
|
||||
|
||||
;; TODO: We have to find another solution for this once we have routes that
|
||||
;; don't require a login but have the bottom controls
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
(:require [re-frame.core :as re-frame]))
|
||||
|
||||
;; can be used to query the user's credentials
|
||||
;; TODO: Organize login credentials and server location differently (i.e. together)
|
||||
|
||||
;; FIXME: this is used for cover images and it's quite ugly tbh
|
||||
(re-frame/reg-sub
|
||||
::login
|
||||
(fn [db]
|
||||
|
|
|
|||
28
test/cljs/airsonic_ui/events_test.cljs
Normal file
28
test/cljs/airsonic_ui/events_test.cljs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(ns airsonic-ui.events-test
|
||||
(:require [cljs.test :refer [deftest testing is]]
|
||||
[clojure.string :as str]
|
||||
[airsonic-ui.events :as events]))
|
||||
|
||||
(enable-console-print!)
|
||||
|
||||
(deftest authentication
|
||||
(testing "Credential verification"
|
||||
(let [server "https://localhost"
|
||||
fx (events/authenticate {:db {}} [:_ "user" "pass" server])
|
||||
request (:http-xhrio fx)]
|
||||
(testing "uses correct server url"
|
||||
(is (str/starts-with? (:uri request) server))
|
||||
(is (str/includes? (:uri request) "/ping")))
|
||||
(testing "saves the given server location"
|
||||
(is (= server (get-in fx [:db :server]))))
|
||||
(testing "invokes correct success callback"
|
||||
(is (= ::events/credentials-verified (first (:on-success request)))))))
|
||||
(testing "On succesfull response"
|
||||
(let [fx (events/credentials-verified {:db {}} [:_ "user" "pass"])
|
||||
credentials {:u "user" :p "pass"}]
|
||||
(testing "credentials are sent to the router for access rights"
|
||||
(is (= credentials (:routes/set-credentials fx))))
|
||||
(testing "credentials are saved in the global state"
|
||||
(is (= credentials (get-in fx [:db :login]))))
|
||||
(testing "the login process is finalized"
|
||||
(is (= [::events/logged-in] (:dispatch fx)))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue