| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 1 | import http |
| 2 | import pytest |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 3 | import unittest |
| 4 | from osm_common.dbbase import DbBase, DbException, deep_update |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 5 | from os import urandom |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 6 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 7 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 8 | def exception_message(message): |
| 9 | return "database exception " + message |
| 10 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 11 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 12 | @pytest.fixture |
| 13 | def db_base(): |
| 14 | return DbBase() |
| 15 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 16 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 17 | def test_constructor(): |
| 18 | db_base = DbBase() |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 19 | assert db_base is not None |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 20 | assert isinstance(db_base, DbBase) |
| 21 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 22 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 23 | def test_db_connect(db_base): |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 24 | with pytest.raises(DbException) as excinfo: |
| 25 | db_base.db_connect(None) |
| 26 | assert str(excinfo.value).startswith(exception_message("Method 'db_connect' not implemented")) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 27 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 28 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 29 | def test_db_disconnect(db_base): |
| 30 | db_base.db_disconnect() |
| 31 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 32 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 33 | def test_get_list(db_base): |
| 34 | with pytest.raises(DbException) as excinfo: |
| 35 | db_base.get_list(None, None) |
| 36 | assert str(excinfo.value).startswith(exception_message("Method 'get_list' not implemented")) |
| 37 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 38 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 39 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 40 | def test_get_one(db_base): |
| 41 | with pytest.raises(DbException) as excinfo: |
| 42 | db_base.get_one(None, None, None, None) |
| 43 | assert str(excinfo.value).startswith(exception_message("Method 'get_one' not implemented")) |
| 44 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 45 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 46 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 47 | def test_create(db_base): |
| 48 | with pytest.raises(DbException) as excinfo: |
| 49 | db_base.create(None, None) |
| 50 | assert str(excinfo.value).startswith(exception_message("Method 'create' not implemented")) |
| 51 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 52 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 53 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 54 | def test_del_list(db_base): |
| 55 | with pytest.raises(DbException) as excinfo: |
| 56 | db_base.del_list(None, None) |
| 57 | assert str(excinfo.value).startswith(exception_message("Method 'del_list' not implemented")) |
| 58 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 59 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 60 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 61 | def test_del_one(db_base): |
| 62 | with pytest.raises(DbException) as excinfo: |
| 63 | db_base.del_one(None, None, None) |
| 64 | assert str(excinfo.value).startswith(exception_message("Method 'del_one' not implemented")) |
| 65 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 66 | |
| 67 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 68 | class TestEncryption(unittest.TestCase): |
| 69 | def setUp(self): |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 70 | 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] | 71 | db_base1 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 72 | db_base2 = DbBase() |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 73 | db_base3 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 74 | # set self.secret_key obtained when connect |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 75 | db_base1.set_secret_key(master_key, replace=True) |
| 76 | db_base1.set_secret_key(urandom(32)) |
| 77 | db_base2.set_secret_key(None, replace=True) |
| 78 | db_base2.set_secret_key(urandom(30)) |
| 79 | db_base3.set_secret_key(master_key) |
| 80 | self.db_bases = [db_base1, db_base2, db_base3] |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 81 | |
| 82 | def test_encrypt_decrypt(self): |
| 83 | TEST = ( |
| 84 | ("plain text 1 ! ", None), |
| 85 | ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"), |
| 86 | ("plain text 3 with usalt ! ", u"1afd5d1a-4a7e-4d9c-8c65-251290183106"), |
| 87 | (u"plain unicode 4 ! ", None), |
| 88 | (u"plain unicode 5 with salt ! ", "1a000d1a-4a7e-4d9c-8c65-251290183106"), |
| 89 | (u"plain unicode 6 with usalt ! ", u"1abcdd1a-4a7e-4d9c-8c65-251290183106"), |
| 90 | ) |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 91 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 92 | for value, salt in TEST: |
| 93 | # no encryption |
| 94 | encrypted = db_base.encrypt(value, schema_version='1.0', salt=salt) |
| 95 | self.assertEqual(encrypted, value, "value '{}' has been encrypted".format(value)) |
| 96 | decrypted = db_base.decrypt(encrypted, schema_version='1.0', salt=salt) |
| 97 | self.assertEqual(decrypted, value, "value '{}' has been decrypted".format(value)) |
| 98 | |
| 99 | # encrypt/decrypt |
| 100 | encrypted = db_base.encrypt(value, schema_version='1.1', salt=salt) |
| 101 | self.assertNotEqual(encrypted, value, "value '{}' has not been encrypted".format(value)) |
| 102 | self.assertIsInstance(encrypted, str, "Encrypted is not ascii text") |
| 103 | decrypted = db_base.decrypt(encrypted, schema_version='1.1', salt=salt) |
| 104 | self.assertEqual(decrypted, value, "value is not equal after encryption/decryption") |
| 105 | |
| 106 | def test_encrypt_decrypt_salt(self): |
| 107 | value = "value to be encrypted!" |
| 108 | encrypted = [] |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 109 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 110 | for salt in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"): |
| 111 | # encrypt/decrypt |
| 112 | encrypted.append(db_base.encrypt(value, schema_version='1.1', salt=salt)) |
| 113 | self.assertNotEqual(encrypted[-1], value, "value '{}' has not been encrypted".format(value)) |
| 114 | self.assertIsInstance(encrypted[-1], str, "Encrypted is not ascii text") |
| 115 | decrypted = db_base.decrypt(encrypted[-1], schema_version='1.1', salt=salt) |
| 116 | self.assertEqual(decrypted, value, "value is not equal after encryption/decryption") |
| 117 | for i in range(0, len(encrypted)): |
| 118 | for j in range(i+1, len(encrypted)): |
| 119 | self.assertNotEqual(encrypted[i], encrypted[j], |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame^] | 120 | "encryption with different salt must contain different result") |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 121 | |
| 122 | |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 123 | class TestDeepUpdate(unittest.TestCase): |
| 124 | def test_update_dict(self): |
| 125 | # Original, patch, expected result |
| 126 | TEST = ( |
| 127 | ({"a": "b"}, {"a": "c"}, {"a": "c"}), |
| 128 | ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}), |
| 129 | ({"a": "b"}, {"a": None}, {}), |
| 130 | ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}), |
| 131 | ({"a": ["b"]}, {"a": "c"}, {"a": "c"}), |
| 132 | ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}), |
| 133 | ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}), |
| 134 | ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}), |
| 135 | ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}), |
| 136 | ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}), |
| 137 | ({1: {"a": "foo"}}, {1: None}, {}), |
| 138 | ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}), |
| 139 | ({"e": None}, {"a": 1}, {"e": None, "a": 1}), |
| 140 | ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}), |
| 141 | ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}), |
| 142 | ) |
| 143 | for t in TEST: |
| 144 | deep_update(t[0], t[1]) |
| 145 | self.assertEqual(t[0], t[2]) |
| 146 | # test deepcopy is done. So that original dictionary does not reference the pach |
| 147 | test_original = {1: {"a": "b"}} |
| 148 | test_patch = {1: {"c": {"d": "e"}}} |
| 149 | test_result = {1: {"a": "b", "c": {"d": "e"}}} |
| 150 | deep_update(test_original, test_patch) |
| 151 | self.assertEqual(test_original, test_result) |
| 152 | test_patch[1]["c"]["f"] = "edition of patch, must not modify original" |
| 153 | self.assertEqual(test_original, test_result) |
| 154 | |
| 155 | def test_update_array(self): |
| 156 | # This TEST contains a list with the the Original, patch, and expected result |
| 157 | TEST = ( |
| 158 | # delete all instances of "a"/"d" |
| 159 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}), |
| 160 | ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}), |
| 161 | # delete and insert at 0 |
| 162 | ({"A": ["a", "b", "c"]}, {"A": {"$b": None, "$+[0]": "b"}}, {"A": ["b", "a", "c"]}), |
| 163 | # delete and edit |
| 164 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[1]": {"c": "d"}}}, {"A": [{"c": "d"}]}), |
| 165 | # insert if not exist |
| 166 | ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}), |
| 167 | ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}), |
| 168 | # edit by filter |
| 169 | ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}}}, {"A": ["a", {"c": "d"}, "a"]}), |
| 170 | ({"A": ["a", "b", "a"]}, {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, {"A": ["b", "a", "a", "c"]}), |
| 171 | ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}), |
| 172 | # index deletion out of range |
| 173 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}), |
| 174 | # nested array->dict |
| 175 | ({"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 176 | {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}), |
| 177 | ({"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]}, |
| 178 | {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 179 | {"A": [{"id": 1, "c": {"d": "e", "f": "g"}}, {"id": 1, "c": {"d": "e", "f": "g"}}]}), |
| 180 | # nested array->array |
| 181 | ({"A": ["a", "b", ["a", "b"]]}, {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}}, |
| 182 | {"A": ["a", ["a", {}, "c"]]}), |
| 183 | # types str and int different, so not found |
| 184 | ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: 1": {"c": "e"}}}, {"A": ["a", {"id": "1", "c": "d"}]}), |
| 185 | |
| 186 | ) |
| 187 | for t in TEST: |
| 188 | print(t) |
| 189 | deep_update(t[0], t[1]) |
| 190 | self.assertEqual(t[0], t[2]) |
| 191 | |
| 192 | def test_update_badformat(self): |
| 193 | # This TEST contains original, incorrect patch and #TODO text that must be present |
| 194 | TEST = ( |
| 195 | # conflict, index 0 is edited twice |
| 196 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}), |
| 197 | # conflict, two insertions at same index |
| 198 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}), |
| 199 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}), |
| 200 | # bad format keys with and without $ |
| 201 | ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}), |
| 202 | # bad format empty $ and yaml incorrect |
| 203 | ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}), |
| 204 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}), |
| 205 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}), |
| 206 | # insertion of None |
| 207 | ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}), |
| 208 | # Not found, insertion of None |
| 209 | ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}), |
| 210 | # index edition out of range |
| 211 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}), |
| 212 | # conflict, two editions on index 2 |
| 213 | ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}), |
| 214 | ) |
| 215 | for t in TEST: |
| 216 | print(t) |
| 217 | self.assertRaises(DbException, deep_update, t[0], t[1]) |
| 218 | try: |
| 219 | deep_update(t[0], t[1]) |
| 220 | except DbException as e: |
| 221 | print(e) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 222 | |
| 223 | |
| 224 | if __name__ == '__main__': |
| 225 | unittest.main() |