Skip to content

Commit

Permalink
Support FCM changes
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jul 31, 2024
1 parent 33f9bc2 commit a23540b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
30 changes: 27 additions & 3 deletions temba/channels/types/firebase/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]))
Expand Down
22 changes: 19 additions & 3 deletions temba/channels/types/firebase/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
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


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"),
Expand All @@ -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
Expand Down

0 comments on commit a23540b

Please sign in to comment.