summaryrefslogtreecommitdiff
path: root/barbicanclient/tests/utils.py
blob: 5e2c067c36238f81117f8e466a4a41beccb282e0 (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
"""
Copyright 2022 Red Hat Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from unittest import mock

from keystoneauth1 import identity
from keystoneauth1 import session


_DEFAULT_ENDPOINT = "http://192.168.1.23/key-manager/"

STABLE_RESPONSE = {
    'version': {
        'id': 'v1',
        'status': 'stable',
        'links': [
            {
                'rel': 'self',
                'href': 'http://192.168.1.23/key-manager/v1/'
            }, {
                'rel': 'describedby',
                'type': 'text/html',
                'href': 'https://docs.openstack.org/'
            }],
        'media-types': [{
            'base': 'application/json',
            'type': 'application/vnd.openstack.key-manager-v1+json'
        }]
    }
}


def get_custom_current_response(min_version="1.0", max_version="1.1"):
    return {
        'version': {
            'id': 'v1',
            'status': 'CURRENT',
            'min_version': min_version,
            'max_version': max_version,
            'links': [
                {
                    'rel': 'self',
                    'href': 'http://192.168.1.23/key-manager/v1/'
                }, {
                    'rel': 'describedby',
                    'type': 'text/html',
                    'href': 'https://docs.openstack.org/'
                }
            ]
        }
    }


def mock_microversion_response(response=STABLE_RESPONSE):
    response_mock = mock.MagicMock()
    response_mock.json.return_value = response
    return response_mock


def get_version_endpoint(endpoint=None):
    return "{}/v1/".format(endpoint or _DEFAULT_ENDPOINT)


def mock_session():
    auth = identity.Password(
        auth_url="http://localhost/identity/v3",
        username="username",
        password="password",
        project_name="project_name",
        default_domain_id='default')
    sess = session.Session(auth=auth)
    return sess


def mock_session_get_endpoint(sess, endpoint_response):
    sess.get_endpoint = mock.MagicMock()
    sess.get_endpoint.return_value = endpoint_response


def mock_session_get(sess, get_response):
    response_mock = mock.MagicMock()
    response_mock.json.return_value = get_response

    sess.get = mock.MagicMock()
    sess.get.return_value = response_mock


def mock_session_with_get_and_get_endpoint(endpoint_response, get_response):
    sess = mock_session()
    mock_session_get(get_response)
    mock_session_get_endpoint(endpoint_response)

    return sess


def get_server_supported_versions(min_version, max_version):
    if min_version and max_version:
        return get_custom_current_response(min_version, max_version)
    return STABLE_RESPONSE