8a2915ef018cc606ad0cd9e3122cffc8bee65917
[leap_pycommon.git] / src / leap / common / config / tests / test_baseconfig.py
1 # -*- coding: utf-8 -*-
2 # test_baseconfig.py
3 # Copyright (C) 2013 LEAP
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 """
18 Tests for baseconfig
19 """
20 import json
21 import unittest
22 import copy
23
24 from leap.common.config.baseconfig import BaseConfig, LocalizedKey
25 from leap.common.testing.basetest import BaseLeapTest
26
27 from mock import Mock
28
29 # reduced eipconfig sample config
30 sample_config = {
31     "gateways": [
32     {
33         "capabilities": {
34             "adblock": False,
35             "transport": ["openvpn"],
36             "user_ips": False
37         },
38         "host": "host.dev.example.org",
39     }, {
40         "capabilities": {
41             "adblock": False,
42             "transport": ["openvpn"],
43             "user_ips": False
44         },
45         "host": "host2.dev.example.org",
46     }
47     ],
48     "default_language": "en",
49     "languages": [
50         "en",
51         "es"
52     ],
53     "name": {
54         "en": "Baseconfig testing environment",
55         "es": "Entorno de pruebas de Baseconfig"
56     },
57     "serial": 1,
58     "version": 1
59 }
60
61 # reduced eipconfig.spec version
62 sample_spec = {
63     'description': 'sample eip service config',
64     'type': 'object',
65     'properties': {
66         'serial': {
67             'type': int,
68             'default': 1,
69             'required': ["True"]
70         },
71         'version': {
72             'type': int,
73             'default': 1,
74             'required': ["True"]
75         },
76         "default_language": {
77             'type': unicode,
78             'default': 'en'
79         },
80         'languages': {
81             'type': list,
82             'default': ['en']
83         },
84         'name': {
85             'type': dict,
86             'format': 'translatable',
87             'default': {u'en': u'Test Provider'}
88         },
89         'gateways': {
90             'type': list,
91             'default': [
92                 {"capabilities": {
93                     "adblock": True,
94                     "transport": ["openvpn"],
95                     "user_ips": False},
96                  "host": "location.example.org",
97                  }]
98         },
99     }
100 }
101
102
103 class TestConfig(BaseConfig):
104     """
105     BaseConfig implementation for testing purposes only.
106     """
107     def get_gateways(self):
108         return self._safe_get_value("gateways")
109
110     def get_serial(self):
111         return self._safe_get_value("serial")
112
113     def get_version(self):
114         return self._safe_get_value("version")
115
116     def _get_spec(self):
117         return sample_spec
118
119     def get_default_language(self):
120         return self._safe_get_value("default_language")
121
122     @LocalizedKey
123     def get_name(self):
124         return self._safe_get_value("name")
125
126
127 class BaseConfigTest(BaseLeapTest):
128
129     def setUp(self):
130         pass
131
132     def tearDown(self):
133         pass
134
135     def _write_config(self, data):
136         """
137         Helper to write some data to a temp config file.
138
139         :param data: data to be used to save in the config file.
140         :data type: dict (valid json)
141         """
142         self.config_file = self.get_tempfile("config.json")
143         conf = open(self.config_file, "w")
144         conf.write(json.dumps(data))
145         conf.close()
146
147     def _get_config(self, fromfile=False, data=sample_config):
148         """
149         Helper that returns a TestConfig object using the data parameter
150         or a sample data.
151
152         :param fromfile: sets if we should use a file or a string
153         :fromfile type: bool
154         :param data: sets the data to be used to load in the TestConfig object
155         :data type: dict (valid json)
156         :rtype: TestConfig
157         """
158         config = TestConfig()
159
160         loaded = False
161         if fromfile:
162             self._write_config(data)
163             loaded = config.load(self.config_file, relative=False)
164         else:
165             json_string = json.dumps(data)
166             loaded = config.load(data=json_string)
167
168         if not loaded:
169             return None
170
171         return config
172
173     def test_loads_from_file(self):
174         config = self._get_config(fromfile=True)
175         self.assertIsNotNone(config)
176
177     def test_loads_from_data(self):
178         config = self._get_config()
179         self.assertIsNotNone(config)
180
181     def test_load_valid_config_from_file(self):
182         config = self._get_config(fromfile=True)
183         self.assertIsNotNone(config)
184
185         self.assertEqual(config.get_version(), sample_config["version"])
186         self.assertEqual(config.get_serial(), sample_config["serial"])
187         self.assertEqual(config.get_gateways(), sample_config["gateways"])
188
189     def test_load_valid_config_from_data(self):
190         config = self._get_config()
191         self.assertIsNotNone(config)
192
193         self.assertEqual(config.get_version(), sample_config["version"])
194         self.assertEqual(config.get_serial(), sample_config["serial"])
195         self.assertEqual(config.get_gateways(), sample_config["gateways"])
196
197     def test_safe_get_value_no_config(self):
198         config = TestConfig()
199
200         with self.assertRaises(AssertionError):
201             config.get_version()
202
203     def test_safe_get_value_non_existent_value(self):
204         config = self._get_config()
205
206         self.assertIsNone(config._safe_get_value('non-existent-value'))
207
208     def test_loaded(self):
209         config = self._get_config()
210         self.assertTrue(config.loaded())
211
212     def test_not_loaded(self):
213         config = TestConfig()
214         self.assertFalse(config.loaded())
215
216     def test_save_and_load(self):
217         config = self._get_config()
218         config.get_path_prefix = Mock(return_value=self.tempdir)
219         config_file = 'test_config.json'
220         self.assertTrue(config.save([config_file]))
221
222         config_saved = TestConfig()
223         config_file_path = self.get_tempfile(config_file)
224         self.assertTrue(config_saved.load(config_file_path, relative=False))
225
226         self.assertEqual(config.get_version(), config_saved.get_version())
227         self.assertEqual(config.get_serial(), config_saved.get_serial())
228         self.assertEqual(config.get_gateways(), config_saved.get_gateways())
229
230     def test_localizations(self):
231         conf = self._get_config()
232
233         self.assertEqual(conf.get_name(lang='en'), sample_config['name']['en'])
234         self.assertEqual(conf.get_name(lang='es'), sample_config['name']['es'])
235
236     def _localized_config(self, lang):
237         """
238         Helper to change default language of the provider config.
239         """
240         conf = copy.deepcopy(sample_config)
241         conf['default_language'] = lang
242         json_string = json.dumps(conf)
243         config = TestConfig()
244         config.load(data=json_string)
245
246         return config
247
248     def test_default_localization1(self):
249         default_language = sample_config['languages'][0]
250         config = self._localized_config(default_language)
251
252         default_name = sample_config['name'][default_language]
253
254         self.assertEqual(config.get_name(lang='xx'), default_name)
255         self.assertEqual(config.get_name(), default_name)
256
257     def test_default_localization2(self):
258         default_language = sample_config['languages'][1]
259         config = self._localized_config(default_language)
260
261         default_name = sample_config['name'][default_language]
262
263         self.assertEqual(config.get_name(lang='xx'), default_name)
264         self.assertEqual(config.get_name(), default_name)
265
266
267 if __name__ == "__main__":
268     unittest.main(verbosity=2)