Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-3291: drop creator from the create/send notification request schema (#83555)

Anna Tomka762512b71d89
diff --git a/frontend/src/metabase/api/notification.ts b/frontend/src/metabase/api/notification.ts
index aceda51daba..7efaef473e6 100644
--- a/frontend/src/metabase/api/notification.ts
+++ b/frontend/src/metabase/api/notification.ts
@@ -1,3 +1,5 @@
+import _ from "underscore";
+
import type {
AdminNotificationDetail,
AdminNotificationListParams,
@@ -97,7 +99,9 @@ export const notificationApi = Api.injectEndpoints({
query: (body) => ({
method: "POST",
url: `/api/notification/send`,
- body,
+ // In edit mode the alert modal passes the full Notification it fetched,
+ // including the server-managed `creator`, which the endpoint ignores.
+ body: _.omit(body, "creator"),
}),
}),
adminListNotifications: builder.query<
diff --git a/frontend/src/metabase/notifications/modals/CreateOrEditQuestionAlertModal/CreateOrEditQuestionAlertModal.unit.spec.tsx b/frontend/src/metabase/notifications/modals/CreateOrEditQuestionAlertModal/CreateOrEditQuestionAlertModal.unit.spec.tsx
index 84d30b4391f..fc89b843ee2 100644
--- a/frontend/src/metabase/notifications/modals/CreateOrEditQuestionAlertModal/CreateOrEditQuestionAlertModal.unit.spec.tsx
+++ b/frontend/src/metabase/notifications/modals/CreateOrEditQuestionAlertModal/CreateOrEditQuestionAlertModal.unit.spec.tsx
@@ -648,6 +648,32 @@ describe("CreateOrEditQuestionAlertModal", () => {
expect(JSON.parse(String(requestBody)).creator_id).toBe(7);
});
});
+
+ it("should not send the creator on 'Send now' in edit mode", async () => {
+ fetchMock.postOnce("path:/api/notification/send", 204);
+
+ setup({
+ isAdmin: true,
+ isEmailSetup: true,
+ editingNotification: createMockNotification(),
+ });
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: /send now/i }),
+ );
+
+ await waitFor(() => {
+ expect(
+ fetchMock.callHistory.calls("path:/api/notification/send"),
+ ).toHaveLength(1);
+ });
+
+ const [call] = fetchMock.callHistory.calls("path:/api/notification/send");
+ const requestBody = JSON.parse(String(call.options?.body));
+
+ expect(requestBody).not.toHaveProperty("creator");
+ expect(requestBody).toHaveProperty("handlers");
+ });
});
function setup({
diff --git a/src/metabase/notification/models.clj b/src/metabase/notification/models.clj
index e48dfe168a5..ed19d2dcfcf 100644
--- a/src/metabase/notification/models.clj
+++ b/src/metabase/notification/models.clj
@@ -804,13 +804,14 @@
(hydrated-notification-schema handler-schema {:with-id? true}))
([handler-schema {:keys [with-id? update-input?] :as opts}]
(let [entries (into (notification-entries opts)
- (cond->> [;; the hydrated User, echoed back by clients on update; `:creator_id`, declared by
- [:creator {:optional true} [:maybe ::EchoedUser]]
- [:subscriptions {:optional true} [:sequential [:ref (if with-id?
+ (cond->> [[:subscriptions {:optional true} [:sequential [:ref (if with-id?
::NotificationSubscription
::CreateNotificationSubscriptionParams)]]]
[:handlers {:optional true} [:sequential handler-schema]]]
- with-id? (into [[:payload_id {:optional true} [:maybe int?]]
+ with-id? (into [;; the hydrated User a saved notification is read with. Create and send
+ ;; requests don't declare it, so the API drops a client-sent copy.
+ [:creator {:optional true} [:maybe ::EchoedUser]]
+ [:payload_id {:optional true} [:maybe int?]]
[:triggering_subscription {:optional true} [:maybe ::NotificationSubscription]]])))
entries (cond-> entries
update-input? (update-input-entries notification-update-spec))]
diff --git a/test/metabase/notification/api/notification_test.clj b/test/metabase/notification/api/notification_test.clj
index 1f5036834bd..e149e13d42e 100644
--- a/test/metabase/notification/api/notification_test.clj
+++ b/test/metabase/notification/api/notification_test.clj
@@ -1395,3 +1395,19 @@
(testing "success if recipients matches allowed domains"
(mt/user-http-request :crowberto :post 204 "notification/send"
(assoc notification :handlers success-handlers)))))))))))
+
+(deftest send-unsaved-notification-echoed-creator-test
+ (testing "POST /api/notification/send accepts a saved notification sent back as fetched, creator included"
+ (notification.tu/with-card-notification
+ [{notification-id :id}
+ {:handlers [{:channel_type :channel/email
+ :recipients [{:type :notification-recipient/user
+ :user_id (mt/user->id :crowberto)}]}]
+ :subscriptions [{:type :notification-subscription/cron
+ :cron_schedule "0 0 0 * * ?"}]}]
+ (notification.tu/with-channel-fixtures [:channel/email]
+ (let [echoed (mt/user-http-request :crowberto :get 200 (format "notification/%d" notification-id))]
+ (is (some? (get-in echoed [:creator :date_joined])))
+ (is (=? {:channel/email [{:recipients ["[email protected]"]}]}
+ (notification.tu/with-captured-channel-send!
+ (mt/user-http-request :crowberto :post 204 "notification/send" echoed)))))))))