Skip to content

Commit

Permalink
Delete associated Group when a Team is deleted
Browse files Browse the repository at this point in the history
No-Issue
  • Loading branch information
cutwater committed Aug 27, 2024
1 parent c9cc8d5 commit e454e7f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
8 changes: 7 additions & 1 deletion galaxy_ng/app/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions galaxy_ng/tests/integration/aap/test_aap_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
7 changes: 2 additions & 5 deletions galaxy_ng/tests/integration/dab/test_dab_rbac_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions galaxy_ng/tests/integration/dab/test_ui_v2.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
15 changes: 12 additions & 3 deletions galaxy_ng/tests/integration/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit e454e7f

Please sign in to comment.