1 # Copyright 2018 Whitestack, LLC
2 # Copyright 2018 Telefonica S.A.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
21 from http
import HTTPStatus
22 from os
import urandom
25 from osm_common
.dbbase
import DbBase
, DbException
, deep_update
29 def exception_message(message
):
30 return "database exception " + message
38 def test_constructor():
40 assert db_base
is not None
41 assert isinstance(db_base
, DbBase
)
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")
52 def test_db_disconnect(db_base
):
53 db_base
.db_disconnect()
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")
62 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
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")
71 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
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")
80 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
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")
89 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
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")
98 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
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")
107 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
110 class TestEncryption(unittest
.TestCase
):
112 master_key
= "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
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
]
124 def test_encrypt_decrypt(self
):
126 ("plain text 1 ! ", None),
127 ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
129 for db_base
in self
.db_bases
:
130 for value
, salt
in TEST
:
132 encrypted
= db_base
.encrypt(value
, schema_version
="1.0", salt
=salt
)
134 encrypted
, value
, "value '{}' has been encrypted".format(value
)
136 decrypted
= db_base
.decrypt(encrypted
, schema_version
="1.0", salt
=salt
)
138 decrypted
, value
, "value '{}' has been decrypted".format(value
)
142 encrypted
= db_base
.encrypt(value
, schema_version
="1.1", salt
=salt
)
144 encrypted
, value
, "value '{}' has not been encrypted".format(value
)
146 self
.assertIsInstance(encrypted
, str, "Encrypted is not ascii text")
147 decrypted
= db_base
.decrypt(encrypted
, schema_version
="1.1", salt
=salt
)
149 decrypted
, value
, "value is not equal after encryption/decryption"
152 def test_encrypt_decrypt_salt(self
):
153 value
= "value to be encrypted!"
155 for db_base
in self
.db_bases
:
156 for salt
in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
159 db_base
.encrypt(value
, schema_version
="1.1", salt
=salt
)
164 "value '{}' has not been encrypted".format(value
),
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
171 decrypted
, value
, "value is not equal after encryption/decryption"
173 for i
in range(0, len(encrypted
)):
174 for j
in range(i
+ 1, len(encrypted
)):
178 "encryption with different salt must contain different result",
180 # decrypt with a different master key
182 decrypted
= self
.db_bases
[-1].decrypt(
183 encrypted
[0], schema_version
="1.1", salt
=None
188 "Decryption with different KEY must generate different result",
190 except DbException
as e
:
193 HTTPStatus
.INTERNAL_SERVER_ERROR
,
194 "Decryption with different KEY does not provide expected http_code",
198 class TestDeepUpdate(unittest
.TestCase
):
199 def test_update_dict(self
):
200 # Original, patch, expected result
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": {}}}),
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
)
230 def test_update_array(self
):
231 # This TEST contains a list with the the Original, patch, and expected result
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
238 {"A": ["a", "b", "c"]},
239 {"A": {"$b": None, "$+[0]": "b"}},
240 {"A": ["b", "a", "c"]},
244 {"A": ["a", "b", "a"]},
245 {"A": {"$a": None, "$[1]": {"c": "d"}}},
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"]}),
253 {"A": ["a", "b", "a"]},
254 {"A": {"$b": {"c": "d"}}},
255 {"A": ["a", {"c": "d"}, "a"]},
258 {"A": ["a", "b", "a"]},
259 {"A": {"$b": None, "$+[0]": "b", "$+": "c"}},
260 {"A": ["b", "a", "a", "c"]},
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"]}),
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"}}]},
272 {"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]},
273 {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}},
276 {"id": 1, "c": {"d": "e", "f": "g"}},
277 {"id": 1, "c": {"d": "e", "f": "g"}},
281 # nested array->array
283 {"A": ["a", "b", ["a", "b"]]},
284 {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}},
285 {"A": ["a", ["a", {}, "c"]]},
287 # types str and int different, so not found
289 {"A": ["a", {"id": "1", "c": "d"}]},
290 {"A": {"$id: 1": {"c": "e"}}},
291 {"A": ["a", {"id": "1", "c": "d"}]},
296 deep_update(t
[0], t
[1])
297 self
.assertEqual(t
[0], t
[2])
299 def test_update_badformat(self
):
300 # This TEST contains original, incorrect patch and #TODO text that must be present
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}}),
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
321 {"A": ["a", {"id": "1", "c": "d"}]},
322 {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}},
327 self
.assertRaises(DbException
, deep_update
, t
[0], t
[1])
329 deep_update(t
[0], t
[1])
330 except DbException
as e
:
334 if __name__
== "__main__":