|
|
@ -1,6 +1,3 @@ |
|
|
|
import json |
|
|
|
|
|
|
|
from django.core import signing |
|
|
|
from hc.api.models import Channel |
|
|
|
from hc.test import BaseTestCase |
|
|
|
from mock import patch |
|
|
@ -14,38 +11,26 @@ class AddHipChatTestCase(BaseTestCase): |
|
|
|
r = self.client.get(self.url) |
|
|
|
self.assertContains(r, "appropriate HipChat room") |
|
|
|
|
|
|
|
def test_instructions_work_when_logged_out(self): |
|
|
|
r = self.client.get(self.url) |
|
|
|
self.assertContains(r, "Before adding HipChat integration, please") |
|
|
|
|
|
|
|
def test_it_redirects_to_addons_install(self): |
|
|
|
self.client.login(username="[email protected]", password="password") |
|
|
|
r = self.client.post(self.url) |
|
|
|
self.assertEqual(r.status_code, 302) |
|
|
|
|
|
|
|
def test_it_returns_capabilities(self): |
|
|
|
r = self.client.get("/integrations/hipchat/capabilities/") |
|
|
|
self.assertContains(r, "callbackUrl") |
|
|
|
self.assertContains(r, "installedUrl") |
|
|
|
|
|
|
|
@patch("hc.api.models.Channel.refresh_hipchat_access_token") |
|
|
|
def test_callback_works(self, mock_refresh): |
|
|
|
state = signing.TimestampSigner().sign("alice") |
|
|
|
payload = json.dumps({"relayState": state, "foo": "foobar"}) |
|
|
|
@patch("hc.front.views.Channel.refresh_hipchat_access_token") |
|
|
|
@patch("hc.front.views.requests.get") |
|
|
|
def test_it_adds_channel(self, mock_get, mock_refresh): |
|
|
|
mock_get.return_value.json.return_value = { |
|
|
|
"oauthId": "test-id" |
|
|
|
} |
|
|
|
mock_get.return_value.text = "{}" |
|
|
|
|
|
|
|
r = self.client.post("/integrations/hipchat/callback/", payload, |
|
|
|
content_type="application/json") |
|
|
|
self.client.login(username="[email protected]", password="password") |
|
|
|
|
|
|
|
s = "https://api.hipchat.com/foo" |
|
|
|
r = self.client.post(self.url + "?installable_url=%s" % s) |
|
|
|
self.assertEqual(r.status_code, 302) |
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
self.assertTrue(mock_refresh.called) |
|
|
|
|
|
|
|
c = Channel.objects.get() |
|
|
|
self.assertEqual(c.kind, "hipchat") |
|
|
|
self.assertTrue("foobar" in c.value) |
|
|
|
|
|
|
|
@patch("hc.api.models.Channel.refresh_hipchat_access_token") |
|
|
|
def test_callback_rejects_bad_signature(self, mock_refresh): |
|
|
|
payload = json.dumps({"relayState": "alice:bad:sig", "foo": "foobar"}) |
|
|
|
|
|
|
|
r = self.client.post("/integrations/hipchat/callback/", payload, |
|
|
|
content_type="application/json") |
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 400) |
|
|
|
self.assertEqual(c.value, "{}") |