Skip to content

Commit

Permalink
Enable new webhooks preview per org (#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
mderynck authored Mar 14, 2023
1 parent 44e4b22 commit e089e29
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
20 changes: 15 additions & 5 deletions engine/apps/api/tests/test_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from unittest import mock
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -105,8 +106,9 @@ def test_get_detail_webhook(webhook_internal_api_setup, make_user_auth_headers):
assert response.json() == expected_payload


@mock.patch("apps.api.views.webhooks.WebhooksView.check_webhooks_2_enabled")
@pytest.mark.django_db
def test_create_webhook(webhook_internal_api_setup, make_user_auth_headers):
def test_create_webhook(mocked_check_webhooks_2_enabled, webhook_internal_api_setup, make_user_auth_headers):
user, token, webhook = webhook_internal_api_setup
client = APIClient()
url = reverse("api-internal:webhooks-list")
Expand Down Expand Up @@ -146,6 +148,7 @@ def test_create_webhook(webhook_internal_api_setup, make_user_auth_headers):
assert webhook.user == user


@mock.patch("apps.api.views.webhooks.WebhooksView.check_webhooks_2_enabled")
@pytest.mark.django_db
@pytest.mark.parametrize(
"field_name,value",
Expand All @@ -156,7 +159,9 @@ def test_create_webhook(webhook_internal_api_setup, make_user_auth_headers):
("url", "https://myserver/{{ alert_payload.id }}/triggered"),
],
)
def test_create_valid_templated_field(webhook_internal_api_setup, make_user_auth_headers, field_name, value):
def test_create_valid_templated_field(
mocked_check_webhooks_2_enabled, webhook_internal_api_setup, make_user_auth_headers, field_name, value
):
user, token, webhook = webhook_internal_api_setup
client = APIClient()
url = reverse("api-internal:webhooks-list")
Expand Down Expand Up @@ -199,6 +204,7 @@ def test_create_valid_templated_field(webhook_internal_api_setup, make_user_auth
assert response.json() == expected_response


@mock.patch("apps.api.views.webhooks.WebhooksView.check_webhooks_2_enabled")
@pytest.mark.django_db
@pytest.mark.parametrize(
"field_name,value",
Expand All @@ -209,7 +215,9 @@ def test_create_valid_templated_field(webhook_internal_api_setup, make_user_auth
("url", "invalid-url/{{}}/triggered"),
],
)
def test_create_invalid_templated_field(webhook_internal_api_setup, make_user_auth_headers, field_name, value):
def test_create_invalid_templated_field(
mocked_check_webhooks_2_enabled, webhook_internal_api_setup, make_user_auth_headers, field_name, value
):
user, token, webhook = webhook_internal_api_setup
client = APIClient()
url = reverse("api-internal:webhooks-list")
Expand All @@ -226,8 +234,9 @@ def test_create_invalid_templated_field(webhook_internal_api_setup, make_user_au
assert response.status_code == status.HTTP_400_BAD_REQUEST


@mock.patch("apps.api.views.webhooks.WebhooksView.check_webhooks_2_enabled")
@pytest.mark.django_db
def test_update_webhook(webhook_internal_api_setup, make_user_auth_headers):
def test_update_webhook(mocked_check_webhooks_2_enabled, webhook_internal_api_setup, make_user_auth_headers):
user, token, webhook = webhook_internal_api_setup
client = APIClient()
url = reverse("api-internal:webhooks-detail", kwargs={"pk": webhook.public_primary_key})
Expand All @@ -246,8 +255,9 @@ def test_update_webhook(webhook_internal_api_setup, make_user_auth_headers):
assert updated_instance.name == "github_button_updated"


@mock.patch("apps.api.views.webhooks.WebhooksView.check_webhooks_2_enabled")
@pytest.mark.django_db
def test_delete_webhook(webhook_internal_api_setup, make_user_auth_headers):
def test_delete_webhook(mocked_check_webhooks_2_enabled, webhook_internal_api_setup, make_user_auth_headers):
user, token, webhook = webhook_internal_api_setup
client = APIClient()
url = reverse("api-internal:webhooks-detail", kwargs={"pk": webhook.public_primary_key})
Expand Down
28 changes: 27 additions & 1 deletion engine/apps/api/views/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.exceptions import ObjectDoesNotExist
from django.apps import apps
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from rest_framework.exceptions import NotFound
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
Expand Down Expand Up @@ -56,3 +57,28 @@ def get_object_from_organization(self):
self.check_object_permissions(self.request, obj)

return obj

def perform_create(self, serializer):
self.check_webhooks_2_enabled()
serializer.save()

def perform_update(self, serializer):
self.check_webhooks_2_enabled()
serializer.save()

def perform_destroy(self, instance):
self.check_webhooks_2_enabled()
instance.delete()

def check_webhooks_2_enabled(self):
DynamicSetting = apps.get_model("base", "DynamicSetting")
enabled_webhooks_2_orgs = DynamicSetting.objects.get_or_create(
name="enabled_webhooks_2_orgs",
defaults={
"json_value": {
"org_ids": [],
}
},
)[0]
if self.request.auth.organization.pk not in enabled_webhooks_2_orgs.json_value["org_ids"]:
raise PermissionDenied("Webhooks 2 not enabled for organization. Permission denied.")

0 comments on commit e089e29

Please sign in to comment.