mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 02:33:39 +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
|
$ 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
|
## Build artifacts
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,5 @@
|
||||||
(:require [airsonic-ui.routes :as routes]))
|
(:require [airsonic-ui.routes :as routes]))
|
||||||
|
|
||||||
(def default-db
|
(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]})
|
:current-route [routes/default-route]})
|
||||||
|
|
|
||||||
|
|
@ -18,31 +18,33 @@
|
||||||
(fn [_]
|
(fn [_]
|
||||||
db/default-db))
|
db/default-db))
|
||||||
|
|
||||||
;; this is called with user and password to try and see if the credentials are
|
;; auth logic
|
||||||
;; correct; if yes, ::auth-success will be fired
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(defn authenticate
|
||||||
::authenticate
|
"Tries to authenticate a user by pinging the server with credentials, saving
|
||||||
(fn [{:keys [db]} [_ user pass server]]
|
them when the request was succesful."
|
||||||
{:db (-> (update db :active-requests inc)
|
[{:keys [db]} [_ user pass server]]
|
||||||
(assoc :server server))
|
{:db (assoc db :server server)
|
||||||
:http-xhrio {:method :get
|
:http-xhrio {:method :get
|
||||||
:uri (api/url server "ping" {:u user :p pass})
|
:uri (api/url server "ping" {:u user :p pass})
|
||||||
:response-format (ajax/json-response-format {:keywords? true})
|
:response-format (ajax/json-response-format {:keywords? true})
|
||||||
:on-success [::auth-success user pass]
|
:on-success [::credentials-verified user pass]
|
||||||
:on-failure [::api-failure]}}))
|
:on-failure [::api-failure]}})
|
||||||
|
|
||||||
;; TODO: Test that credentials are associated
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::auth-success
|
::authenticate authenticate)
|
||||||
(fn [{:keys [db]} [_ user pass response]]
|
|
||||||
;; TODO: Handle failures differently
|
(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}]
|
(let [login {:u user :p pass}]
|
||||||
{:routes/set-credentials login
|
{:routes/set-credentials login
|
||||||
:db (-> (update db :active-requests #(max (dec %) 0))
|
:db (assoc db :login login)
|
||||||
(assoc :login login))
|
:dispatch [::logged-in]}))
|
||||||
:dispatch [::logged-in]})))
|
|
||||||
|
(re-frame/reg-event-fx
|
||||||
|
::credentials-verified credentials-verified)
|
||||||
|
|
||||||
;; TODO: We have to find another solution for this once we have routes that
|
;; TODO: We have to find another solution for this once we have routes that
|
||||||
;; don't require a login but have the bottom controls
|
;; don't require a login but have the bottom controls
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
(:require [re-frame.core :as re-frame]))
|
(:require [re-frame.core :as re-frame]))
|
||||||
|
|
||||||
;; can be used to query the user's credentials
|
;; 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
|
(re-frame/reg-sub
|
||||||
::login
|
::login
|
||||||
(fn [db]
|
(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