0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-08 14:40:05 +00:00

Add tests

cc: 
This commit is contained in:
Pēteris Caune 2025-02-25 12:01:21 +02:00
parent fe5cccb829
commit 4948f4bbb4
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
8 changed files with 421 additions and 24 deletions

View file

@ -0,0 +1,163 @@
from __future__ import annotations
import json
from datetime import timedelta as td
from unittest.mock import Mock, patch
from django.test.utils import override_settings
from django.utils.timezone import now
from hc.api.models import Channel, Check, Flip, Notification, Ping
from hc.test import BaseTestCase
MOCK_GITHUB = Mock()
MOCK_GITHUB.get_installation_access_token.return_value = "test-token"
@patch("hc.api.transports.close_old_connections", Mock())
@patch("hc.api.transports.github", MOCK_GITHUB)
@override_settings(GITHUB_PRIVATE_KEY="test-private-key")
class NotifyGitHubTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.check = Check(project=self.project)
self.check.name = "DB Backup"
self.check.tags = "foo bar baz"
# Transport classes should use flip.new_status,
# so the status "paused" should not appear anywhere
self.check.status = "paused"
self.check.last_ping = now()
self.check.n_pings = 1
self.check.save()
self.ping = Ping(owner=self.check)
self.ping.created = now() - td(minutes=10)
self.ping.n = 112233
self.ping.save()
self.channel = Channel(project=self.project)
self.channel.kind = "github"
self.channel.value = json.dumps({"installation_id": 123, "repo": "alice/foo"})
self.channel.save()
self.channel.checks.add(self.check)
self.flip = Flip(owner=self.check)
self.flip.created = now()
self.flip.old_status = "new"
self.flip.new_status = "down"
self.flip.reason = "timeout"
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_works(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.channel.notify(self.flip)
assert Notification.objects.count() == 1
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["title"], "DB Backup is DOWN")
self.assertIn("[DB Backup]", payload["body"])
self.assertIn(self.check.cloaked_url(), payload["body"])
self.assertIn("grace time passed", payload["body"])
self.assertIn("**Project:** Alices Project\n", payload["body"])
self.assertIn("**Tags:** `foo` `bar` `baz` \n", payload["body"])
self.assertIn("**Period:** 1 day\n", payload["body"])
self.assertIn("**Total Pings:** 112233\n", payload["body"])
self.assertIn("**Last Ping:** Success, 10 minutes ago", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_handles_reason_failure(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.flip.reason = "fail"
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("received a failure signal", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_shows_exitstatus(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.ping.kind = "fail"
self.ping.exitstatus = 123
self.ping.save()
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("**Last Ping:** Exit status 123, 10 minutes ago", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_shows_cron_schedule(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.check.kind = "cron"
self.check.schedule = "* * * * MON-FRI"
self.check.tz = "Europe/Riga"
self.check.save()
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("**Schedule:** `* * * * MON-FRI`\n", payload["body"])
self.assertIn("**Time Zone:** Europe/Riga\n", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_shows_oncalendar_schedule(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.check.kind = "oncalendar"
self.check.schedule = "Mon 2-29"
self.check.tz = "Europe/Riga"
self.check.save()
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("**Schedule:** `Mon 2-29`\n", payload["body"])
self.assertIn("**Time Zone:** Europe/Riga\n", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_returns_error(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 400
self.channel.notify(self.flip)
n = Notification.objects.get()
self.assertEqual(n.error, "Received status code 400")
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_shows_last_ping_body(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.ping.body_raw = b"Hello World"
self.ping.save()
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("**Last Ping Body:**\n", payload["body"])
self.assertIn("Hello World", payload["body"])
@patch("hc.api.transports.curl.request", autospec=True)
def test_it_checks_for_backticks_in_body(self, mock_post: Mock) -> None:
mock_post.return_value.status_code = 200
self.ping.body_raw = b"``` surprise"
self.ping.save()
self.channel.notify(self.flip)
payload = mock_post.call_args.kwargs["json"]
self.assertNotIn("Last Ping Body", payload["body"])
self.assertNotIn("```", payload["body"])
@override_settings(GITHUB_PRIVATE_KEY=None)
def test_it_requires_github_private_key(self) -> None:
self.channel.notify(self.flip)
n = Notification.objects.get()
self.assertEqual(n.error, "GitHub notifications are not enabled.")

View file

@ -1668,15 +1668,21 @@ class GitHub(HttpTransport):
return status != "down"
def notify(self, flip: Flip, notification: Notification) -> None:
if not settings.GITHUB_PRIVATE_KEY:
raise TransportError("GitHub notifications are not enabled.")
ping = self.last_ping(flip)
ctx = {
"flip": flip,
"check": flip.owner,
"status": flip.new_status,
"ping": ping,
"body": get_ping_body(ping, maxlen=1000),
}
body = get_ping_body(ping, maxlen=1000)
if body and "```" not in body:
ctx["body"] = body
url = f"https://api.github.com/repos/{self.channel.github.repo}/issues"
payload = {
"title": tmpl("github_title.html", **ctx),

View file

@ -0,0 +1,35 @@
from __future__ import annotations
from django.test.utils import override_settings
from hc.test import BaseTestCase
@override_settings(GITHUB_CLIENT_ID="fake-client-id")
class AddGitHubTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.url = f"/projects/{self.project.code}/add_github/"
def test_prompt_works(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url)
self.assertContains(r, "create an issue", status_code=200)
self.assertContains(r, "github.com/login/oauth/authorize")
self.assertTrue("add_github_state" in self.client.session)
self.assertTrue("add_github_project" in self.client.session)
@override_settings(GITHUB_CLIENT_ID=None)
def test_it_requires_client_id(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)
def test_it_requires_rw_access(self) -> None:
self.bobs_membership.role = "r"
self.bobs_membership.save()
self.client.login(username="bob@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)

View file

@ -0,0 +1,68 @@
from __future__ import annotations
from django.test.utils import override_settings
from hc.api.models import Channel
from hc.test import BaseTestCase
@override_settings(GITHUB_CLIENT_ID="fake-client-id")
class AddGitHubSaveTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.url = f"/projects/{self.project.code}/add_github/save/"
session = self.client.session
session["add_github_project"] = "DEADBEEF"
session["add_github_token"] = "CAFEBABE"
session["add_github_repos"] = {"alice/foo": 123}
session.save()
def test_it_works(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {"repo_name": "alice/foo"})
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.kind, "github")
self.assertEqual(c.name, "alice/foo")
self.assertEqual(c.github.installation_id, 123)
self.assertEqual(c.github.repo, "alice/foo")
def test_it_rejects_get(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 405)
@override_settings(GITHUB_CLIENT_ID=None)
def test_it_requires_client_id(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {"repo_name": "alice/foo"})
self.assertEqual(r.status_code, 404)
def test_it_requires_rw_access(self) -> None:
self.bobs_membership.role = "r"
self.bobs_membership.save()
self.client.login(username="bob@example.org", password="password")
r = self.client.post(self.url, {"repo_name": "alice/foo"})
self.assertEqual(r.status_code, 403)
def test_it_handles_empty_form(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {})
self.assertEqual(r.status_code, 400)
def test_it_handles_unexpected_repo_name(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {"repo_name": "alice/bar"})
self.assertEqual(r.status_code, 403)
def test_it_handles_no_session(self) -> None:
session = self.client.session
session.clear()
session.save()
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {"repo_name": "alice/foo"})
self.assertEqual(r.status_code, 403)

View file

@ -0,0 +1,116 @@
from __future__ import annotations
from unittest.mock import patch, Mock
from django.test.utils import override_settings
from hc.test import BaseTestCase
@override_settings(GITHUB_CLIENT_ID="fake-client-id")
@override_settings(GITHUB_PUBLIC_LINK="http://example.org")
class AddGitHubSelectTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.url = "/integrations/add_github/"
@patch("hc.front.views.github", autospec=True)
def test_it_works(self, github: Mock) -> None:
self.client.login(username="alice@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session["add_github_state"] = "test-state"
session.save()
github.get_user_access_token.return_value = "test-token"
github.get_repos.return_value = {"alice/foo": 123}
r = self.client.get(self.url + "?state=test-state&code=test-code")
get_token_args, _ = github.get_user_access_token.call_args
self.assertEqual(get_token_args[0], "test-code")
get_repos_args, _ = github.get_repos.call_args
self.assertEqual(get_repos_args[0], "test-token")
self.assertContains(r, "Save Integration", status_code=200)
self.assertContains(r, "alice/foo")
self.assertContains(r, f"/projects/{self.project.code}/add_github/save/")
self.assertContains(r, "http://example.org/installations/new")
self.assertEqual(self.client.session["add_github_token"], "test-token")
self.assertEqual(self.client.session["add_github_repos"], {"alice/foo": 123})
@patch("hc.front.views.github", autospec=True)
def test_it_skips_oauth_code_exchange(self, github: Mock) -> None:
self.client.login(username="alice@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session["add_github_token"] = "test-token"
session.save()
github.get_repos.return_value = {"alice/foo": 123}
r = self.client.get(self.url + "?state=test-state&code=test-code")
self.assertFalse(github.get_user_access_token.called)
self.assertContains(r, "alice/foo")
@override_settings(GITHUB_CLIENT_ID=None)
def test_it_requires_client_id(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)
def test_it_requires_rw_access(self) -> None:
self.bobs_membership.role = "r"
self.bobs_membership.save()
self.client.login(username="bob@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session["add_github_token"] = "test-token"
session.save()
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
def test_it_handles_no_session(self) -> None:
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url + "?state=test-state&code=test-code")
self.assertEqual(r.status_code, 403)
def test_it_handles_no_state_and_no_token(self) -> None:
self.client.login(username="alice@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session.save()
r = self.client.get(self.url + "?state=test-state&code=test-code")
self.assertEqual(r.status_code, 403)
@patch("hc.front.views.github", autospec=True)
def test_it_handles_wrong_state(self, github: Mock) -> None:
self.client.login(username="alice@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session["add_github_state"] = "test-state"
session.save()
r = self.client.get(self.url + "?state=wrong-state&code=test-code")
self.assertEqual(r.status_code, 403)
@patch("hc.front.views.github", autospec=True)
def test_it_redirects_to_install_page(self, github: Mock) -> None:
self.client.login(username="alice@example.org", password="password")
session = self.client.session
session["add_github_project"] = str(self.project.code)
session["add_github_token"] = "test-token"
session.save()
github.get_repos.return_value = {}
r = self.client.get(self.url + "?state=test-state&code=test-code")
self.assertRedirects(
r, "http://example.org/installations/new", fetch_redirect_response=False
)

View file

@ -67,6 +67,7 @@ project_urls = [
path("add_discord/", views.add_discord, name="hc-add-discord"),
path("add_email/", views.add_email, name="hc-add-email"),
path("add_github/", views.add_github, name="hc-add-github"),
path("add_github/save/", views.add_github_save, name="hc-add-github-save"),
path("add_gotify/", views.add_gotify, name="hc-add-gotify"),
path("add_group/", views.add_group, name="hc-add-group"),
path("add_matrix/", views.add_matrix, name="hc-add-matrix"),

View file

@ -2762,32 +2762,11 @@ def log_events(request: HttpRequest, code: UUID) -> HttpResponse:
def add_github(request: HttpRequest, code: UUID) -> HttpResponse:
project = _get_rw_project_for_user(request, code)
if request.method == "POST":
if "add_github_repos" not in request.session:
return HttpResponseForbidden()
request.session.pop("add_github_project")
request.session.pop("add_github_token")
repos = request.session.pop("add_github_repos")
repo_name = request.POST["repo_name"]
if repo_name not in repos:
return HttpResponseForbidden()
channel = Channel(kind="github", project=project)
channel.value = json.dumps(
{"installation_id": repos[repo_name], "repo": repo_name}
)
channel.name = repo_name
channel.save()
channel.assign_all_checks()
messages.success(request, "Success, integration added!")
return redirect("hc-channels", project.code)
state = token_urlsafe()
authorize_url = "https://github.com/login/oauth/authorize?"
authorize_url += urlencode({"client_id": settings.GITHUB_CLIENT_ID, "state": state})
ctx = {
"project": project,
"authorize_url": authorize_url,
}
@ -2831,4 +2810,33 @@ def add_github_select(request: HttpRequest) -> HttpResponse:
return render(request, "integrations/add_github_form.html", ctx)
@require_setting("GITHUB_CLIENT_ID")
@login_required
@require_POST
def add_github_save(request: HttpRequest, code: UUID) -> HttpResponse:
project = _get_rw_project_for_user(request, code)
if "add_github_repos" not in request.session:
return HttpResponseForbidden()
if "repo_name" not in request.POST:
return HttpResponseBadRequest()
request.session.pop("add_github_project")
request.session.pop("add_github_token")
repos = request.session.pop("add_github_repos")
repo_name = request.POST["repo_name"]
if repo_name not in repos:
return HttpResponseForbidden()
channel = Channel(kind="github", project=project)
channel.value = json.dumps({"installation_id": repos[repo_name], "repo": repo_name})
channel.name = repo_name
channel.save()
channel.assign_all_checks()
messages.success(request, "Success, integration added!")
return redirect("hc-channels", project.code)
# Forks: add custom views after this line

View file

@ -10,7 +10,7 @@
<h2>Integration Settings</h2>
<form method="post" action="{% url 'hc-add-github' project.code %}" class="form-horizontal">
<form method="post" action="{% url 'hc-add-github-save' project.code %}" class="form-horizontal">
{% csrf_token %}
<div class="form-group {{ form.repo_name.css_classes }}">
<label for="repo_name" class="col-sm-2 control-label">Repository</label>