1abd1c7a1a1a8824785ca302797093694f702cbd
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
23 from osm_common
.dbbase
import DbBase
, DbException
, deep_update
24 from os
import urandom
25 from http
import HTTPStatus
28 def exception_message(message
):
29 return "database exception " + message
37 def test_constructor():
39 assert db_base
is not None
40 assert isinstance(db_base
, DbBase
)
43 def test_db_connect(db_base
):
44 with pytest
.raises(DbException
) as excinfo
:
45 db_base
.db_connect(None)
46 assert str(excinfo
.value
).startswith(exception_message("Method 'db_connect' not implemented"))
49 def test_db_disconnect(db_base
):
50 db_base
.db_disconnect()
53 def test_get_list(db_base
):
54 with pytest
.raises(DbException
) as excinfo
:
55 db_base
.get_list(None, None)
56 assert str(excinfo
.value
).startswith(exception_message("Method 'get_list' not implemented"))
57 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
60 def test_get_one(db_base
):
61 with pytest
.raises(DbException
) as excinfo
:
62 db_base
.get_one(None, None, None, None)
63 assert str(excinfo
.value
).startswith(exception_message("Method 'get_one' not implemented"))
64 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
67 def test_create(db_base
):
68 with pytest
.raises(DbException
) as excinfo
:
69 db_base
.create(None, None)
70 assert str(excinfo
.value
).startswith(exception_message("Method 'create' not implemented"))
71 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
74 def test_create_list(db_base
):
75 with pytest
.raises(DbException
) as excinfo
:
76 db_base
.create_list(None, None)
77 assert str(excinfo
.value
).startswith(exception_message("Method 'create_list' not implemented"))
78 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
81 def test_del_list(db_base
):
82 with pytest
.raises(DbException
) as excinfo
:
83 db_base
.del_list(None, None)
84 assert str(excinfo
.value
).startswith(exception_message("Method 'del_list' not implemented"))
85 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
88 def test_del_one(db_base
):
89 with pytest
.raises(DbException
) as excinfo
:
90 db_base
.del_one(None, None, None)
91 assert str(excinfo
.value
).startswith(exception_message("Method 'del_one' not implemented"))
92 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
95 class TestEncryption(unittest
.TestCase
):
97 master_key
= "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
101 # set self.secret_key obtained when connect
102 db_base1
.set_secret_key(master_key
, replace
=True)
103 db_base1
.set_secret_key(urandom(32))
104 db_base2
.set_secret_key(None, replace
=True)
105 db_base2
.set_secret_key(urandom(30))
106 db_base3
.set_secret_key(master_key
)
107 self
.db_bases
= [db_base1
, db_base2
, db_base3
]
109 def test_encrypt_decrypt(self
):
111 ("plain text 1 ! ", None),
112 ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
113 ("plain text 3 with usalt ! ", u
"1afd5d1a-4a7e-4d9c-8c65-251290183106"),
114 (u
"plain unicode 4 ! ", None),
115 (u
"plain unicode 5 with salt ! ", "1a000d1a-4a7e-4d9c-8c65-251290183106"),
116 (u
"plain unicode 6 with usalt ! ", u
"1abcdd1a-4a7e-4d9c-8c65-251290183106"),
118 for db_base
in self
.db_bases
:
119 for value
, salt
in TEST
:
121 encrypted
= db_base
.encrypt(value
, schema_version
='1.0', salt
=salt
)
122 self
.assertEqual(encrypted
, value
, "value '{}' has been encrypted".format(value
))
123 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.0', salt
=salt
)
124 self
.assertEqual(decrypted
, value
, "value '{}' has been decrypted".format(value
))
127 encrypted
= db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
)
128 self
.assertNotEqual(encrypted
, value
, "value '{}' has not been encrypted".format(value
))
129 self
.assertIsInstance(encrypted
, str, "Encrypted is not ascii text")
130 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.1', salt
=salt
)
131 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
133 def test_encrypt_decrypt_salt(self
):
134 value
= "value to be encrypted!"
136 for db_base
in self
.db_bases
:
137 for salt
in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
139 encrypted
.append(db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
))
140 self
.assertNotEqual(encrypted
[-1], value
, "value '{}' has not been encrypted".format(value
))
141 self
.assertIsInstance(encrypted
[-1], str, "Encrypted is not ascii text")
142 decrypted
= db_base
.decrypt(encrypted
[-1], schema_version
='1.1', salt
=salt
)
143 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
144 for i
in range(0, len(encrypted
)):
145 for j
in range(i
+1, len(encrypted
)):
146 self
.assertNotEqual(encrypted
[i
], encrypted
[j
],
147 "encryption with different salt must contain different result")
148 # decrypt with a different master key
150 decrypted
= self
.db_bases
[-1].decrypt(encrypted
[0], schema_version
='1.1', salt
=None)
151 self
.assertNotEqual(encrypted
[0], decrypted
, "Decryption with different KEY must generate different result")
152 except DbException
as e
:
153 self
.assertEqual(e
.http_code
, HTTPStatus
.INTERNAL_SERVER_ERROR
,
154 "Decryption with different KEY does not provide expected http_code")
157 class TestDeepUpdate(unittest
.TestCase
):
158 def test_update_dict(self
):
159 # Original, patch, expected result
161 ({"a": "b"}, {"a": "c"}, {"a": "c"}),
162 ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}),
163 ({"a": "b"}, {"a": None}, {}),
164 ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}),
165 ({"a": ["b"]}, {"a": "c"}, {"a": "c"}),
166 ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}),
167 ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}),
168 ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}),
169 ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}),
170 ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}),
171 ({1: {"a": "foo"}}, {1: None}, {}),
172 ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}),
173 ({"e": None}, {"a": 1}, {"e": None, "a": 1}),
174 ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}),
175 ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}),
178 deep_update(t
[0], t
[1])
179 self
.assertEqual(t
[0], t
[2])
180 # test deepcopy is done. So that original dictionary does not reference the pach
181 test_original
= {1: {"a": "b"}}
182 test_patch
= {1: {"c": {"d": "e"}}}
183 test_result
= {1: {"a": "b", "c": {"d": "e"}}}
184 deep_update(test_original
, test_patch
)
185 self
.assertEqual(test_original
, test_result
)
186 test_patch
[1]["c"]["f"] = "edition of patch, must not modify original"
187 self
.assertEqual(test_original
, test_result
)
189 def test_update_array(self
):
190 # This TEST contains a list with the the Original, patch, and expected result
192 # delete all instances of "a"/"d"
193 ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}),
194 ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}),
195 # delete and insert at 0
196 ({"A": ["a", "b", "c"]}, {"A": {"$b": None, "$+[0]": "b"}}, {"A": ["b", "a", "c"]}),
198 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[1]": {"c": "d"}}}, {"A": [{"c": "d"}]}),
199 # insert if not exist
200 ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}),
201 ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}),
203 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}}}, {"A": ["a", {"c": "d"}, "a"]}),
204 ({"A": ["a", "b", "a"]}, {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, {"A": ["b", "a", "a", "c"]}),
205 ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}),
206 # index deletion out of range
207 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}),
209 ({"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}},
210 {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}),
211 ({"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]},
212 {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}},
213 {"A": [{"id": 1, "c": {"d": "e", "f": "g"}}, {"id": 1, "c": {"d": "e", "f": "g"}}]}),
214 # nested array->array
215 ({"A": ["a", "b", ["a", "b"]]}, {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}},
216 {"A": ["a", ["a", {}, "c"]]}),
217 # types str and int different, so not found
218 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: 1": {"c": "e"}}}, {"A": ["a", {"id": "1", "c": "d"}]}),
223 deep_update(t
[0], t
[1])
224 self
.assertEqual(t
[0], t
[2])
226 def test_update_badformat(self
):
227 # This TEST contains original, incorrect patch and #TODO text that must be present
229 # conflict, index 0 is edited twice
230 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}),
231 # conflict, two insertions at same index
232 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}),
233 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}),
234 # bad format keys with and without $
235 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}),
236 # bad format empty $ and yaml incorrect
237 ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}),
238 ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}),
239 ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}),
241 ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}),
242 # Not found, insertion of None
243 ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}),
244 # index edition out of range
245 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}),
246 # conflict, two editions on index 2
247 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}),
251 self
.assertRaises(DbException
, deep_update
, t
[0], t
[1])
253 deep_update(t
[0], t
[1])
254 except DbException
as e
:
258 if __name__
== '__main__':