From 2a1e1fbbb74c66761079778ddf48e5c66ee7be50 Mon Sep 17 00:00:00 2001 From: Sergio Isidoro Date: Sun, 26 Jan 2020 11:19:33 +0200 Subject: [PATCH 1/2] Support for init_app Fixes: #24 --- github_webhook/webhook.py | 13 +++++++++++-- tests/test_webhook.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 85787d3..0a8b6c2 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -16,11 +16,20 @@ class Webhook(object): :param secret: Optional secret, used to authenticate the hook comes from Github """ - def __init__(self, app, endpoint="/postreceive", secret=None): - app.add_url_rule(rule=endpoint, endpoint=endpoint, view_func=self._postreceive, methods=["POST"]) + def __init__(self, app=None, endpoint="/postreceive", secret=None): + self.app = app + self.set_secret(secret) + if app is not None: + self.init_app(app, endpoint, secret) + def init_app(self, app, endpoint="/postreceive", secret=None): self._hooks = collections.defaultdict(list) self._logger = logging.getLogger("webhook") + if secret is not None: + self.set_secret(secret) + app.add_url_rule(rule=endpoint, endpoint=endpoint, view_func=self._postreceive, methods=["POST"]) + + def set_secret(self, secret=None): if secret is not None and not isinstance(secret, six.binary_type): secret = secret.encode("utf-8") self._secret = secret diff --git a/tests/test_webhook.py b/tests/test_webhook.py index b6e64a2..b6527b5 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -65,6 +65,44 @@ def test_constructor(): ) +def test_init_app_flow(): + # GIVEN + app = mock.Mock() + + # WHEN + webhook = Webhook() + webhook.init_app(app) + + # THEN + app.add_url_rule.assert_called_once_with( + endpoint="/postreceive", rule="/postreceive", view_func=webhook._postreceive, methods=["POST"] + ) + + +def test_init_app_flow_should_not_accidentally_override_secrets(): + # GIVEN + app = mock.Mock() + + # WHEN + webhook = Webhook(secret="hello-world-of-secrecy") + webhook.init_app(app) + + # THEN + assert webhook._secret is not None + + +def test_init_app_flow_should_override_secrets(): + # GIVEN + app = mock.Mock() + + # WHEN + webhook = Webhook(secret="hello-world-of-secrecy") + webhook.init_app(app, secret="a-new-world-of-secrecy") + + # THEN + assert webhook._secret == "a-new-world-of-secrecy".encode("utf-8") + + def test_run_push_hook(webhook, handler, push_request): # WHEN webhook._postreceive() From ddd33ac743dbac35222057f95cafa91bff78652a Mon Sep 17 00:00:00 2001 From: Alex Chamberlain Date: Fri, 28 Feb 2020 09:18:40 +0000 Subject: [PATCH 2/2] Use property instead of setter Signed-off-by: Alex Chamberlain --- github_webhook/webhook.py | 11 ++++++++--- tests/test_webhook.py | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 0a8b6c2..f4ca352 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -18,7 +18,7 @@ class Webhook(object): def __init__(self, app=None, endpoint="/postreceive", secret=None): self.app = app - self.set_secret(secret) + self.secret = secret if app is not None: self.init_app(app, endpoint, secret) @@ -26,10 +26,15 @@ def init_app(self, app, endpoint="/postreceive", secret=None): self._hooks = collections.defaultdict(list) self._logger = logging.getLogger("webhook") if secret is not None: - self.set_secret(secret) + self.secret = secret app.add_url_rule(rule=endpoint, endpoint=endpoint, view_func=self._postreceive, methods=["POST"]) - def set_secret(self, secret=None): + @property + def secret(self): + return self._secret + + @secret.setter + def secret(self, secret): if secret is not None and not isinstance(secret, six.binary_type): secret = secret.encode("utf-8") self._secret = secret diff --git a/tests/test_webhook.py b/tests/test_webhook.py index b6527b5..29eba7d 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -88,7 +88,7 @@ def test_init_app_flow_should_not_accidentally_override_secrets(): webhook.init_app(app) # THEN - assert webhook._secret is not None + assert webhook.secret is not None def test_init_app_flow_should_override_secrets(): @@ -100,7 +100,7 @@ def test_init_app_flow_should_override_secrets(): webhook.init_app(app, secret="a-new-world-of-secrecy") # THEN - assert webhook._secret == "a-new-world-of-secrecy".encode("utf-8") + assert webhook.secret == "a-new-world-of-secrecy".encode("utf-8") def test_run_push_hook(webhook, handler, push_request):