def test_github_signature_fail(self): """ Error if we're to check github signatures, but they're missing entirely """ session = MockPublisherSession(self) resource = WebhookResource( options={ u"topic": u"com.test.webhook", u"github_secret": u"deadbeef", }, session=session, ) request = yield renderResource( resource, b"/", method=b"POST", headers={b"Content-Type": []}, body=b'{"foo": "has happened"}') self.assertEqual(len(session._published_messages), 0) self.assertEqual(request.code, 400) received_json = json.loads(request.get_written_data().decode('utf8')) self.assertEqual( received_json, { "error": "Malformed request to the REST bridge.", "args": [], "kwargs": {} } )
def test_basic(self): """ A message, when a request has gone through to it, publishes a WAMP message on the configured topic. """ session = MockPublisherSession(self) resource = WebhookResource({u"topic": u"com.test.webhook"}, session) with LogCapturer() as l: request = yield renderResource( resource, b"/", method=b"POST", headers={b"Content-Type": []}, body=b'{"foo": "has happened"}') self.assertEqual(len(session._published_messages), 1) self.assertEqual( { u"body": u'{"foo": "has happened"}', u"headers": { u"Content-Type": [], u'Date': [u'Sun, 1 Jan 2013 15:21:01 GMT'], u'Host': [u'localhost:8000'] } }, session._published_messages[0]["args"][0]) self.assertEqual(request.code, 202) self.assertEqual(request.get_written_data(), b"OK") logs = l.get_category("AR201") self.assertEqual(len(logs), 1) self.assertEqual(logs[0]["code"], 202)
def test_github_signature_invalid(self): """ A correctly-formatted GitHub signature, but it's invalid """ session = MockPublisherSession(self) resource = WebhookResource( options={ "topic": "com.test.webhook", "github_secret": "deadbeef", }, session=session, ) request = yield renderResource(resource, b"/", method=b"POST", headers={b"Content-Type": []}, body=b'{"foo": "has happened"}') self.assertEqual(len(session._published_messages), 0) self.assertEqual(request.code, 400) received_json = json.loads(request.get_written_data().decode('utf8')) self.assertEqual(received_json, { "error": "Malformed request to the REST bridge.", "args": [], "kwargs": {}, })
def test_github_signature_valid(self): """ A correctly-formatted GitHub signature """ session = MockPublisherSession(self) resource = WebhookResource( options={ u"topic": u"com.test.webhook", u"github_secret": github_test_token, }, session=session, ) yield renderResource( resource, b"/webhook", method=b"POST", headers={ b"Content-Type": [], b"X-Hub-Signature": [b"sha1=5054d1d2e6f5d293fbea8fdeed5117f2854ccf7a"], }, body=github_request_data, ) self.assertEqual(len(session._published_messages), 1) msg = session._published_messages[0] data = msg['args'][0] self.assertEqual( data['body'].encode('utf8'), github_request_data.strip(), )
def create(transport, path, config): personality = transport.worker.personality personality.WEB_SERVICE_CHECKERS['webhook'](personality, config) # create a vanilla session: the webhook will use this to inject events # webhook_session_config = ComponentConfig(realm=config['realm'], extra=None) webhook_session = ApplicationSession(webhook_session_config) # add the webhook session to the router # transport._worker._router_session_factory.add(webhook_session, authrole=config.get('role', 'anonymous')) # now create the webhook Twisted Web resource # resource = WebhookResource(config.get('options', {}), webhook_session) return RouterWebServiceWebhook(transport, path, config, resource)