From c70b75242f4fc2ca1e21d686cf5a19900410a95e Mon Sep 17 00:00:00 2001 From: Jay Zeng Date: Thu, 16 May 2024 14:35:05 -0400 Subject: [PATCH] Fix calling params if not set If the param, e.g. orgs, is not set, the the params will be [""], which will cause the job not finding any tenants since checking orgs empty would fail and tenants = tenants.filter(org_id__in=orgs) will be run --- rbac/internal/views.py | 13 +++++++++++-- tests/internal/test_views.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/rbac/internal/views.py b/rbac/internal/views.py index 91775a10f..9bee30059 100644 --- a/rbac/internal/views.py +++ b/rbac/internal/views.py @@ -473,6 +473,14 @@ def ocm_performance(request): return HttpResponse('Invalid method, only "POST" is allowed.', status=405) +def get_param_list(request, param_name): + """Get a list of params from a request.""" + params = request.GET.get(param_name, []) + if params: + params = params.split(",") + return params + + def role_migration(request): """View method for running role migrations from V1 to V2 spiceDB schema. @@ -481,9 +489,10 @@ def role_migration(request): if request.method != "POST": return HttpResponse('Invalid method, only "POST" is allowed.', status=405) logger.info("Running V1 Role migration.") + args = { - "exclude_apps": request.GET.get("exclude_apps", "").split(","), - "orgs": request.GET.get("orgs", "").split(","), + "exclude_apps": get_param_list(request, "exclude_apps"), + "orgs": get_param_list(request, "orgs"), } migrate_roles_in_worker.delay(args) return HttpResponse("Role migration from V1 to V2 are running in a background worker.", status=202) diff --git a/tests/internal/test_views.py b/tests/internal/test_views.py index 2deac266b..65e8664fe 100644 --- a/tests/internal/test_views.py +++ b/tests/internal/test_views.py @@ -479,3 +479,16 @@ def test_run_migrations_of_roles(self, migration_mock): response.content.decode(), "Role migration from V1 to V2 are running in a background worker.", ) + + # Without params + migration_mock.reset_mock() + response = self.client.post( + f"/_private/api/utils/role_migration/", + **self.request.META, + ) + migration_mock.assert_called_once_with({"exclude_apps": [], "orgs": []}) + self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) + self.assertEqual( + response.content.decode(), + "Role migration from V1 to V2 are running in a background worker.", + )