From e8bd71bbabef66b8b631b5a8dbcf06234042c4c2 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Tue, 14 Nov 2023 09:56:58 -0300 Subject: [PATCH] Enable filtering users by public primary key (#3339) Related to https://github.com/grafana/oncall/issues/3164 This will allow the following request to check if a user is currently on-call: `GET /api/internal/v1/users/?search=UCGEIXI1MR1NZ&is_currently_oncall=true` --- CHANGELOG.md | 1 + engine/apps/api/tests/test_user.py | 25 +++++++++++++++++++++++++ engine/apps/api/views/user.py | 1 + 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a27a1002..f06ebc9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added user timezone field to the users public API response ([#3311](https://github.com/grafana/oncall/pull/3311)) +- Allow filtering users by public primary key in internal API ([#3339](https://github.com/grafana/oncall/pull/3339)) ### Changed diff --git a/engine/apps/api/tests/test_user.py b/engine/apps/api/tests/test_user.py index 8e69ba8c..bc876c83 100644 --- a/engine/apps/api/tests/test_user.py +++ b/engine/apps/api/tests/test_user.py @@ -262,6 +262,31 @@ def test_list_users_filtered_by_granted_permission( assert user3.public_primary_key not in returned_user_pks +@pytest.mark.django_db +def test_list_users_filtered_by_public_primary_key( + make_organization, + make_user_for_organization, + make_token_for_organization, + make_user_auth_headers, +): + organization = make_organization() + admin_user = make_user_for_organization(organization) + user1 = make_user_for_organization(organization) + make_user_for_organization(organization) + _, token = make_token_for_organization(organization) + + client = APIClient() + url = reverse("api-internal:user-list") + + response = client.get( + f"{url}?search={user1.public_primary_key}", format="json", **make_user_auth_headers(admin_user, token) + ) + + assert response.status_code == status.HTTP_200_OK + returned_user_pks = [u["pk"] for u in response.json()["results"]] + assert returned_user_pks == [user1.public_primary_key] + + @pytest.mark.django_db def test_notification_chain_verbal( make_organization, diff --git a/engine/apps/api/views/user.py b/engine/apps/api/views/user.py index 6434d57a..7b3557cb 100644 --- a/engine/apps/api/views/user.py +++ b/engine/apps/api/views/user.py @@ -222,6 +222,7 @@ class UserView( "^slack_user_identity__cached_slack_login", "^slack_user_identity__cached_name", "^teams__name", + "=public_primary_key", ) filterset_class = UserFilter