Skip to content

Commit fc046c4

Browse files
author
Roman Rudakov
authored
Merge pull request #20 from rrudakov/users-unit-tests
Add some unit tests for users API
2 parents ba5dc81 + cdfa969 commit fc046c4

28 files changed

+1215
-357
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Education API
22

33
![Clojure CI](https://github.com/rrudakov/ring_leart/workflows/Clojure%20CI/badge.svg?branch=master)
4+
[![codecov](https://codecov.io/gh/rrudakov/education_api/branch/master/graph/badge.svg)](https://codecov.io/gh/rrudakov/education_api)
45

56
A Clojure ring-based application.
67

@@ -10,7 +11,7 @@ It runs automatically on heroku.
1011

1112
## License
1213

13-
Copyright © 2020 FIXME
14+
Copyright © 2020 Roman Rudakov
1415

1516
This program and the accompanying materials are made available under the
1617
terms of the Eclipse Public License 2.0 which is available at

src/education/config.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
(ns education.config
22
(:require [aero.core :refer [read-config]]
3-
[buddy.auth :refer [throw-unauthorized]]
43
[buddy.auth.backends.token :refer [jws-backend]]
5-
[clojure.java.io :as io]
6-
[next.jdbc.result-set :as rs]))
4+
[clojure.java.io :as io]))
75

86
(defn config
97
"Read application config from resources."

src/education/database/component.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(ns education.database.component
22
(:require [com.stuartsierra.component :as component]
33
[education.config :as config]
4-
[next.jdbc :as j]
54
[next.jdbc.connection :as connection]
65
[ragtime.jdbc :as jdbc]
76
[ragtime.repl :as repl])

src/education/database/roles.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
(ns education.database.roles
22
(:require [clojure.string :as str]
3-
[next.jdbc.sql :as sql]
4-
[next.jdbc.result-set :as rs]
5-
[honeysql.core :as hsql]))
3+
[honeysql.core :as hsql]
4+
[next.jdbc.sql :as sql]))
65

76
(defn get-all-roles
87
"Fetch all roles from database."

src/education/database/users.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
(defn- enrich-user-with-roles
4040
"Fetch roles for `user` and assoc `:roles` key with them."
4141
[conn user]
42-
(->> user
43-
(roles/get-user-roles conn)
44-
(assoc user :users/roles)))
42+
(-> user
43+
(assoc :users/roles (roles/get-user-roles conn user))
44+
(dissoc :users/user_password)))
4545

4646
(defn get-user
4747
"Fetch user from database by `id`."
@@ -74,7 +74,7 @@
7474
(jdbc/with-transaction [tx conn]
7575
(let [{:keys [roles]} user
7676
new-roles (->> available-roles
77-
(filter #(some #{(:roles/role_name %)} roles))
77+
(filter #(some #{(:roles/role_name %)} (map keyword roles)))
7878
(map :roles/id))]
7979
(sql/delete! tx :user_roles {:user_id id})
8080
(sql/insert-multi! tx :user_roles

src/education/http/component.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[com.stuartsierra.component :as component]
44
[education.config :as config]
55
[education.http.routes :refer [api-routes]]
6+
[muuntaja.middleware :refer [wrap-format]]
67
[org.httpkit.server :as server]
78
[ring.middleware.cors :refer [wrap-cors]]
89
[ring.middleware.defaults :refer [api-defaults wrap-defaults]]))
@@ -13,7 +14,7 @@
1314
(start [this]
1415
(if srv
1516
this
16-
(let [port (config/application-port config)
17+
(let [port (config/application-port config)
1718
auth-backend (config/auth-backend config)]
1819
(println (str ";; Running web server at http://127.0.0.1:" port "/"))
1920
(assoc this :srv
@@ -25,7 +26,7 @@
2526
:access-control-allow-methods [:get :post :patch :put :delete])
2627
(wrap-authorization auth-backend)
2728
(wrap-authentication auth-backend)
28-
(muuntaja.middleware/wrap-format)
29+
(wrap-format)
2930
(wrap-defaults api-defaults))
3031
{:port port})))))
3132

src/education/http/endpoints/articles.clj

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
(ns education.http.endpoints.articles
22
(:require [compojure.api.sweet :refer [context DELETE GET PATCH POST]]
33
[education.database.articles :as articlesdb]
4-
[education.http.constants :refer :all]
4+
[education.http.constants :as const]
55
[education.http.restructure :refer [require-roles]]
66
[education.specs.articles :as specs]
77
[education.specs.error :as err]
8-
[ring.swagger.schema :refer [describe]]
9-
[ring.util.http-response
10-
:refer
11-
[created internal-server-error no-content not-found ok forbidden]]))
8+
[ring.util.http-response :as status]))
129

1310
;; Converters
1411
(defn to-short-article-response
@@ -40,18 +37,18 @@
4037
(fn [{:keys [identity]}]
4138
(let [user (:user identity)
4239
new-id (str (articlesdb/add-article db user article))]
43-
(created (str "/articles/" new-id) {:id new-id}))))
40+
(status/created (str "/articles/" new-id) {:id new-id}))))
4441

4542
(defn- update-article-handler
4643
"Update existing article handler."
4744
[db article-id article]
4845
(fn [{:keys [identity]}]
4946
(if (articlesdb/can-update? db (:user identity) article-id)
5047
(case (articlesdb/update-article db article-id article)
51-
1 (no-content)
52-
0 (not-found {:message not-found-error-message})
53-
(internal-server-error {:message server-error-message}))
54-
(forbidden {:message no-access-error-message}))))
48+
1 (status/no-content)
49+
0 (status/not-found {:message const/not-found-error-message})
50+
(status/internal-server-error {:message const/server-error-message}))
51+
(status/forbidden {:message const/no-access-error-message}))))
5552

5653
(defn- get-all-articles-handler
5754
"Get all recent articles handler."
@@ -61,49 +58,49 @@
6158
(articlesdb/get-user-articles db user-id limit))]
6259
(->> articles
6360
(map to-short-article-response)
64-
ok)))
61+
status/ok)))
6562

6663
(defn- get-latest-full-articles-handler
6764
"Get latest full articles handler."
6865
[db number]
6966
(let [articles (articlesdb/get-latest-full-sized-articles db number)]
7067
(->> articles
7168
(map to-full-article-response)
72-
ok)))
69+
status/ok)))
7370

7471
(defn- get-article-by-id-handler
7572
"Get article by `article-id` handler."
7673
[db article-id]
7774
(let [article (articlesdb/get-article-by-id db article-id)]
7875
(if (nil? article)
79-
(not-found {:message not-found-error-message})
80-
(ok (to-full-article-response article)))))
76+
(status/not-found {:message const/not-found-error-message})
77+
(status/ok (to-full-article-response article)))))
8178

8279
(defn- get-last-main-featured-article-handler
8380
"Get last main featured article handler."
8481
[db]
8582
(let [mf-article (articlesdb/get-last-featured-article db)]
8683
(if (nil? mf-article)
87-
(not-found {:message not-found-error-message})
88-
(ok (to-short-article-response mf-article)))))
84+
(status/not-found {:message const/not-found-error-message})
85+
(status/ok (to-short-article-response mf-article)))))
8986

9087
(defn- get-last-featured-articles-handler
9188
"Get last featured articles handler."
9289
[db limit]
9390
(->> (articlesdb/get-last-featured-articles db limit)
9491
(map to-short-article-response)
95-
ok))
92+
status/ok))
9693

9794
(defn- delete-article-handler
9895
"Delete article by `article-id` handler."
9996
[db article-id]
10097
(fn [{:keys [identity]}]
10198
(if (articlesdb/can-update? db (:user identity) article-id)
10299
(case (articlesdb/delete-article db article-id)
103-
1 (no-content)
104-
0 (not-found {:message not-found-error-message})
105-
(internal-server-error {:message server-error-message}))
106-
(forbidden {:message no-access-error-message}))))
100+
1 (status/no-content)
101+
0 (status/not-found {:message const/not-found-error-message})
102+
(status/internal-server-error {:message const/server-error-message}))
103+
(status/forbidden {:message const/no-access-error-message}))))
107104

108105
;; Define routes
109106
(defn articles-routes

src/education/http/endpoints/roles.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
(context "" []
2828
:tags ["roles"]
2929
(GET "/roles" []
30-
:middleware [[require-roles #{:admin}]]
31-
:return ::specs/roles-response
32-
:summary "Return list of all available roles"
33-
:responses {401 {:description "Access denied!"
34-
:schema ::err/error-response}}
35-
(roles-handler db))))
30+
:middleware [[require-roles #{:admin}]]
31+
:return ::specs/roles-response
32+
:summary "Return list of all available roles"
33+
:responses {401 {:description "Access denied!"
34+
:schema ::err/error-response}}
35+
(roles-handler db))))

0 commit comments

Comments
 (0)