| Eduardo Sousa | a011781 | 2019-02-05 15:57:09 +0000 | [diff] [blame] | 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 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 20 | import http |
| 21 | import pytest |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 22 | import unittest |
| 23 | from osm_common.dbbase import DbBase, DbException, deep_update |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 24 | from os import urandom |
| tierno | bd5a402 | 2019-01-30 09:48:38 +0000 | [diff] [blame] | 25 | from http import HTTPStatus |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 26 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 27 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 28 | def exception_message(message): |
| 29 | return "database exception " + message |
| 30 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 31 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 32 | @pytest.fixture |
| 33 | def db_base(): |
| 34 | return DbBase() |
| 35 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 36 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 37 | def test_constructor(): |
| 38 | db_base = DbBase() |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 39 | assert db_base is not None |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 40 | assert isinstance(db_base, DbBase) |
| 41 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 42 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 43 | def test_db_connect(db_base): |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 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")) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 47 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 48 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 49 | def test_db_disconnect(db_base): |
| 50 | db_base.db_disconnect() |
| 51 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 52 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 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 |
| 58 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 59 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 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 |
| 65 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 66 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 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 |
| 72 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 73 | |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 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 |
| 79 | |
| 80 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 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 |
| 86 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 87 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 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 |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 93 | |
| 94 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 95 | class TestEncryption(unittest.TestCase): |
| 96 | def setUp(self): |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 97 | master_key = "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'" |
| tierno | cfc5272 | 2018-10-23 11:41:49 +0200 | [diff] [blame] | 98 | db_base1 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 99 | db_base2 = DbBase() |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 100 | db_base3 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 101 | # set self.secret_key obtained when connect |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 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] |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 108 | |
| 109 | def test_encrypt_decrypt(self): |
| 110 | TEST = ( |
| 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"), |
| 117 | ) |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 118 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 119 | for value, salt in TEST: |
| 120 | # no encryption |
| 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)) |
| 125 | |
| 126 | # encrypt/decrypt |
| 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") |
| 132 | |
| 133 | def test_encrypt_decrypt_salt(self): |
| 134 | value = "value to be encrypted!" |
| 135 | encrypted = [] |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 136 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 137 | for salt in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"): |
| 138 | # encrypt/decrypt |
| 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], |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 147 | "encryption with different salt must contain different result") |
| tierno | bd5a402 | 2019-01-30 09:48:38 +0000 | [diff] [blame] | 148 | # decrypt with a different master key |
| 149 | try: |
| 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") |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 155 | |
| 156 | |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 157 | class TestDeepUpdate(unittest.TestCase): |
| 158 | def test_update_dict(self): |
| 159 | # Original, patch, expected result |
| 160 | TEST = ( |
| 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": {}}}), |
| 176 | ) |
| 177 | for t in TEST: |
| 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) |
| 188 | |
| 189 | def test_update_array(self): |
| 190 | # This TEST contains a list with the the Original, patch, and expected result |
| 191 | TEST = ( |
| 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"]}), |
| 197 | # delete and edit |
| 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"]}), |
| 202 | # edit by filter |
| 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"]}), |
| 208 | # nested array->dict |
| 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"}]}), |
| 219 | |
| 220 | ) |
| 221 | for t in TEST: |
| 222 | print(t) |
| 223 | deep_update(t[0], t[1]) |
| 224 | self.assertEqual(t[0], t[2]) |
| 225 | |
| 226 | def test_update_badformat(self): |
| 227 | # This TEST contains original, incorrect patch and #TODO text that must be present |
| 228 | TEST = ( |
| 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}}), |
| 240 | # insertion of None |
| 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"}}}), |
| 248 | ) |
| 249 | for t in TEST: |
| 250 | print(t) |
| 251 | self.assertRaises(DbException, deep_update, t[0], t[1]) |
| 252 | try: |
| 253 | deep_update(t[0], t[1]) |
| 254 | except DbException as e: |
| 255 | print(e) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 256 | |
| 257 | |
| 258 | if __name__ == '__main__': |
| 259 | unittest.main() |