diff --git a/backend/src/app/rpc/commands/webhooks.clj b/backend/src/app/rpc/commands/webhooks.clj index 33341bb34e..867ac46903 100644 --- a/backend/src/app/rpc/commands/webhooks.clj +++ b/backend/src/app/rpc/commands/webhooks.clj @@ -120,7 +120,7 @@ {::doc/added "1.17" ::sm/params schema:create-webhook} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id] :as params}] - (check-webhook-edition-permissions! pool profile-id team-id profile-id) + (t/check-edition-permissions! pool profile-id team-id) (validate-quotes! cfg params) (validate-webhook! cfg nil params) (insert-webhook! cfg params)) diff --git a/backend/test/backend_tests/rpc_webhooks_test.clj b/backend/test/backend_tests/rpc_webhooks_test.clj index 3b39c8b52d..d05743c8a1 100644 --- a/backend/test/backend_tests/rpc_webhooks_test.clj +++ b/backend/test/backend_tests/rpc_webhooks_test.clj @@ -155,8 +155,7 @@ :return {:status 200}}] (let [owner (th/create-profile* 1 {:is-active true}) viewer (th/create-profile* 2 {:is-active true}) - team (th/create-team* 1 {:profile-id (:id owner)}) - whook (volatile! nil)] + team (th/create-team* 1 {:profile-id (:id owner)})] (th/create-team-role* {:team-id (:id team) :profile-id (:id viewer) :role :viewer}) @@ -164,52 +163,15 @@ (let [roles (th/db-query :team-profile-rel {:team-id (:id team)})] (t/is (= 2 (count roles)))) - (t/testing "viewer creates a webhook" + (t/testing "viewer cannot create a webhook (requires editor role)" (let [viewers-webhook (create-webhook-params (:id viewer) (:id team)) out (th/command! viewers-webhook)] - (t/is (nil? (:error out))) - (t/is (= 1 (:call-count @http-mock))) - - (let [result (:result out)] - (check-webhook-format result) - (t/is (= (:uri viewers-webhook) (:uri result))) - (t/is (= (:team-id viewers-webhook) (:team-id result))) - (t/is (= (::rpc/profile-id viewers-webhook) (:profile-id result))) - (t/is (= (:mtype viewers-webhook) (:mtype result))) - (vreset! whook result)))) - - (th/reset-mock! http-mock) - - (t/testing "viewer updates it's own webhook (success)" - (let [params {::th/type :update-webhook - ::rpc/profile-id (:id viewer) - :id (:id @whook) - :uri (:uri @whook) - :mtype "application/transit+json" - :is-active false} - out (th/command! params) - result (:result out)] - - (t/is (nil? (:error out))) (t/is (= 0 (:call-count @http-mock))) - (check-webhook-format result) - (t/is (= (:is-active params) (:is-active result))) - (t/is (= (:team-id @whook) (:team-id result))) - (t/is (= (:mtype params) (:mtype result))) - (vreset! whook result))) - - (th/reset-mock! http-mock) - - (t/testing "viewer deletes it's own webhook (success)" - (let [params {::th/type :delete-webhook - ::rpc/profile-id (:id viewer) - :id (:id @whook)} - out (th/command! params)] - (t/is (= 0 (:call-count @http-mock))) - (t/is (nil? (:error out))) - (t/is (nil? (:result out))) - (let [rows (th/db-exec! ["select * from webhook"])] - (t/is (= 0 (count rows)))))) + (let [error (:error out) + error-data (ex-data error)] + (t/is (th/ex-info? error)) + (t/is (= (:type error-data) :not-found)) + (t/is (= (:code error-data) :object-not-found))))) (th/reset-mock! http-mock)))) @@ -268,6 +230,26 @@ (t/is (= (:type error-data) :not-found)) (t/is (= (:code error-data) :object-not-found))))))) +(t/deftest webhooks-viewer-cannot-create + (with-mocks [http-mock {:target 'app.http.client/req + :return {:status 200}}] + (let [owner (th/create-profile* 1 {:is-active true}) + viewer (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)})] + (th/create-team-role* {:team-id (:id team) + :profile-id (:id viewer) + :role :viewer}) + + (t/testing "viewer cannot create a webhook on the team" + (let [params (create-webhook-params (:id viewer) (:id team)) + out (th/command! params)] + (t/is (= 0 (:call-count @http-mock))) + (let [error (:error out) + error-data (ex-data error)] + (t/is (th/ex-info? error)) + (t/is (= (:type error-data) :not-found)) + (t/is (= (:code error-data) :object-not-found)))))))) + (t/deftest webhooks-quotes (with-mocks [http-mock {:target 'app.http.client/req :return {:status 200}}]