Adding release notes and enabling import order check
[osm/common.git] / osm_common / tests / test_dbbase.py
1 # Copyright 2018 Whitestack, LLC
2 # Copyright 2018 Telefonica S.A.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
18 ##
19
20 import http
21 from http import HTTPStatus
22 from os import urandom
23 import unittest
24
25 from osm_common.dbbase import DbBase, DbException, deep_update
26 import pytest
27
28
29 def exception_message(message):
30 return "database exception " + message
31
32
33 @pytest.fixture
34 def db_base():
35 return DbBase()
36
37
38 def test_constructor():
39 db_base = DbBase()
40 assert db_base is not None
41 assert isinstance(db_base, DbBase)
42
43
44 def test_db_connect(db_base):
45 with pytest.raises(DbException) as excinfo:
46 db_base.db_connect(None)
47 assert str(excinfo.value).startswith(
48 exception_message("Method 'db_connect' not implemented")
49 )
50
51
52 def test_db_disconnect(db_base):
53 db_base.db_disconnect()
54
55
56 def test_get_list(db_base):
57 with pytest.raises(DbException) as excinfo:
58 db_base.get_list(None, None)
59 assert str(excinfo.value).startswith(
60 exception_message("Method 'get_list' not implemented")
61 )
62 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
63
64
65 def test_get_one(db_base):
66 with pytest.raises(DbException) as excinfo:
67 db_base.get_one(None, None, None, None)
68 assert str(excinfo.value).startswith(
69 exception_message("Method 'get_one' not implemented")
70 )
71 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
72
73
74 def test_create(db_base):
75 with pytest.raises(DbException) as excinfo:
76 db_base.create(None, None)
77 assert str(excinfo.value).startswith(
78 exception_message("Method 'create' not implemented")
79 )
80 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
81
82
83 def test_create_list(db_base):
84 with pytest.raises(DbException) as excinfo:
85 db_base.create_list(None, None)
86 assert str(excinfo.value).startswith(
87 exception_message("Method 'create_list' not implemented")
88 )
89 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
90
91
92 def test_del_list(db_base):
93 with pytest.raises(DbException) as excinfo:
94 db_base.del_list(None, None)
95 assert str(excinfo.value).startswith(
96 exception_message("Method 'del_list' not implemented")
97 )
98 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
99
100
101 def test_del_one(db_base):
102 with pytest.raises(DbException) as excinfo:
103 db_base.del_one(None, None, None)
104 assert str(excinfo.value).startswith(
105 exception_message("Method 'del_one' not implemented")
106 )
107 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
108
109
110 class TestEncryption(unittest.TestCase):
111 def setUp(self):
112 master_key = "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
113 db_base1 = DbBase()
114 db_base2 = DbBase()
115 db_base3 = DbBase()
116 # set self.secret_key obtained when connect
117 db_base1.set_secret_key(master_key, replace=True)
118 db_base1.set_secret_key(urandom(32))
119 db_base2.set_secret_key(None, replace=True)
120 db_base2.set_secret_key(urandom(30))
121 db_base3.set_secret_key(master_key)
122 self.db_bases = [db_base1, db_base2, db_base3]
123
124 def test_encrypt_decrypt(self):
125 TEST = (
126 ("plain text 1 ! ", None),
127 ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
128 )
129 for db_base in self.db_bases:
130 for value, salt in TEST:
131 # no encryption
132 encrypted = db_base.encrypt(value, schema_version="1.0", salt=salt)
133 self.assertEqual(
134 encrypted, value, "value '{}' has been encrypted".format(value)
135 )
136 decrypted = db_base.decrypt(encrypted, schema_version="1.0", salt=salt)
137 self.assertEqual(
138 decrypted, value, "value '{}' has been decrypted".format(value)
139 )
140
141 # encrypt/decrypt
142 encrypted = db_base.encrypt(value, schema_version="1.1", salt=salt)
143 self.assertNotEqual(
144 encrypted, value, "value '{}' has not been encrypted".format(value)
145 )
146 self.assertIsInstance(encrypted, str, "Encrypted is not ascii text")
147 decrypted = db_base.decrypt(encrypted, schema_version="1.1", salt=salt)
148 self.assertEqual(
149 decrypted, value, "value is not equal after encryption/decryption"
150 )
151
152 def test_encrypt_decrypt_salt(self):
153 value = "value to be encrypted!"
154 encrypted = []
155 for db_base in self.db_bases:
156 for salt in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
157 # encrypt/decrypt
158 encrypted.append(
159 db_base.encrypt(value, schema_version="1.1", salt=salt)
160 )
161 self.assertNotEqual(
162 encrypted[-1],
163 value,
164 "value '{}' has not been encrypted".format(value),
165 )
166 self.assertIsInstance(encrypted[-1], str, "Encrypted is not ascii text")
167 decrypted = db_base.decrypt(
168 encrypted[-1], schema_version="1.1", salt=salt
169 )
170 self.assertEqual(
171 decrypted, value, "value is not equal after encryption/decryption"
172 )
173 for i in range(0, len(encrypted)):
174 for j in range(i + 1, len(encrypted)):
175 self.assertNotEqual(
176 encrypted[i],
177 encrypted[j],
178 "encryption with different salt must contain different result",
179 )
180 # decrypt with a different master key
181 try:
182 decrypted = self.db_bases[-1].decrypt(
183 encrypted[0], schema_version="1.1", salt=None
184 )
185 self.assertNotEqual(
186 encrypted[0],
187 decrypted,
188 "Decryption with different KEY must generate different result",
189 )
190 except DbException as e:
191 self.assertEqual(
192 e.http_code,
193 HTTPStatus.INTERNAL_SERVER_ERROR,
194 "Decryption with different KEY does not provide expected http_code",
195 )
196
197
198 class TestDeepUpdate(unittest.TestCase):
199 def test_update_dict(self):
200 # Original, patch, expected result
201 TEST = (
202 ({"a": "b"}, {"a": "c"}, {"a": "c"}),
203 ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}),
204 ({"a": "b"}, {"a": None}, {}),
205 ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}),
206 ({"a": ["b"]}, {"a": "c"}, {"a": "c"}),
207 ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}),
208 ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}),
209 ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}),
210 ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}),
211 ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}),
212 ({1: {"a": "foo"}}, {1: None}, {}),
213 ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}),
214 ({"e": None}, {"a": 1}, {"e": None, "a": 1}),
215 ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}),
216 ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}),
217 )
218 for t in TEST:
219 deep_update(t[0], t[1])
220 self.assertEqual(t[0], t[2])
221 # test deepcopy is done. So that original dictionary does not reference the pach
222 test_original = {1: {"a": "b"}}
223 test_patch = {1: {"c": {"d": "e"}}}
224 test_result = {1: {"a": "b", "c": {"d": "e"}}}
225 deep_update(test_original, test_patch)
226 self.assertEqual(test_original, test_result)
227 test_patch[1]["c"]["f"] = "edition of patch, must not modify original"
228 self.assertEqual(test_original, test_result)
229
230 def test_update_array(self):
231 # This TEST contains a list with the the Original, patch, and expected result
232 TEST = (
233 # delete all instances of "a"/"d"
234 ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}),
235 ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}),
236 # delete and insert at 0
237 (
238 {"A": ["a", "b", "c"]},
239 {"A": {"$b": None, "$+[0]": "b"}},
240 {"A": ["b", "a", "c"]},
241 ),
242 # delete and edit
243 (
244 {"A": ["a", "b", "a"]},
245 {"A": {"$a": None, "$[1]": {"c": "d"}}},
246 {"A": [{"c": "d"}]},
247 ),
248 # insert if not exist
249 ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}),
250 ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}),
251 # edit by filter
252 (
253 {"A": ["a", "b", "a"]},
254 {"A": {"$b": {"c": "d"}}},
255 {"A": ["a", {"c": "d"}, "a"]},
256 ),
257 (
258 {"A": ["a", "b", "a"]},
259 {"A": {"$b": None, "$+[0]": "b", "$+": "c"}},
260 {"A": ["b", "a", "a", "c"]},
261 ),
262 ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}),
263 # index deletion out of range
264 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}),
265 # nested array->dict
266 (
267 {"A": ["a", "b", {"id": "1", "c": {"d": 2}}]},
268 {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}},
269 {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]},
270 ),
271 (
272 {"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]},
273 {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}},
274 {
275 "A": [
276 {"id": 1, "c": {"d": "e", "f": "g"}},
277 {"id": 1, "c": {"d": "e", "f": "g"}},
278 ]
279 },
280 ),
281 # nested array->array
282 (
283 {"A": ["a", "b", ["a", "b"]]},
284 {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}},
285 {"A": ["a", ["a", {}, "c"]]},
286 ),
287 # types str and int different, so not found
288 (
289 {"A": ["a", {"id": "1", "c": "d"}]},
290 {"A": {"$id: 1": {"c": "e"}}},
291 {"A": ["a", {"id": "1", "c": "d"}]},
292 ),
293 )
294 for t in TEST:
295 print(t)
296 deep_update(t[0], t[1])
297 self.assertEqual(t[0], t[2])
298
299 def test_update_badformat(self):
300 # This TEST contains original, incorrect patch and #TODO text that must be present
301 TEST = (
302 # conflict, index 0 is edited twice
303 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}),
304 # conflict, two insertions at same index
305 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}),
306 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}),
307 # bad format keys with and without $
308 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}),
309 # bad format empty $ and yaml incorrect
310 ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}),
311 ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}),
312 ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}),
313 # insertion of None
314 ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}),
315 # Not found, insertion of None
316 ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}),
317 # index edition out of range
318 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}),
319 # conflict, two editions on index 2
320 (
321 {"A": ["a", {"id": "1", "c": "d"}]},
322 {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}},
323 ),
324 )
325 for t in TEST:
326 print(t)
327 self.assertRaises(DbException, deep_update, t[0], t[1])
328 try:
329 deep_update(t[0], t[1])
330 except DbException as e:
331 print(e)
332
333
334 if __name__ == "__main__":
335 unittest.main()