Skip to content

Commit

Permalink
Update logic for fetching CF entities used in proxy headers (#146)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
markdboyd authored Nov 15, 2024
1 parent 1f96e84 commit 316eb59
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cf_auth_proxy/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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))
242 changes: 228 additions & 14 deletions tests/unit/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -172,9 +322,9 @@ def test_gets_cf_user_permitted_orgs():
"href": "http://mock.cf/v3/organization/org-guid-1"
}
}
}
]
} """
}
]
} """
response_2 = """
{
"pagination": {
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 316eb59

Please sign in to comment.