Skip to content

Commit

Permalink
account: WIP implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Sep 5, 2024
1 parent f5378e9 commit 6f3bfe0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
25 changes: 23 additions & 2 deletions src/apossiblespace/parts/account.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
(ns apossiblespace.parts.account
(:require
[com.brunobonacci.mulog :as mulog]
[ring.util.response :as response]
[apossiblespace.parts.db :as db]
[apossiblespace.parts.auth :as auth]))

;; TODO: Should this be in a users namespace?
(defn- fetch-user
"Retrieve a user record from the database"
[id]
(db/query-one (db/sql-format {:select [:id :email :username :display_name :role]
:from [:users]
:where [:= :id id]})))

;; TODO: It would be good to have an api namespace to do things like handling
;; standard errors (404, 403, etc) and whatever else code ends up being
;; boilerplate.
(defn get-account
"Retrieve own account info"
[]
{:success "GET account"})
[request]
(mulog/log ::get-account :request request)
(let [user-id (get-in request [:identity :user-id])
user-record (fetch-user user-id)]
(if user-record
(-> (response/response user-record)
(response/status 200))
(-> (response/response {:error "User not found"})
(response/status 404)))))

(defn update-account
"Update own account info"
Expand All @@ -19,6 +38,7 @@
[confirm]
{:success "DELETE account"})

;; FIXME: This needs to be returning a Ring response!
(defn register-account
"Creates a record for a new user account"
[{:keys [email username] :as user-data}]
Expand All @@ -35,3 +55,4 @@
(mulog/log ::register :email email :username username :status :success)
(db/insert! :users (auth/prepare-user-record user-data))
{:success "User registered successfully"}))))

17 changes: 12 additions & 5 deletions test/apossiblespace/account_test.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
(ns apossiblespace.account-test
(:require [clojure.test :refer [deftest is testing use-fixtures]]
[apossiblespace.test-helpers :refer [with-test-db]]
[apossiblespace.test-helpers :refer [with-test-db register-test-user]]
[apossiblespace.test-factory :as factory]
[apossiblespace.parts.db :as db]
[apossiblespace.parts.auth :as auth]
[apossiblespace.parts.account :as account]))

(use-fixtures :once with-test-db)

;; TODO: Use register-test-user from the helpers
(deftest test-get-account
(testing "disallows access without a valid token" (is true))
(testing "allows access with a valid token" (is true))
(testing "returns correct user information" (is true)))
(testing "returns currently signed in user's information"
(let [user (register-test-user)
mock-request {:identity {:user-id (:id user)}}
response (account/get-account mock-request)]
(is (= 200 (:status response)))
(is (= {:email (:email user)
:username (:username user)
:display_name (:display_name user)
:role (:role user)
:id (:id user)} (:body response)))
(is (not (contains? response :password_hash))))))

(deftest test-update-account
(testing "disallows access without a valid token" (is true))
Expand Down

0 comments on commit 6f3bfe0

Please sign in to comment.