26
26
from google .auth import transport
27
27
28
28
29
+ METRICS_HEADER_VALUE = "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
30
+
29
31
CLIENT_ID = "username"
30
32
CLIENT_SECRET = "password"
31
33
# Base64 encoding of "username:password"
@@ -751,7 +753,13 @@ def test_refresh_workforce_with_client_auth_and_no_workforce_project_success(
751
753
assert not credentials .expired
752
754
assert credentials .token == response ["access_token" ]
753
755
754
- def test_refresh_impersonation_without_client_auth_success (self ):
756
+ @mock .patch (
757
+ "google.auth.metrics.token_request_access_token_impersonate" ,
758
+ return_value = METRICS_HEADER_VALUE ,
759
+ )
760
+ def test_refresh_impersonation_without_client_auth_success (
761
+ self , mock_metrics_header_value
762
+ ):
755
763
# Simulate service account access token expires in 2800 seconds.
756
764
expire_time = (
757
765
_helpers .utcnow ().replace (microsecond = 0 ) + datetime .timedelta (seconds = 2800 )
@@ -776,6 +784,7 @@ def test_refresh_impersonation_without_client_auth_success(self):
776
784
impersonation_headers = {
777
785
"Content-Type" : "application/json" ,
778
786
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
787
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
779
788
}
780
789
impersonation_request_data = {
781
790
"delegates" : None ,
@@ -815,7 +824,13 @@ def test_refresh_impersonation_without_client_auth_success(self):
815
824
assert not credentials .expired
816
825
assert credentials .token == impersonation_response ["accessToken" ]
817
826
818
- def test_refresh_workforce_impersonation_without_client_auth_success (self ):
827
+ @mock .patch (
828
+ "google.auth.metrics.token_request_access_token_impersonate" ,
829
+ return_value = METRICS_HEADER_VALUE ,
830
+ )
831
+ def test_refresh_workforce_impersonation_without_client_auth_success (
832
+ self , mock_metrics_header_value
833
+ ):
819
834
# Simulate service account access token expires in 2800 seconds.
820
835
expire_time = (
821
836
_helpers .utcnow ().replace (microsecond = 0 ) + datetime .timedelta (seconds = 2800 )
@@ -843,6 +858,7 @@ def test_refresh_workforce_impersonation_without_client_auth_success(self):
843
858
impersonation_headers = {
844
859
"Content-Type" : "application/json" ,
845
860
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
861
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
846
862
}
847
863
impersonation_request_data = {
848
864
"delegates" : None ,
@@ -1000,7 +1016,13 @@ def test_refresh_with_client_auth_success(self):
1000
1016
assert not credentials .expired
1001
1017
assert credentials .token == self .SUCCESS_RESPONSE ["access_token" ]
1002
1018
1003
- def test_refresh_impersonation_with_client_auth_success_ignore_default_scopes (self ):
1019
+ @mock .patch (
1020
+ "google.auth.metrics.token_request_access_token_impersonate" ,
1021
+ return_value = METRICS_HEADER_VALUE ,
1022
+ )
1023
+ def test_refresh_impersonation_with_client_auth_success_ignore_default_scopes (
1024
+ self , mock_metrics_header_value
1025
+ ):
1004
1026
# Simulate service account access token expires in 2800 seconds.
1005
1027
expire_time = (
1006
1028
_helpers .utcnow ().replace (microsecond = 0 ) + datetime .timedelta (seconds = 2800 )
@@ -1028,6 +1050,7 @@ def test_refresh_impersonation_with_client_auth_success_ignore_default_scopes(se
1028
1050
impersonation_headers = {
1029
1051
"Content-Type" : "application/json" ,
1030
1052
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
1053
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
1031
1054
}
1032
1055
impersonation_request_data = {
1033
1056
"delegates" : None ,
@@ -1071,7 +1094,13 @@ def test_refresh_impersonation_with_client_auth_success_ignore_default_scopes(se
1071
1094
assert not credentials .expired
1072
1095
assert credentials .token == impersonation_response ["accessToken" ]
1073
1096
1074
- def test_refresh_impersonation_with_client_auth_success_use_default_scopes (self ):
1097
+ @mock .patch (
1098
+ "google.auth.metrics.token_request_access_token_impersonate" ,
1099
+ return_value = METRICS_HEADER_VALUE ,
1100
+ )
1101
+ def test_refresh_impersonation_with_client_auth_success_use_default_scopes (
1102
+ self , mock_metrics_header_value
1103
+ ):
1075
1104
# Simulate service account access token expires in 2800 seconds.
1076
1105
expire_time = (
1077
1106
_helpers .utcnow ().replace (microsecond = 0 ) + datetime .timedelta (seconds = 2800 )
@@ -1099,6 +1128,7 @@ def test_refresh_impersonation_with_client_auth_success_use_default_scopes(self)
1099
1128
impersonation_headers = {
1100
1129
"Content-Type" : "application/json" ,
1101
1130
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
1131
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
1102
1132
}
1103
1133
impersonation_request_data = {
1104
1134
"delegates" : None ,
@@ -1488,7 +1518,13 @@ def test_project_id_without_scopes(self):
1488
1518
1489
1519
assert credentials .get_project_id (None ) is None
1490
1520
1491
- def test_get_project_id_cloud_resource_manager_success (self ):
1521
+ @mock .patch (
1522
+ "google.auth.metrics.token_request_access_token_impersonate" ,
1523
+ return_value = METRICS_HEADER_VALUE ,
1524
+ )
1525
+ def test_get_project_id_cloud_resource_manager_success (
1526
+ self , mock_metrics_header_value
1527
+ ):
1492
1528
# STS token exchange request/response.
1493
1529
token_response = self .SUCCESS_RESPONSE .copy ()
1494
1530
token_headers = {"Content-Type" : "application/x-www-form-urlencoded" }
@@ -1513,6 +1549,7 @@ def test_get_project_id_cloud_resource_manager_success(self):
1513
1549
"Content-Type" : "application/json" ,
1514
1550
"x-goog-user-project" : self .QUOTA_PROJECT_ID ,
1515
1551
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
1552
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
1516
1553
}
1517
1554
impersonation_request_data = {
1518
1555
"delegates" : None ,
@@ -1638,7 +1675,11 @@ def test_workforce_pool_get_project_id_cloud_resource_manager_success(self):
1638
1675
# No additional requests.
1639
1676
assert len (request .call_args_list ) == 2
1640
1677
1641
- def test_refresh_impersonation_with_lifetime (self ):
1678
+ @mock .patch (
1679
+ "google.auth.metrics.token_request_access_token_impersonate" ,
1680
+ return_value = METRICS_HEADER_VALUE ,
1681
+ )
1682
+ def test_refresh_impersonation_with_lifetime (self , mock_metrics_header_value ):
1642
1683
# Simulate service account access token expires in 2800 seconds.
1643
1684
expire_time = (
1644
1685
_helpers .utcnow ().replace (microsecond = 0 ) + datetime .timedelta (seconds = 2800 )
@@ -1663,6 +1704,7 @@ def test_refresh_impersonation_with_lifetime(self):
1663
1704
impersonation_headers = {
1664
1705
"Content-Type" : "application/json" ,
1665
1706
"authorization" : "Bearer {}" .format (token_response ["access_token" ]),
1707
+ "x-goog-api-client" : METRICS_HEADER_VALUE ,
1666
1708
}
1667
1709
impersonation_request_data = {
1668
1710
"delegates" : None ,
0 commit comments