From 316eb59005d5b1807de5968b1327b4176a931ab4 Mon Sep 17 00:00:00 2001 From: Mark Boyd Date: Fri, 15 Nov 2024 09:56:04 -0500 Subject: [PATCH] Update logic for fetching CF entities used in proxy headers (#146) * update proxy logic to return only unique values for CF spaces and orgs * add unit test for returning only unique user spaces * update test fixture * add more unit tests to ensure header values are unique --- cf_auth_proxy/cf.py | 6 +- tests/unit/test_cf.py | 242 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 231 insertions(+), 17 deletions(-) diff --git a/cf_auth_proxy/cf.py b/cf_auth_proxy/cf.py index 428f322..0bc0069 100644 --- a/cf_auth_proxy/cf.py +++ b/cf_auth_proxy/cf.py @@ -28,7 +28,7 @@ def get_spaces_for_user(user_id, token): first_response = s.get(url, params=params) resources = iterate_cf_resource(s, first_response) spaces = [r["relationships"]["space"]["data"]["guid"] for r in resources] - return spaces + return list(set(spaces)) def get_permitted_orgs_for_user(user_id, token): @@ -42,7 +42,7 @@ def get_permitted_orgs_for_user(user_id, token): first_response = s.get(url, params=params) resources = iterate_cf_resource(s, first_response) orgs = [r["relationships"]["organization"]["data"]["guid"] for r in resources] - return orgs + return list(set(orgs)) def get_all_orgs_for_user(user_id, token): @@ -56,4 +56,4 @@ def get_all_orgs_for_user(user_id, token): first_response = s.get(url, params=params) resources = iterate_cf_resource(s, first_response) orgs = [r["relationships"]["organization"]["data"]["guid"] for r in resources] - return orgs + return list(set(orgs)) diff --git a/tests/unit/test_cf.py b/tests/unit/test_cf.py index 7ffbd78..d5bce12 100644 --- a/tests/unit/test_cf.py +++ b/tests/unit/test_cf.py @@ -122,12 +122,162 @@ def test_gets_spaces(): ) -def test_gets_cf_user_permitted_orgs(): +def test_gets_unique_spaces(): with requests_mock.Mocker() as m: response_1 = """ { "pagination": { - "total_results": 2, + "total_results": 3, + "total_pages": 2, + "first": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=space_developer%2Cspace_manager&user_guids=a-user-guid" + }, + "last": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=2&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid" + }, + "next": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=2&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid" + }, + "previous": null + }, + "resources": [ + { + "guid": "role-guid-1", + "created_at": "2019-12-12T18:59:12Z", + "updated_at": "2019-12-12T18:59:13Z", + "type": "space_developer", + "relationships": { + "user": { + "data": { + "guid": "a-user-guid" + } + }, + "space": { + "data": { + "guid": "space-guid-1" + } + }, + "organization": { + "data": null + } + }, + "links": { + "self": { + "href": "http://mock.cf/v3/roles/role-guid-1" + }, + "user": { + "href": "http://mock.cf/v3/users/a-user-guid" + }, + "space": { + "href": "http://mock.cf/v3/spaces/space-guid-1" + } + } + } + ] + } """ + response_2 = """ + { + "pagination": { + "total_results": 3, + "total_pages": 2, + "first": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid" + }, + "last": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=2&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid" + }, + "next": null, + "previous": { + "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid" + } + }, + "resources": [ + { + "guid": "role-guid-2", + "created_at": "2019-12-12T18:59:12Z", + "updated_at": "2019-12-12T18:59:13Z", + "type": "space_developer", + "relationships": { + "user": { + "data": { + "guid": "a-user-guid" + } + }, + "space": { + "data": { + "guid": "space-guid-2" + } + }, + "organization": { + "data": null + } + }, + "links": { + "self": { + "href": "http://mock.cf/v3/roles/role-guid-2" + }, + "user": { + "href": "http://mock.cf/v3/users/a-user-guid" + }, + "space": { + "href": "http://mock.cf/v3/spaces/space-guid-2" + } + } + }, + { + "guid": "role-guid-3", + "created_at": "2019-12-12T18:59:12Z", + "updated_at": "2019-12-12T18:59:13Z", + "type": "space_developer", + "relationships": { + "user": { + "data": { + "guid": "a-user-guid" + } + }, + "space": { + "data": { + "guid": "space-guid-2" + } + }, + "organization": { + "data": null + } + }, + "links": { + "self": { + "href": "http://mock.cf/v3/roles/role-guid-3" + }, + "user": { + "href": "http://mock.cf/v3/users/a-user-guid" + }, + "space": { + "href": "http://mock.cf/v3/spaces/space-guid-2" + } + } + } + ] + } + """ + m.get( + "http://mock.cf/v3/roles?user_guids=a-user-id&types=space_developer,space_manager,space_auditor", + text=response_1, + ) + m.get( + "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=2&per_page=1&types=space_developer%2Cspace_manager%2Cspace_auditor&user_guids=a-user-guid", + text=response_2, + ) + assert sorted(cf.get_spaces_for_user("a-user-id", "a_token")) == sorted( + ["space-guid-1", "space-guid-2"] + ) + + +def test_gets_cf_user_permitted_orgs(): + with requests_mock.Mocker() as m: + response_1 = """ +{ + "pagination": { + "total_results": 3, "total_pages": 2, "first": { "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=organization_manager%2Corganization_auditor&user_guids=a-user-guid" @@ -172,9 +322,9 @@ def test_gets_cf_user_permitted_orgs(): "href": "http://mock.cf/v3/organization/org-guid-1" } } - } - ] - } """ + } + ] +} """ response_2 = """ { "pagination": { @@ -223,9 +373,41 @@ def test_gets_cf_user_permitted_orgs(): "href": "http://mock.cf/v3/organizations/org-guid-2" } } - } - ] - } + }, + { + "guid": "role-guid-3", + "created_at": "2019-12-12T18:59:12Z", + "updated_at": "2019-12-12T18:59:13Z", + "type": "organization_auditor", + "relationships": { + "user": { + "data": { + "guid": "a-user-guid" + } + }, + "organization": { + "data": { + "guid": "org-guid-2" + } + }, + "space": { + "data": null + } + }, + "links": { + "self": { + "href": "http://mock.cf/v3/roles/role-guid-3" + }, + "user": { + "href": "http://mock.cf/v3/users/a-user-guid" + }, + "organization": { + "href": "http://mock.cf/v3/organizations/org-guid-2" + } + } + } + ] +} """ m.get( "http://mock.cf/v3/roles?user_guids=a-user-id&types=organization_manager,organization_auditor", @@ -245,7 +427,7 @@ def test_gets_cf_user_all_orgs(): response_1 = """ { "pagination": { - "total_results": 2, + "total_results": 3, "total_pages": 2, "first": { "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=organization_user&user_guids=a-user-guid" @@ -294,9 +476,9 @@ def test_gets_cf_user_all_orgs(): ] } """ response_2 = """ - { +{ "pagination": { - "total_results": 2, + "total_results": 3, "total_pages": 2, "first": { "href": "http://mock.cf/v3/roles?order_by=%2Bcreated_at&page=1&per_page=1&types=organization_user&user_guids=a-user-guid" @@ -341,9 +523,41 @@ def test_gets_cf_user_all_orgs(): "href": "http://mock.cf/v3/organizations/org-guid-2" } } - } - ] - } + }, + { + "guid": "role-guid-3", + "created_at": "2019-12-12T18:59:12Z", + "updated_at": "2019-12-12T18:59:13Z", + "type": "org_user", + "relationships": { + "user": { + "data": { + "guid": "a-user-guid" + } + }, + "organization": { + "data": { + "guid": "org-user-2" + } + }, + "space": { + "data": null + } + }, + "links": { + "self": { + "href": "http://mock.cf/v3/roles/role-guid-3" + }, + "user": { + "href": "http://mock.cf/v3/users/a-user-guid" + }, + "organization": { + "href": "http://mock.cf/v3/organizations/org-guid-2" + } + } + } + ] +} """ m.get( "http://mock.cf/v3/roles?user_guids=a-user-id&types=organization_user",