From 74668551a7bf6a882f49c3e4c811c3169163606b Mon Sep 17 00:00:00 2001 From: "carson.wang" Date: Fri, 14 Aug 2020 18:39:19 +0800 Subject: [PATCH] Add tests & Doesn't get LineNotify token using setting --- hc/api/transports.py | 6 ++-- hc/front/forms.py | 7 +++-- hc/front/tests/test_add_linenotify.py | 27 +++++++++++++++-- hc/front/views.py | 42 +++++++++++++-------------- hc/settings.py | 3 -- templates/front/channels.html | 2 -- templates/front/welcome.html | 2 -- 7 files changed, 53 insertions(+), 36 deletions(-) diff --git a/hc/api/transports.py b/hc/api/transports.py index 3aa36e8d..0b673aeb 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -626,10 +626,10 @@ class LineNotify(HttpTransport): def notify(self, check): headers = { - "Content-Type": "multipart/form-data", - "Authorization": "Bearer %s" % settings.LINE_NOTIFY_ACCESS_TOKEN, + "Content-Type": "application/x-www-form-urlencoded", + "Authorization": "Bearer %s" % self.channel.linenotify_token, } payload = { "message": tmpl("linenotify_message.html", check=check) } - return self.post(URL, headers=headers, params=payload) + return self.post(self.URL, headers=headers, params=payload) diff --git a/hc/front/forms.py b/hc/front/forms.py index 8aac9966..f569c874 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -104,8 +104,6 @@ class AddOpsGenieForm(forms.Form): region = forms.ChoiceField(initial="us", choices=(("us", "US"), ("eu", "EU"))) key = forms.CharField(max_length=40) -class AddLineNotifyForm(forms.Form): - token = forms.CharField(max_length=40) PRIO_CHOICES = [ ("-2", "Lowest Priority"), @@ -261,3 +259,8 @@ class AddZulipForm(forms.Form): def get_value(self): return json.dumps(dict(self.cleaned_data), sort_keys=True) + + +class AddLineNotifyForm(forms.Form): + error_css_class = "has-error" + token = forms.CharField(max_length=50) diff --git a/hc/front/tests/test_add_linenotify.py b/hc/front/tests/test_add_linenotify.py index f8777bb2..aecaa711 100644 --- a/hc/front/tests/test_add_linenotify.py +++ b/hc/front/tests/test_add_linenotify.py @@ -1,8 +1,6 @@ -from django.test.utils import override_settings from hc.api.models import Channel from hc.test import BaseTestCase -@override_settings(LINE_NOTIFY_ACCESS_TOKEN="foo") class AddLineNotifyTestCase(BaseTestCase): url = "/integrations/add_linenotify/" @@ -14,3 +12,28 @@ class AddLineNotifyTestCase(BaseTestCase): self.client.login(username="alice@example.org", password="password") r = self.client.get(self.url) self.assertContains(r, "LineNotify") + + def test_it_works(self): + form = {"token": "helloworld"} + + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.url, form) + self.assertRedirects(r, self.channels_url) + + c = Channel.objects.get() + self.assertEqual(c.kind, "linenotify") + self.assertEqual(c.value, "helloworld") + self.assertEqual(c.project, self.project) + + def test_it_handles_json_linenotify_value(self): + c = Channel(kind="linenotify", value="foo123") + self.assertEqual(c.linenotify_token, "foo123") + + def test_it_save_token(self): + form = {"token": "foo123"} + + self.client.login(username="alice@example.org", password="password") + self.client.post(self.url, form) + + c = Channel.objects.get() + self.assertEqual(c.value, "foo123") diff --git a/hc/front/views.py b/hc/front/views.py index 6e1e855d..4ca448cc 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -273,7 +273,6 @@ def index(request): "enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_whatsapp": settings.TWILIO_USE_WHATSAPP, - "enable_linenotify": settings.LINE_NOTIFY_ACCESS_TOKEN is not None, "registration_open": settings.REGISTRATION_OPEN, } @@ -712,7 +711,6 @@ def channels(request, code): "enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_whatsapp": settings.TWILIO_USE_WHATSAPP, - "enable_linenotify": settings.LINE_NOTIFY_ACCESS_TOKEN is not None, "use_payments": settings.USE_PAYMENTS, } @@ -1383,26 +1381,6 @@ def add_opsgenie(request, code): ctx = {"page": "channels", "project": project, "form": form} return render(request, "integrations/add_opsgenie.html", ctx) -# @require_setting("LINE_NOTIFY_ACCESS_TOKEN") -@login_required -def add_linenotify(request, code): - project = _get_project_for_user(request, code) - - if request.method == "POST": - form = forms.AddLineNotifyForm(request.POST) - if form.is_valid(): - channel = Channel(project=project, kind="linenotify") - channel.value = form.cleaned_data["value"] - channel.save() - - channel.assign_all_checks() - return redirect("hc-p-channels", project.code) - else: - form = forms.AddLineNotifyForm() - - ctx = {"page": "channels", "project": project, "form": form} - return render(request, "integrations/add_linenotify.html", ctx) - @login_required def add_victorops(request, code): @@ -1799,3 +1777,23 @@ def add_spike(request, code): ctx = {"page": "channels", "project": project, "form": form} return render(request, "integrations/add_spike.html", ctx) + + +@login_required +def add_linenotify(request, code): + project = _get_project_for_user(request, code) + + if request.method == "POST": + form = forms.AddLineNotifyForm(request.POST) + if form.is_valid(): + channel = Channel(project=project, kind="linenotify") + channel.value = form.cleaned_data["token"] + channel.save() + + channel.assign_all_checks() + return redirect("hc-p-channels", project.code) + else: + form = forms.AddLineNotifyForm() + + ctx = {"page": "channels", "project": project, "form": form} + return render(request, "integrations/add_linenotify.html", ctx) diff --git a/hc/settings.py b/hc/settings.py index ea078a03..9dd4c992 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -208,9 +208,6 @@ PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY") # Trello TRELLO_APP_KEY = os.getenv("TRELLO_APP_KEY") -# Line notify integration -LINE_NOTIFY_ACCESS_TOKEN = os.getenv("LINE_NOTIFY_ACCESS_TOKEN") - # Matrix MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER") MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") diff --git a/templates/front/channels.html b/templates/front/channels.html index 8e1a5f12..7277bf2f 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -412,7 +412,6 @@ Add Integration - {% comment %} {% if enable_linenotify %} {% endcomment %}
  • LineNotify icon @@ -421,7 +420,6 @@

    Get a LineNotify message when a check goes up or down.

    Add Integration
  • - {% comment %} {% endif %} {% endcomment %}