Skip to content

Commit

Permalink
account: Fix in update handler
Browse files Browse the repository at this point in the history
Previously it would fail with 500 if after filtering no updateable
fields were left in the request.
  • Loading branch information
goshatch committed Sep 12, 2024
1 parent 676b130 commit a163625
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/apossiblespace/parts/account.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@
body (:body request)
update-data (-> body
remove-disallowed-update-fields
prepare-user-data)
updated-user (first (db/update! :users update-data [:= :id user-id]))]
(if updated-user
prepare-user-data)]
(if (= {} update-data)
(do
(mulog/log ::update-account-success :user-id user-id)
(-> (response/response (db/remove-sensitive-data updated-user))
(response/status 200)))
(do
(mulog/log ::update-account-not-found :user-id user-id)
(throw (ex-info "User not found" {:type :not-found}))))))
(mulog/log ::update-account-nothing-to-update :user-id user-id)
(throw (ex-info "Nothing to update" {:type :validation})))
(let [updated-user (first (db/update! :users update-data [:= :id user-id]))]
(if updated-user
(do
(mulog/log ::update-account-success :user-id user-id)
(-> (response/response (db/remove-sensitive-data updated-user))
(response/status 200)))
(do
(mulog/log ::update-account-not-found :user-id user-id)
(throw (ex-info "User not found" {:type :not-found}))))))))

(defn delete-account
"Delete own account"
Expand Down
7 changes: 6 additions & 1 deletion test/apossiblespace/parts/account_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
updated-fields)
(is (not (contains? (:body response) :password_hash))))))

(testing "returns updated user information" (is true)))
(testing "does not update where no updatable data is passed"
(let [user (register-test-user)
mock-request {:identity {:sub (:id user)}
:body {:username "something"}}]
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Nothing to update"
(account/update-account mock-request))))))

(deftest test-delete-account
(testing "does not delete without a confirmation param"
Expand Down

0 comments on commit a163625

Please sign in to comment.