From 56136c7d5252b358cf6ac353724a91a0c60c2466 Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Sat, 31 Oct 2026 09:50:54 -0700 Subject: [PATCH] fix(msteams): fail on a missing conversation id The conversations API response must carry an id. Index the response so a malformed payload raises instead of returning None and building an activity URL containing "None". --- src/sentry/integrations/msteams/client.py | 2 +- .../integrations/msteams/test_client.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/sentry/integrations/msteams/client.py b/src/sentry/integrations/msteams/client.py index c36621cfcd97..11e4d9d72615 100644 --- a/src/sentry/integrations/msteams/client.py +++ b/src/sentry/integrations/msteams/client.py @@ -51,7 +51,7 @@ def get_member_list(self, team_id: str, continuation_token: str | None = None): def get_user_conversation_id(self, user_id: str, tenant_id: str): data = {"members": [{"id": user_id}], "channelData": {"tenant": {"id": tenant_id}}} resp = self.post(self.CONVERSATION_URL, data=data) - return resp.get("id") + return resp["id"] def send_message(self, conversation_id: str, data): return self.post(self.ACTIVITY_URL % conversation_id, data=data) diff --git a/tests/sentry/integrations/msteams/test_client.py b/tests/sentry/integrations/msteams/test_client.py index 95610f503eed..0ecff66bb7da 100644 --- a/tests/sentry/integrations/msteams/test_client.py +++ b/tests/sentry/integrations/msteams/test_client.py @@ -148,6 +148,28 @@ def test_skips_client_secret_expiry_gauge_when_invalid(self) -> None: get_token_data() mock_metrics.gauge.assert_not_called() + @responses.activate + def test_get_user_conversation_id(self) -> None: + responses.add( + responses.POST, + "https://smba.trafficmanager.net/amer/v3/conversations", + json={"id": "some_conversation_id"}, + ) + assert ( + self.msteams_client.get_user_conversation_id("user_id", "tenant_id") + == "some_conversation_id" + ) + + @responses.activate + def test_get_user_conversation_id_missing_id_raises(self) -> None: + responses.add( + responses.POST, + "https://smba.trafficmanager.net/amer/v3/conversations", + json={}, + ) + with pytest.raises(KeyError): + self.msteams_client.get_user_conversation_id("user_id", "tenant_id") + @responses.activate def test_simple(self) -> None: self.msteams_client.get_channel_list("foobar")