feat: server skeleton with authenticate and complete_mfa tools
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
66
tests/test_server.py
Normal file
66
tests/test_server.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import json
|
||||
import queue
|
||||
import threading
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import server
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_state():
|
||||
original_state = server._auth_state
|
||||
original_client = server._client
|
||||
for q in (server._mfa_input_queue, server._login_result_queue):
|
||||
while not q.empty():
|
||||
try:
|
||||
q.get_nowait()
|
||||
except queue.Empty:
|
||||
break
|
||||
yield
|
||||
server._auth_state = original_state
|
||||
server._client = original_client
|
||||
|
||||
|
||||
def test_check_auth_unauthenticated():
|
||||
server._auth_state = "unauthenticated"
|
||||
assert server._check_auth() == "Not authenticated. Ask Claude to call authenticate() first."
|
||||
|
||||
|
||||
def test_check_auth_authenticated():
|
||||
server._auth_state = "authenticated"
|
||||
assert server._check_auth() is None
|
||||
|
||||
|
||||
def test_authenticate_success():
|
||||
with patch("server.Garmin") as mock_garmin_cls:
|
||||
mock_garmin_cls.return_value = MagicMock()
|
||||
# mock login() returns immediately, thread puts success in queue
|
||||
result = server.authenticate()
|
||||
assert result == "Authenticated successfully."
|
||||
assert server._auth_state == "authenticated"
|
||||
|
||||
|
||||
def test_authenticate_mfa_required():
|
||||
with patch("server.Garmin") as mock_garmin_cls:
|
||||
mock_garmin_cls.return_value = MagicMock()
|
||||
with patch.object(server._login_result_queue, "get", side_effect=queue.Empty):
|
||||
result = server.authenticate()
|
||||
assert "MFA required" in result
|
||||
assert server._auth_state == "mfa_pending"
|
||||
|
||||
|
||||
def test_complete_mfa_success():
|
||||
server._auth_state = "mfa_pending"
|
||||
server._login_result_queue.put(("success", None))
|
||||
result = server.complete_mfa("123456")
|
||||
assert result == "MFA accepted. Authenticated successfully."
|
||||
assert server._auth_state == "authenticated"
|
||||
assert server._mfa_input_queue.get_nowait() == "123456"
|
||||
|
||||
|
||||
def test_complete_mfa_not_in_progress():
|
||||
server._auth_state = "unauthenticated"
|
||||
result = server.complete_mfa("123456")
|
||||
assert result == "No MFA in progress. Call authenticate() first."
|
||||
Reference in New Issue
Block a user