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

Support FCM changes #5411

Merged
merged 1 commit into from
Jul 31, 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
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=_("Copy the FCM authentication JSON file content to this field"),
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
Loading