From 86e3b6b152a0eee12edb4134206397b9602f3ac9 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Wed, 20 Nov 2024 05:30:00 -0500 Subject: [PATCH] Add tests for new Credential private api class --- .../_temporary_private_credential_api.py | 7 ++- .../_temporary_private_credential_api_test.py | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/_temporary_private_credential_api_test.py diff --git a/src/awx_plugins/interfaces/_temporary_private_credential_api.py b/src/awx_plugins/interfaces/_temporary_private_credential_api.py index 34379ece..65f986d8 100644 --- a/src/awx_plugins/interfaces/_temporary_private_credential_api.py +++ b/src/awx_plugins/interfaces/_temporary_private_credential_api.py @@ -16,8 +16,11 @@ class Credential: Satisfies ManagedCredentialType inputs want(s). """ - def __init__(self: 'Credential') -> None: - self._inputs: dict[str, GenericOptionalPrimitiveType] = {} + def __init__( + self: 'Credential', + inputs: dict[str, GenericOptionalPrimitiveType] | None = None, + ) -> None: + self._inputs: dict[str, GenericOptionalPrimitiveType] = inputs or {} def get_input( self: 'Credential', diff --git a/tests/_temporary_private_credential_api_test.py b/tests/_temporary_private_credential_api_test.py new file mode 100644 index 00000000..0998816f --- /dev/null +++ b/tests/_temporary_private_credential_api_test.py @@ -0,0 +1,45 @@ +"""Tests for the temporarily hosted private helpers.""" + +import pytest + +from awx_plugins.interfaces._temporary_private_credential_api import ( + Credential, + GenericOptionalPrimitiveType, +) + + +def test_credential_instantiation() -> None: + """Check that credential type can be instantiated.""" + assert Credential() + + +@pytest.mark.parametrize( + ('inputs', 'key', 'expected'), + ( + ({'foo': 'bar'}, 'foo', 'bar'), + ({'foo1': 'bar1'}, 'baz', None), + ), +) +def test_credential_get_input( + inputs: dict[str, GenericOptionalPrimitiveType], + key: str, + expected: str, +) -> None: + """Check that get_input operates on the dict we provided.""" + assert Credential(inputs=inputs).get_input(key) == expected + + +@pytest.mark.parametrize( + ('inputs', 'key', 'expected'), + ( + ({'foo2': 'bar2'}, 'foo2', True), + ({'foo3': 'bar3'}, 'baz', False), + ), +) +def test_credential_has_input( + inputs: dict[str, GenericOptionalPrimitiveType], + key: str, + expected: bool, +) -> None: + """Check that has_input behaves like dict in operator.""" + assert Credential(inputs=inputs).has_input(key) == expected