File: test_json.py

package info (click to toggle)
ansible-core 2.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,760 kB
  • sloc: python: 175,447; cs: 4,929; sh: 4,732; xml: 34; makefile: 21
file content (32 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (2)
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
"""
This is testing JSON functionality from module_utils which is only available in a controller context.
Do not add tests here which should pass in a target context.
"""

from __future__ import annotations

import json
import pytest

from ansible.errors import AnsibleRuntimeError
from ansible.module_utils._internal._json._profiles import _tagless


@pytest.mark.parametrize("value", (
    r'"\ud8f3"',
    r'["\ud8f3"]',
    r'[["\ud8f3"]]',
    r'[[{"key": "\ud8f3"}]]',
    r'{"key": "\ud8f3"}',
    r'{"key": ["\ud8f3"]}',
    r'{"key": {"subkey": "\ud8f3"}}',
    r'{"key": {"subkey": ["\ud8f3"]}}',
    r'{"key": [{"subkey": ["\ud8f3"]}]}',
    r'{"\ud8f3": "value"}',
    r'{"key": {"\ud8f3": "subvalue"}}',
    r'{"key": [{"\ud8f3": "subvalue"}]}',
))
def test_invalid_utf8_decoding(value: str) -> None:
    """Verify that strings which cannot be encoded as valid UTF8 result in an error during deserialization."""
    with pytest.raises(AnsibleRuntimeError, match='^Refusing to deserialize an invalid UTF8 string value'):
        json.loads(value, cls=_tagless.Decoder)