Pull JSON schemas at build/test not run time
[dcaegen2/platform.git] / mod / onboardingapi / dcae_cli / util / tests / test_config.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 """
23 Tests the config functionality
24 """
25 import os, json
26 from functools import partial
27 from mock import patch
28
29 import pytest
30 import click
31
32 import dcae_cli
33 from dcae_cli.util import config, write_pref
34 from dcae_cli.util.config import get_app_dir, get_config, get_config_path
35
36
37 def test_no_config(monkeypatch, tmpdir):
38     '''Tests the creation and initialization of a config on a clean install'''
39     monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
40
41     mock_config = {}
42
43     config_file = tmpdir.join("config.json")
44     config_file.write(json.dumps(mock_config))
45
46     assert get_config() == mock_config
47
48
49 def test_init_config(monkeypatch):
50     monkeypatch.setattr(config, '_init_config_server_url',
51             lambda: "http://some-nexus-in-the-sky.com")
52     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
53             lambda server_url, path: { "db_url": "conn" })
54
55     expected = {'db_url': 'conn',
56             'server_url': 'http://some-nexus-in-the-sky.com',
57             'active_profile': 'default' }
58     assert expected == config._init_config()
59
60     # Test using of db fallback
61
62     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
63             lambda server_url, path: { "db_url": "" })
64
65     db_url = "postgresql://king:of@mountain:5432/dcae_onboarding_db"
66
67     def fake_init_config_db_url():
68         return db_url
69
70     monkeypatch.setattr(config, "_init_config_db_url",
71             fake_init_config_db_url)
72
73     assert db_url == config._init_config()["db_url"]
74
75     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
76             lambda server_url, path: {})
77
78     assert db_url == config._init_config()["db_url"]
79
80     # Simulate error trying to fetch
81
82     def fetch_simulate_error(server_url, path):
83         raise RuntimeError("Simulated error")
84
85     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
86             fetch_simulate_error)
87     # Case when user opts out of manually setting up
88     monkeypatch.setattr(click, "confirm", lambda msg: False)
89
90     with pytest.raises(config.ConfigurationInitError):
91         config._init_config()
92
93
94 def test_reinit_config(monkeypatch, tmpdir):
95     monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
96
97     new_config = { "db_url": "some-db" }
98
99     def init():
100         return new_config
101
102     assert config._reinit_config(init) == new_config
103
104     old_config = { "db_url": "other-db", "hidden": "yo" }
105     write_pref(old_config, get_config_path())
106
107     new_config["hidden"] = "yo"
108     assert config._reinit_config(init) == new_config
109
110
111 if __name__ == '__main__':
112     '''Test area'''
113     pytest.main([__file__, ])