Skip to content

Commit

Permalink
Refactor API test for updating a group
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Jun 13, 2024
1 parent a04942f commit fbf903d
Showing 1 changed file with 56 additions and 16 deletions.
72 changes: 56 additions & 16 deletions lib/galaxy_test/api/test_groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Optional
from typing import (
List,
Optional,
)

from galaxy_test.base.populators import DatasetPopulator
from ._framework import ApiTestCase
Expand Down Expand Up @@ -72,37 +75,62 @@ def test_show_unknown_raises_400(self):
self._assert_status_code_is(response, 400)

def test_update(self):
group = self.test_create_valid(group_name=f"group-test-{self.dataset_populator.get_random_name()}")
user_id = self.dataset_populator.user_id()
user_private_role_id = self.dataset_populator.user_private_role_id()
original_name = f"group-test-{self.dataset_populator.get_random_name()}"
group = self.test_create_valid(group_name=original_name)

self._assert_group_has_expected_values(
group["id"],
name=original_name,
user_ids=[user_id],
role_ids=[user_private_role_id],
)

group_id = group["id"]
updated_name = f"group-test-updated-{self.dataset_populator.get_random_name()}"
update_response = self._put(f"groups/{group_id}", data={"name": updated_name}, admin=True, json=True)
self._assert_status_code_is_ok(update_response)

# Update replace with another user
# Only the name should be updated
self._assert_group_has_expected_values(
group_id,
name=updated_name,
user_ids=[user_id],
role_ids=[user_private_role_id],
)

# Add another user to the group
another_user_id = None
with self._different_user():
another_user_id = self.dataset_populator.user_id()
another_role_id = self.dataset_populator.user_private_role_id()
assert another_user_id is not None
update_response = self._put(f"groups/{group_id}", data={"user_ids": [another_user_id]}, admin=True, json=True)
self._assert_status_code_is_ok(update_response)

# get group and check if it was updated
response = self._get(f"groups/{group_id}", admin=True)
self._assert_status_code_is_ok(response)
users = self._get(f"groups/{group_id}/users", admin=True).json()
assert len(users) == 1
assert users[0]["id"] == another_user_id
# Check if the user was added
self._assert_group_has_expected_values(
group_id,
name=updated_name,
user_ids=[user_id, another_user_id],
role_ids=[user_private_role_id],
)

# Remove private role from group
update_response = self._put(f"groups/{group_id}", data={"role_ids": []}, admin=True, json=True)
# Add another role to the group
update_response = self._put(f"groups/{group_id}", data={"role_ids": [another_role_id]}, admin=True, json=True)
self._assert_status_code_is_ok(update_response)

# get group and check if it was updated
response = self._get(f"groups/{group_id}", admin=True)
self._assert_status_code_is_ok(response)
roles = self._get(f"groups/{group_id}/roles", admin=True).json()
assert len(roles) == 0
# Check if the role was added
self._assert_group_has_expected_values(
group_id,
name=updated_name,
user_ids=[user_id, another_user_id],
role_ids=[user_private_role_id, another_role_id],
)

# TODO: Test removing users and roles
# Currently not possible because the API can only add users and roles

def test_update_only_admin(self):
group = self.test_create_valid()
Expand Down Expand Up @@ -177,6 +205,18 @@ def _assert_valid_group(self, group, assert_id=None):
if assert_id is not None:
assert group["id"] == assert_id

def _assert_group_has_expected_values(self, group_id: str, name: str, user_ids: List[str], role_ids: List[str]):
group = self._get(f"groups/{group_id}", admin=True).json()
assert group["name"] == name
users = self._get(f"groups/{group_id}/users", admin=True).json()
assert len(users) == len(user_ids)
for user in users:
assert user["id"] in user_ids
roles = self._get(f"groups/{group_id}/roles", admin=True).json()
assert len(roles) == len(role_ids)
for role in roles:
assert role["id"] in role_ids

def _build_valid_group_payload(self, name: Optional[str] = None):
name = name or self.dataset_populator.get_random_name()
user_id = self.dataset_populator.user_id()
Expand Down

0 comments on commit fbf903d

Please sign in to comment.