summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/dns/tests/unit/plugins/plugin_utils/test_unsafe.py
blob: cbda5f1420156a3f0fbf51116d7035464785912a (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
# Copyright (c) 2024, Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

# Make coding more python3-ish
from __future__ import absolute_import, division, print_function

__metaclass__ = type


import pytest

from ansible.utils.unsafe_proxy import AnsibleUnsafe

from ansible_collections.community.dns.plugins.plugin_utils.unsafe import (
    make_unsafe,
)


TEST_MAKE_UNSAFE = [
    (
        u'text',
        [],
        [
            (),
        ],
    ),
    (
        u'{{text}}',
        [
            (),
        ],
        [],
    ),
    (
        b'text',
        [],
        [
            (),
        ],
    ),
    (
        b'{{text}}',
        [
            (),
        ],
        [],
    ),
    (
        {
            'skey': 'value',
            'ukey': '{{value}}',
            1: [
                'value',
                '{{value}}',
                {
                    1.0: '{{value}}',
                    2.0: 'value',
                },
            ],
        },
        [
            ('ukey', ),
            (1, 1),
            (1, 2, 1.0),
        ],
        [
            ('skey', ),
            (1, 0),
            (1, 2, 2.0),
        ],
    ),
    (
        ['value', '{{value}}'],
        [
            (1, ),
        ],
        [
            (0, ),
        ],
    ),
]


@pytest.mark.parametrize("value, check_unsafe_paths, check_safe_paths", TEST_MAKE_UNSAFE)
def test_make_unsafe(value, check_unsafe_paths, check_safe_paths):
    unsafe_value = make_unsafe(value)
    assert unsafe_value == value
    for check_path in check_unsafe_paths:
        obj = unsafe_value
        for elt in check_path:
            obj = obj[elt]
        assert isinstance(obj, AnsibleUnsafe)
    for check_path in check_safe_paths:
        obj = unsafe_value
        for elt in check_path:
            obj = obj[elt]
        assert not isinstance(obj, AnsibleUnsafe)


def test_make_unsafe_dict_key():
    value = {
        b'test': 1,
        u'test': 2,
    }
    unsafe_value = make_unsafe(value)
    assert unsafe_value == value
    for obj in unsafe_value:
        assert not isinstance(obj, AnsibleUnsafe)

    value = {
        b'{{test}}': 1,
        u'{{test}}': 2,
    }
    unsafe_value = make_unsafe(value)
    assert unsafe_value == value
    for obj in unsafe_value:
        assert isinstance(obj, AnsibleUnsafe)


def test_make_unsafe_set():
    value = set([b'test', u'test'])
    unsafe_value = make_unsafe(value)
    assert unsafe_value == value
    for obj in unsafe_value:
        assert not isinstance(obj, AnsibleUnsafe)

    value = set([b'{{test}}', u'{{test}}'])
    unsafe_value = make_unsafe(value)
    assert unsafe_value == value
    for obj in unsafe_value:
        assert isinstance(obj, AnsibleUnsafe)