From a23540b833158bcc62d7cdd89e2779a2c6be8829 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Tue, 23 Jul 2024 13:26:49 +0000 Subject: [PATCH] Support FCM changes --- temba/channels/types/firebase/tests.py | 30 +++++++++++++++++++++++--- temba/channels/types/firebase/views.py | 22 ++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/temba/channels/types/firebase/tests.py b/temba/channels/types/firebase/tests.py index 663479c376d..c56ad808ab6 100644 --- a/temba/channels/types/firebase/tests.py +++ b/temba/channels/types/firebase/tests.py @@ -20,7 +20,7 @@ def setUp(self): address="87654", role="SR", schemes=["fcm"], - config={"FCM_TITLE": "Title", "FCM_KEY": "87654"}, + config={"FCM_TITLE": "Title", "FCM_CREDENTIALS_JSON": {"foo": "bar", "private_key_id": "87654"}}, ) @patch("requests.get") @@ -34,14 +34,38 @@ def test_claim(self, mock_get): self.assertContains(response, url) response = self.client.post( - url, {"title": "FCM Channel", "address": "abcde12345", "send_notification": "True"}, follow=True + url, + { + "title": "FCM Channel", + "authentication_json": '"foo" "bar", "baz": "abc", "private_key_id": "abcde12345"}', + "send_notification": "True", + }, + follow=True, + ) + self.assertFormError( + response.context["form"], None, "Invalid authentication JSON, missing private_key_id field" + ) + + response = self.client.post( + url, + { + "title": "FCM Channel", + "authentication_json": '{"foo": "bar", "baz": "abc", "private_key_id": "abcde12345"}', + "send_notification": "True", + }, + follow=True, ) channel = Channel.objects.get(address="abcde12345") self.assertRedirects(response, reverse("channels.channel_configuration", args=[channel.uuid])) self.assertEqual(channel.channel_type, "FCM") self.assertEqual( - channel.config, {"FCM_KEY": "abcde12345", "FCM_TITLE": "FCM Channel", "FCM_NOTIFICATION": True} + channel.config, + { + "FCM_CREDENTIALS_JSON": {"foo": "bar", "baz": "abc", "private_key_id": "abcde12345"}, + "FCM_TITLE": "FCM Channel", + "FCM_NOTIFICATION": True, + }, ) response = self.client.get(reverse("channels.channel_configuration", args=[channel.uuid])) diff --git a/temba/channels/types/firebase/views.py b/temba/channels/types/firebase/views.py index f54dc34c880..56a0ba0805c 100644 --- a/temba/channels/types/firebase/views.py +++ b/temba/channels/types/firebase/views.py @@ -3,6 +3,8 @@ from django import forms from django.utils.translation import gettext_lazy as _ +from temba.utils.fields import InputWidget + from ...models import Channel from ...views import ClaimViewMixin @@ -10,8 +12,11 @@ class ClaimView(ClaimViewMixin, SmartFormView): class Form(ClaimViewMixin.Form): title = forms.CharField(label=_("Notification Title")) - address = forms.CharField( - label=_("FCM Key"), help_text=_("The key provided on the the Firebase Console when you created your app.") + + authentication_json = forms.JSONField( + widget=InputWidget({"textarea": True}), + help_text=_("FCM autentication JSON"), + initial={}, ) send_notification = forms.CharField( label=_("Send notification"), @@ -20,12 +25,23 @@ class Form(ClaimViewMixin.Form): widget=forms.CheckboxInput(), ) + def clean(self): + authentication_json = self.cleaned_data.get("authentication_json", {}) + + self.cleaned_data["address"] = authentication_json.get("private_key_id", "") + + if not authentication_json or not self.cleaned_data["address"]: + raise forms.ValidationError(_("Invalid authentication JSON, missing private_key_id field")) + + return super().clean() + form_class = Form def form_valid(self, form): title = form.cleaned_data.get("title") + authentication_json = form.cleaned_data.get("authentication_json") address = form.cleaned_data.get("address") - config = {"FCM_TITLE": title, "FCM_KEY": address} + config = {"FCM_TITLE": title, "FCM_CREDENTIALS_JSON": authentication_json} if form.cleaned_data.get("send_notification") == "True": config["FCM_NOTIFICATION"] = True