|
| 1 | +import httpretty |
1 | 2 | import pytest
|
2 | 3 | from django.urls import reverse
|
3 | 4 | from rest_framework import status
|
4 | 5 | from rest_framework.test import APIClient
|
5 | 6 |
|
| 7 | +from apps.api import permissions |
| 8 | +from apps.auth_token.tests.helpers import setup_service_account_api_mocks |
| 9 | + |
6 | 10 |
|
7 | 11 | @pytest.mark.django_db
|
8 | 12 | def test_get_escalation_chains(make_organization_and_user_with_token):
|
@@ -54,6 +58,43 @@ def test_create_escalation_chain(make_organization_and_user_with_token):
|
54 | 58 | assert response.data == expected_data
|
55 | 59 |
|
56 | 60 |
|
| 61 | +@pytest.mark.django_db |
| 62 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 63 | +def test_create_escalation_chain_via_service_account( |
| 64 | + make_organization, |
| 65 | + make_service_account_for_organization, |
| 66 | + make_token_for_service_account, |
| 67 | + make_team, |
| 68 | +): |
| 69 | + organization = make_organization(grafana_url="http://grafana.test") |
| 70 | + team = make_team(organization=organization) |
| 71 | + service_account = make_service_account_for_organization(organization) |
| 72 | + token_string = "glsa_token" |
| 73 | + make_token_for_service_account(service_account, token_string) |
| 74 | + |
| 75 | + perms = { |
| 76 | + permissions.RBACPermission.Permissions.ESCALATION_CHAINS_WRITE.value: ["*"], |
| 77 | + } |
| 78 | + setup_service_account_api_mocks(organization.grafana_url, perms) |
| 79 | + |
| 80 | + client = APIClient() |
| 81 | + url = reverse("api-public:escalation_chains-list") |
| 82 | + data = {"name": "test", "team_id": team.public_primary_key} |
| 83 | + response = client.post( |
| 84 | + url, |
| 85 | + data=data, |
| 86 | + format="json", |
| 87 | + HTTP_AUTHORIZATION=f"{token_string}", |
| 88 | + HTTP_X_GRAFANA_URL=organization.grafana_url, |
| 89 | + ) |
| 90 | + if not organization.is_rbac_permissions_enabled: |
| 91 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 92 | + else: |
| 93 | + assert response.status_code == status.HTTP_201_CREATED |
| 94 | + escalation_chain = organization.escalation_chains.get(name="test") |
| 95 | + assert escalation_chain.team == team |
| 96 | + |
| 97 | + |
57 | 98 | @pytest.mark.django_db
|
58 | 99 | def test_change_name(make_organization_and_user_with_token):
|
59 | 100 | organization, user, token = make_organization_and_user_with_token()
|
|
0 commit comments