Skip to content

Commit

Permalink
fix: remove organization default from BaseOrgModel (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshimkus-rh authored Oct 22, 2024
1 parent ae8bfc0 commit 6cbd379
Show file tree
Hide file tree
Showing 27 changed files with 728 additions and 312 deletions.
6 changes: 6 additions & 0 deletions src/aap_eda/core/management/commands/create_initial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,8 @@ def _copy_registry_credentials(self):
if not credentials:
return

default_organization = models.utils.get_default_organization()

cred_type = models.CredentialType.objects.filter(
name=enums.DefaultCredentialType.REGISTRY
).first()
Expand Down Expand Up @@ -1099,6 +1101,7 @@ def _copy_registry_credentials(self):
"managed": False,
"credential_type": cred_type,
"inputs": inputs_to_store(inputs),
"organization": default_organization,
},
)
if created:
Expand Down Expand Up @@ -1126,6 +1129,8 @@ def _copy_scm_credentials(self):
if not credentials:
return

default_organization = models.utils.get_default_organization()

cred_type = models.CredentialType.objects.filter(
name=enums.DefaultCredentialType.SOURCE_CONTROL
).first()
Expand All @@ -1141,6 +1146,7 @@ def _copy_scm_credentials(self):
"managed": False,
"credential_type": cred_type,
"inputs": inputs_to_store(inputs),
"organization": default_organization,
},
)
if created:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Generated by Django 4.2.7 on 2024-10-21 22:14

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0050_update_credential_type_help_text"),
]

operations = [
migrations.AlterField(
model_name="activation",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="auditrule",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="decisionenvironment",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="edacredential",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="eventstream",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="project",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="rulebook",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
migrations.AlterField(
model_name="rulebookprocess",
name="organization",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.organization",
),
),
]
3 changes: 0 additions & 3 deletions src/aap_eda/core/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from django.db import models

from .utils import get_default_organization_id

__all__ = ("BaseOrgModel", "UniqueNamedModel")


Expand All @@ -28,7 +26,6 @@ class Meta:
on_delete=models.CASCADE,
blank=False,
null=False,
default=get_default_organization_id,
)


Expand Down
6 changes: 5 additions & 1 deletion src/aap_eda/core/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
from .organization import Organization


def get_default_organization():
return Organization.objects.get(name=settings.DEFAULT_ORGANIZATION_NAME)


def get_default_organization_id():
return Organization.objects.get(name=settings.DEFAULT_ORGANIZATION_NAME).id
return get_default_organization().id
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import logging

import pytest
from django.conf import settings

from aap_eda.core import models
from aap_eda.settings import default


Expand All @@ -32,6 +34,18 @@ def _factory(logger):
return _factory


#################################################################
# Organization
#################################################################
@pytest.fixture
def default_organization() -> models.Organization:
"Corresponds to migration add_default_organization"
return models.Organization.objects.get_or_create(
name=settings.DEFAULT_ORGANIZATION_NAME,
description="The default organization",
)[0]


#################################################################
# Redis
#################################################################
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/api/test_activation_with_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,13 @@ def test_create_activation_with_conflict_credentials(
name="credential-1",
inputs={"sasl_username": "adam", "sasl_password": "secret"},
credential_type_id=user_credential_type.id,
organization_id=default_organization.id,
),
models.EdaCredential(
name="credential-2",
inputs={"sasl_username": "bearny", "sasl_password": "demo"},
credential_type_id=user_credential_type.id,
organization_id=default_organization.id,
),
]
)
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/api/test_credential_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ def test_delete_credential_type(superuser_client: APIClient):

@pytest.mark.django_db
def test_delete_credential_type_with_credentials(
superuser_client: APIClient, preseed_credential_types
superuser_client: APIClient,
default_organization: models.Organization,
preseed_credential_types,
):
credential_type = models.CredentialType.objects.create(
name="user_type",
Expand All @@ -310,6 +312,7 @@ def test_delete_credential_type_with_credentials(
name="credential-1",
inputs={"username": "adam"},
credential_type=credential_type,
organization=default_organization,
)

response = superuser_client.delete(
Expand Down Expand Up @@ -491,6 +494,7 @@ def test_update_managed_credential_type(
)
def test_update_credential_type_with_created_credentials(
superuser_client: APIClient,
default_organization: models.Organization,
preseed_credential_types,
data,
status_code,
Expand All @@ -507,6 +511,7 @@ def test_update_credential_type_with_created_credentials(
name="test-eda-credential",
inputs={"username": "adam"},
credential_type_id=user_type.id,
organization=default_organization,
)

response = superuser_client.patch(
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/api/test_decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_create_decision_environment(
name="eda-credential",
description="Default Credential",
credential_type=credential_type,
organization=default_organization,
inputs=inputs_to_store(
{
"username": "dummy-user",
Expand Down Expand Up @@ -144,6 +145,7 @@ def test_create_decision_environment_with_empty_credential(
description="Default Credential",
credential_type=credential_type,
inputs=inputs_to_store(credential_inputs),
organization=default_organization,
)
data_in = {
"name": "de1",
Expand Down Expand Up @@ -237,6 +239,7 @@ def test_retrieve_decision_environment_not_exist(admin_client: APIClient):
def test_partial_update_decision_environment(
default_decision_environment: models.DecisionEnvironment,
admin_client: APIClient,
default_organization: models.Organization,
preseed_credential_types,
credential_type,
status_code,
Expand All @@ -247,6 +250,7 @@ def test_partial_update_decision_environment(
name="eda-credential",
description="Default Credential",
credential_type=credential_type,
organization=default_organization,
inputs=inputs_to_store(
{
"username": "dummy-user",
Expand Down Expand Up @@ -292,6 +296,7 @@ def test_partial_update_decision_environment(
@pytest.mark.django_db
def test_partial_update_decision_environment_with_image_url_and_host(
default_decision_environment: models.DecisionEnvironment,
default_organization: models.Organization,
admin_client: APIClient,
preseed_credential_types,
inputs,
Expand All @@ -306,6 +311,7 @@ def test_partial_update_decision_environment_with_image_url_and_host(
description="Default Credential",
credential_type=credential_type,
inputs=inputs_to_store(inputs),
organization=default_organization,
)
data = {"eda_credential_id": credential.id, "image_url": "quay.io"}
response = admin_client.patch(
Expand Down
Loading

0 comments on commit 6cbd379

Please sign in to comment.