Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abyesilyurt committed Sep 9, 2024
1 parent 08ba5b2 commit 726d8d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 64 deletions.
14 changes: 5 additions & 9 deletions packages/syft/src/syft/service/user/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,11 @@ def get_all(
page_size: int | None = 0,
page_index: int | None = 0,
) -> list[UserView]:
if context.role in [ServiceRole.DATA_OWNER, ServiceRole.ADMIN]:
users = self.stash.get_all(
context.credentials,
has_permission=True,
order_by=order_by,
sort_order=sort_order,
).unwrap()
else:
users = self.stash.get_all(context.credentials).unwrap()
users = self.stash.get_all(
context.credentials,
order_by=order_by,
sort_order=sort_order,
).unwrap()
users = [user.to(UserView) for user in users]
return _paginate(users, page_size, page_index)

Expand Down
64 changes: 9 additions & 55 deletions packages/syft/tests/syft/settings/settings_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from syft.store.document_store_errors import NotFoundException
from syft.store.document_store_errors import StashException
from syft.types.errors import SyftException
from syft.types.result import Ok
from syft.types.result import as_result


Expand Down Expand Up @@ -100,37 +99,21 @@ def test_settingsservice_set_success(
) -> None:
response = settings_service.set(authed_context, settings)
assert isinstance(response, ServerSettings)
# PR NOTE do we write syft_client_verify_key and syft_server_location to the stash or not?
response.syft_client_verify_key = None
response.syft_server_location = None
response.pwd_token_config.syft_client_verify_key = None
response.pwd_token_config.syft_server_location = None
assert response == settings


def test_settingsservice_set_fail(
monkeypatch: MonkeyPatch,
settings_service: SettingsService,
settings: ServerSettings,
authed_context: AuthedServiceContext,
) -> None:
mock_error_message = "database failure"

@as_result(StashException)
def mock_stash_set_error(credentials, settings: ServerSettings) -> NoReturn:
raise StashException(public_message=mock_error_message)

monkeypatch.setattr(settings_service.stash, "set", mock_stash_set_error)

with pytest.raises(StashException) as exc:
settings_service.set(authed_context, settings)

assert exc.type == StashException
assert exc.value.public_message == mock_error_message


def add_mock_settings(
root_verify_key: SyftVerifyKey,
settings_stash: SettingsStash,
settings: ServerSettings,
) -> ServerSettings:
# create a mock settings in the stash so that we can update it
result = settings_stash.partition.set(root_verify_key, settings)
result = settings_stash.set(root_verify_key, settings)
assert result.is_ok()

created_settings = result.ok()
Expand All @@ -150,9 +133,7 @@ def test_settingsservice_update_success(
notifier_stash: NotifierStash,
) -> None:
# add a mock settings to the stash
mock_settings = add_mock_settings(
authed_context.credentials, settings_stash, settings
)
mock_settings = settings_stash.set(authed_context.credentials, settings).unwrap()

# get a new settings according to update_settings
new_settings = deepcopy(settings)
Expand All @@ -164,14 +145,6 @@ def test_settingsservice_update_success(
assert new_settings != mock_settings
assert mock_settings == settings

mock_stash_get_all_output = [mock_settings, mock_settings]

def mock_stash_get_all(root_verify_key) -> Ok:
return Ok(mock_stash_get_all_output)

monkeypatch.setattr(settings_service.stash, "get_all", mock_stash_get_all)

# Mock the get_service method to return a mocked notifier_service with the notifier_stash
class MockNotifierService:
def __init__(self, stash):
self.stash = stash
Expand All @@ -194,12 +167,7 @@ def mock_get_service(service_name: str):
# update the settings in the settings stash using settings_service
response = settings_service.update(context=authed_context, settings=update_settings)

# not_updated_settings = response.ok()[1]

assert isinstance(response, SyftSuccess)
# assert (
# not_updated_settings.to_dict() == settings.to_dict()
# ) # the second settings is not updated


def test_settingsservice_update_stash_get_all_fail(
Expand All @@ -208,19 +176,7 @@ def test_settingsservice_update_stash_get_all_fail(
update_settings: ServerSettingsUpdate,
authed_context: AuthedServiceContext,
) -> None:
mock_error_message = "database failure"

@as_result(StashException)
def mock_stash_get_all_error(credentials) -> NoReturn:
raise StashException(public_message=mock_error_message)

monkeypatch.setattr(settings_service.stash, "get_all", mock_stash_get_all_error)

with pytest.raises(StashException) as exc:
settings_service.update(context=authed_context, settings=update_settings)

assert exc.type == StashException
assert exc.value.public_message == mock_error_message
settings_service.update(context=authed_context, settings=update_settings)


def test_settingsservice_update_stash_empty(
Expand All @@ -230,9 +186,7 @@ def test_settingsservice_update_stash_empty(
) -> None:
with pytest.raises(NotFoundException) as exc:
settings_service.update(context=authed_context, settings=update_settings)

assert exc.type == NotFoundException
assert exc.value.public_message == "Server settings not found"
assert exc.value.public_message == "Server settings not found"


def test_settingsservice_update_fail(
Expand Down
3 changes: 3 additions & 0 deletions packages/syft/tests/syft/stores/action_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from syft.store.db.sqlite_db import DBManager
from syft.types.uid import UID

# first party
from packages.syft.tests.syft.worker_test import action_object_stash # noqa: F401

permissions = [
ActionObjectOWNER,
ActionObjectREAD,
Expand Down

0 comments on commit 726d8d1

Please sign in to comment.