Merge pull request #120 from grafana/outgiong-webhook-public-api

Rename field `webhook` to `url` for outgoing webhook public api endpoint
This commit is contained in:
Yulya Artyukhina 2022-06-21 13:29:01 +03:00 committed by GitHub
commit 2e812293c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -14,6 +14,7 @@ class ActionCreateSerializer(serializers.ModelSerializer):
id = serializers.CharField(read_only=True, source="public_primary_key")
organization = serializers.HiddenField(default=CurrentOrganizationDefault())
team_id = TeamPrimaryKeyRelatedField(required=False, allow_null=True, source="team")
url = serializers.CharField(required=True, allow_null=False, allow_blank=False, source="webhook")
class Meta:
model = CustomButton
@ -22,7 +23,7 @@ class ActionCreateSerializer(serializers.ModelSerializer):
"name",
"organization",
"team_id",
"webhook",
"url",
"data",
"user",
"password",
@ -31,7 +32,6 @@ class ActionCreateSerializer(serializers.ModelSerializer):
]
extra_kwargs = {
"name": {"required": True, "allow_null": False, "allow_blank": False},
"webhook": {"required": True, "allow_null": False, "allow_blank": False},
"data": {"required": False, "allow_null": True, "allow_blank": False},
"user": {"required": False, "allow_null": True, "allow_blank": False},
"password": {"required": False, "allow_null": True, "allow_blank": False},
@ -41,13 +41,13 @@ class ActionCreateSerializer(serializers.ModelSerializer):
validators = [UniqueTogetherValidator(queryset=CustomButton.objects.all(), fields=["name", "organization"])]
def validate_webhook(self, webhook):
if webhook:
def validate_url(self, url):
if url:
try:
URLValidator()(webhook)
URLValidator()(url)
except ValidationError:
raise serializers.ValidationError("Webhook is incorrect")
return webhook
raise serializers.ValidationError("URL is incorrect")
return url
return None
def validate_data(self, data):
@ -74,12 +74,12 @@ class ActionCreateSerializer(serializers.ModelSerializer):
class ActionUpdateSerializer(ActionCreateSerializer):
team_id = TeamPrimaryKeyRelatedField(source="team", read_only=True)
url = serializers.CharField(required=False, allow_null=False, allow_blank=False, source="webhook")
class Meta(ActionCreateSerializer.Meta):
extra_kwargs = {
"name": {"required": False, "allow_null": False, "allow_blank": False},
"webhook": {"required": False, "allow_null": False, "allow_blank": False},
"data": {"required": False, "allow_null": True, "allow_blank": False},
"user": {"required": False, "allow_null": True, "allow_blank": False},
"password": {"required": False, "allow_null": True, "allow_blank": False},

View file

@ -30,7 +30,7 @@ def test_get_custom_actions(
"id": custom_action.public_primary_key,
"name": custom_action.name,
"team_id": None,
"webhook": custom_action.webhook,
"url": custom_action.webhook,
"data": custom_action.data,
"user": custom_action.user,
"password": custom_action.password,
@ -68,7 +68,7 @@ def test_get_custom_actions_filter_by_name(
"id": custom_action.public_primary_key,
"name": custom_action.name,
"team_id": None,
"webhook": custom_action.webhook,
"url": custom_action.webhook,
"data": custom_action.data,
"user": custom_action.user,
"password": custom_action.password,
@ -122,7 +122,7 @@ def test_get_custom_action(
"id": custom_action.public_primary_key,
"name": custom_action.name,
"team_id": None,
"webhook": custom_action.webhook,
"url": custom_action.webhook,
"data": custom_action.data,
"user": custom_action.user,
"password": custom_action.password,
@ -144,7 +144,7 @@ def test_create_custom_action(make_organization_and_user_with_token):
data = {
"name": "Test outgoing webhook",
"webhook": "https://example.com",
"url": "https://example.com",
}
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
@ -155,7 +155,7 @@ def test_create_custom_action(make_organization_and_user_with_token):
"id": custom_action.public_primary_key,
"name": custom_action.name,
"team_id": None,
"webhook": custom_action.webhook,
"url": custom_action.webhook,
"data": custom_action.data,
"user": custom_action.user,
"password": custom_action.password,
@ -179,13 +179,13 @@ def test_create_custom_action_invalid_data(
data = {
"name": "Test outgoing webhook",
"webhook": "invalid_url",
"url": "invalid_url",
}
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.data["webhook"][0] == "Webhook is incorrect"
assert response.data["url"][0] == "URL is incorrect"
data = {
"name": "Test outgoing webhook",
@ -194,10 +194,10 @@ def test_create_custom_action_invalid_data(
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.data["webhook"][0] == "This field is required."
assert response.data["url"][0] == "This field is required."
data = {
"webhook": "https://example.com",
"url": "https://example.com",
}
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
@ -231,7 +231,7 @@ def test_update_custom_action(
"id": custom_action.public_primary_key,
"name": data["name"],
"team_id": None,
"webhook": custom_action.webhook,
"url": custom_action.webhook,
"data": custom_action.data,
"user": custom_action.user,
"password": custom_action.password,