diff --git a/src/apossiblespace/parts/account.clj b/src/apossiblespace/parts/account.clj index 71d93bc..0dcf417 100644 --- a/src/apossiblespace/parts/account.clj +++ b/src/apossiblespace/parts/account.clj @@ -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" @@ -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}] @@ -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"})))) + diff --git a/test/apossiblespace/account_test.clj b/test/apossiblespace/account_test.clj index b14d627..1344ee0 100644 --- a/test/apossiblespace/account_test.clj +++ b/test/apossiblespace/account_test.clj @@ -1,6 +1,6 @@ (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] @@ -8,11 +8,18 @@ (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))