|
| 1 | +""" |
| 2 | +Unit tests for webex_health_check tool. |
| 3 | +
|
| 4 | +The webexpythonsdk and python-dotenv stubs are set up before any project |
| 5 | +module is imported so no real credentials or SDK installation are needed. |
| 6 | +""" |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import types |
| 10 | +import unittest |
| 11 | +from unittest.mock import MagicMock, patch |
| 12 | + |
| 13 | +# ── Provide fake env var and SDK before any tools module is imported ────────── |
| 14 | +os.environ.setdefault("WEBEX_ACCESS_TOKEN", "test-token") |
| 15 | + |
| 16 | +_sdk_mod = types.ModuleType("webexpythonsdk") |
| 17 | +_sdk_mod.WebexAPI = MagicMock(return_value=MagicMock()) |
| 18 | +sys.modules.setdefault("webexpythonsdk", _sdk_mod) |
| 19 | + |
| 20 | +if "dotenv" not in sys.modules: |
| 21 | + _dotenv_mod = types.ModuleType("dotenv") |
| 22 | + _dotenv_mod.load_dotenv = lambda *a, **kw: None |
| 23 | + sys.modules["dotenv"] = _dotenv_mod |
| 24 | + |
| 25 | +from webex_bot_mcp.tools.diagnostics import webex_health_check # noqa: E402 |
| 26 | +from webex_bot_mcp.tools.common import WebexTokenMissingError # noqa: E402 |
| 27 | +import webex_bot_mcp.tools.diagnostics as _diag_mod # noqa: E402 |
| 28 | + |
| 29 | + |
| 30 | +# ── helpers ─────────────────────────────────────────────────────────────────── |
| 31 | + |
| 32 | +def _fake_me(**overrides): |
| 33 | + me = MagicMock() |
| 34 | + me.id = "bot-id-123" |
| 35 | + me.displayName = "Test Bot" |
| 36 | + me.emails = ["testbot@example.com"] |
| 37 | + me.orgId = "org-456" |
| 38 | + for k, v in overrides.items(): |
| 39 | + setattr(me, k, v) |
| 40 | + return me |
| 41 | + |
| 42 | + |
| 43 | +def _fake_room(room_id="R1", title="Room One", room_type="group"): |
| 44 | + r = MagicMock() |
| 45 | + r.id = room_id |
| 46 | + r.title = title |
| 47 | + r.type = room_type |
| 48 | + return r |
| 49 | + |
| 50 | + |
| 51 | +class TestWebexHealthCheckHealthy(unittest.TestCase): |
| 52 | + """All checks pass → overall_status healthy.""" |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + self.mock_api = MagicMock() |
| 56 | + self.mock_api.people.me.return_value = _fake_me() |
| 57 | + self.mock_api.rooms.list.return_value = iter([ |
| 58 | + _fake_room("R1", "Alpha", "group"), |
| 59 | + _fake_room("R2", "Beta", "direct"), |
| 60 | + ]) |
| 61 | + |
| 62 | + def test_overall_status_healthy(self): |
| 63 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 64 | + result = webex_health_check() |
| 65 | + self.assertTrue(result["success"]) |
| 66 | + self.assertEqual(result["data"]["overall_status"], "healthy") |
| 67 | + |
| 68 | + def test_token_check_fields(self): |
| 69 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 70 | + result = webex_health_check() |
| 71 | + token = result["data"]["checks"]["token"] |
| 72 | + self.assertEqual(token["status"], "ok") |
| 73 | + self.assertEqual(token["bot_id"], "bot-id-123") |
| 74 | + self.assertEqual(token["bot_name"], "Test Bot") |
| 75 | + self.assertEqual(token["bot_email"], "testbot@example.com") |
| 76 | + self.assertIn("duration_ms", token) |
| 77 | + |
| 78 | + def test_rooms_check_fields(self): |
| 79 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 80 | + result = webex_health_check() |
| 81 | + rooms = result["data"]["checks"]["rooms"] |
| 82 | + self.assertEqual(rooms["status"], "ok") |
| 83 | + self.assertEqual(rooms["total_visible"], 2) |
| 84 | + self.assertEqual(rooms["room_types"], {"group": 1, "direct": 1}) |
| 85 | + self.assertEqual(len(rooms["sample"]), 2) |
| 86 | + |
| 87 | + def test_top_level_metadata(self): |
| 88 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 89 | + result = webex_health_check() |
| 90 | + data = result["data"] |
| 91 | + self.assertIn("duration_ms", data) |
| 92 | + self.assertIn("server_version", data) |
| 93 | + self.assertIn("python_version", data) |
| 94 | + self.assertIn("timestamp", result) |
| 95 | + self.assertIn("server_version", result) |
| 96 | + |
| 97 | + |
| 98 | +class TestWebexHealthCheckNoRooms(unittest.TestCase): |
| 99 | + """include_rooms=False skips room check.""" |
| 100 | + |
| 101 | + def setUp(self): |
| 102 | + self.mock_api = MagicMock() |
| 103 | + self.mock_api.people.me.return_value = _fake_me() |
| 104 | + |
| 105 | + def test_rooms_check_absent(self): |
| 106 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 107 | + result = webex_health_check(include_rooms=False) |
| 108 | + self.assertNotIn("rooms", result["data"]["checks"]) |
| 109 | + |
| 110 | + def test_still_healthy(self): |
| 111 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 112 | + result = webex_health_check(include_rooms=False) |
| 113 | + self.assertEqual(result["data"]["overall_status"], "healthy") |
| 114 | + |
| 115 | + def test_rooms_list_not_called(self): |
| 116 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 117 | + webex_health_check(include_rooms=False) |
| 118 | + self.mock_api.rooms.list.assert_not_called() |
| 119 | + |
| 120 | + |
| 121 | +class TestWebexHealthCheckTokenMissing(unittest.TestCase): |
| 122 | + """WebexTokenMissingError → unhealthy, room check skipped.""" |
| 123 | + |
| 124 | + def _raise_missing(self): |
| 125 | + raise WebexTokenMissingError("No token available.") |
| 126 | + |
| 127 | + def test_overall_status_unhealthy(self): |
| 128 | + with patch.object(_diag_mod, "get_webex_api", side_effect=self._raise_missing): |
| 129 | + result = webex_health_check() |
| 130 | + self.assertTrue(result["success"]) |
| 131 | + self.assertEqual(result["data"]["overall_status"], "unhealthy") |
| 132 | + |
| 133 | + def test_token_check_error(self): |
| 134 | + with patch.object(_diag_mod, "get_webex_api", side_effect=self._raise_missing): |
| 135 | + result = webex_health_check() |
| 136 | + token = result["data"]["checks"]["token"] |
| 137 | + self.assertEqual(token["status"], "error") |
| 138 | + self.assertEqual(token["error_code"], "E401") |
| 139 | + |
| 140 | + def test_rooms_skipped(self): |
| 141 | + with patch.object(_diag_mod, "get_webex_api", side_effect=self._raise_missing): |
| 142 | + result = webex_health_check() |
| 143 | + rooms = result["data"]["checks"]["rooms"] |
| 144 | + self.assertEqual(rooms["status"], "skipped") |
| 145 | + |
| 146 | + |
| 147 | +class TestWebexHealthCheckTokenAuthError(unittest.TestCase): |
| 148 | + """HTTP 401 from people.me → unhealthy.""" |
| 149 | + |
| 150 | + def setUp(self): |
| 151 | + self.mock_api = MagicMock() |
| 152 | + self.mock_api.people.me.side_effect = Exception("401 Unauthorized") |
| 153 | + |
| 154 | + def test_overall_status_unhealthy(self): |
| 155 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 156 | + result = webex_health_check() |
| 157 | + self.assertEqual(result["data"]["overall_status"], "unhealthy") |
| 158 | + |
| 159 | + def test_token_error_code_e401(self): |
| 160 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 161 | + result = webex_health_check() |
| 162 | + self.assertEqual(result["data"]["checks"]["token"]["error_code"], "E401") |
| 163 | + |
| 164 | + |
| 165 | +class TestWebexHealthCheckRoomError(unittest.TestCase): |
| 166 | + """Token OK but rooms.list fails → degraded.""" |
| 167 | + |
| 168 | + def setUp(self): |
| 169 | + self.mock_api = MagicMock() |
| 170 | + self.mock_api.people.me.return_value = _fake_me() |
| 171 | + self.mock_api.rooms.list.side_effect = Exception("network error") |
| 172 | + |
| 173 | + def test_overall_status_degraded(self): |
| 174 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 175 | + result = webex_health_check() |
| 176 | + self.assertEqual(result["data"]["overall_status"], "degraded") |
| 177 | + |
| 178 | + def test_token_still_ok(self): |
| 179 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 180 | + result = webex_health_check() |
| 181 | + self.assertEqual(result["data"]["checks"]["token"]["status"], "ok") |
| 182 | + |
| 183 | + def test_rooms_status_error(self): |
| 184 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 185 | + result = webex_health_check() |
| 186 | + self.assertEqual(result["data"]["checks"]["rooms"]["status"], "error") |
| 187 | + |
| 188 | + |
| 189 | +class TestWebexHealthCheckSampleCap(unittest.TestCase): |
| 190 | + """Sample rooms list is capped at 3 entries.""" |
| 191 | + |
| 192 | + def setUp(self): |
| 193 | + self.mock_api = MagicMock() |
| 194 | + self.mock_api.people.me.return_value = _fake_me() |
| 195 | + self.mock_api.rooms.list.return_value = iter([ |
| 196 | + _fake_room(f"R{i}", f"Room {i}") for i in range(8) |
| 197 | + ]) |
| 198 | + |
| 199 | + def test_sample_capped_at_three(self): |
| 200 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 201 | + result = webex_health_check() |
| 202 | + self.assertEqual(len(result["data"]["checks"]["rooms"]["sample"]), 3) |
| 203 | + |
| 204 | + def test_total_visible_is_full_count(self): |
| 205 | + with patch.object(_diag_mod, "get_webex_api", return_value=self.mock_api): |
| 206 | + result = webex_health_check() |
| 207 | + self.assertEqual(result["data"]["checks"]["rooms"]["total_visible"], 8) |
| 208 | + |
| 209 | + |
| 210 | +if __name__ == "__main__": |
| 211 | + unittest.main() |
0 commit comments