Stop the Bambu Cloud TOTP tests reaching the network

verify_totp fetches a CSRF token from the bambulab.com web origin before
posting the code (#2696) and returns early when it cannot get one. These
tests patch only post, so the pre-flight GET went out for real: it succeeded
wherever bambulab.com was reachable and returned a tokenless 403 on a CI
runner, where six tests then asserted on a post that never happened.

Stub the handshake for the module. It is covered end to end, no-token path
included, in tests/unit/test_cloud_totp_csrf.py.
This commit is contained in:
maziggy 2026-08-02 11:45:18 +02:00
parent 3da4eee16e
commit 6484fddc1a

View file

@ -7,6 +7,27 @@ import pytest
from backend.app.services.bambu_cloud import BambuCloudService from backend.app.services.bambu_cloud import BambuCloudService
@pytest.fixture(autouse=True)
def _stub_csrf_handshake():
"""Keep the CSRF pre-flight off the network for every test in this module.
``verify_totp`` fetches a CSRF token from the ``bambulab.com`` web origin
before posting the code (#2696), and returns early without posting when it
cannot get one. The tests below patch only ``post``, so that GET went out
over the real network: it succeeded on any machine that could reach
bambulab.com which is why this file passed locally and returned a
tokenless 403 on a CI runner, where six tests then failed asserting on a
``post`` that never happened.
The handshake itself is covered end to end in
``tests/unit/test_cloud_totp_csrf.py``, including the no-token path, so
stubbing it here removes a network dependency rather than any coverage.
"""
with patch.object(BambuCloudService, "_fetch_csrf_token", new_callable=AsyncMock) as fetch:
fetch.return_value = "csrf-token-for-tests"
yield fetch
class TestBambuCloudLogin: class TestBambuCloudLogin:
"""Test login flow detection (email vs TOTP).""" """Test login flow detection (email vs TOTP)."""