Skip to content

Commit

Permalink
Merge pull request #8829 from OpenMined/settings_update
Browse files Browse the repository at this point in the history
Use autosplat for settings update. Add docstring to Update service
  • Loading branch information
IonesioJunior authored May 27, 2024
2 parents 8a6d825 + da735ca commit 9417d07
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/syft/src/syft/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ def reconstruct_args_kwargs(
final_kwargs[param_key] = param.default
else:
raise Exception(f"Missing {param_key} not in kwargs.")

if "context":
final_kwargs["context"] = kwargs["context"]

return (args, final_kwargs)


Expand Down
30 changes: 28 additions & 2 deletions packages/syft/src/syft/service/settings/settings_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,36 @@ def set(
else:
return SyftError(message=result.err())

@service_method(path="settings.update", name="update")
@service_method(path="settings.update", name="update", autosplat=["settings"])
def update(
self, context: AuthedServiceContext, settings: NodeSettingsUpdate
) -> Result[SyftSuccess, SyftError]:
"""
Update the Node Settings using the provided values.
Args:
name: Optional[str]
Node name
organization: Optional[str]
Organization name
description: Optional[str]
Node description
on_board: Optional[bool]
Show onboarding panel when a user logs in for the first time
signup_enabled: Optional[bool]
Enable/Disable registration
admin_email: Optional[str]
Administrator email
association_request_auto_approval: Optional[bool]
Returns:
Result[SyftSuccess, SyftError]: A result indicating the success or failure of the update operation.
Example:
>>> node_client.update(name='foo', organization='bar', description='baz', signup_enabled=True)
SyftSuccess: Settings updated successfully.
"""

result = self.stash.get_all(context.credentials)
if result.is_ok():
current_settings = result.ok()
Expand Down Expand Up @@ -139,7 +165,7 @@ def allow_guest_signup(

result = method(context=context, settings=settings)

if result.is_err():
if isinstance(result, SyftError):
return SyftError(message=f"Failed to update settings: {result.err()}")

message = "enabled" if enable else "disabled"
Expand Down
8 changes: 4 additions & 4 deletions packages/syft/tests/syft/settings/settings_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def mock_stash_get_all(root_verify_key) -> Ok:
monkeypatch.setattr(settings_service.stash, "get_all", mock_stash_get_all)

# update the settings in the settings stash using settings_service
response = settings_service.update(authed_context, update_settings)
response = settings_service.update(context=authed_context, settings=update_settings)

# not_updated_settings = response.ok()[1]

Expand All @@ -174,7 +174,7 @@ def mock_stash_get_all_error(credentials) -> Err:
return Err(mock_error_message)

monkeypatch.setattr(settings_service.stash, "get_all", mock_stash_get_all_error)
response = settings_service.update(authed_context, update_settings)
response = settings_service.update(context=authed_context, settings=update_settings)

assert isinstance(response, SyftError)
assert response.message == mock_error_message
Expand All @@ -185,7 +185,7 @@ def test_settingsservice_update_stash_empty(
update_settings: NodeSettingsUpdate,
authed_context: AuthedServiceContext,
) -> None:
response = settings_service.update(authed_context, update_settings)
response = settings_service.update(context=authed_context, settings=update_settings)

assert isinstance(response, SyftError)
assert response.message == "No settings found"
Expand Down Expand Up @@ -214,7 +214,7 @@ def mock_stash_update_error(credentials, update_settings: NodeSettings) -> Err:

monkeypatch.setattr(settings_service.stash, "update", mock_stash_update_error)

response = settings_service.update(authed_context, update_settings)
response = settings_service.update(context=authed_context, settings=update_settings)

assert isinstance(response, SyftError)
assert response.message == mock_update_error_message
Expand Down
12 changes: 6 additions & 6 deletions packages/syft/tests/syft/users/user_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def mock_find_all(credentials: SyftVerifyKey, **kwargs) -> Ok | Err:
expected_output = [guest_user.to(UserView)]

# Search via id
response = user_service.search(authed_context, id=guest_user.id)
response = user_service.search(context=authed_context, id=guest_user.id)
assert isinstance(response, list)
assert all(
r.to_dict() == expected.to_dict()
Expand All @@ -238,15 +238,15 @@ def mock_find_all(credentials: SyftVerifyKey, **kwargs) -> Ok | Err:
# assert response.to_dict() == expected_output.to_dict()

# Search via email
response = user_service.search(authed_context, email=guest_user.email)
response = user_service.search(context=authed_context, email=guest_user.email)
assert isinstance(response, list)
assert all(
r.to_dict() == expected.to_dict()
for r, expected in zip(response, expected_output)
)

# Search via name
response = user_service.search(authed_context, name=guest_user.name)
response = user_service.search(context=authed_context, name=guest_user.name)
assert isinstance(response, list)
assert all(
r.to_dict() == expected.to_dict()
Expand All @@ -255,7 +255,7 @@ def mock_find_all(credentials: SyftVerifyKey, **kwargs) -> Ok | Err:

# Search via verify_key
response = user_service.search(
authed_context,
context=authed_context,
verify_key=guest_user.verify_key,
)
assert isinstance(response, list)
Expand All @@ -266,7 +266,7 @@ def mock_find_all(credentials: SyftVerifyKey, **kwargs) -> Ok | Err:

# Search via multiple kwargs
response = user_service.search(
authed_context, name=guest_user.name, email=guest_user.email
context=authed_context, name=guest_user.name, email=guest_user.email
)
assert isinstance(response, list)
assert all(
Expand All @@ -279,7 +279,7 @@ def test_userservice_search_with_invalid_kwargs(
user_service: UserService, authed_context: AuthedServiceContext
) -> None:
# Search with invalid kwargs
response = user_service.search(authed_context, role=ServiceRole.GUEST)
response = user_service.search(context=authed_context, role=ServiceRole.GUEST)
assert isinstance(response, SyftError)
assert "Invalid Search parameters" in response.message

Expand Down

0 comments on commit 9417d07

Please sign in to comment.