|
| 1 | +(ns gpml.handler.detail-delete-test |
| 2 | + (:require |
| 3 | + [clojure.java.jdbc :as jdbc] |
| 4 | + [clojure.test :refer [deftest is testing use-fixtures]] |
| 5 | + [gpml.db.country :as db.country] |
| 6 | + [gpml.db.event :as db.event] |
| 7 | + [gpml.db.plastic-strategy :as db.plastic-strategy] |
| 8 | + [gpml.db.resource :as db.resource] |
| 9 | + [gpml.fixtures :as fixtures] |
| 10 | + [gpml.handler.detail :as detail] |
| 11 | + [gpml.service.permissions :as srv.permissions] |
| 12 | + [gpml.test-util :as test-util] |
| 13 | + [integrant.core :as ig] |
| 14 | + [ring.mock.request :as mock])) |
| 15 | + |
| 16 | +(use-fixtures :each fixtures/with-test-system) |
| 17 | + |
| 18 | +(def ^:private resource-data |
| 19 | + {:title "Test Resource" |
| 20 | + :type "Financing Resource" |
| 21 | + :publish_year 2021 |
| 22 | + :summary "Test Summary" |
| 23 | + :valid_from "2021-01-01" |
| 24 | + :valid_to "2021-12-31" |
| 25 | + :geo_coverage_type "global" |
| 26 | + :language "en" |
| 27 | + :document_preview false}) |
| 28 | + |
| 29 | +(def ^:private event-data |
| 30 | + {:title "Test Event" |
| 31 | + :description "Test Event Description" |
| 32 | + :start_date "2021-04-01" |
| 33 | + :end_date "2021-04-01" |
| 34 | + :city "Amsterdam" |
| 35 | + :country nil |
| 36 | + :geo_coverage_type "global" |
| 37 | + :remarks nil |
| 38 | + :document_preview false |
| 39 | + :review_status nil |
| 40 | + :tags nil |
| 41 | + :language "en"}) |
| 42 | + |
| 43 | +(deftest delete-resource-with-plastic-strategy-bookmark-test |
| 44 | + (let [system (ig/init fixtures/*system* [::detail/delete]) |
| 45 | + config (get system [:duct/const :gpml.config/common]) |
| 46 | + conn (get-in config [:db :spec]) |
| 47 | + handler (::detail/delete system) |
| 48 | + admin-id (test-util/create-test-stakeholder config |
| 49 | + "admin@mail.invalid" |
| 50 | + "APPROVED" |
| 51 | + "ADMIN")] |
| 52 | + |
| 53 | + (testing "Deleting a resource with plastic strategy bookmarks succeeds" |
| 54 | + (let [country (db.country/new-country conn {:name "Test Country" |
| 55 | + :iso_code_a3 "TST" |
| 56 | + :description "Test Country"}) |
| 57 | + ps-result (db.plastic-strategy/create-plastic-strategy conn {:country-id (:id country)}) |
| 58 | + ps-id (:id ps-result) |
| 59 | + resource (db.resource/new-resource conn resource-data) |
| 60 | + resource-id (:id resource) |
| 61 | + _ (srv.permissions/create-resource-context {:conn conn |
| 62 | + :logger (:logger config)} |
| 63 | + {:context-type :resource |
| 64 | + :resource-id resource-id}) |
| 65 | + _ (jdbc/insert! conn :plastic_strategy_resource_bookmark |
| 66 | + {:plastic_strategy_id ps-id |
| 67 | + :resource_id resource-id |
| 68 | + :section_key "test-section"}) |
| 69 | + resp (handler (-> (mock/request :delete "/") |
| 70 | + (assoc :user {:id admin-id} |
| 71 | + :parameters |
| 72 | + {:path {:topic-type "resource" |
| 73 | + :topic-id resource-id}})))] |
| 74 | + (is (= 200 (:status resp))) |
| 75 | + (is (empty? (jdbc/query conn |
| 76 | + ["SELECT * FROM resource WHERE id = ?" resource-id]))) |
| 77 | + (is (empty? (jdbc/query conn |
| 78 | + ["SELECT * FROM plastic_strategy_resource_bookmark WHERE resource_id = ?" resource-id]))))) |
| 79 | + |
| 80 | + (testing "Deleting an event with plastic strategy bookmarks succeeds" |
| 81 | + (let [country (db.country/new-country conn {:name "Test Country 2" |
| 82 | + :iso_code_a3 "TS2" |
| 83 | + :description "Test Country 2"}) |
| 84 | + ps-result (db.plastic-strategy/create-plastic-strategy conn {:country-id (:id country)}) |
| 85 | + ps-id (:id ps-result) |
| 86 | + event (db.event/new-event conn event-data) |
| 87 | + event-id (:id event) |
| 88 | + _ (srv.permissions/create-resource-context {:conn conn |
| 89 | + :logger (:logger config)} |
| 90 | + {:context-type :event |
| 91 | + :resource-id event-id}) |
| 92 | + _ (jdbc/insert! conn :plastic_strategy_event_bookmark |
| 93 | + {:plastic_strategy_id ps-id |
| 94 | + :event_id event-id |
| 95 | + :section_key "test-section"}) |
| 96 | + resp (handler (-> (mock/request :delete "/") |
| 97 | + (assoc :user {:id admin-id} |
| 98 | + :parameters |
| 99 | + {:path {:topic-type "event" |
| 100 | + :topic-id event-id}})))] |
| 101 | + (is (= 200 (:status resp))) |
| 102 | + (is (empty? (jdbc/query conn |
| 103 | + ["SELECT * FROM event WHERE id = ?" event-id]))) |
| 104 | + (is (empty? (jdbc/query conn |
| 105 | + ["SELECT * FROM plastic_strategy_event_bookmark WHERE event_id = ?" event-id]))))) |
| 106 | + |
| 107 | + (testing "Unauthorized user cannot delete resource" |
| 108 | + (let [user-id (test-util/create-test-stakeholder config |
| 109 | + "user@mail.invalid" |
| 110 | + "APPROVED" |
| 111 | + "USER") |
| 112 | + resource (db.resource/new-resource conn (assoc resource-data :title "Another Resource")) |
| 113 | + resource-id (:id resource) |
| 114 | + _ (srv.permissions/create-resource-context {:conn conn |
| 115 | + :logger (:logger config)} |
| 116 | + {:context-type :resource |
| 117 | + :resource-id resource-id}) |
| 118 | + resp (handler (-> (mock/request :delete "/") |
| 119 | + (assoc :user {:id user-id} |
| 120 | + :parameters |
| 121 | + {:path {:topic-type "resource" |
| 122 | + :topic-id resource-id}})))] |
| 123 | + (is (= 403 (:status resp))))))) |
0 commit comments