Skip to content

Commit d320a40

Browse files
amimasJohnVillalovos
authored andcommitted
refactor: enhance request body validation in unit test and update method
Used `responses.matchers.json_params_matchers` in unit test to validate request body instead of manual assertion. Modified the `update` method of `ProjectFeatureFlagManager` so that caller provided data is not mutated or updated.
1 parent 0901d97 commit d320a40

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

gitlab/v4/objects/feature_flags.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ def update(
9797
new_data: The dictionary of attributes to update.
9898
**kwargs: Extra options to send to the server (e.g. sudo)
9999
"""
100-
new_data = new_data or {}
100+
# Avoid mutating the caller-provided new_data dict by working on a copy.
101+
data = dict(new_data or {})
101102
# When used via CLI, we have 'new_name' to distinguish from the ID 'name'.
102103
# When used via .save(), the object passes 'name' directly in new_data.
103-
if "new_name" in new_data:
104-
new_data["name"] = new_data.pop("new_name")
105-
return super().update(id, new_data, **kwargs)
104+
if "new_name" in data:
105+
data["name"] = data.pop("new_name")
106+
return super().update(id, data, **kwargs)

tests/unit/objects/test_project_feature_flag_user_lists.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ def test_create_user_list_with_list_conversion(project):
1616
"http://localhost/api/v4/projects/1/feature_flags_user_lists",
1717
json={"iid": 1, "name": "list", "user_xids": "1,2,3"},
1818
status=201,
19+
match=[
20+
responses.matchers.json_params_matcher(
21+
{"name": "list", "user_xids": "1,2,3"}
22+
)
23+
],
1924
)
2025

2126
project.feature_flags_user_lists.create(
2227
{"name": "list", "user_xids": [1, 2, 3]}
2328
)
2429

2530
assert len(rs.calls) == 1
26-
# Verify that the list [1, 2, 3] was converted to "1,2,3" in the JSON body
27-
assert b'"user_xids": "1,2,3"' in rs.calls[0].request.body

tests/unit/objects/test_project_feature_flags.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ def test_feature_flag_rename(project):
1515
flag_content = {"name": "old_name", "version": "new_version_flag", "active": True}
1616
flag = ProjectFeatureFlag(project.feature_flags, flag_content)
1717

18-
# Simulate fetching from API (populates _attrs)
19-
flag._attrs = flag_content.copy()
20-
flag._updated_attrs = {}
21-
2218
# Rename locally
2319
flag.name = "new_name"
2420

@@ -28,13 +24,12 @@ def test_feature_flag_rename(project):
2824
"http://localhost/api/v4/projects/1/feature_flags/old_name",
2925
json={"name": "new_name", "version": "new_version_flag", "active": True},
3026
status=200,
27+
match=[responses.matchers.json_params_matcher({"name": "new_name"})],
3128
)
3229

3330
flag.save()
3431

3532
assert len(rs.calls) == 1
3633
# URL should use the old name (ID)
3734
assert rs.calls[0].request.url.endswith("/feature_flags/old_name")
38-
# Body should contain the new name
39-
assert b'"name": "new_name"' in rs.calls[0].request.body
4035
assert flag.name == "new_name"

0 commit comments

Comments
 (0)