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
|
|
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import LegacyAccessControlRole
|
2022-06-22 14:46:23 -03:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def user_public_api_setup(
|
|
|
|
|
make_organization_and_user_with_slack_identities,
|
|
|
|
|
make_public_api_token,
|
|
|
|
|
):
|
|
|
|
|
organization, user, slack_team_identity, slack_user_identity = make_organization_and_user_with_slack_identities()
|
|
|
|
|
_, token = make_public_api_token(user, organization)
|
|
|
|
|
return organization, user, token, slack_team_identity, slack_user_identity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_user(
|
|
|
|
|
user_public_api_setup,
|
|
|
|
|
):
|
2022-11-29 09:41:56 +01:00
|
|
|
_, user, token, slack_team_identity, slack_user_identity = user_public_api_setup
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:users-detail", args=[user.public_primary_key])
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=token)
|
|
|
|
|
|
|
|
|
|
expected_response = {
|
|
|
|
|
"id": user.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user.user_id,
|
2022-06-03 08:09:47 -06:00
|
|
|
"email": user.email,
|
|
|
|
|
"slack": {"user_id": slack_user_identity.slack_id, "team_id": slack_team_identity.slack_id},
|
|
|
|
|
"username": user.username,
|
|
|
|
|
"role": "admin",
|
|
|
|
|
"is_phone_number_verified": False,
|
2023-11-10 22:17:11 +05:30
|
|
|
"timezone": user.timezone,
|
2024-02-01 17:16:57 -03:00
|
|
|
"teams": [],
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.json() == expected_response
|
|
|
|
|
|
|
|
|
|
# get current user
|
|
|
|
|
url = reverse("api-public:users-detail", args=["current"])
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=token)
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.json() == expected_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_users_list(
|
|
|
|
|
user_public_api_setup,
|
2024-02-01 17:16:57 -03:00
|
|
|
make_team,
|
2022-06-03 08:09:47 -06:00
|
|
|
make_user_for_organization,
|
|
|
|
|
):
|
|
|
|
|
organization, user_1, token, slack_team_identity, slack_user_identity = user_public_api_setup
|
2024-02-01 17:16:57 -03:00
|
|
|
team = make_team(organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
user_2 = make_user_for_organization(organization)
|
2024-02-01 17:16:57 -03:00
|
|
|
user_2.teams.add(team)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:users-list")
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=token)
|
|
|
|
|
|
|
|
|
|
expected_response = {
|
|
|
|
|
"count": 2,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
|
|
|
|
"id": user_1.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user_1.user_id,
|
2022-06-03 08:09:47 -06:00
|
|
|
"email": user_1.email,
|
|
|
|
|
"slack": {"user_id": slack_user_identity.slack_id, "team_id": slack_team_identity.slack_id},
|
|
|
|
|
"username": user_1.username,
|
|
|
|
|
"role": "admin",
|
|
|
|
|
"is_phone_number_verified": False,
|
2023-11-10 22:17:11 +05:30
|
|
|
"timezone": user_1.timezone,
|
2024-02-01 17:16:57 -03:00
|
|
|
"teams": [],
|
2022-06-03 08:09:47 -06:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": user_2.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user_2.user_id,
|
2022-06-03 08:09:47 -06:00
|
|
|
"email": user_2.email,
|
|
|
|
|
"slack": None,
|
|
|
|
|
"username": user_2.username,
|
|
|
|
|
"role": "admin",
|
|
|
|
|
"is_phone_number_verified": False,
|
2023-11-10 22:17:11 +05:30
|
|
|
"timezone": user_2.timezone,
|
2024-02-01 17:16:57 -03:00
|
|
|
"teams": [team.public_primary_key],
|
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": 100,
|
|
|
|
|
"total_pages": 1,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.json() == expected_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_users_list_short(
|
|
|
|
|
user_public_api_setup,
|
|
|
|
|
make_user_for_organization,
|
|
|
|
|
):
|
2022-11-29 09:41:56 +01:00
|
|
|
organization, user_1, token, _, _ = user_public_api_setup
|
2022-06-03 08:09:47 -06:00
|
|
|
user_2 = make_user_for_organization(organization)
|
|
|
|
|
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:users-list")
|
|
|
|
|
response = client.get(f"{url}?short=true", format="json", HTTP_AUTHORIZATION=token)
|
|
|
|
|
|
|
|
|
|
expected_response = {
|
|
|
|
|
"count": 2,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
|
|
|
|
"id": user_1.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user_1.user_id,
|
2022-06-03 08:09:47 -06:00
|
|
|
"email": user_1.email,
|
|
|
|
|
"username": user_1.username,
|
|
|
|
|
"role": "admin",
|
|
|
|
|
"is_phone_number_verified": False,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": user_2.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user_2.user_id,
|
2022-06-03 08:09:47 -06:00
|
|
|
"email": user_2.email,
|
|
|
|
|
"username": user_2.username,
|
|
|
|
|
"role": "admin",
|
|
|
|
|
"is_phone_number_verified": False,
|
|
|
|
|
},
|
|
|
|
|
],
|
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": 100,
|
|
|
|
|
"total_pages": 1,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.json() == expected_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_forbidden_access(
|
|
|
|
|
make_organization_and_user,
|
|
|
|
|
make_organization_and_user_with_token,
|
|
|
|
|
):
|
|
|
|
|
_, user = make_organization_and_user()
|
|
|
|
|
_, _, another_org_token = make_organization_and_user_with_token()
|
|
|
|
|
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:users-detail", args=[user.public_primary_key])
|
|
|
|
|
|
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=another_org_token)
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
2022-06-22 14:46:23 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2022-11-29 09:41:56 +01:00
|
|
|
def test_get_users_list_all_role_users(user_public_api_setup, make_user_for_organization):
|
2022-06-22 14:46:23 -03:00
|
|
|
organization, admin, token, _, _ = user_public_api_setup
|
2022-11-29 09:41:56 +01:00
|
|
|
editor = make_user_for_organization(organization, role=LegacyAccessControlRole.EDITOR)
|
|
|
|
|
viewer = make_user_for_organization(organization, role=LegacyAccessControlRole.VIEWER)
|
2022-06-22 14:46:23 -03:00
|
|
|
|
|
|
|
|
client = APIClient()
|
|
|
|
|
|
|
|
|
|
url = reverse("api-public:users-list")
|
|
|
|
|
response = client.get(f"{url}?short=true", format="json", HTTP_AUTHORIZATION=token)
|
|
|
|
|
|
|
|
|
|
expected_users = [(admin, "admin"), (editor, "editor"), (viewer, "viewer")]
|
|
|
|
|
expected_response = {
|
|
|
|
|
"count": 3,
|
|
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
|
|
|
|
"id": user.public_primary_key,
|
2024-09-24 15:16:22 -04:00
|
|
|
"grafana_id": user.user_id,
|
2022-06-22 14:46:23 -03:00
|
|
|
"email": user.email,
|
|
|
|
|
"username": user.username,
|
|
|
|
|
"role": role,
|
|
|
|
|
"is_phone_number_verified": False,
|
|
|
|
|
}
|
|
|
|
|
for user, role in expected_users
|
|
|
|
|
],
|
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": 100,
|
|
|
|
|
"total_pages": 1,
|
2022-06-22 14:46:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
assert response.json() == expected_response
|