Browse Source

Add the MSTEAMS_ENABLED setting

pull/474/head
Pēteris Caune 4 years ago
parent
commit
5f31b8b873
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
12 changed files with 102 additions and 39 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +1
    -0
      docker/.env
  3. +0
    -39
      hc/api/tests/test_notify.py
  4. +71
    -0
      hc/api/tests/test_notify_msteams.py
  5. +3
    -0
      hc/api/transports.py
  6. +7
    -0
      hc/front/tests/test_add_msteams.py
  7. +3
    -0
      hc/front/views.py
  8. +3
    -0
      hc/settings.py
  9. +3
    -0
      templates/docs/self_hosted_configuration.html
  10. +6
    -0
      templates/docs/self_hosted_configuration.md
  11. +2
    -0
      templates/front/channels.html
  12. +2
    -0
      templates/front/welcome.html

+ 1
- 0
CHANGELOG.md View File

@ -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


+ 1
- 0
docker/.env View File

@ -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


+ 0
- 39
hc/api/tests/test_notify.py View File

@ -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("&lt;u&gt;underline&lt;/u&gt;", text)
self.assertIn(r"\\backslash\\ ", text)
self.assertIn("&quot;quoted&quot;", 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
- 0
hc/api/tests/test_notify_msteams.py View 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_ &amp; 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("&lt;u&gt;underline&lt;/u&gt;", text)
self.assertIn(r"\\backslash\\ ", text)
self.assertIn("&quot;quoted&quot;", 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.")

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

@ -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)


+ 7
- 0
hc/front/tests/test_add_msteams.py View File

@ -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="[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.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="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)

+ 3
- 0
hc/front/views.py View File

@ -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)


+ 3
- 0
hc/settings.py View File

@ -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")


+ 3
- 0
templates/docs/self_hosted_configuration.html View File

@ -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,


+ 6
- 0
templates/docs/self_hosted_configuration.md View File

@ -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`


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

@ -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' %}"


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

@ -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…
Cancel
Save