Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Sanitize tags for filtering providers in projects #730

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions client/src/planwise/client/projects2/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[planwise.client.effects :as effects]
[planwise.client.projects2.db :as db]
[planwise.client.utils :as utils]
[planwise.common :as common]
[clojure.string :as string]
[clojure.spec.alpha :as s]))


Expand Down Expand Up @@ -270,10 +272,12 @@
:projects2/save-tag
in-projects2
(fn [{:keys [db]} [_ tag]]
(let [path [:current-project :config :providers :tags]
n (count (get-in db path))]
{:db (update-in db path (comp vec conj) tag)
:dispatch [:projects2/persist-current-project]})))
(let [tag (common/sanitize-tag tag)
path [:current-project :config :providers :tags]
n (count (get-in db path))]
(when-not (string/blank? tag)
{:db (update-in db path (comp vec conj) tag)
:dispatch [:projects2/persist-current-project]}))))

(rf/reg-event-fx
:projects2/delete-tag
Expand Down
9 changes: 8 additions & 1 deletion common/src/planwise/common.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns planwise.common
(:require [clojure.string :refer [lower-case]]))
(:require [clojure.string :as string :refer [lower-case]]))

(defn is-budget
[analysis-type]
Expand Down Expand Up @@ -47,3 +47,10 @@
(get-capacity-unit project true))
([project lowercase?]
(get-project-unit project [:config :providers :capacity-unit] "units" lowercase?)))

(defn sanitize-tag
[tag]
(-> tag
string/trim
(string/replace #"\s+" "-")
(string/replace #"[^a-zA-Z0-9.-]" "")))
9 changes: 7 additions & 2 deletions src/planwise/component/providers_set.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[clojure.edn :as edn]
[planwise.util.files :as files]
[planwise.util.collections :refer [csv-data->maps]]
[planwise.common :as common]
[clojure.string :as str]
[clojure.set :as set]))

Expand Down Expand Up @@ -104,8 +105,10 @@
:version version
:region-id (:region-id filter-options)}
all-providers (db-find-providers-in-region db-spec config)
filter-tags (->> (:tags filter-options)
(map common/sanitize-tag))
providers-partition (group-by
#(provider-matches-tags? % (:tags filter-options))
#(provider-matches-tags? % filter-tags)
all-providers)]
{:providers (or (get providers-partition true) [])
:disabled-providers (or (get providers-partition false) [])}))
Expand All @@ -115,7 +118,9 @@
(count-providers-filter-by-tags store provider-set-id region-id tags nil))
([store provider-set-id region-id tags version]
(let [db-spec (get-db store)
tags (str/join " & " tags)
tags (->> tags
(map common/sanitize-tag)
(str/join " & "))
count-fn (fn [tags version]
(let [{:keys [last-version]} (get-provider-set store provider-set-id)]
(:count (db-count-providers-with-tags db-spec {:provider-set-id provider-set-id
Expand Down
12 changes: 10 additions & 2 deletions test/planwise/component/providers_set_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[planwise.component.providers-set :as providers-set]
[planwise.boundary.projects2 :as projects2]
[planwise.test-system :as test-system]
[planwise.common :as common]
[clj-time.core :as time]
[integrant.core :as ig])
(:import [org.postgis PGgeometry]))
Expand Down Expand Up @@ -159,7 +160,7 @@

(defn- validate-filter-count
[store id tags number]
(is (= (:filtered (providers-set/count-providers-filter-by-tags store id 1 tags)) number)))
(is (= number (:filtered (providers-set/count-providers-filter-by-tags store id 1 tags)))))

(deftest filtering-providers
(test-system/with-system (test-config fixture-filtering-providers-tags)
Expand All @@ -170,7 +171,14 @@
(validate-filter-count store 1 ["inexistent"] 0)
(validate-filter-count store 1 ["private"] 2)
(validate-filter-count store 2 ["private"] 0)
(validate-filter-count store 2 ["-"] 0))))
(validate-filter-count store 2 ["-"] 0)
;; sanitizes input tags
(validate-filter-count store 1 ["pri&vate"] 2))))

(deftest sanitize-tag
(is (= "" (common/sanitize-tag "&|")))
(is (= "general-medicine" (common/sanitize-tag "general medicine")))
(is (= "private" (common/sanitize-tag "pri&vate"))))

;; ----------------------------------------------------------------------
;; Testing deleting provider-set
Expand Down