|
26 | 26 | from aap_eda.core.utils.crypto.base import SecretValue
|
27 | 27 | from tests.integration.constants import api_url_v1
|
28 | 28 |
|
| 29 | +EXTRA_VAR = """ |
| 30 | +--- |
| 31 | +db_host: localhost |
| 32 | +db_password: password |
| 33 | +""" |
| 34 | + |
29 | 35 | OVERLAP_EXTRA_VAR = """
|
30 | 36 | ---
|
31 | 37 | sasl_plain_username: demo
|
@@ -75,6 +81,25 @@ def kafka_credential_type() -> models.CredentialType:
|
75 | 81 | )
|
76 | 82 |
|
77 | 83 |
|
| 84 | +@pytest.fixture |
| 85 | +def custom_credential_type() -> models.CredentialType: |
| 86 | + return models.CredentialType.objects.create( |
| 87 | + name="custom_user_type", |
| 88 | + inputs={ |
| 89 | + "fields": [ |
| 90 | + {"id": "username"}, |
| 91 | + {"id": "password"}, |
| 92 | + ] |
| 93 | + }, |
| 94 | + injectors={ |
| 95 | + "extra_vars": { |
| 96 | + "custom_username": "{{ username }}", |
| 97 | + "custom_password": "{{ password }}", |
| 98 | + } |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + |
78 | 103 | @pytest.mark.parametrize(
|
79 | 104 | ("inputs", "result"),
|
80 | 105 | [
|
@@ -486,3 +511,134 @@ def test_create_activation_without_extra_vars_duplicate_credentials(
|
486 | 511 | " credential type: user_type. Please check injectors."
|
487 | 512 | in response.data["non_field_errors"]
|
488 | 513 | )
|
| 514 | + |
| 515 | + |
| 516 | +@pytest.mark.django_db |
| 517 | +def test_create_activation_with_extra_vars_user_credential( |
| 518 | + admin_client: APIClient, |
| 519 | + default_decision_environment: models.DecisionEnvironment, |
| 520 | + default_rulebook: models.Rulebook, |
| 521 | + user_credential_type: models.CredentialType, |
| 522 | + default_organization: models.Organization, |
| 523 | + preseed_credential_types, |
| 524 | +): |
| 525 | + test_activation = { |
| 526 | + "name": "test_activation", |
| 527 | + "decision_environment_id": default_decision_environment.id, |
| 528 | + "rulebook_id": default_rulebook.id, |
| 529 | + "extra_var": EXTRA_VAR, |
| 530 | + "organization_id": default_organization.id, |
| 531 | + } |
| 532 | + pwd = "secret" |
| 533 | + |
| 534 | + eda_credential = models.EdaCredential.objects.create( |
| 535 | + name="credential-1", |
| 536 | + inputs={"sasl_username": "adam", "sasl_password": pwd}, |
| 537 | + credential_type_id=user_credential_type.id, |
| 538 | + organization=default_organization, |
| 539 | + ) |
| 540 | + |
| 541 | + eda_credential_ids = [eda_credential.id] |
| 542 | + test_activation["eda_credentials"] = eda_credential_ids |
| 543 | + |
| 544 | + response = admin_client.post( |
| 545 | + f"{api_url_v1}/activations/", data=test_activation |
| 546 | + ) |
| 547 | + assert response.status_code == status.HTTP_201_CREATED |
| 548 | + assert response.data["extra_var"] |
| 549 | + original_extra_var = yaml.safe_load(EXTRA_VAR) |
| 550 | + extra_var = yaml.safe_load(response.data["extra_var"]) |
| 551 | + assert extra_var["sasl_username"] == "adam" |
| 552 | + assert extra_var["sasl_password"] == "secret" |
| 553 | + for key, value in original_extra_var.items(): |
| 554 | + assert value == extra_var[key] |
| 555 | + |
| 556 | + |
| 557 | +@pytest.mark.django_db |
| 558 | +def test_create_activation_with_extra_vars_vault_aap_credential( |
| 559 | + admin_client: APIClient, |
| 560 | + default_decision_environment: models.DecisionEnvironment, |
| 561 | + default_rulebook: models.Rulebook, |
| 562 | + default_organization: models.Organization, |
| 563 | + default_vault_credential: models.EdaCredential, |
| 564 | + default_aap_credential: models.EdaCredential, |
| 565 | + preseed_credential_types, |
| 566 | +): |
| 567 | + test_activation = { |
| 568 | + "decision_environment_id": default_decision_environment.id, |
| 569 | + "rulebook_id": default_rulebook.id, |
| 570 | + "extra_var": EXTRA_VAR, |
| 571 | + "organization_id": default_organization.id, |
| 572 | + } |
| 573 | + |
| 574 | + for test_credential in [default_vault_credential, default_aap_credential]: |
| 575 | + test_activation["name"] = f"{test_credential.name}-activation" |
| 576 | + eda_credential_ids = [test_credential.id] |
| 577 | + test_activation["eda_credentials"] = eda_credential_ids |
| 578 | + |
| 579 | + response = admin_client.post( |
| 580 | + f"{api_url_v1}/activations/", data=test_activation |
| 581 | + ) |
| 582 | + assert response.status_code == status.HTTP_201_CREATED |
| 583 | + assert response.data["extra_var"] |
| 584 | + original_extra_var = yaml.safe_load(EXTRA_VAR) |
| 585 | + extra_var = yaml.safe_load(response.data["extra_var"]) |
| 586 | + for key, value in original_extra_var.items(): |
| 587 | + assert value == extra_var[key] |
| 588 | + |
| 589 | + |
| 590 | +@pytest.mark.django_db |
| 591 | +def test_create_activation_with_extra_vars_mix_credential( |
| 592 | + admin_client: APIClient, |
| 593 | + default_decision_environment: models.DecisionEnvironment, |
| 594 | + default_rulebook: models.Rulebook, |
| 595 | + default_vault_credential: models.EdaCredential, |
| 596 | + user_credential_type: models.CredentialType, |
| 597 | + custom_credential_type: models.CredentialType, |
| 598 | + default_organization: models.Organization, |
| 599 | + preseed_credential_types, |
| 600 | +): |
| 601 | + test_activation = { |
| 602 | + "name": "test_activation", |
| 603 | + "decision_environment_id": default_decision_environment.id, |
| 604 | + "rulebook_id": default_rulebook.id, |
| 605 | + "extra_var": EXTRA_VAR, |
| 606 | + "organization_id": default_organization.id, |
| 607 | + } |
| 608 | + |
| 609 | + sasl_data = "secret" |
| 610 | + eda_credential = models.EdaCredential.objects.create( |
| 611 | + name="credential-1", |
| 612 | + inputs={"sasl_username": "adam", "sasl_password": sasl_data}, |
| 613 | + credential_type_id=user_credential_type.id, |
| 614 | + organization=default_organization, |
| 615 | + ) |
| 616 | + |
| 617 | + data = "password" |
| 618 | + custom_eda_credential = models.EdaCredential.objects.create( |
| 619 | + name="credential-2", |
| 620 | + inputs={"username": "fred", "password": data}, |
| 621 | + credential_type_id=custom_credential_type.id, |
| 622 | + organization=default_organization, |
| 623 | + ) |
| 624 | + |
| 625 | + eda_credential_ids = [ |
| 626 | + default_vault_credential.id, |
| 627 | + eda_credential.id, |
| 628 | + custom_eda_credential.id, |
| 629 | + ] |
| 630 | + test_activation["eda_credentials"] = eda_credential_ids |
| 631 | + |
| 632 | + response = admin_client.post( |
| 633 | + f"{api_url_v1}/activations/", data=test_activation |
| 634 | + ) |
| 635 | + assert response.status_code == status.HTTP_201_CREATED |
| 636 | + assert response.data["extra_var"] |
| 637 | + original_extra_var = yaml.safe_load(EXTRA_VAR) |
| 638 | + extra_var = yaml.safe_load(response.data["extra_var"]) |
| 639 | + assert extra_var["sasl_username"] == "adam" |
| 640 | + assert extra_var["sasl_password"] == "secret" |
| 641 | + assert extra_var["custom_username"] == "fred" |
| 642 | + assert extra_var["custom_password"] == "password" |
| 643 | + for key, value in original_extra_var.items(): |
| 644 | + assert value == extra_var[key] |
0 commit comments