|
| 1 | +import sys |
| 2 | +import types |
| 3 | +import unittest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +# Provide minimal stubs when running tests outside the target servers. |
| 7 | +if 'django' not in sys.modules: |
| 8 | + django_stub = types.ModuleType("django") |
| 9 | + django_stub.setup = lambda: None |
| 10 | + sys.modules['django'] = django_stub |
| 11 | + |
| 12 | +if 'MySQLdb' not in sys.modules: |
| 13 | + mysql_stub = types.ModuleType("MySQLdb") |
| 14 | + mysql_stub.connect = lambda *args, **kwargs: None |
| 15 | + cursors_stub = types.SimpleNamespace(SSCursor=object) |
| 16 | + mysql_stub.cursors = cursors_stub |
| 17 | + sys.modules['MySQLdb'] = mysql_stub |
| 18 | + sys.modules['MySQLdb.cursors'] = types.ModuleType("MySQLdb.cursors") |
| 19 | + sys.modules['MySQLdb.cursors'].SSCursor = object |
| 20 | + |
| 21 | +from plogical import mysqlUtilities |
| 22 | + |
| 23 | + |
| 24 | +class DummyConnection: |
| 25 | + def __init__(self, literal_side_effect=None): |
| 26 | + self.literal_calls = [] |
| 27 | + self.literal_side_effect = literal_side_effect |
| 28 | + self.closed = False |
| 29 | + |
| 30 | + def literal(self, value): |
| 31 | + if self.literal_side_effect: |
| 32 | + raise self.literal_side_effect |
| 33 | + self.literal_calls.append(value) |
| 34 | + return f"'{value}'" |
| 35 | + |
| 36 | + def close(self): |
| 37 | + self.closed = True |
| 38 | + |
| 39 | + |
| 40 | +class DummyCursor: |
| 41 | + def __init__(self): |
| 42 | + self.executed = [] |
| 43 | + self.fetchone_value = None |
| 44 | + |
| 45 | + def execute(self, query, params=None): |
| 46 | + self.executed.append((query, params)) |
| 47 | + |
| 48 | + def fetchone(self): |
| 49 | + return self.fetchone_value |
| 50 | + |
| 51 | + |
| 52 | +class MysqlUtilitiesChangePasswordTests(unittest.TestCase): |
| 53 | + def test_numeric_password_is_coerced_and_sanitized(self): |
| 54 | + connection = DummyConnection() |
| 55 | + cursor = DummyCursor() |
| 56 | + |
| 57 | + with mock.patch.object(mysqlUtilities.mysqlUtilities, 'setupConnection', return_value=(connection, cursor)), \ |
| 58 | + mock.patch.object(mysqlUtilities.mysqlUtilities, 'resolve_mysql_username', return_value='demo_user'), \ |
| 59 | + mock.patch('plogical.mysqlUtilities.os.path.exists', return_value=False): |
| 60 | + result = mysqlUtilities.mysqlUtilities.changePassword('demo_user', 987654321) |
| 61 | + |
| 62 | + self.assertEqual(result, 1) |
| 63 | + self.assertIn('987654321', connection.literal_calls) |
| 64 | + self.assertEqual(len(cursor.executed), 2) # USE mysql + password update |
| 65 | + self.assertIn("PASSWORD('987654321')", cursor.executed[-1][0]) |
| 66 | + |
| 67 | + def test_logs_error_when_literal_fails(self): |
| 68 | + connection = DummyConnection(literal_side_effect=ValueError("boom")) |
| 69 | + cursor = DummyCursor() |
| 70 | + |
| 71 | + with mock.patch.object(mysqlUtilities.mysqlUtilities, 'setupConnection', return_value=(connection, cursor)), \ |
| 72 | + mock.patch.object(mysqlUtilities.mysqlUtilities, 'resolve_mysql_username', return_value='demo_user'), \ |
| 73 | + mock.patch('plogical.mysqlUtilities.os.path.exists', return_value=False), \ |
| 74 | + mock.patch('plogical.mysqlUtilities.logging.CyberCPLogFileWriter.writeToFile') as log_mock: |
| 75 | + result = mysqlUtilities.mysqlUtilities.changePassword('demo_user', 'secret') |
| 76 | + |
| 77 | + self.assertEqual(result, 0) |
| 78 | + log_calls = [call.args[0] for call in log_mock.call_args_list] |
| 79 | + self.assertTrue(any('[mysqlUtilities.changePassword.literal]' in msg for msg in log_calls)) |
| 80 | + |
| 81 | + |
| 82 | +class MysqlUtilitiesResolveUserTests(unittest.TestCase): |
| 83 | + def test_resolves_username_when_orm_missing(self): |
| 84 | + class _StubManager: |
| 85 | + def get(self, *args, **kwargs): |
| 86 | + raise Exception("not found") |
| 87 | + |
| 88 | + dbusers_stub = types.SimpleNamespace(objects=_StubManager()) |
| 89 | + databases_stub = types.SimpleNamespace(objects=_StubManager()) |
| 90 | + |
| 91 | + cursor = DummyCursor() |
| 92 | + cursor.fetchone_value = None |
| 93 | + |
| 94 | + with mock.patch('plogical.mysqlUtilities.DBUsers', dbusers_stub, create=True), \ |
| 95 | + mock.patch('plogical.mysqlUtilities.Databases', databases_stub, create=True): |
| 96 | + resolved = mysqlUtilities.mysqlUtilities.resolve_mysql_username('mystery_user', cursor) |
| 97 | + |
| 98 | + self.assertEqual(resolved, 'mystery_user') |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + unittest.main() |
| 103 | + |
0 commit comments