1
import logging
2
import time
3
+import warnings
4
from unittest.mock import Mock, patch
5
6
import pytest
14
)
15
16
17
+def _set_initiate_response(mock_client, body, headers=None):
18
+ """SEC-339: route an `initiate()` mock response through the
19
+ ``with_raw_response.create`` surface that the SDK consumes for
20
+ deprecation-header gating.
21
+
22
+ ``headers`` defaults to ``None`` (no Deprecation header → no warning,
23
+ matching custom-auth-config / non-OAuth-scheme behavior). Pass
24
+ ``{"Deprecation": "@..."}`` to simulate the apollo retiring branch.
25
+ """
26
+ raw = Mock()
27
+ raw.parse.return_value = body
28
+ raw.headers = headers or {}
29
+ mock_client.connected_accounts.with_raw_response.create.return_value = raw
30
+ return raw
31
+
32
+
33
class TestAuthScheme:
34
def test_oauth2_with_access_token_sets_active_status(self):
35
scheme = AuthScheme()
339
mock_response.id = "conn-123"
340
mock_response.connection_data.val.status = "PENDING"
341
mock_response.connection_data.val.redirect_url = "https://redirect"
325
- mock_client.connected_accounts.create.return_value = mock_response
342
+ _set_initiate_response(mock_client, mock_response)
343
344
connected_accounts.initiate(user_id="user-1", auth_config_id="auth-1")
345
359
mock_response.id = "conn-123"
360
mock_response.connection_data.val.status = "PENDING"
361
mock_response.connection_data.val.redirect_url = "https://redirect"
345
- mock_client.connected_accounts.create.return_value = mock_response
362
+ _set_initiate_response(mock_client, mock_response)
363
364
config = {
365
"auth_scheme": "API_KEY",
378
mock_client.connected_accounts.list.assert_called_once_with(
379
user_ids=["user-1"], auth_config_ids=["auth-1"], statuses=["ACTIVE"]
380
)
364
- call_kwargs = mock_client.connected_accounts.create.call_args.kwargs
381
+ call_kwargs = (
382
+ mock_client.connected_accounts.with_raw_response.create.call_args.kwargs
383
+ )
384
assert call_kwargs["auth_config"] == {"id": "auth-1"}
385
assert call_kwargs["connection"]["user_id"] == "user-1"
386
assert call_kwargs["connection"]["callback_url"] == "https://cb"
492
mock_response.id = "conn-active"
493
mock_response.connection_data.val.status = "ACTIVE"
494
mock_response.connection_data.val.redirect_url = None
476
- mock_client.connected_accounts.create.return_value = mock_response
495
+ _set_initiate_response(mock_client, mock_response)
496
497
scheme = AuthScheme()
498
config = scheme.oauth2(
523
mock_from_id.assert_called_once_with(id="conn-123", client=mock_client)
524
mock_request.wait_for_connection.assert_called_once_with(timeout=42.0)
525
assert result == "connected"
526
+
527
+
528
+# SEC-339: initiate() must gate its DeprecationWarning on the response
529
+# `Deprecation` HTTP header (RFC 9745) that apollo emits only on the
530
+# retiring branch (Composio-managed + redirectable OAuth). These tests pin
531
+# that contract so the previous false-positive behavior — warning purely
532
+# off auth_scheme, which over-fired for custom auth configs — can't come
533
+# back. See https://docs.composio.dev/docs/changelog/2026/04/24
534
+class TestInitiateDeprecationHeaderGate:
535
+ @pytest.fixture
536
+ def mock_client(self):
537
+ client = Mock()
538
+ client.connected_accounts.list = Mock()
539
+ return client
540
+
541
+ @pytest.fixture(autouse=True)
542
+ def _reset_warning_flag(self):
543
+ """Reset the module-level one-time warning guard before each test
544
+ so warning emission is deterministic regardless of test order."""
545
+ import composio.core.models.connected_accounts as ca_mod
546
+
547
+ ca_mod._legacy_initiate_warning_emitted = False
548
+ yield
549
+ ca_mod._legacy_initiate_warning_emitted = False
550
+
551
+ @staticmethod
552
+ def _no_existing_accounts(mock_client):
553
+ empty = Mock()
554
+ empty.items = []
555
+ mock_client.connected_accounts.list.return_value = empty
556
+
557
+ @staticmethod
558
+ def _make_response():
559
+ body = Mock()
560
+ body.id = "conn-dep"
561
+ body.connection_data.val.status = "INITIATED"
562
+ body.connection_data.val.redirect_url = "https://redirect"
563
+ return body
564
+
565
+ def test_warns_once_when_response_carries_deprecation_header(self, mock_client):
566
+ """Managed + redirectable-OAuth path: apollo sets `Deprecation`,
567
+ SDK emits a `DeprecationWarning` pointing callers at link()."""
568
+ self._no_existing_accounts(mock_client)
569
+ body = self._make_response()
570
+ _set_initiate_response(
571
+ mock_client,
572
+ body,
573
+ headers={
574
+ "Deprecation": "@1776988800",
575
+ "Sunset": "Fri, 08 May 2026 00:00:00 GMT",
576
+ "Link": (
577
+ "<https://docs.composio.dev/docs/changelog/2026/04/24>; "
578
+ 'rel="deprecation"'
579
+ ),
580
+ },
581
+ )
582
+
583
+ connected_accounts = ConnectedAccounts(client=mock_client)
584
+ with warnings.catch_warnings(record=True) as caught:
585
+ warnings.simplefilter("always")
586
+ req = connected_accounts.initiate(user_id="user-1", auth_config_id="auth-1")
587
+
588
+ assert isinstance(req, ConnectionRequest)
589
+ deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning)]
590
+ assert len(deprecations) == 1
591
+ message = str(deprecations[0].message)
592
+ assert "composio.connected_accounts.link()" in message
593
+ assert "2026-07-03" in message
594
+
595
+ def test_does_not_warn_when_response_has_no_deprecation_header(self, mock_client):
596
+ """Custom auth config / non-OAuth scheme: apollo returns a clean
597
+ response, SDK must stay silent. Regression for the prior
598
+ auth_scheme-only check that over-fired here."""
599
+ self._no_existing_accounts(mock_client)
600
+ _set_initiate_response(mock_client, self._make_response(), headers={})
601
+
602
+ connected_accounts = ConnectedAccounts(client=mock_client)
603
+ with warnings.catch_warnings(record=True) as caught:
604
+ warnings.simplefilter("always")
605
+ connected_accounts.initiate(user_id="user-1", auth_config_id="auth-1")
606
+
607
+ deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning)]
608
+ assert deprecations == []
609
+
610
+ def test_warns_at_most_once_per_process_across_calls(self, mock_client):
611
+ """The one-time guard must hold across multiple calls in the same
612
+ process even when each response carries the Deprecation header."""
613
+ self._no_existing_accounts(mock_client)
614
+ # Same headers for both calls; helper rewires the same return on
615
+ # each invocation, so both calls see the Deprecation header.
616
+ _set_initiate_response(
617
+ mock_client,
618
+ self._make_response(),
619
+ headers={"Deprecation": "@1776988800"},
620
+ )
621
+
622
+ connected_accounts = ConnectedAccounts(client=mock_client)
623
+ with warnings.catch_warnings(record=True) as caught:
624
+ warnings.simplefilter("always")
625
+ connected_accounts.initiate(user_id="user-1", auth_config_id="auth-1")
626
+ empty = Mock()
627
+ empty.items = []
628
+ mock_client.connected_accounts.list.return_value = empty
629
+ connected_accounts.initiate(user_id="user-1", auth_config_id="auth-1")
630
+
631
+ deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning)]
632
+ assert len(deprecations) == 1