Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions outlook_web/services/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TOKEN_URL_TEMPLATE = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"
TOKEN_URL_GRAPH = TOKEN_URL_TEMPLATE.format(tenant="common")
DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default"
DEFAULT_REFRESH_SCOPE = f"{DEFAULT_GRAPH_SCOPE} offline_access"
GRAPH_MAIL_READ_SCOPES = ("Mail.Read", "Mail.ReadWrite")

# Graph API 返回 401 时表示账号授权失效(与 token endpoint 失败不同)
Expand Down Expand Up @@ -266,15 +267,15 @@ def test_refresh_token_with_rotation(
proxy_url: str = None,
*,
tenant: str = "common",
scope: str = DEFAULT_GRAPH_SCOPE,
scope: str = DEFAULT_REFRESH_SCOPE,
max_retries: int = 3,
) -> tuple[bool, str | None, str | None]:
"""测试 refresh token 是否有效;如服务端返回新的 refresh_token,则一并返回(用于滚动更新)。
支持指数退避重试,遇到 429 时优先读取 Retry-After 头。"""
import time

proxies = build_proxies(proxy_url)
resolved_scope = (scope or DEFAULT_GRAPH_SCOPE).strip() or DEFAULT_GRAPH_SCOPE
resolved_scope = (scope or DEFAULT_REFRESH_SCOPE).strip() or DEFAULT_REFRESH_SCOPE
url = build_token_url(tenant)
data = {
"client_id": client_id,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_graph_permission_precheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ def test_token_refresh_failure_returns_auth_expired(self, mock_post):
self.assertFalse(result.get("success"))
self.assertIsNone(result.get("scope"))

@patch("outlook_web.services.graph.requests.post")
def test_refresh_token_request_includes_offline_access(self, mock_post):
"""刷新请求必须申请可滚动更新的 refresh token。"""
from outlook_web.services.graph import test_refresh_token_with_rotation

mock_post.return_value = _make_graph_token_response("at-123", refresh_token="rt-new")

success, error_message, new_refresh_token = test_refresh_token_with_rotation("cid", "rt-old")

self.assertTrue(success)
self.assertIsNone(error_message)
self.assertEqual(new_refresh_token, "rt-new")
self.assertIn("offline_access", mock_post.call_args.kwargs["data"]["scope"].split())

# ------------------------------------------------------------------
# get_emails_graph 跳过无权限调用
# ------------------------------------------------------------------
Expand Down