@@ -1030,6 +1030,61 @@ def test_default_environ_external_credentials_identity_pool_impersonated(
10301030 assert project_id is mock .sentinel .project_id
10311031 assert credentials .scopes == ["https://www.google.com/calendar/feeds" ]
10321032
1033+ # The credential.get_project_id should have been used in _get_external_account_credentials and default
1034+ assert get_project_id .call_count == 2
1035+
1036+
1037+ @EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
1038+ @mock .patch .dict (os .environ )
1039+ def test_default_environ_external_credentials_project_from_env (
1040+ get_project_id , monkeypatch , tmpdir
1041+ ):
1042+ project_from_env = "project_from_env"
1043+ os .environ [environment_vars .PROJECT ] = project_from_env
1044+
1045+ config_file = tmpdir .join ("config.json" )
1046+ config_file .write (json .dumps (IMPERSONATED_IDENTITY_POOL_DATA ))
1047+ monkeypatch .setenv (environment_vars .CREDENTIALS , str (config_file ))
1048+
1049+ credentials , project_id = _default .default (
1050+ scopes = ["https://www.google.com/calendar/feeds" ]
1051+ )
1052+
1053+ assert isinstance (credentials , identity_pool .Credentials )
1054+ assert not credentials .is_user
1055+ assert not credentials .is_workforce_pool
1056+ assert project_id == project_from_env
1057+ assert credentials .scopes == ["https://www.google.com/calendar/feeds" ]
1058+
1059+ # The credential.get_project_id should have been used only in _get_external_account_credentials
1060+ assert get_project_id .call_count == 1
1061+
1062+
1063+ @EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
1064+ @mock .patch .dict (os .environ )
1065+ def test_default_environ_external_credentials_legacy_project_from_env (
1066+ get_project_id , monkeypatch , tmpdir
1067+ ):
1068+ project_from_env = "project_from_env"
1069+ os .environ [environment_vars .LEGACY_PROJECT ] = project_from_env
1070+
1071+ config_file = tmpdir .join ("config.json" )
1072+ config_file .write (json .dumps (IMPERSONATED_IDENTITY_POOL_DATA ))
1073+ monkeypatch .setenv (environment_vars .CREDENTIALS , str (config_file ))
1074+
1075+ credentials , project_id = _default .default (
1076+ scopes = ["https://www.google.com/calendar/feeds" ]
1077+ )
1078+
1079+ assert isinstance (credentials , identity_pool .Credentials )
1080+ assert not credentials .is_user
1081+ assert not credentials .is_workforce_pool
1082+ assert project_id == project_from_env
1083+ assert credentials .scopes == ["https://www.google.com/calendar/feeds" ]
1084+
1085+ # The credential.get_project_id should have been used only in _get_external_account_credentials
1086+ assert get_project_id .call_count == 1
1087+
10331088
10341089@EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
10351090def test_default_environ_external_credentials_aws_impersonated (
0 commit comments