@@ -449,6 +449,7 @@ def test_credentials_with_scopes_requested_refresh_success(
449449 assert creds .id_token == mock .sentinel .id_token
450450 assert creds .has_scopes (scopes )
451451 assert creds .rapt_token == new_rapt_token
452+ assert creds .granted_scopes == scopes
452453
453454 # Check that the credentials are valid (have a token and are not
454455 # expired.)
@@ -466,7 +467,7 @@ def test_credentials_with_only_default_scopes_requested(
466467 token = "token"
467468 new_rapt_token = "new_rapt_token"
468469 expiry = _helpers .utcnow () + datetime .timedelta (seconds = 500 )
469- grant_response = {"id_token" : mock .sentinel .id_token }
470+ grant_response = {"id_token" : mock .sentinel .id_token , "scope" : "email profile" }
470471 refresh_grant .return_value = (
471472 # Access token
472473 token ,
@@ -513,6 +514,7 @@ def test_credentials_with_only_default_scopes_requested(
513514 assert creds .id_token == mock .sentinel .id_token
514515 assert creds .has_scopes (default_scopes )
515516 assert creds .rapt_token == new_rapt_token
517+ assert creds .granted_scopes == default_scopes
516518
517519 # Check that the credentials are valid (have a token and are not
518520 # expired.)
@@ -530,10 +532,7 @@ def test_credentials_with_scopes_returned_refresh_success(
530532 token = "token"
531533 new_rapt_token = "new_rapt_token"
532534 expiry = _helpers .utcnow () + datetime .timedelta (seconds = 500 )
533- grant_response = {
534- "id_token" : mock .sentinel .id_token ,
535- "scopes" : " " .join (scopes ),
536- }
535+ grant_response = {"id_token" : mock .sentinel .id_token , "scope" : " " .join (scopes )}
537536 refresh_grant .return_value = (
538537 # Access token
539538 token ,
@@ -580,6 +579,7 @@ def test_credentials_with_scopes_returned_refresh_success(
580579 assert creds .id_token == mock .sentinel .id_token
581580 assert creds .has_scopes (scopes )
582581 assert creds .rapt_token == new_rapt_token
582+ assert creds .granted_scopes == scopes
583583
584584 # Check that the credentials are valid (have a token and are not
585585 # expired.)
@@ -590,7 +590,72 @@ def test_credentials_with_scopes_returned_refresh_success(
590590 "google.auth._helpers.utcnow" ,
591591 return_value = datetime .datetime .min + _helpers .REFRESH_THRESHOLD ,
592592 )
593- def test_credentials_with_scopes_refresh_failure_raises_refresh_error (
593+ def test_credentials_with_only_default_scopes_requested_different_granted_scopes (
594+ self , unused_utcnow , refresh_grant
595+ ):
596+ default_scopes = ["email" , "profile" ]
597+ token = "token"
598+ new_rapt_token = "new_rapt_token"
599+ expiry = _helpers .utcnow () + datetime .timedelta (seconds = 500 )
600+ grant_response = {"id_token" : mock .sentinel .id_token , "scope" : "email" }
601+ refresh_grant .return_value = (
602+ # Access token
603+ token ,
604+ # New refresh token
605+ None ,
606+ # Expiry,
607+ expiry ,
608+ # Extra data
609+ grant_response ,
610+ # rapt token
611+ new_rapt_token ,
612+ )
613+
614+ request = mock .create_autospec (transport .Request )
615+ creds = credentials .Credentials (
616+ token = None ,
617+ refresh_token = self .REFRESH_TOKEN ,
618+ token_uri = self .TOKEN_URI ,
619+ client_id = self .CLIENT_ID ,
620+ client_secret = self .CLIENT_SECRET ,
621+ default_scopes = default_scopes ,
622+ rapt_token = self .RAPT_TOKEN ,
623+ enable_reauth_refresh = True ,
624+ )
625+
626+ # Refresh credentials
627+ creds .refresh (request )
628+
629+ # Check jwt grant call.
630+ refresh_grant .assert_called_with (
631+ request ,
632+ self .TOKEN_URI ,
633+ self .REFRESH_TOKEN ,
634+ self .CLIENT_ID ,
635+ self .CLIENT_SECRET ,
636+ default_scopes ,
637+ self .RAPT_TOKEN ,
638+ True ,
639+ )
640+
641+ # Check that the credentials have the token and expiry
642+ assert creds .token == token
643+ assert creds .expiry == expiry
644+ assert creds .id_token == mock .sentinel .id_token
645+ assert creds .has_scopes (default_scopes )
646+ assert creds .rapt_token == new_rapt_token
647+ assert creds .granted_scopes == ["email" ]
648+
649+ # Check that the credentials are valid (have a token and are not
650+ # expired.)
651+ assert creds .valid
652+
653+ @mock .patch ("google.oauth2.reauth.refresh_grant" , autospec = True )
654+ @mock .patch (
655+ "google.auth._helpers.utcnow" ,
656+ return_value = datetime .datetime .min + _helpers .REFRESH_THRESHOLD ,
657+ )
658+ def test_credentials_with_scopes_refresh_different_granted_scopes (
594659 self , unused_utcnow , refresh_grant
595660 ):
596661 scopes = ["email" , "profile" ]
@@ -628,10 +693,7 @@ def test_credentials_with_scopes_refresh_failure_raises_refresh_error(
628693 )
629694
630695 # Refresh credentials
631- with pytest .raises (
632- exceptions .RefreshError , match = "Not all requested scopes were granted"
633- ):
634- creds .refresh (request )
696+ creds .refresh (request )
635697
636698 # Check jwt grant call.
637699 refresh_grant .assert_called_with (
@@ -651,6 +713,7 @@ def test_credentials_with_scopes_refresh_failure_raises_refresh_error(
651713 assert creds .id_token == mock .sentinel .id_token
652714 assert creds .has_scopes (scopes )
653715 assert creds .rapt_token == new_rapt_token
716+ assert creds .granted_scopes == scopes_returned
654717
655718 # Check that the credentials are valid (have a token and are not
656719 # expired.)
0 commit comments