summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/tests/test_crud.py
blob: a94dfad6254f9b6c98cd9bfc14581a143aff41f6 (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
# pylint: disable=C0102

import json
from typing import NamedTuple

import pytest
from jsonschema import validate

from ..controllers._crud import ArrayHorizontalContainer, \
    ArrayVerticalContainer, Form, FormField, HorizontalContainer, SecretStr, \
    VerticalContainer, serialize


def assertObjectEquals(a, b):
    assert json.dumps(a) == json.dumps(b)


class NamedTupleMock(NamedTuple):
    foo: int
    var: str


class NamedTupleSecretMock(NamedTuple):
    foo: int
    var: str
    key: SecretStr


@pytest.mark.parametrize("inp,out", [
    (["foo", "var"], ["foo", "var"]),
    (NamedTupleMock(1, "test"), {"foo": 1, "var": "test"}),
    (NamedTupleSecretMock(1, "test", "imaginethisisakey"), {"foo": 1, "var": "test",
                                                            "key": "***********"}),
    ((1, 2, 3), [1, 2, 3]),
    (set((1, 2, 3)), [1, 2, 3]),
])
def test_serialize(inp, out):
    assertObjectEquals(serialize(inp), out)


def test_schema():
    form = Form(path='/cluster/user/create',
                root_container=VerticalContainer('Create user', key='create_user', fields=[
                    FormField('User entity', key='user_entity', field_type=str),
                    ArrayHorizontalContainer('Capabilities', key='caps', fields=[
                        FormField('left', field_type=str, key='left'),
                        FormField('right', key='right', field_type=str)
                    ]),
                    ArrayVerticalContainer('ah', key='ah', fields=[
                        FormField('top', key='top', field_type=str),
                        FormField('bottom', key='bottom', field_type=str)
                    ]),
                    HorizontalContainer('oh', key='oh', fields=[
                        FormField('left', key='left', field_type=str),
                        FormField('right', key='right', field_type=str)
                    ]),
                    VerticalContainer('ov', key='ov', fields=[
                        FormField('top', key='top', field_type=str),
                        FormField('bottom', key='bottom', field_type=bool)
                    ]),
                ]))
    form_dict = form.to_dict()
    schema = {'schema': form_dict['control_schema'], 'layout': form_dict['ui_schema']}
    validate(instance={'user_entity': 'foo',
                       'caps': [{'left': 'foo', 'right': 'foo2'}],
                       'ah': [{'top': 'foo', 'bottom': 'foo2'}],
                       'oh': {'left': 'foo', 'right': 'foo2'},
                       'ov': {'top': 'foo', 'bottom': True}}, schema=schema['schema'])