Skip to content

The agent that grows with you

test(teams): cover connect when the namespace exists but App is unbound

xxxigm27640c584425
diff --git a/tests/gateway/test_teams.py b/tests/gateway/test_teams.py
index 4a12761cda3..8f96b4717a7 100644
--- a/tests/gateway/test_teams.py
+++ b/tests/gateway/test_teams.py
@@ -4,7 +4,7 @@ import json
import sys
import types
from types import SimpleNamespace
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
@@ -169,8 +169,18 @@ _ensure_teams_mock()
_teams_mod = load_plugin_adapter("teams")
_teams_mod.AIOHTTP_AVAILABLE = True
-# SDK import is deferred (#62935); bind mocked symbols the same way connect() does.
-assert _teams_mod.check_teams_requirements() is True
+# SDK import is deferred (#62935); bind mocked symbols the same way connect()
+# does, but skip the real lazy-installer so collection does not pip-install
+# microsoft-teams-apps.
+
+
+def _bind_mock_sdk(feature, importer, target_globals, **kwargs):
+ target_globals.update(importer())
+ return True
+
+
+with patch("tools.lazy_deps.ensure_and_bind", _bind_mock_sdk):
+ assert _teams_mod.check_teams_requirements() is True
_teams_mod.TEAMS_SDK_AVAILABLE = True
# Ensure SDK symbols that were None (import failed on Python <3.12) are
@@ -349,9 +359,31 @@ class TestTeamsConnect:
@pytest.mark.anyio
async def test_connect_fails_without_sdk(self, monkeypatch):
monkeypatch.setattr(_teams_mod, "TEAMS_SDK_AVAILABLE", False)
+ monkeypatch.setattr(_teams_mod, "App", None)
+ monkeypatch.setattr(_teams_mod, "ClientOptions", None)
# Simulate the SDK being unavailable AND not installable (offline /
# locked-down env): the lazy-installer can't rebind the globals, so
- # TEAMS_SDK_AVAILABLE stays False and connect() must fail.
+ # App stays None and connect() must fail without calling it.
+ monkeypatch.setattr(
+ "tools.lazy_deps.ensure_and_bind",
+ lambda *_a, **_k: False,
+ )
+ adapter = TeamsAdapter(_make_config(
+ client_id="id", client_secret="secret", tenant_id="tenant",
+ ))
+ result = await adapter.connect()
+ assert result is False
+
+ @pytest.mark.anyio
+ async def test_connect_fails_when_namespace_exists_but_app_unbound(self, monkeypatch):
+ """find_spec('microsoft_teams') can be true from sibling packages
+ without microsoft-teams-apps. connect() must not call App() while
+ it is still None — that was ``'NoneType' object is not callable``.
+ """
+ monkeypatch.setattr(_teams_mod, "TEAMS_SDK_AVAILABLE", True)
+ monkeypatch.setattr(_teams_mod, "App", None)
+ monkeypatch.setattr(_teams_mod, "ClientOptions", None)
+ monkeypatch.setattr(_teams_mod, "AIOHTTP_AVAILABLE", True)
monkeypatch.setattr(
"tools.lazy_deps.ensure_and_bind",
lambda *_a, **_k: False,
@@ -361,6 +393,7 @@ class TestTeamsConnect:
))
result = await adapter.connect()
assert result is False
+ assert adapter._app is None
# ---------------------------------------------------------------------------
diff --git a/tests/gateway/test_teams_dotenv_isolation.py b/tests/gateway/test_teams_dotenv_isolation.py
index c495f880a25..794d32ed9ad 100644
--- a/tests/gateway/test_teams_dotenv_isolation.py
+++ b/tests/gateway/test_teams_dotenv_isolation.py
@@ -115,6 +115,35 @@ def _purge_teams_adapter_modules() -> None:
class TestTeamsAdapterImportDoesNotLeakDotenv:
+ def test_adapter_import_without_sdk_does_not_raise(self, monkeypatch):
+ """Plugin import must not crash when microsoft_teams is absent.
+
+ ``find_spec("microsoft_teams.apps")`` raises ModuleNotFoundError on
+ 3.11+ if the parent namespace is missing — that unregistered Teams.
+ """
+ for name in list(sys.modules):
+ if name == "microsoft_teams" or name.startswith("microsoft_teams."):
+ monkeypatch.delitem(sys.modules, name, raising=False)
+ _purge_teams_adapter_modules()
+
+ import plugins.platforms.teams.adapter as teams_adapter
+
+ # Import must succeed even when the parent namespace is absent
+ # (CI: no microsoft-teams-apps). Symbols stay unbound until connect.
+ assert teams_adapter.App is None
+
+ def test_namespace_without_apps_is_not_sdk_available(self, monkeypatch):
+ """A sibling microsoft_teams package must not count as the Teams SDK."""
+ ns = types.ModuleType("microsoft_teams")
+ ns.__path__ = [] # type: ignore[attr-defined]
+ monkeypatch.setitem(sys.modules, "microsoft_teams", ns)
+ _purge_teams_adapter_modules()
+
+ import plugins.platforms.teams.adapter as teams_adapter
+
+ assert teams_adapter.App is None
+ assert teams_adapter.TEAMS_SDK_AVAILABLE is False
+
def test_adapter_import_does_not_load_cwd_dotenv(self, tmp_path, monkeypatch):
_plant_cwd_dotenv(tmp_path, monkeypatch)
_install_fake_teams_sdk(monkeypatch)