@@ -207,6 +207,82 @@ def test_create_oauth2_session_oauth2_scheme_with_token_endpoint_auth_method(
207207 assert token_endpoint == "https://example.com/token"
208208 assert client .token_endpoint_auth_method == "client_secret_jwt"
209209
210+ def _oauth2_scheme_with_scopes (self ):
211+ """Build an OAuth2 scheme that declares scopes."""
212+ return OAuth2 (
213+ type_ = "oauth2" ,
214+ flows = OAuthFlows (
215+ authorizationCode = OAuthFlowAuthorizationCode (
216+ authorizationUrl = "https://example.com/auth" ,
217+ tokenUrl = "https://example.com/token" ,
218+ scopes = {"read" : "Read access" , "write" : "Write access" },
219+ )
220+ ),
221+ )
222+
223+ def _capturing_post (self , captured ):
224+ """Stub for OAuth2Session.post that records the token-request body."""
225+
226+ def _post (* args , ** kwargs ):
227+ captured ["data" ] = kwargs .get ("data" )
228+ response = Mock ()
229+ response .status_code = 200
230+ response .json .return_value = {
231+ "access_token" : "new_access_token" ,
232+ "token_type" : "Bearer" ,
233+ "expires_in" : 3600 ,
234+ "refresh_token" : "new_refresh_token" ,
235+ }
236+ return response
237+
238+ return _post
239+
240+ def test_refresh_request_omits_scope (self ):
241+ """Refresh requests must not carry scope (some providers reject it)."""
242+ credential = AuthCredential (
243+ auth_type = AuthCredentialTypes .OAUTH2 ,
244+ oauth2 = OAuth2Auth (
245+ client_id = "test_client_id" ,
246+ client_secret = "test_client_secret" ,
247+ redirect_uri = "https://example.com/callback" ,
248+ ),
249+ )
250+
251+ client , token_endpoint = create_oauth2_session (
252+ self ._oauth2_scheme_with_scopes (), credential
253+ )
254+ assert client is not None
255+
256+ captured = {}
257+ client .post = self ._capturing_post (captured )
258+ client .refresh_token (token_endpoint , refresh_token = "old_refresh_token" )
259+
260+ assert "scope" not in captured ["data" ]
261+
262+ def test_token_exchange_omits_scope (self ):
263+ """Authorization-code exchange must not carry scope (it is redundant)."""
264+ credential = AuthCredential (
265+ auth_type = AuthCredentialTypes .OAUTH2 ,
266+ oauth2 = OAuth2Auth (
267+ client_id = "test_client_id" ,
268+ client_secret = "test_client_secret" ,
269+ redirect_uri = "https://example.com/callback" ,
270+ ),
271+ )
272+
273+ client , token_endpoint = create_oauth2_session (
274+ self ._oauth2_scheme_with_scopes (), credential
275+ )
276+ assert client is not None
277+
278+ captured = {}
279+ client .post = self ._capturing_post (captured )
280+ client .fetch_token (
281+ token_endpoint , grant_type = "authorization_code" , code = "test_code"
282+ )
283+
284+ assert "scope" not in captured ["data" ]
285+
210286 def test_update_credential_with_tokens (self ):
211287 """Test update_credential_with_tokens function."""
212288 credential = AuthCredential (
0 commit comments