Debian Codegraph @

test/units/utils/test_json.py

raw
1 """
2 This is testing JSON functionality from module_utils which is only available in a controller context.
3 Do not add tests here which should pass in a target context.
4 """
5
6 from __future__ import annotations
7
8 import json
9 import pytest
10
11 from ansible.errors import AnsibleRuntimeError
12 from ansible.module_utils._internal._json._profiles import _tagless
13
14
15 @pytest.mark.parametrize("value", (
16 r'"\ud8f3"',
17 r'["\ud8f3"]',
18 r'[["\ud8f3"]]',
19 r'[[{"key": "\ud8f3"}]]',
20 r'{"key": "\ud8f3"}',
21 r'{"key": ["\ud8f3"]}',
22 r'{"key": {"subkey": "\ud8f3"}}',
23 r'{"key": {"subkey": ["\ud8f3"]}}',
24 r'{"key": [{"subkey": ["\ud8f3"]}]}',
25 r'{"\ud8f3": "value"}',
26 r'{"key": {"\ud8f3": "subvalue"}}',
27 r'{"key": [{"\ud8f3": "subvalue"}]}',
28 ))
29 def test_invalid_utf8_decoding(value: str) -> None:
30 """Verify that strings which cannot be encoded as valid UTF8 result in an error during deserialization."""
31 with pytest.raises(AnsibleRuntimeError, match='^Refusing to deserialize an invalid UTF8 string value'):
32 json.loads(value, cls=_tagless.Decoder)