Skip to content

Commit

Permalink
add more project tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgessler committed May 8, 2024
1 parent b044b80 commit 5ce36c9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/test/glam/server/rest_api/fixtures.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns glam.server.rest-api.fixtures
(:require [clojure.test :refer :all]
[glam.fixtures :refer [rest-handler]]
[glam.fixtures :refer [rest-handler xtdb-node]]
[glam.xtdb.easy :as gxe]
[ring.mock.request :as mock]))

(def user-cookie nil)
(def admin-cookie nil)
(def admin-id nil)
(def user-cookie nil)
(def user-id nil)

(defn get-session-cookie [resp]
(-> resp
Expand All @@ -30,7 +33,9 @@
b-cookie (get-session-cookie (rest-handler (-> (mock/request :post "/rest-api/v1/session/login")
(mock/json-body {:username "[email protected]" :password "fake-password2"}))))]
(with-redefs [admin-cookie a-cookie
user-cookie b-cookie]
user-cookie b-cookie
admin-id (-> (gxe/find-entity xtdb-node [[:user/name "[email protected]"]]) :user/id)
user-id (-> (gxe/find-entity xtdb-node [[:user/name "[email protected]"]]) :user/id)]
(f))))

(defn admin-req [method url]
Expand Down
47 changes: 42 additions & 5 deletions src/test/glam/server/rest_api/project_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[clojure.string]
[ring.mock.request :as mock]
[glam.xtdb.easy :as gxe]
[glam.server.rest-api.fixtures :refer [with-user-cookies admin-req user-req]]
[glam.server.rest-api.fixtures :refer [with-user-cookies
admin-req user-req
admin-id user-id]]
[glam.fixtures :refer [xtdb-node
with-xtdb
with-parser
Expand Down Expand Up @@ -36,15 +38,50 @@
(mock/json-body {:name "test-project2"}))
{:keys [status body]} (rest-handler req)
body (read-string (slurp body))]
(is (= 403 status))
(reset! prj-id (:id body))))
(is (= 403 status))))

(testing "Admin can see all projects"
(let [{:keys [status body]} (rest-handler (admin-req :get "/rest-api/v1/projects"))
body (read-string (slurp body))]
(is (= 1 (count body)))))
(is (= 1 (count body))))
(let [{:keys [status body]} (rest-handler (-> (admin-req :get (str "/rest-api/v1/project/" @prj-id))
(mock/query-string {:includeDocuments false})))]
(is (= 200 status))))

(testing "User sees no projects by default"
(let [{:keys [status body]} (rest-handler (user-req :get "/rest-api/v1/projects"))
body (read-string (slurp body))]
(is (= 0 (count body)))))))
(is (= 0 (count body))))
(let [{:keys [status body] :as resp} (rest-handler (-> (user-req :get (str "/rest-api/v1/project/" @prj-id))
(mock/query-string {:includeDocuments false})))]
(is (= 404 status))))

(testing "User can see a project after being granted read access"
(let [{:keys [status]} (rest-handler (-> (admin-req :patch (str "/rest-api/v1/admin/layers/project/" @prj-id))
(mock/json-body {:action "setPrivileges"
:userId user-id
:privileges "reader"})))]
(is (= 200 status)))
(let [{:keys [status body]} (rest-handler (user-req :get "/rest-api/v1/projects"))
body (read-string (slurp body))]
(is (= 200 status))
(is (= 1 (count body))))
(let [{:keys [status body]} (rest-handler (-> (user-req :get (str "/rest-api/v1/project/" @prj-id))
(mock/query-string {:includeDocuments false})))]
(is (= 200 status))))

(testing "User cannot delete projects"
(let [{:keys [status body]} (rest-handler (user-req :delete (str "/rest-api/v1/admin/layers/project/" @prj-id)))]
(is (= 403 status))))

(testing "Admin can delete projects"
(let [{:keys [status body]} (rest-handler (admin-req :delete (str "/rest-api/v1/admin/layers/project/" @prj-id)))]
(is (= 200 status)))
(let [{:keys [status body]} (rest-handler (admin-req :get "/rest-api/v1/projects"))
body (read-string (slurp body))]
(is (= 0 (count body))))
(let [{:keys [status body]} (rest-handler (-> (admin-req :get (str "/rest-api/v1/project/" @prj-id))
(mock/query-string {:includeDocuments false})))]
(is (= 404 status))))))

;; TODO: add some deletion tests to make sure we get everything

0 comments on commit 5ce36c9

Please sign in to comment.