Skip to content

Commit f604462

Browse files
authored
Merge pull request #2374 from akvo/fix/cascade-delete-ps-bookmark-entity-fkeys
fix: add ON DELETE CASCADE to plastic strategy bookmark entity foreign keys
2 parents 8c7c203 + c5b7570 commit f604462

3 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ALTER TABLE public.plastic_strategy_resource_bookmark
2+
DROP CONSTRAINT plastic_strategy_resource_bookmark_resource_id_fkey;
3+
4+
ALTER TABLE public.plastic_strategy_event_bookmark
5+
DROP CONSTRAINT plastic_strategy_event_bookmark_event_id_fkey;
6+
7+
ALTER TABLE public.plastic_strategy_policy_bookmark
8+
DROP CONSTRAINT plastic_strategy_policy_bookmark_policy_id_fkey;
9+
10+
ALTER TABLE public.plastic_strategy_technology_bookmark
11+
DROP CONSTRAINT plastic_strategy_technology_bookmark_technology_id_fkey;
12+
13+
ALTER TABLE public.plastic_strategy_initiative_bookmark
14+
DROP CONSTRAINT plastic_strategy_initiative_bookmark_initiative_id_fkey;
15+
16+
ALTER TABLE public.plastic_strategy_case_study_bookmark
17+
DROP CONSTRAINT plastic_strategy_case_study_bookmark_case_study_id_fkey;
18+
19+
ALTER TABLE public.plastic_strategy_organisation_bookmark
20+
DROP CONSTRAINT plastic_strategy_organisation_bookmark_organisation_id_fkey;
21+
22+
ALTER TABLE public.plastic_strategy_resource_bookmark
23+
ADD CONSTRAINT plastic_strategy_resource_bookmark_resource_id_fkey
24+
FOREIGN KEY (resource_id)
25+
REFERENCES public.resource(id);
26+
27+
ALTER TABLE public.plastic_strategy_event_bookmark
28+
ADD CONSTRAINT plastic_strategy_event_bookmark_event_id_fkey
29+
FOREIGN KEY (event_id)
30+
REFERENCES public.event(id);
31+
32+
ALTER TABLE public.plastic_strategy_policy_bookmark
33+
ADD CONSTRAINT plastic_strategy_policy_bookmark_policy_id_fkey
34+
FOREIGN KEY (policy_id)
35+
REFERENCES public.policy(id);
36+
37+
ALTER TABLE public.plastic_strategy_technology_bookmark
38+
ADD CONSTRAINT plastic_strategy_technology_bookmark_technology_id_fkey
39+
FOREIGN KEY (technology_id)
40+
REFERENCES public.technology(id);
41+
42+
ALTER TABLE public.plastic_strategy_initiative_bookmark
43+
ADD CONSTRAINT plastic_strategy_initiative_bookmark_initiative_id_fkey
44+
FOREIGN KEY (initiative_id)
45+
REFERENCES public.initiative(id);
46+
47+
ALTER TABLE public.plastic_strategy_case_study_bookmark
48+
ADD CONSTRAINT plastic_strategy_case_study_bookmark_case_study_id_fkey
49+
FOREIGN KEY (case_study_id)
50+
REFERENCES public.case_study(id);
51+
52+
ALTER TABLE public.plastic_strategy_organisation_bookmark
53+
ADD CONSTRAINT plastic_strategy_organisation_bookmark_organisation_id_fkey
54+
FOREIGN KEY (organisation_id)
55+
REFERENCES public.organisation(id);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
ALTER TABLE public.plastic_strategy_resource_bookmark
2+
DROP CONSTRAINT plastic_strategy_resource_bookmark_resource_id_fkey;
3+
4+
ALTER TABLE public.plastic_strategy_event_bookmark
5+
DROP CONSTRAINT plastic_strategy_event_bookmark_event_id_fkey;
6+
7+
ALTER TABLE public.plastic_strategy_policy_bookmark
8+
DROP CONSTRAINT plastic_strategy_policy_bookmark_policy_id_fkey;
9+
10+
ALTER TABLE public.plastic_strategy_technology_bookmark
11+
DROP CONSTRAINT plastic_strategy_technology_bookmark_technology_id_fkey;
12+
13+
ALTER TABLE public.plastic_strategy_initiative_bookmark
14+
DROP CONSTRAINT plastic_strategy_initiative_bookmark_initiative_id_fkey;
15+
16+
ALTER TABLE public.plastic_strategy_case_study_bookmark
17+
DROP CONSTRAINT plastic_strategy_case_study_bookmark_case_study_id_fkey;
18+
19+
ALTER TABLE public.plastic_strategy_organisation_bookmark
20+
DROP CONSTRAINT plastic_strategy_organisation_bookmark_organisation_id_fkey;
21+
22+
ALTER TABLE public.plastic_strategy_resource_bookmark
23+
ADD CONSTRAINT plastic_strategy_resource_bookmark_resource_id_fkey
24+
FOREIGN KEY (resource_id)
25+
REFERENCES public.resource(id)
26+
ON DELETE CASCADE;
27+
28+
ALTER TABLE public.plastic_strategy_event_bookmark
29+
ADD CONSTRAINT plastic_strategy_event_bookmark_event_id_fkey
30+
FOREIGN KEY (event_id)
31+
REFERENCES public.event(id)
32+
ON DELETE CASCADE;
33+
34+
ALTER TABLE public.plastic_strategy_policy_bookmark
35+
ADD CONSTRAINT plastic_strategy_policy_bookmark_policy_id_fkey
36+
FOREIGN KEY (policy_id)
37+
REFERENCES public.policy(id)
38+
ON DELETE CASCADE;
39+
40+
ALTER TABLE public.plastic_strategy_technology_bookmark
41+
ADD CONSTRAINT plastic_strategy_technology_bookmark_technology_id_fkey
42+
FOREIGN KEY (technology_id)
43+
REFERENCES public.technology(id)
44+
ON DELETE CASCADE;
45+
46+
ALTER TABLE public.plastic_strategy_initiative_bookmark
47+
ADD CONSTRAINT plastic_strategy_initiative_bookmark_initiative_id_fkey
48+
FOREIGN KEY (initiative_id)
49+
REFERENCES public.initiative(id)
50+
ON DELETE CASCADE;
51+
52+
ALTER TABLE public.plastic_strategy_case_study_bookmark
53+
ADD CONSTRAINT plastic_strategy_case_study_bookmark_case_study_id_fkey
54+
FOREIGN KEY (case_study_id)
55+
REFERENCES public.case_study(id)
56+
ON DELETE CASCADE;
57+
58+
ALTER TABLE public.plastic_strategy_organisation_bookmark
59+
ADD CONSTRAINT plastic_strategy_organisation_bookmark_organisation_id_fkey
60+
FOREIGN KEY (organisation_id)
61+
REFERENCES public.organisation(id)
62+
ON DELETE CASCADE;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)