Skip to content

Commit 5435488

Browse files
author
Eric Novotny
committed
fix office filter to removes other office roles
1 parent b96902b commit 5435488

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

cwms/users/users.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@ def get_user_profile() -> dict[str, Any]:
3535
return dict(response)
3636

3737

38+
def filter_users_by_office(data: dict, office: str) -> dict:
39+
"""
40+
Filter users JSON to only include users that have roles for the specified office.
41+
Each user's roles dict will only contain the entry for that office.
42+
43+
Args:
44+
data: The full users JSON as a Python dict.
45+
office: The office key to filter by (e.g., 'MVP', 'LRL').
46+
47+
Returns:
48+
A new dict with the same structure, filtered to the specified office.
49+
"""
50+
filtered_users = []
51+
52+
for user in data.get("users", []):
53+
roles = user.get("roles", {})
54+
55+
if office in roles:
56+
# Build a copy of the user with only the target office's roles
57+
filtered_user = {k: v for k, v in user.items() if k != "roles"}
58+
filtered_user["roles"] = {office: roles[office]}
59+
filtered_users.append(filtered_user)
60+
61+
return {
62+
"page": data.get("page"),
63+
"page-size": data.get("page-size"),
64+
"total": len(filtered_users),
65+
"users": filtered_users,
66+
}
67+
68+
3869
def get_users(
3970
office_id: Optional[str] = None,
4071
username_like: Optional[str] = None,
@@ -63,11 +94,7 @@ def get_users(
6394
# currently support filtering by office on the backend. This is a
6495
# temporary workaround until the API supports office filtering.
6596
if office_id:
66-
data = response
67-
filtered_users = [
68-
user for user in data["users"] if office_id in user.get("roles", {})
69-
]
70-
response = {**data, "users": filtered_users, "total": len(filtered_users)}
97+
response = filter_users_by_office(response, office_id)
7198
return Data(response, selector="users")
7299

73100

tests/cda/users/users_CDA_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ def test_get_users_by_office():
113113
# returned page when filtering by the test user's office.
114114
user_name = str(TEST_USER_NAME)
115115
office_id = TEST_OFFICE_ID
116-
users = cwms.get_users(office_id=office_id, page_size=200)
117-
users_list = users.json.get("users", [])
116+
users = cwms.get_users(office_id=office_id, page_size=200).json
117+
users_list = users.get("users", [])
118118
assert isinstance(users_list, list)
119119
assert any(
120120
str(u.get("user-name", "")).lower() == user_name.lower() for u in users_list
121121
)
122+
for user in users["users"]:
123+
roles = user["roles"]
124+
assert set(roles.keys()) == {
125+
office_id
126+
}, f"{user['user-name']} has unexpected office keys: {set(roles.keys())}"
122127

123128

124129
def test_get_users_by_office_not_present_in_other_office():

0 commit comments

Comments
 (0)