forked from GithubBackups/healthchecks
Add the MSTEAMS_ENABLED setting
This commit is contained in:
parent
6c3debaf11
commit
5f31b8b873
@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
|
|||||||
- Add the WEBHOOKS_ENABLED setting (#471)
|
- Add the WEBHOOKS_ENABLED setting (#471)
|
||||||
- Add the SLACK_ENABLED setting (#471)
|
- Add the SLACK_ENABLED setting (#471)
|
||||||
- Add the MATTERMOST_ENABLED setting (#471)
|
- Add the MATTERMOST_ENABLED setting (#471)
|
||||||
|
- Add the MSTEAMS_ENABLED setting (#471)
|
||||||
|
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
|
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
|
||||||
|
@ -26,6 +26,7 @@ MATRIX_ACCESS_TOKEN=
|
|||||||
MATRIX_HOMESERVER=
|
MATRIX_HOMESERVER=
|
||||||
MATRIX_USER_ID=
|
MATRIX_USER_ID=
|
||||||
MATTERMOST_ENABLED=True
|
MATTERMOST_ENABLED=True
|
||||||
|
MSTEAMS_ENABLED=True
|
||||||
PD_VENDOR_KEY=
|
PD_VENDOR_KEY=
|
||||||
PING_BODY_LIMIT=10000
|
PING_BODY_LIMIT=10000
|
||||||
PING_EMAIL_DOMAIN=localhost
|
PING_EMAIL_DOMAIN=localhost
|
||||||
|
@ -283,45 +283,6 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
with self.assertRaises(NotImplementedError):
|
with self.assertRaises(NotImplementedError):
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.request")
|
|
||||||
def test_msteams(self, mock_post):
|
|
||||||
self._setup_data("msteams", "http://example.com/webhook")
|
|
||||||
mock_post.return_value.status_code = 200
|
|
||||||
|
|
||||||
self.check.name = "_underscores_ & more"
|
|
||||||
|
|
||||||
self.channel.notify(self.check)
|
|
||||||
assert Notification.objects.count() == 1
|
|
||||||
|
|
||||||
args, kwargs = mock_post.call_args
|
|
||||||
payload = kwargs["json"]
|
|
||||||
self.assertEqual(payload["@type"], "MessageCard")
|
|
||||||
|
|
||||||
# summary and title should be the same, except
|
|
||||||
# title should have any special HTML characters escaped
|
|
||||||
self.assertEqual(payload["summary"], "“_underscores_ & more” is DOWN.")
|
|
||||||
self.assertEqual(payload["title"], "“_underscores_ & more” is DOWN.")
|
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.request")
|
|
||||||
def test_msteams_escapes_html_and_markdown_in_desc(self, mock_post):
|
|
||||||
self._setup_data("msteams", "http://example.com/webhook")
|
|
||||||
mock_post.return_value.status_code = 200
|
|
||||||
|
|
||||||
self.check.desc = """
|
|
||||||
TEST _underscore_ `backticks` <u>underline</u> \\backslash\\ "quoted"
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.channel.notify(self.check)
|
|
||||||
|
|
||||||
args, kwargs = mock_post.call_args
|
|
||||||
text = kwargs["json"]["sections"][0]["text"]
|
|
||||||
|
|
||||||
self.assertIn(r"\_underscore\_", text)
|
|
||||||
self.assertIn(r"\`backticks\`", text)
|
|
||||||
self.assertIn("<u>underline</u>", text)
|
|
||||||
self.assertIn(r"\\backslash\\ ", text)
|
|
||||||
self.assertIn(""quoted"", text)
|
|
||||||
|
|
||||||
@patch("hc.api.transports.os.system")
|
@patch("hc.api.transports.os.system")
|
||||||
@override_settings(SHELL_ENABLED=True)
|
@override_settings(SHELL_ENABLED=True)
|
||||||
def test_shell(self, mock_system):
|
def test_shell(self, mock_system):
|
||||||
|
71
hc/api/tests/test_notify_msteams.py
Normal file
71
hc/api/tests/test_notify_msteams.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
from datetime import timedelta as td
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from django.utils.timezone import now
|
||||||
|
from hc.api.models import Channel, Check, Notification
|
||||||
|
from hc.test import BaseTestCase
|
||||||
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
|
|
||||||
|
class NotifyTestCase(BaseTestCase):
|
||||||
|
def _setup_data(self, value, status="down", email_verified=True):
|
||||||
|
self.check = Check(project=self.project)
|
||||||
|
self.check.status = status
|
||||||
|
self.check.last_ping = now() - td(minutes=61)
|
||||||
|
self.check.save()
|
||||||
|
|
||||||
|
self.channel = Channel(project=self.project)
|
||||||
|
self.channel.kind = "msteams"
|
||||||
|
self.channel.value = value
|
||||||
|
self.channel.email_verified = email_verified
|
||||||
|
self.channel.save()
|
||||||
|
self.channel.checks.add(self.check)
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.request")
|
||||||
|
def test_msteams(self, mock_post):
|
||||||
|
self._setup_data("http://example.com/webhook")
|
||||||
|
mock_post.return_value.status_code = 200
|
||||||
|
|
||||||
|
self.check.name = "_underscores_ & more"
|
||||||
|
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
assert Notification.objects.count() == 1
|
||||||
|
|
||||||
|
args, kwargs = mock_post.call_args
|
||||||
|
payload = kwargs["json"]
|
||||||
|
self.assertEqual(payload["@type"], "MessageCard")
|
||||||
|
|
||||||
|
# summary and title should be the same, except
|
||||||
|
# title should have any special HTML characters escaped
|
||||||
|
self.assertEqual(payload["summary"], "“_underscores_ & more” is DOWN.")
|
||||||
|
self.assertEqual(payload["title"], "“_underscores_ & more” is DOWN.")
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.request")
|
||||||
|
def test_msteams_escapes_html_and_markdown_in_desc(self, mock_post):
|
||||||
|
self._setup_data("http://example.com/webhook")
|
||||||
|
mock_post.return_value.status_code = 200
|
||||||
|
|
||||||
|
self.check.desc = """
|
||||||
|
TEST _underscore_ `backticks` <u>underline</u> \\backslash\\ "quoted"
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
args, kwargs = mock_post.call_args
|
||||||
|
text = kwargs["json"]["sections"][0]["text"]
|
||||||
|
|
||||||
|
self.assertIn(r"\_underscore\_", text)
|
||||||
|
self.assertIn(r"\`backticks\`", text)
|
||||||
|
self.assertIn("<u>underline</u>", text)
|
||||||
|
self.assertIn(r"\\backslash\\ ", text)
|
||||||
|
self.assertIn(""quoted"", text)
|
||||||
|
|
||||||
|
@override_settings(MSTEAMS_ENABLED=False)
|
||||||
|
def test_it_requires_msteams_enabled(self):
|
||||||
|
self._setup_data("http://example.com/webhook")
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
n = Notification.objects.get()
|
||||||
|
self.assertEqual(n.error, "MS Teams notifications are not enabled.")
|
@ -610,6 +610,9 @@ class MsTeams(HttpTransport):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
def notify(self, check):
|
def notify(self, check):
|
||||||
|
if not settings.MSTEAMS_ENABLED:
|
||||||
|
return "MS Teams notifications are not enabled."
|
||||||
|
|
||||||
text = tmpl("msteams_message.json", check=check)
|
text = tmpl("msteams_message.json", check=check)
|
||||||
payload = json.loads(text)
|
payload = json.loads(text)
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
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
|
||||||
|
|
||||||
@ -31,3 +32,9 @@ class AddMsTeamsTestCase(BaseTestCase):
|
|||||||
self.client.login(username="bob@example.org", password="password")
|
self.client.login(username="bob@example.org", password="password")
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
self.assertEqual(r.status_code, 403)
|
self.assertEqual(r.status_code, 403)
|
||||||
|
|
||||||
|
@override_settings(MSTEAMS_ENABLED=False)
|
||||||
|
def test_it_handles_disabled_integration(self):
|
||||||
|
self.client.login(username="alice@example.org", password="password")
|
||||||
|
r = self.client.get(self.url)
|
||||||
|
self.assertEqual(r.status_code, 404)
|
||||||
|
@ -296,6 +296,7 @@ def index(request):
|
|||||||
"enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None,
|
"enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None,
|
||||||
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
||||||
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
||||||
|
"enable_msteams": settings.MSTEAMS_ENABLED is True,
|
||||||
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
||||||
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
||||||
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
||||||
@ -765,6 +766,7 @@ def channels(request, code):
|
|||||||
"enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None,
|
"enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None,
|
||||||
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
||||||
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
||||||
|
"enable_msteams": settings.MSTEAMS_ENABLED is True,
|
||||||
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
||||||
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
||||||
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
||||||
@ -1783,6 +1785,7 @@ def trello_settings(request):
|
|||||||
return render(request, "integrations/trello_settings.html", ctx)
|
return render(request, "integrations/trello_settings.html", ctx)
|
||||||
|
|
||||||
|
|
||||||
|
@require_setting("MSTEAMS_ENABLED")
|
||||||
@login_required
|
@login_required
|
||||||
def add_msteams(request, code):
|
def add_msteams(request, code):
|
||||||
project = _get_rw_project_for_user(request, code)
|
project = _get_rw_project_for_user(request, code)
|
||||||
|
@ -202,6 +202,9 @@ MATRIX_ACCESS_TOKEN = os.getenv("MATRIX_ACCESS_TOKEN")
|
|||||||
# Mattermost
|
# Mattermost
|
||||||
MATTERMOST_ENABLED = envbool("MATTERMOST_ENABLED", "True")
|
MATTERMOST_ENABLED = envbool("MATTERMOST_ENABLED", "True")
|
||||||
|
|
||||||
|
# MS Teams
|
||||||
|
MSTEAMS_ENABLED = envbool("MSTEAMS_ENABLED", "True")
|
||||||
|
|
||||||
# PagerDuty
|
# PagerDuty
|
||||||
PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
|
PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
|
||||||
|
|
||||||
|
@ -143,6 +143,9 @@ integration.</p>
|
|||||||
<h2 id="MATTERMOST_ENABLED"><code>MATTERMOST_ENABLED</code></h2>
|
<h2 id="MATTERMOST_ENABLED"><code>MATTERMOST_ENABLED</code></h2>
|
||||||
<p>Default: <code>True</code></p>
|
<p>Default: <code>True</code></p>
|
||||||
<p>A boolean that turns on/off the Mattermost integration. Enabled by default.</p>
|
<p>A boolean that turns on/off the Mattermost integration. Enabled by default.</p>
|
||||||
|
<h2 id="MSTEAMS_ENABLED"><code>MSTEAMS_ENABLED</code></h2>
|
||||||
|
<p>Default: <code>True</code></p>
|
||||||
|
<p>A boolean that turns on/off the MS Teams integration. Enabled by default.</p>
|
||||||
<h2 id="PD_VENDOR_KEY"><code>PD_VENDOR_KEY</code></h2>
|
<h2 id="PD_VENDOR_KEY"><code>PD_VENDOR_KEY</code></h2>
|
||||||
<p>Default: <code>None</code></p>
|
<p>Default: <code>None</code></p>
|
||||||
<p><a href="https://www.pagerduty.com/">PagerDuty</a> vendor key,
|
<p><a href="https://www.pagerduty.com/">PagerDuty</a> vendor key,
|
||||||
|
@ -236,6 +236,12 @@ Default: `True`
|
|||||||
|
|
||||||
A boolean that turns on/off the Mattermost integration. Enabled by default.
|
A boolean that turns on/off the Mattermost integration. Enabled by default.
|
||||||
|
|
||||||
|
## `MSTEAMS_ENABLED` {: #MSTEAMS_ENABLED }
|
||||||
|
|
||||||
|
Default: `True`
|
||||||
|
|
||||||
|
A boolean that turns on/off the MS Teams integration. Enabled by default.
|
||||||
|
|
||||||
## `PD_VENDOR_KEY` {: #PD_VENDOR_KEY }
|
## `PD_VENDOR_KEY` {: #PD_VENDOR_KEY }
|
||||||
|
|
||||||
Default: `None`
|
Default: `None`
|
||||||
|
@ -276,6 +276,7 @@
|
|||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if enable_msteams %}
|
||||||
<li>
|
<li>
|
||||||
<img src="{% static 'img/integrations/msteams.png' %}"
|
<img src="{% static 'img/integrations/msteams.png' %}"
|
||||||
class="icon" alt="Microsoft Teams" />
|
class="icon" alt="Microsoft Teams" />
|
||||||
@ -284,6 +285,7 @@
|
|||||||
<p>Chat and collaboration platform for Microsoft Office 365 customers.</p>
|
<p>Chat and collaboration platform for Microsoft Office 365 customers.</p>
|
||||||
<a href="{% url 'hc-add-msteams' project.code %}" class="btn btn-primary">Add Integration</a>
|
<a href="{% url 'hc-add-msteams' project.code %}" class="btn btn-primary">Add Integration</a>
|
||||||
</li>
|
</li>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<img src="{% static 'img/integrations/opsgenie.png' %}"
|
<img src="{% static 'img/integrations/opsgenie.png' %}"
|
||||||
|
@ -472,6 +472,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if enable_msteams %}
|
||||||
<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/msteams.png' %}" class="icon" alt="" />
|
<img src="{% static 'img/integrations/msteams.png' %}" class="icon" alt="" />
|
||||||
@ -481,6 +482,7 @@
|
|||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<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">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user