Browse Source

Add tests & Doesn't get LineNotify token using setting

pull/415/head
carson.wang 4 years ago
committed by Pēteris Caune
parent
commit
74668551a7
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
7 changed files with 53 additions and 36 deletions
  1. +3
    -3
      hc/api/transports.py
  2. +5
    -2
      hc/front/forms.py
  3. +25
    -2
      hc/front/tests/test_add_linenotify.py
  4. +20
    -22
      hc/front/views.py
  5. +0
    -3
      hc/settings.py
  6. +0
    -2
      templates/front/channels.html
  7. +0
    -2
      templates/front/welcome.html

+ 3
- 3
hc/api/transports.py View File

@ -626,10 +626,10 @@ class LineNotify(HttpTransport):
def notify(self, check): def notify(self, check):
headers = { 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 = { payload = {
"message": tmpl("linenotify_message.html", check=check) "message": tmpl("linenotify_message.html", check=check)
} }
return self.post(URL, headers=headers, params=payload)
return self.post(self.URL, headers=headers, params=payload)

+ 5
- 2
hc/front/forms.py View File

@ -104,8 +104,6 @@ class AddOpsGenieForm(forms.Form):
region = forms.ChoiceField(initial="us", choices=(("us", "US"), ("eu", "EU"))) region = forms.ChoiceField(initial="us", choices=(("us", "US"), ("eu", "EU")))
key = forms.CharField(max_length=40) key = forms.CharField(max_length=40)
class AddLineNotifyForm(forms.Form):
token = forms.CharField(max_length=40)
PRIO_CHOICES = [ PRIO_CHOICES = [
("-2", "Lowest Priority"), ("-2", "Lowest Priority"),
@ -261,3 +259,8 @@ class AddZulipForm(forms.Form):
def get_value(self): def get_value(self):
return json.dumps(dict(self.cleaned_data), sort_keys=True) 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)

+ 25
- 2
hc/front/tests/test_add_linenotify.py View File

@ -1,8 +1,6 @@
from django.test.utils import override_settings
from hc.api.models import Channel from hc.api.models import Channel
from hc.test import BaseTestCase from hc.test import BaseTestCase
@override_settings(LINE_NOTIFY_ACCESS_TOKEN="foo")
class AddLineNotifyTestCase(BaseTestCase): class AddLineNotifyTestCase(BaseTestCase):
url = "/integrations/add_linenotify/" url = "/integrations/add_linenotify/"
@ -14,3 +12,28 @@ class AddLineNotifyTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertContains(r, "LineNotify") self.assertContains(r, "LineNotify")
def test_it_works(self):
form = {"token": "helloworld"}
self.client.login(username="[email protected]", 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="[email protected]", password="password")
self.client.post(self.url, form)
c = Channel.objects.get()
self.assertEqual(c.value, "foo123")

+ 20
- 22
hc/front/views.py View File

@ -273,7 +273,6 @@ def index(request):
"enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_telegram": settings.TELEGRAM_TOKEN is not None,
"enable_trello": settings.TRELLO_APP_KEY is not None, "enable_trello": settings.TRELLO_APP_KEY is not None,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP, "enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
"enable_linenotify": settings.LINE_NOTIFY_ACCESS_TOKEN is not None,
"registration_open": settings.REGISTRATION_OPEN, "registration_open": settings.REGISTRATION_OPEN,
} }
@ -712,7 +711,6 @@ def channels(request, code):
"enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_telegram": settings.TELEGRAM_TOKEN is not None,
"enable_trello": settings.TRELLO_APP_KEY is not None, "enable_trello": settings.TRELLO_APP_KEY is not None,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP, "enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
"enable_linenotify": settings.LINE_NOTIFY_ACCESS_TOKEN is not None,
"use_payments": settings.USE_PAYMENTS, "use_payments": settings.USE_PAYMENTS,
} }
@ -1383,26 +1381,6 @@ def add_opsgenie(request, code):
ctx = {"page": "channels", "project": project, "form": form} ctx = {"page": "channels", "project": project, "form": form}
return render(request, "integrations/add_opsgenie.html", ctx) 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 @login_required
def add_victorops(request, code): def add_victorops(request, code):
@ -1799,3 +1777,23 @@ def add_spike(request, code):
ctx = {"page": "channels", "project": project, "form": form} ctx = {"page": "channels", "project": project, "form": form}
return render(request, "integrations/add_spike.html", ctx) 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)

+ 0
- 3
hc/settings.py View File

@ -208,9 +208,6 @@ PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
# Trello # Trello
TRELLO_APP_KEY = os.getenv("TRELLO_APP_KEY") TRELLO_APP_KEY = os.getenv("TRELLO_APP_KEY")
# Line notify integration
LINE_NOTIFY_ACCESS_TOKEN = os.getenv("LINE_NOTIFY_ACCESS_TOKEN")
# Matrix # Matrix
MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER") MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER")
MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") MATRIX_USER_ID = os.getenv("MATRIX_USER_ID")


+ 0
- 2
templates/front/channels.html View File

@ -412,7 +412,6 @@
<a href="{% url 'hc-add-zulip' project.code %}" class="btn btn-primary">Add Integration</a> <a href="{% url 'hc-add-zulip' project.code %}" class="btn btn-primary">Add Integration</a>
</li> </li>
{% comment %} {% if enable_linenotify %} {% endcomment %}
<li> <li>
<img src="{% static 'img/integrations/linenotify.png' %}" <img src="{% static 'img/integrations/linenotify.png' %}"
class="icon" alt="LineNotify icon" /> class="icon" alt="LineNotify icon" />
@ -421,7 +420,6 @@
<p>Get a LineNotify message when a check goes up or down.</p> <p>Get a LineNotify message when a check goes up or down.</p>
<a href="{% url 'hc-add-linenotify' project.code %}" class="btn btn-primary">Add Integration</a> <a href="{% url 'hc-add-linenotify' project.code %}" class="btn btn-primary">Add Integration</a>
</li> </li>
{% comment %} {% endif %} {% endcomment %}
<li class="link-to-github"> <li class="link-to-github">
<img src="{% static 'img/integrations/missing.png' %}" <img src="{% static 'img/integrations/missing.png' %}"


+ 0
- 2
templates/front/welcome.html View File

@ -643,14 +643,12 @@
</div> </div>
</div> </div>
{% comment %} {% if enable_linenotify %} {% endcomment %}
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6"> <div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
<div class="integration"> <div class="integration">
<img src="{% static 'img/integrations/linenotify.png' %}" class="icon" alt="" /> <img src="{% static 'img/integrations/linenotify.png' %}" class="icon" alt="" />
<h3>LineNotify<br><small>Chat</small></h3> <h3>LineNotify<br><small>Chat</small></h3>
</div> </div>
</div> </div>
{% comment %} {% endif %} {% endcomment %}
</div> </div>
<div class="row tour-section"> <div class="row tour-section">


Loading…
Cancel
Save