Group testcases by view functions

This commit is contained in:
Pēteris Caune 2016-07-30 19:32:33 +03:00
parent da24bcd6aa
commit bff4eb4f80
6 changed files with 137 additions and 110 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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