diff --git a/galaxy_ng/app/models/organization.py b/galaxy_ng/app/models/organization.py index f941b700ef..b5a74d6217 100644 --- a/galaxy_ng/app/models/organization.py +++ b/galaxy_ng/app/models/organization.py @@ -6,7 +6,7 @@ from django.db import models from django.db.models import signals from django.dispatch import receiver -from django_lifecycle import AFTER_UPDATE, BEFORE_CREATE, LifecycleModelMixin, hook +from django_lifecycle import AFTER_UPDATE, AFTER_DELETE, BEFORE_CREATE, LifecycleModelMixin, hook from pulpcore.plugin.models import Group as PulpGroup from galaxy_ng.app.models.auth import Group @@ -84,6 +84,12 @@ def _rename_related_group(self, **kwargs): self.group.name = self.group_name() self.group.save() + @hook(AFTER_DELETE) + def _delete_related_group(self, **kwargs): + group = getattr(self, "group", None) + if group: + group.delete() + @receiver(signal=signals.post_save, sender=Group) @receiver(signal=signals.post_save, sender=PulpGroup) diff --git a/galaxy_ng/tests/integration/aap/test_aap_rbac.py b/galaxy_ng/tests/integration/aap/test_aap_rbac.py index 9491dd7fd0..5cbac366fa 100644 --- a/galaxy_ng/tests/integration/aap/test_aap_rbac.py +++ b/galaxy_ng/tests/integration/aap/test_aap_rbac.py @@ -97,10 +97,9 @@ def test_aap_service_index_and_claims_processing( rr = ga.get(f'/api/galaxy/_ui/v2/teams/?name={team_name}') assert rr['count'] == 0 - # FIXME: cascade delete on the group from the team isn't working - # group_name = org_name + '::' + team_name - # rr = ga.get(f'/api/galaxy/_ui/v2/groups/?name={group_name}') - # assert rr['count'] == 0 + group_name = org_name + '::' + team_name + rr = ga.get(f'/api/galaxy/_ui/v2/groups/?name={group_name}') + assert rr['count'] == 0 # delete the org in the gateway oid = org_data['id'] diff --git a/galaxy_ng/tests/integration/dab/test_dab_rbac_contract.py b/galaxy_ng/tests/integration/dab/test_dab_rbac_contract.py index 548640a953..c37a411237 100644 --- a/galaxy_ng/tests/integration/dab/test_dab_rbac_contract.py +++ b/galaxy_ng/tests/integration/dab/test_dab_rbac_contract.py @@ -5,6 +5,8 @@ from galaxykit import GalaxyClient from galaxykit.utils import GalaxyClientError +from galaxy_ng.tests.integration.utils.tools import random_name + GALAXY_API_PATH_PREFIX = "/api/galaxy" # cant import from settings on integration tests @@ -90,11 +92,6 @@ def test_role_definition_options(galaxy_client): PULP_ROLE_URL = "pulp/api/v3/roles/" -def random_name(prefix, *, length=12, sep='-'): - suffix = secrets.token_hex((length + 1) // 2) - return f"{prefix}{sep}{suffix}" - - # These fixtures are function-scoped, so they will be deleted. # Deleting the role will delete all associated permissions. @pytest.fixture diff --git a/galaxy_ng/tests/integration/dab/test_ui_v2.py b/galaxy_ng/tests/integration/dab/test_ui_v2.py index 0213d1a541..106495e3d8 100644 --- a/galaxy_ng/tests/integration/dab/test_ui_v2.py +++ b/galaxy_ng/tests/integration/dab/test_ui_v2.py @@ -1,10 +1,14 @@ import copy import json +from http import HTTPStatus import pytest from galaxykit.client import GalaxyClient +from galaxykit.utils import GalaxyClientError + from galaxy_ng.tests.integration.utils.namespaces import generate_namespace +from galaxy_ng.tests.integration.utils.tools import random_name pytestmark = pytest.mark.qa # noqa: F821 @@ -312,3 +316,45 @@ def test_ui_v2_user_edit_invalid_data( assert exc.response.status_code == 400 assert invalid_payload[1] in exc.response.text + +@pytest.mark.deployment_standalone +@pytest.mark.min_hub_version("4.10dev") +def test_ui_v2_teams( + settings, + galaxy_client, + random_username, +): + """Test teams creation and deletion.""" + + if settings.get('ALLOW_LOCAL_RESOURCE_MANAGEMENT') is False: + pytest.skip(reason="this only works local resource management enabled") + + client = galaxy_client("admin", ignore_cache=True) + + # Create a team + team_name = random_name('team') + team = client.post('_ui/v2/teams/', body={"name": team_name}) + assert team["name"] == team_name + + # Check that team exists + team = client.get(f"_ui/v2/teams/{team['id']}/") + assert team["name"] == team_name + + # Check that associated group exists + group = client.get(f"_ui/v1/groups/{team['group']['id']}") + assert group["id"] == team["group"]["id"] + assert group["name"] == f"Default::{team_name}" + + # Delete a team + response = client.delete(f"_ui/v2/teams/{team['id']}/", parse_json=False) + assert response.status_code == HTTPStatus.NO_CONTENT + + # Check that team does not exist + with pytest.raises(GalaxyClientError) as ctx: + client.get(f"_ui/v2/teams/{team['id']}/") + assert ctx.value.response.status_code == HTTPStatus.NOT_FOUND + + # Check that associated group does not exist + with pytest.raises(GalaxyClientError) as ctx: + client.get(f"_ui/v1/groups/{team['group']['id']}") + assert ctx.value.response.status_code == HTTPStatus.NOT_FOUND diff --git a/galaxy_ng/tests/integration/utils/tools.py b/galaxy_ng/tests/integration/utils/tools.py index 3ed18b8920..deccb703d6 100644 --- a/galaxy_ng/tests/integration/utils/tools.py +++ b/galaxy_ng/tests/integration/utils/tools.py @@ -2,11 +2,13 @@ import shutil import uuid -from random import randint -import random +from random import SystemRandom import string +random = SystemRandom() + + def is_docker_installed(): return shutil.which("docker") is not None @@ -16,10 +18,17 @@ def uuid4(): return str(uuid.uuid4()) +# REFACTORING: Deprecate and replace with random_name() function. def generate_random_string(length=8): return str(uuid.uuid4().hex)[:length] +def random_name(prefix: str, *, length: int = 8, sep: str = '-'): + """Generate random name.""" + suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=length)) + return f"{prefix}{sep}{suffix}" + + def iterate_all(api_client, url, gc=None): """Iterate through all of the items on every page in a paginated list view.""" next = url @@ -42,7 +51,7 @@ def iterate_all(api_client, url, gc=None): def generate_random_artifact_version(): """Return a string with random integers using format xx.yy.xx.""" - return f"{randint(0, 100)}.{randint(0, 100)}.{randint(1, 100)}" + return f"{random.randint(0, 100)}.{random.randint(0, 100)}.{random.randint(1, 100)}" def gen_string(size=10, chars=string.ascii_lowercase):