Skip to content

Commit

Permalink
fix: tests on activation after merge conflicts (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei authored Feb 5, 2025
1 parent 91f2177 commit c0cd2ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
19 changes: 18 additions & 1 deletion src/aap_eda/api/serializers/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,10 @@ def prepare_update(self, activation: models.Activation):
# update with new event streams
self.validated_data[
"rulebook_rulesets"
] = _update_event_stream_source(self.validated_data, vault_data)
] = _update_event_stream_source(self.validated_data)

self._insert_pg_notify_credential(activation)

elif rulebook_id:
# user selects a rulebook but no mapping, clear existing mappings
self.validated_data["source_mappings"] = ""
Expand All @@ -679,6 +682,20 @@ def prepare_update(self, activation: models.Activation):
),
)

def _insert_pg_notify_credential(self, activation: models.Activation):
if "eda_credentials" in self.validated_data:
eda_credentials = self.validated_data.get("eda_credentials")
else:
eda_credentials = [
cred.id for cred in activation.eda_credentials.all()
]
postgres_cred = models.EdaCredential.objects.get(
name=settings.DEFAULT_SYSTEM_PG_NOTIFY_CREDENTIAL_NAME
)
if postgres_cred.id not in eda_credentials:
eda_credentials.append(postgres_cred.id)
self.validated_data["eda_credentials"] = eda_credentials

def update(
self, instance: models.Activation, validated_data: dict
) -> None:
Expand Down
42 changes: 35 additions & 7 deletions tests/integration/api/test_activation_with_event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,13 @@ def test_bad_src_activation_with_event_stream(

@pytest.mark.django_db
def test_update_activation_with_everything(
admin_client: APIClient, preseed_credential_types
admin_client: APIClient,
preseed_credential_types,
create_initial_data_command,
):
response = _create_activation_with_everything(admin_client)
response = _create_activation_with_everything(
admin_client, create_initial_data_command
)
activation_id = response.data["id"]

fks2 = create_activation_related_data(
Expand Down Expand Up @@ -800,14 +804,31 @@ def test_update_activation_with_everything(
).first()
for key, val in test_activation2.items():
if key == "extra_var":
assert activation.extra_var == (
input_extra_var = (
"collections:\n- community.shared\n- benthomasson.eda\n"
"custom_password: dummy-password\n"
"custom_username: test-2-user\n"
)
assert input_extra_var in activation.extra_var
cred_extra_vars = [
"postgres_db_host",
"postgres_db_name",
"postgres_db_password",
"postgres_db_port",
"postgres_db_user",
"postgres_sslmode",
"postgres_sslpassword",
]
for var in cred_extra_vars:
assert var in activation.extra_var
continue
if key == "eda_credentials":
assert activation.eda_credentials.all()[0].id == val[0]
cred_ids = [cred.id for cred in activation.eda_credentials.all()]
assert val[0] in cred_ids
postgres_cred = models.EdaCredential.objects.get(
name=settings.DEFAULT_SYSTEM_PG_NOTIFY_CREDENTIAL_NAME
)
assert postgres_cred.id in cred_ids
continue
if key == "source_mappings":
continue
Expand Down Expand Up @@ -859,9 +880,13 @@ def test_update_activation_with_everything(

@pytest.mark.django_db
def test_copy_activation_with_everything(
admin_client: APIClient, preseed_credential_types
admin_client: APIClient,
preseed_credential_types,
create_initial_data_command,
):
response = _create_activation_with_everything(admin_client)
response = _create_activation_with_everything(
admin_client, create_initial_data_command
)
data1 = response.data

response2 = admin_client.post(
Expand Down Expand Up @@ -906,7 +931,10 @@ def test_copy_activation_with_everything(
)


def _create_activation_with_everything(admin_client):
def _create_activation_with_everything(
admin_client, create_initial_data_command
):
create_initial_data_command.handle()
fks = create_activation_related_data(["demo"], rulesets=TEST_RULESETS)
organization = models.Organization.objects.get(pk=fks["organization_id"])
credential = custom_credential("test", organization)
Expand Down

0 comments on commit c0cd2ea

Please sign in to comment.