Skip to content

Commit e9766a5

Browse files
committed
Apply controller autovalues on policy updates
1 parent 26f1122 commit e9766a5

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

gcl_iam/api/controllers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def update(self, uuid, **kwargs):
108108
if "project_id" in kwargs and self._ctx_project_id:
109109
self._force_project_id(kwargs["project_id"])
110110
dm = super(PolicyBasedController, self).get(uuid, **filters)
111+
kwargs = self._apply_autovalues(kwargs)
111112
dm.update_dm(values=kwargs)
112113
dm.update()
113114
return dm
@@ -174,6 +175,7 @@ def update(self, uuid, **kwargs):
174175
self._enforce("update")
175176
dm = super().get(uuid)
176177

178+
kwargs = self._apply_autovalues(kwargs)
177179
dm.update_dm(values=kwargs)
178180
dm.update()
179181
return dm

gcl_iam/tests/unit/test_controllers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,54 @@ def test_override_project_id_from_ctx(self, user_context):
166166
assert kwargs == {"project_id": FAKE_PROJECT_ID}
167167

168168

169+
class TestPolicyBasedController:
170+
def test_update_applies_autovalues(self, user_context):
171+
pc = controllers.PolicyBasedController(request=mock.Mock())
172+
pc._enforce_and_override_project_id_in_kwargs = mock.Mock()
173+
pc._apply_autovalues = mock.Mock(return_value={"name": "server"})
174+
dm = mock.Mock()
175+
resource_id = uuid.uuid4()
176+
177+
with mock.patch.object(
178+
controllers.controllers.BaseResourceController,
179+
"get",
180+
return_value=dm,
181+
) as get:
182+
result = pc.update(resource_id, name="client")
183+
184+
pc._enforce_and_override_project_id_in_kwargs.assert_called_once_with(
185+
"update", {}
186+
)
187+
get.assert_called_once_with(resource_id)
188+
pc._apply_autovalues.assert_called_once_with({"name": "client"})
189+
dm.update_dm.assert_called_once_with(values={"name": "server"})
190+
dm.update.assert_called_once_with()
191+
assert result is dm
192+
193+
194+
class TestPolicyBasedWithoutProjectController:
195+
def test_update_applies_autovalues(self, user_context):
196+
pc = controllers.PolicyBasedWithoutProjectController(request=mock.Mock())
197+
pc._enforce = mock.Mock()
198+
pc._apply_autovalues = mock.Mock(return_value={"name": "server"})
199+
dm = mock.Mock()
200+
resource_id = uuid.uuid4()
201+
202+
with mock.patch.object(
203+
controllers.controllers.BaseResourceController,
204+
"get",
205+
return_value=dm,
206+
) as get:
207+
result = pc.update(resource_id, name="client")
208+
209+
pc._enforce.assert_called_once_with("update")
210+
get.assert_called_once_with(resource_id)
211+
pc._apply_autovalues.assert_called_once_with({"name": "client"})
212+
dm.update_dm.assert_called_once_with(values={"name": "server"})
213+
dm.update.assert_called_once_with()
214+
assert result is dm
215+
216+
169217
class TestPolicyBasedCheckOtpController:
170218
def test_check_otp_verified_true(self, otp_enabled_context):
171219
pc = controllers.PolicyBasedCheckOtpController(request=mock.Mock())

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ classifiers = [
2121
]
2222
dependencies = [
2323
"bazooka>=1.1.0,<2.0.0", # Apache-2.0
24-
"restalchemy>=15.1.2,<16.0.0", # Apache-2.0
24+
"restalchemy>=15.2.0,<16.0.0", # Apache-2.0
2525
"izulu>=0.50.0,<1.0.0", # MIT License
2626
"pyjwt>=2.9.0,<3.0.0", # MIT License
2727
"cryptography>=45.0.5,<47.0.0", # BSD-3 License

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)