2022-06-03 08:09:47 -06:00
|
|
|
import pytest
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
from apps.webhooks.models import Webhook
|
2022-06-17 15:41:46 +03:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2023-08-22 14:05:52 -06:00
|
|
|
def test_get_custom_actions(make_organization_and_user_with_token, make_custom_webhook):
|
2022-06-03 08:09:47 -06:00
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = make_custom_webhook(organization=organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
|
|
|
|
expected_payload = {
|
|
|
|
|
"count": 1,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-06-17 15:41:46 +03:00
|
|
|
"data": custom_action.data,
|
|
|
|
|
"user": custom_action.user,
|
|
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
],
|
augment API response pagination attributes (#2471)
# What this PR does
This PR:
- adds a few attributes to paginated API responses
- removes channel filter "send demo alert" internal API endpoint + tests
(this endpoint was marked as deprecated + not consumed by the web UI)
With the new paginated API response schema, the web UI will no longer
need to:
- hardcode `ITEMS_PER_PAGE` for each table
- manually calculate total number of pages
(these two things ☝️ will be done in
https://github.com/grafana/oncall/issues/2476)
For `GET /api/internal/v1/alertgroups` the response will now look like
this:
```diff
{
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>
}
```
For all other paginated API responses, the response will now look like:
```diff
{
"count": <int>,
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>,
++ "current_page_number": <int>,
++ "total_pages": <int>
}
```
## TODO
- [x] update public API docs to include these new attributes
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-14 17:19:40 +02:00
|
|
|
"current_page_number": 1,
|
|
|
|
|
"page_size": 50,
|
|
|
|
|
"total_pages": 1,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.data == expected_payload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_custom_actions_filter_by_name(
|
|
|
|
|
make_organization_and_user_with_token,
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook,
|
2022-06-03 08:09:47 -06:00
|
|
|
):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = make_custom_webhook(organization=organization)
|
|
|
|
|
make_custom_webhook(organization=organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
response = client.get(f"{url}?name={custom_action.name}", format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
|
|
|
|
expected_payload = {
|
|
|
|
|
"count": 1,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-06-17 15:41:46 +03:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-06-17 15:41:46 +03:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
],
|
augment API response pagination attributes (#2471)
# What this PR does
This PR:
- adds a few attributes to paginated API responses
- removes channel filter "send demo alert" internal API endpoint + tests
(this endpoint was marked as deprecated + not consumed by the web UI)
With the new paginated API response schema, the web UI will no longer
need to:
- hardcode `ITEMS_PER_PAGE` for each table
- manually calculate total number of pages
(these two things ☝️ will be done in
https://github.com/grafana/oncall/issues/2476)
For `GET /api/internal/v1/alertgroups` the response will now look like
this:
```diff
{
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>
}
```
For all other paginated API responses, the response will now look like:
```diff
{
"count": <int>,
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>,
++ "current_page_number": <int>,
++ "total_pages": <int>
}
```
## TODO
- [x] update public API docs to include these new attributes
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-14 17:19:40 +02:00
|
|
|
"current_page_number": 1,
|
|
|
|
|
"page_size": 50,
|
|
|
|
|
"total_pages": 1,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.data == expected_payload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_custom_actions_filter_by_name_empty_result(
|
|
|
|
|
make_organization_and_user_with_token,
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook,
|
2022-06-03 08:09:47 -06:00
|
|
|
):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook(organization=organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
response = client.get(f"{url}?name=NonExistentName", format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
augment API response pagination attributes (#2471)
# What this PR does
This PR:
- adds a few attributes to paginated API responses
- removes channel filter "send demo alert" internal API endpoint + tests
(this endpoint was marked as deprecated + not consumed by the web UI)
With the new paginated API response schema, the web UI will no longer
need to:
- hardcode `ITEMS_PER_PAGE` for each table
- manually calculate total number of pages
(these two things ☝️ will be done in
https://github.com/grafana/oncall/issues/2476)
For `GET /api/internal/v1/alertgroups` the response will now look like
this:
```diff
{
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>
}
```
For all other paginated API responses, the response will now look like:
```diff
{
"count": <int>,
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>,
++ "current_page_number": <int>,
++ "total_pages": <int>
}
```
## TODO
- [x] update public API docs to include these new attributes
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-14 17:19:40 +02:00
|
|
|
expected_payload = {
|
|
|
|
|
"count": 0,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [],
|
|
|
|
|
"current_page_number": 1,
|
|
|
|
|
"page_size": 50,
|
|
|
|
|
"total_pages": 1,
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.data == expected_payload
|
2022-06-17 15:41:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_custom_action(
|
|
|
|
|
make_organization_and_user_with_token,
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook,
|
2022-06-17 15:41:46 +03:00
|
|
|
):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = make_custom_webhook(organization=organization)
|
2022-06-17 15:41:46 +03:00
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-detail", kwargs={"pk": custom_action.public_primary_key})
|
|
|
|
|
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
|
|
|
|
expected_payload = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-06-17 15:41:46 +03:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-06-17 15:41:46 +03:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-06-17 15:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.data == expected_payload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2023-09-26 08:54:41 -06:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"data",
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "Test outgoing webhook",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "Test outgoing webhook",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
"user": None,
|
|
|
|
|
"password": None,
|
|
|
|
|
"data": None,
|
|
|
|
|
"authorization_header": None,
|
|
|
|
|
"forward_whole_payload": True,
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "Test outgoing webhook",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
"user": "",
|
|
|
|
|
"password": "",
|
|
|
|
|
"data": "",
|
|
|
|
|
"authorization_header": "",
|
|
|
|
|
"forward_whole_payload": True,
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_create_custom_action(make_organization_and_user_with_token, data):
|
2022-06-17 15:41:46 +03:00
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = Webhook.objects.get(public_primary_key=response.data["id"])
|
2022-06-17 15:41:46 +03:00
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-06-17 15:41:46 +03:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-06-17 15:41:46 +03:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-06-17 15:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
|
assert response.data == expected_result
|
|
|
|
|
|
|
|
|
|
|
2022-10-19 12:32:21 +01:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_create_custom_action_nested_data(make_organization_and_user_with_token):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"name": "Test outgoing webhook with nested data",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
# Assert that nested field access still works as long as the variable
|
|
|
|
|
# is quoted, making it valid JSON.
|
|
|
|
|
# This ensures backwards compatibility from when templates were required
|
|
|
|
|
# to be JSON.
|
|
|
|
|
"data": '{"nested_item": "{{ alert_payload.foo.bar }}"}',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = Webhook.objects.get(public_primary_key=response.data["id"])
|
2022-10-19 12:32:21 +01:00
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-10-19 12:32:21 +01:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-10-19 12:32:21 +01:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-10-19 12:32:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
|
assert response.json() == expected_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_create_custom_action_valid_after_render(make_organization_and_user_with_token):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"name": "Test outgoing webhook with nested data",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
# Assert that nested field access still works as long as the variable
|
|
|
|
|
# is quoted, making it valid JSON.
|
|
|
|
|
# This ensures backwards compatibility from when templates were required
|
|
|
|
|
# to be JSON.
|
|
|
|
|
"data": '{"name": "{{ alert_payload.name }}", "labels": {{ alert_payload.labels | tojson }}}',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = Webhook.objects.get(public_primary_key=response.data["id"])
|
2022-10-19 12:32:21 +01:00
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-10-19 12:32:21 +01:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-10-19 12:32:21 +01:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-10-19 12:32:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
|
assert response.json() == expected_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_create_custom_action_valid_after_render_use_all_data(make_organization_and_user_with_token):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-list")
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"name": "Test outgoing webhook with nested data",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
# Assert that nested field access still works as long as the variable
|
|
|
|
|
# is quoted, making it valid JSON.
|
|
|
|
|
# This ensures backwards compatibility from when templates were required
|
|
|
|
|
# to be JSON.
|
|
|
|
|
"data": "{{ alert_payload | tojson }}",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = Webhook.objects.get(public_primary_key=response.data["id"])
|
2022-10-19 12:32:21 +01:00
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
|
|
|
|
"name": custom_action.name,
|
|
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-10-19 12:32:21 +01:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-10-19 12:32:21 +01:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-10-19 12:32:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
|
assert response.json() == expected_result
|
|
|
|
|
|
|
|
|
|
|
2022-06-17 15:41:46 +03:00
|
|
|
@pytest.mark.django_db
|
2023-10-03 08:33:05 -06:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"data",
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "RENAMED",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "RENAMED 1",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
"user": None,
|
|
|
|
|
"password": None,
|
|
|
|
|
"data": None,
|
|
|
|
|
"authorization_header": None,
|
|
|
|
|
"forward_whole_payload": True,
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
"name": "RENAMED 2",
|
|
|
|
|
"url": "https://example.com",
|
|
|
|
|
"user": "",
|
|
|
|
|
"password": "",
|
|
|
|
|
"data": "",
|
|
|
|
|
"authorization_header": "",
|
|
|
|
|
"forward_whole_payload": True,
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2022-06-17 15:41:46 +03:00
|
|
|
def test_update_custom_action(
|
|
|
|
|
make_organization_and_user_with_token,
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook,
|
2023-10-03 08:33:05 -06:00
|
|
|
data,
|
2022-06-17 15:41:46 +03:00
|
|
|
):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = make_custom_webhook(organization=organization)
|
2022-06-17 15:41:46 +03:00
|
|
|
|
|
|
|
|
url = reverse("api-public:actions-detail", kwargs={"pk": custom_action.public_primary_key})
|
|
|
|
|
assert custom_action.name != data["name"]
|
|
|
|
|
|
|
|
|
|
response = client.put(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
2023-10-03 08:33:05 -06:00
|
|
|
custom_action.refresh_from_db()
|
2022-06-17 15:41:46 +03:00
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
|
"id": custom_action.public_primary_key,
|
2023-10-03 08:33:05 -06:00
|
|
|
"name": custom_action.name,
|
2022-06-17 15:41:46 +03:00
|
|
|
"team_id": None,
|
2023-08-22 14:05:52 -06:00
|
|
|
"url": custom_action.url,
|
2022-06-17 15:41:46 +03:00
|
|
|
"data": custom_action.data,
|
2023-08-22 14:05:52 -06:00
|
|
|
"user": custom_action.username,
|
2022-06-17 15:41:46 +03:00
|
|
|
"password": custom_action.password,
|
|
|
|
|
"authorization_header": custom_action.authorization_header,
|
2023-08-22 14:05:52 -06:00
|
|
|
"forward_whole_payload": custom_action.forward_all,
|
|
|
|
|
"is_webhook_enabled": custom_action.is_webhook_enabled,
|
|
|
|
|
"trigger_template": custom_action.trigger_template,
|
|
|
|
|
"headers": custom_action.headers,
|
|
|
|
|
"http_method": custom_action.http_method,
|
|
|
|
|
"trigger_type": Webhook.PUBLIC_TRIGGER_TYPES_MAP[custom_action.trigger_type],
|
2024-02-23 08:55:44 -03:00
|
|
|
"integration_filter": [i.public_primary_key for i in custom_action.filtered_integrations.all()] or None,
|
2022-06-17 15:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.data == expected_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_delete_custom_action(
|
|
|
|
|
make_organization_and_user_with_token,
|
2023-08-22 14:05:52 -06:00
|
|
|
make_custom_webhook,
|
2022-06-17 15:41:46 +03:00
|
|
|
):
|
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
2023-08-22 14:05:52 -06:00
|
|
|
custom_action = make_custom_webhook(organization=organization)
|
2022-06-17 15:41:46 +03:00
|
|
|
url = reverse("api-public:actions-detail", kwargs={"pk": custom_action.public_primary_key})
|
|
|
|
|
|
|
|
|
|
assert custom_action.deleted_at is None
|
|
|
|
|
|
|
|
|
|
response = client.delete(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
|
|
|
|
|
|
|
|
|
custom_action.refresh_from_db()
|
|
|
|
|
assert custom_action.deleted_at is not None
|
|
|
|
|
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
assert response.data["detail"] == "Not found."
|