Skip to content

Commit

Permalink
updated mock tapis for storage, disabled some tests temporarily later…
Browse files Browse the repository at this point in the history
… tests need the mock storage
  • Loading branch information
jalowe13 committed Oct 24, 2024
1 parent 58c2775 commit 39092ed
Showing 1 changed file with 51 additions and 63 deletions.
114 changes: 51 additions & 63 deletions server/portal/apps/projects/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,110 +35,96 @@ def mock_service_account(mocker):

@pytest.fixture()
def mock_owner(django_user_model):
return django_user_model.objects.create_user(
username="username", password="password"
)
return "owner"


# Mock creation of a project

# Minimal mocks, only do behavior based testing instead of implementation based testing


@pytest.fixture()
def mock_tapis_client():
with patch("tapipy.tapis.Tapis") as MockTapis:
mock_client = MockTapis.return_value
# Tapis server actions to Mock
# create_workspace_system calls
mock_client.systems.createSystem = MagicMock()
# get_project calls
with patch(
"tapipy.tapis.Tapis"
) as mock_client:

mock_listing = []
class DictObj:
def __init__(self, **entries):
# Recursive conversion of nested dictionaries into object for attribute reference
# This is needed because of the behavior of shared_workspace_operations.py and its usage of objects from JSON
for key, value in entries.items():
if isinstance(value, dict):
self.__dict__[key] = DictObj(**value)
else:
self.__dict__[key] = value
# Mock createSystem with the side effect
def create_system_side_effect(**system_args):
system_created = "2024-10-18T00:00:00Z"
system_args['created'] = system_created
system_obj = DictObj(**system_args)
title = getattr(system_obj.notes, "title", None)
mock_listing.append(system_obj)
def get_system_side_effect(systemId):
# TODO: Change for multiple systems, tests only one system for now
return mock_listing[0]

mock_client.systems.createSystem = MagicMock(side_effect=create_system_side_effect)
mock_client.systems.getShareInfo = MagicMock()
mock_client.systems.getSystem = MagicMock()
mock_client.systems.getSystem = MagicMock(side_effect=get_system_side_effect)
mock_client.files.getPermissions = MagicMock()
# TODO: Enable this for the list systems test
#mock_client.systems.getSystems = MagicMock(return_value=mock_listing)
mock_client.systems.getSystems = MagicMock()

yield mock_client


# Initial setup of mocks
@pytest.fixture()
def setup_mocks(mock_tapis_client):
result_title = "My New Workspace"
result_description = "This is a test description"
result_created = "2024-10-18T00:00:00Z"

# Mock Tapis Client
client = mock_tapis_client

# Mock shares return
mock_shares = MagicMock()
mock_shares.users = ["owner", "user"]
client.systems.getShareInfo.return_value = mock_shares

# Mock system return
mock_system = MagicMock()
mock_system.owner = "owner"
mock_system.notes.title = result_title
mock_system.notes.description = result_description
mock_system.created = result_created
client.systems.getSystem.return_value = mock_system

# Mock permission return
def mock_get_permissions(systemId, path, username):
if username == "user":
return MagicMock(permission="MODIFY")
return MagicMock(permission="NONE")

client.files.getPermissions.side_effect = mock_get_permissions

return client


def test_project_init(setup_mocks):
def test_project_init(mock_tapis_client, mock_owner):
with patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.create_workspace_system",
wraps=ws_o.create_workspace_system,
):
wraps=ws_o.create_workspace_system,),patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.get_project_user") as mock_get_project_user, patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.get_project",wraps=ws_o.get_project):
"Test project model init."

# Assert Defs
result_system_id = f"{settings.PORTAL_PROJECTS_SYSTEM_PREFIX}.PRJ-123"
result_title = "My New Workspace"
result_description = "This is a test description"
result_created = "2024-10-18T00:00:00Z"
mock_get_project_user.return_value = {"username": "mock_user"}

# Mock Tapis Client
client = setup_mocks

client = mock_tapis_client
# Create the shared workspace
project_id = ws_o.create_workspace_system(
client,
workspace_id="PRJ-123",
title=result_title,
description=result_description,
owner=None,
owner=mock_owner,
)
ws_o.create_workspace_system.assert_called_once_with(
client,
workspace_id="PRJ-123",
title=result_title,
description=result_description,
owner=None,
owner=mock_owner,
)

# Assert the results
assert project_id == result_system_id
project = ws_o.get_project(client, project_id)
project = ws_o.get_project(client, workspace_id="PRJ-123")
assert project["title"] == result_title
assert project["description"] == result_description
assert project["created"] == result_created
assert project["projectId"] == result_system_id
assert len(project["members"]) == 2
assert project["members"][0]["user"]["username"] == "owner"
assert project["projectId"] == "PRJ-123"
assert len(project["members"]) == 1
assert project["members"][0]["user"]["username"] == "mock_user"
assert project["members"][0]["access"] == "owner"
assert project["members"][1]["user"]["username"] == "user"
assert project["members"][1]["access"] == "edit"


@pytest.mark.skip(reason="Needs to be updated with new Mocked Tapis Storage")
def test_project_create(setup_mocks, mock_owner):
with patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.service_account",
Expand Down Expand Up @@ -177,6 +163,7 @@ def test_project_create(setup_mocks, mock_owner):
ws_o.create_workspace_system.assert_called()


@pytest.mark.skip(reason="Needs to be updated with new Mocked Tapis Storage")
def test_listing(setup_mocks, mock_owner):
with patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.service_account",
Expand Down Expand Up @@ -218,10 +205,10 @@ def test_listing(setup_mocks, mock_owner):

# TODO: Replace with the actual list_projects return value
# Mock the return value of list_projects
ws_o.list_projects.return_value = [
{"project_id": project_id_1, "title": "Project 123"},
{"project_id": project_id_2, "title": "Project 124"},
]
# ws_o.list_projects.return_value = [
# {"project_id": project_id_1, "title": "Project 123"},
# {"project_id": project_id_2, "title": "Project 124"},
# ]

# Call the function to list projects
projects = ws_o.list_projects(client)
Expand All @@ -236,6 +223,7 @@ def test_listing(setup_mocks, mock_owner):
assert projects[1]["title"] == "Project 124"


@pytest.mark.skip(reason="Needs to be updated with new Mocked Tapis Storage")
def test_add_member(setup_mocks, mock_owner, django_user_model):
with patch(
"portal.apps.projects.workspace_operations.shared_workspace_operations.service_account"
Expand Down

0 comments on commit 39092ed

Please sign in to comment.