summaryrefslogtreecommitdiffstats
path: root/awxkit/test/cli/test_format.py
blob: adbe0ef463394eee287c16cc1bbc1421970f84c6 (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
import io
import json

import yaml

from awxkit.api.pages import Page
from awxkit.api.pages.users import Users, User
from awxkit.cli import CLI
from awxkit.cli.format import format_response
from awxkit.cli.resource import Import


def test_json_empty_list():
    page = Page.from_json({'results': []})
    formatted = format_response(page)
    assert json.loads(formatted) == {'results': []}


def test_yaml_empty_list():
    page = Page.from_json({'results': []})
    formatted = format_response(page, fmt='yaml')
    assert yaml.safe_load(formatted) == {'results': []}


def test_json_list():
    users = {
        'results': [
            {'username': 'betty'},
            {'username': 'tom'},
            {'username': 'anne'},
        ]
    }
    page = Users.from_json(users)
    formatted = format_response(page)
    assert json.loads(formatted) == users


def test_yaml_list():
    users = {
        'results': [
            {'username': 'betty'},
            {'username': 'tom'},
            {'username': 'anne'},
        ]
    }
    page = Users.from_json(users)
    formatted = format_response(page, fmt='yaml')
    assert yaml.safe_load(formatted) == users


def test_yaml_import():
    class MockedV2:
        def import_assets(self, data):
            self._parsed_data = data

    def _dummy_authenticate():
        pass

    yaml_fd = io.StringIO(
        """
        workflow_job_templates:
          - name: Workflow1
        """
    )
    yaml_fd.name = 'file.yaml'
    cli = CLI(stdin=yaml_fd)
    cli.parse_args(['--conf.format', 'yaml'])
    cli.v2 = MockedV2()
    cli.authenticate = _dummy_authenticate

    Import().handle(cli, None)
    assert cli.v2._parsed_data['workflow_job_templates'][0]['name']