summaryrefslogtreecommitdiffstats
path: root/awxkit/test/test_credentials.py
blob: 265981d1a30d72e2eaebd5941e1694df0205f3b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pytest


from awxkit.api.pages import credentials
from awxkit.utils import PseudoNamespace


def set_config_cred_to_desired(config, location):
    split = location.split('.')
    config_ref = config.credentials
    for _location in split[:-1]:
        setattr(config_ref, _location, PseudoNamespace())
        config_ref = config_ref[_location]
    setattr(config_ref, split[-1], 'desired')


class MockCredentialType(object):
    def __init__(self, name, kind, managed=True):
        self.name = name
        self.kind = kind
        self.managed = managed


@pytest.mark.parametrize(
    'field, kind, config_cred, desired_field, desired_value',
    [
        ('field', 'ssh', PseudoNamespace(field=123), 'field', 123),
        ('subscription', 'azure', PseudoNamespace(subscription_id=123), 'subscription', 123),
        ('project_id', 'gce', PseudoNamespace(project=123), 'project', 123),
        ('authorize_password', 'net', PseudoNamespace(authorize=123), 'authorize_password', 123),
    ],
)
def test_get_payload_field_and_value_from_config_cred(field, kind, config_cred, desired_field, desired_value):
    ret_field, ret_val = credentials.get_payload_field_and_value_from_kwargs_or_config_cred(field, kind, {}, config_cred)
    assert ret_field == desired_field
    assert ret_val == desired_value


@pytest.mark.parametrize(
    'field, kind, kwargs, desired_field, desired_value',
    [
        ('field', 'ssh', dict(field=123), 'field', 123),
        ('subscription', 'azure', dict(subscription=123), 'subscription', 123),
        ('project_id', 'gce', dict(project_id=123), 'project', 123),
        ('authorize_password', 'net', dict(authorize_password=123), 'authorize_password', 123),
    ],
)
def test_get_payload_field_and_value_from_kwarg(field, kind, kwargs, desired_field, desired_value):
    ret_field, ret_val = credentials.get_payload_field_and_value_from_kwargs_or_config_cred(field, kind, kwargs, PseudoNamespace())
    assert ret_field == desired_field
    assert ret_val == desired_value