diff --git a/hc/front/tests/test_add_channel.py b/hc/front/tests/test_add_channel.py index 5bea3aae..c1ba14f4 100644 --- a/hc/front/tests/test_add_channel.py +++ b/hc/front/tests/test_add_channel.py @@ -7,7 +7,7 @@ from hc.test import BaseTestCase @override_settings(PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="url") class AddChannelTestCase(BaseTestCase): - def test_it_works(self): + def test_it_adds_email(self): url = "/integrations/add/" form = {"kind": "email", "value": "alice@example.org"} @@ -51,108 +51,8 @@ class AddChannelTestCase(BaseTestCase): def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") - for frag in ("email", "webhook", "pd", "pushover", "hipchat", "victorops"): + kinds = ("email", "webhook", "pd", "pushover", "hipchat", "victorops") + for frag in kinds: url = "/integrations/add_%s/" % frag r = self.client.get(url) self.assertContains(r, "Integration Settings", status_code=200) - - @override_settings(SLACK_CLIENT_ID=None) - def test_slack_webhook_instructions_work(self): - self.client.login(username="alice@example.org", password="password") - r = self.client.get("/integrations/add_slack/") - self.assertContains(r, "Integration Settings", status_code=200) - - @override_settings(SLACK_CLIENT_ID="foo") - def test_slack_button(self): - self.client.login(username="alice@example.org", password="password") - r = self.client.get("/integrations/add_slack/") - self.assertContains(r, "slack.com/oauth/authorize", status_code=200) - - @override_settings(SLACK_CLIENT_ID="foo") - def test_slack_landing_page(self): - r = self.client.get("/integrations/add_slack/") - self.assertContains(r, "Before adding Slack integration", - status_code=200) - - def test_it_adds_pushover_channel(self): - self.client.login(username="alice@example.org", password="password") - - session = self.client.session - session["po_nonce"] = "n" - session.save() - - params = "pushover_user_key=a&nonce=n&prio=0" - r = self.client.get("/integrations/add_pushover/?%s" % params) - assert r.status_code == 302 - - channels = list(Channel.objects.all()) - assert len(channels) == 1 - assert channels[0].value == "a|0" - - def test_it_validates_pushover_priority(self): - self.client.login(username="alice@example.org", password="password") - - session = self.client.session - session["po_nonce"] = "n" - session.save() - - params = "pushover_user_key=a&nonce=n&prio=abc" - r = self.client.get("/integrations/add_pushover/?%s" % params) - assert r.status_code == 400 - - def test_it_validates_pushover_nonce(self): - self.client.login(username="alice@example.org", password="password") - - session = self.client.session - session["po_nonce"] = "n" - session.save() - - params = "pushover_user_key=a&nonce=INVALID&prio=0" - r = self.client.get("/integrations/add_pushover/?%s" % params) - assert r.status_code == 403 - - def test_it_adds_two_webhook_urls_and_redirects(self): - form = {"value_down": "http://foo.com", "value_up": "https://bar.com"} - - self.client.login(username="alice@example.org", password="password") - r = self.client.post("/integrations/add_webhook/", form) - self.assertRedirects(r, "/integrations/") - - c = Channel.objects.get() - self.assertEqual(c.value, "http://foo.com\nhttps://bar.com") - - def test_it_adds_webhook_using_team_access(self): - form = {"value_down": "http://foo.com", "value_up": "https://bar.com"} - - # Logging in as bob, not alice. Bob has team access so this - # should work. - self.client.login(username="bob@example.org", password="password") - self.client.post("/integrations/add_webhook/", form) - - c = Channel.objects.get() - self.assertEqual(c.user, self.alice) - self.assertEqual(c.value, "http://foo.com\nhttps://bar.com") - - def test_it_rejects_non_http_webhook_urls(self): - form = {"value_down": "foo", "value_up": "bar"} - - self.client.login(username="alice@example.org", password="password") - r = self.client.post("/integrations/add_webhook/", form) - self.assertContains(r, "Enter a valid URL.") - - self.assertEqual(Channel.objects.count(), 0) - - def test_it_handles_empty_down_url(self): - form = {"value_down": "", "value_up": "http://foo.com"} - - self.client.login(username="alice@example.org", password="password") - self.client.post("/integrations/add_webhook/", form) - - c = Channel.objects.get() - self.assertEqual(c.value, "\nhttp://foo.com") - - @override_settings(PUSHBULLET_CLIENT_ID="foo") - def test_pushbullet_instructions_work(self): - self.client.login(username="alice@example.org", password="password") - r = self.client.get("/integrations/add_pushbullet/") - self.assertContains(r, "www.pushbullet.com/authorize", status_code=200) diff --git a/hc/front/tests/test_add_pushbover.py b/hc/front/tests/test_add_pushbover.py new file mode 100644 index 00000000..103299de --- /dev/null +++ b/hc/front/tests/test_add_pushbover.py @@ -0,0 +1,49 @@ +from django.test.utils import override_settings +from hc.api.models import Channel +from hc.test import BaseTestCase + + +@override_settings(PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="url") +class AddPushoverTestCase(BaseTestCase): + def test_it_adds_channel(self): + self.client.login(username="alice@example.org", password="password") + + session = self.client.session + session["po_nonce"] = "n" + session.save() + + params = "pushover_user_key=a&nonce=n&prio=0" + r = self.client.get("/integrations/add_pushover/?%s" % params) + assert r.status_code == 302 + + channels = list(Channel.objects.all()) + assert len(channels) == 1 + assert channels[0].value == "a|0" + + @override_settings(PUSHOVER_API_TOKEN=None) + def test_it_requires_api_token(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_pushover/") + self.assertEqual(r.status_code, 404) + + def test_it_validates_priority(self): + self.client.login(username="alice@example.org", password="password") + + session = self.client.session + session["po_nonce"] = "n" + session.save() + + params = "pushover_user_key=a&nonce=n&prio=abc" + r = self.client.get("/integrations/add_pushover/?%s" % params) + assert r.status_code == 400 + + def test_it_validates_nonce(self): + self.client.login(username="alice@example.org", password="password") + + session = self.client.session + session["po_nonce"] = "n" + session.save() + + params = "pushover_user_key=a&nonce=INVALID&prio=0" + r = self.client.get("/integrations/add_pushover/?%s" % params) + assert r.status_code == 403 diff --git a/hc/front/tests/test_pushbullet_callback.py b/hc/front/tests/test_add_pushbullet.py similarity index 57% rename from hc/front/tests/test_pushbullet_callback.py rename to hc/front/tests/test_add_pushbullet.py index 55831a44..aec9649f 100644 --- a/hc/front/tests/test_pushbullet_callback.py +++ b/hc/front/tests/test_add_pushbullet.py @@ -7,10 +7,21 @@ from mock import patch @override_settings(PUSHBULLET_CLIENT_ID="t1", PUSHBULLET_CLIENT_SECRET="s1") -class PushbulletCallbackTestCase(BaseTestCase): +class AddPushbulletTestCase(BaseTestCase): + + def test_it_shows_instructions(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_pushbullet/") + self.assertContains(r, "www.pushbullet.com/authorize", status_code=200) + + @override_settings(PUSHBULLET_CLIENT_ID=None) + def test_it_requires_client_id(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_pushbullet/") + self.assertEqual(r.status_code, 404) @patch("hc.front.views.requests.post") - def test_it_works(self, mock_post): + def test_it_handles_oauth_response(self, mock_post): oauth_response = {"access_token": "test-token"} mock_post.return_value.text = json.dumps(oauth_response) diff --git a/hc/front/tests/test_slack_callback.py b/hc/front/tests/test_add_slack.py similarity index 60% rename from hc/front/tests/test_slack_callback.py rename to hc/front/tests/test_add_slack.py index db7016f0..b7456055 100644 --- a/hc/front/tests/test_slack_callback.py +++ b/hc/front/tests/test_add_slack.py @@ -1,14 +1,33 @@ import json +from django.test.utils import override_settings from hc.api.models import Channel from hc.test import BaseTestCase from mock import patch -class SlackCallbackTestCase(BaseTestCase): +class AddSlackTestCase(BaseTestCase): + + @override_settings(SLACK_CLIENT_ID=None) + def test_webhook_instructions_work(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_slack/") + self.assertContains(r, "Integration Settings", status_code=200) + + @override_settings(SLACK_CLIENT_ID="foo") + def test_slack_button(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_slack/") + self.assertContains(r, "slack.com/oauth/authorize", status_code=200) + + @override_settings(SLACK_CLIENT_ID="foo") + def test_landing_page(self): + r = self.client.get("/integrations/add_slack/") + self.assertContains(r, "Before adding Slack integration", + status_code=200) @patch("hc.front.views.requests.post") - def test_it_works(self, mock_post): + def test_it_handles_oauth_response(self, mock_post): oauth_response = { "ok": True, "team_name": "foo", @@ -34,7 +53,7 @@ class SlackCallbackTestCase(BaseTestCase): self.assertEqual(ch.slack_webhook_url, "http://example.org") @patch("hc.front.views.requests.post") - def test_it_handles_error(self, mock_post): + def test_it_handles_oauth_error(self, mock_post): oauth_response = { "ok": False, "error": "something went wrong" diff --git a/hc/front/tests/test_add_webhook.py b/hc/front/tests/test_add_webhook.py new file mode 100644 index 00000000..0570706b --- /dev/null +++ b/hc/front/tests/test_add_webhook.py @@ -0,0 +1,45 @@ +from hc.api.models import Channel +from hc.test import BaseTestCase + + +class AddWebhookTestCase(BaseTestCase): + + def test_it_adds_two_webhook_urls_and_redirects(self): + form = {"value_down": "http://foo.com", "value_up": "https://bar.com"} + + self.client.login(username="alice@example.org", password="password") + r = self.client.post("/integrations/add_webhook/", form) + self.assertRedirects(r, "/integrations/") + + c = Channel.objects.get() + self.assertEqual(c.value, "http://foo.com\nhttps://bar.com") + + def test_it_adds_webhook_using_team_access(self): + form = {"value_down": "http://foo.com", "value_up": "https://bar.com"} + + # Logging in as bob, not alice. Bob has team access so this + # should work. + self.client.login(username="bob@example.org", password="password") + self.client.post("/integrations/add_webhook/", form) + + c = Channel.objects.get() + self.assertEqual(c.user, self.alice) + self.assertEqual(c.value, "http://foo.com\nhttps://bar.com") + + def test_it_rejects_non_http_webhook_urls(self): + form = {"value_down": "foo", "value_up": "bar"} + + self.client.login(username="alice@example.org", password="password") + r = self.client.post("/integrations/add_webhook/", form) + self.assertContains(r, "Enter a valid URL.") + + self.assertEqual(Channel.objects.count(), 0) + + def test_it_handles_empty_down_url(self): + form = {"value_down": "", "value_up": "http://foo.com"} + + self.client.login(username="alice@example.org", password="password") + self.client.post("/integrations/add_webhook/", form) + + c = Channel.objects.get() + self.assertEqual(c.value, "\nhttp://foo.com") diff --git a/hc/front/views.py b/hc/front/views.py index 7bb76b89..b20194b1 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -8,7 +8,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.urlresolvers import reverse from django.db.models import Count -from django.http import HttpResponseBadRequest, HttpResponseForbidden +from django.http import HttpResponseBadRequest, HttpResponseForbidden, Http404 from django.shortcuts import get_object_or_404, redirect, render from django.utils import timezone from django.utils.crypto import get_random_string @@ -421,6 +421,9 @@ def add_hipchat(request): @login_required def add_pushbullet(request): + if settings.PUSHBULLET_CLIENT_ID is None: + raise Http404("pushbullet integration is not available") + if "code" in request.GET: code = request.GET.get("code", "") if len(code) < 8: @@ -464,7 +467,7 @@ def add_pushbullet(request): @login_required def add_pushover(request): if settings.PUSHOVER_API_TOKEN is None or settings.PUSHOVER_SUBSCRIPTION_URL is None: - return HttpResponseForbidden() + raise Http404("pushover integration is not available") if request.method == "POST": # Initiate the subscription