Skip to content

Commit 305f5bd

Browse files
authored
test(auth): assert quota project header injection in google-auth tests (#17448)
Other languages like Rust, .NET, and Swift have tests verifying that the user-project configuration propagates as an HTTP request header (x-goog-user-project). The Python auth tests were only checking that the quota project property was assigned, without asserting the outbound HTTP header payload.
1 parent af19393 commit 305f5bd

6 files changed

Lines changed: 33 additions & 10 deletions

File tree

packages/google-auth/tests/compute_engine/test_credentials.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,11 @@ def test_with_quota_project(self, sign, get, utcnow):
865865
# Check that the signer have been initialized with a Request object
866866
assert isinstance(self.credentials._signer._request, transport.Request)
867867

868+
headers = {}
869+
self.credentials.token = "fake-token"
870+
self.credentials.before_request(request, "GET", "https://example.com", headers)
871+
assert headers.get("x-goog-user-project") == "project-foo"
872+
868873
@mock.patch(
869874
"google.auth._helpers.utcnow",
870875
return_value=_helpers.utcfromtimestamp(0),

packages/google-auth/tests/oauth2/test_credentials.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,10 @@ def test_with_quota_project(self):
844844

845845
new_creds = creds.with_quota_project("new-project-456")
846846
assert new_creds.quota_project_id == "new-project-456"
847+
request = mock.create_autospec(transport.Request, instance=True)
847848
headers = {}
848-
creds.apply(headers)
849-
assert "x-goog-user-project" in headers
849+
new_creds.before_request(request, "GET", "https://example.com", headers)
850+
assert headers.get("x-goog-user-project") == "new-project-456"
850851

851852
def test_with_universe_domain(self):
852853
creds = credentials.Credentials(token="token")

packages/google-auth/tests/oauth2/test_service_account.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,11 @@ def test_with_quota_project(self):
224224
credentials = self.make_credentials()
225225
new_credentials = credentials.with_quota_project("new-project-456")
226226
assert new_credentials.quota_project_id == "new-project-456"
227+
request = mock.create_autospec(transport.Request, instance=True)
227228
hdrs = {}
228-
new_credentials.apply(hdrs, token="tok")
229-
assert "x-goog-user-project" in hdrs
229+
new_credentials.token = "tok"
230+
new_credentials.before_request(request, "GET", "https://example.com", hdrs)
231+
assert hdrs.get("x-goog-user-project") == "new-project-456"
230232

231233
def test_copy_regional_access_boundary_manager_state_and_config_with_scopes(self):
232234
credentials = self.make_credentials()

packages/google-auth/tests/test_external_account.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,13 @@ def test_with_quota_project(self):
454454
quota_project_creds = credentials.with_quota_project("project-foo")
455455

456456
assert quota_project_creds.quota_project_id == "project-foo"
457+
request = mock.create_autospec(transport.Request, instance=True)
458+
headers = {}
459+
quota_project_creds.token = "fake-token"
460+
quota_project_creds.before_request(
461+
request, "GET", "https://example.com", headers
462+
)
463+
assert headers.get("x-goog-user-project") == "project-foo"
457464

458465
def test_with_quota_project_workforce_pool(self):
459466
credentials = self.make_workforce_pool_credentials(

packages/google-auth/tests_async/oauth2/test_credentials_async.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from google.auth import _helpers
2525
from google.auth import exceptions
26+
from google.auth import transport
2627
from google.oauth2 import _credentials_async as _credentials_async
2728
from google.oauth2 import credentials
2829
from tests.oauth2 import test_credentials
@@ -344,7 +345,8 @@ def test_apply_with_no_quota_project_id(self):
344345
creds.apply(headers)
345346
assert "x-goog-user-project" not in headers
346347

347-
def test_with_quota_project(self):
348+
@pytest.mark.asyncio
349+
async def test_with_quota_project(self):
348350
creds = _credentials_async.Credentials(
349351
token="token",
350352
refresh_token=self.REFRESH_TOKEN,
@@ -356,9 +358,10 @@ def test_with_quota_project(self):
356358

357359
new_creds = creds.with_quota_project("new-project-456")
358360
assert new_creds.quota_project_id == "new-project-456"
361+
request = mock.create_autospec(transport.Request, instance=True)
359362
headers = {}
360-
creds.apply(headers)
361-
assert "x-goog-user-project" in headers
363+
await new_creds.before_request(request, "GET", "https://example.com", headers)
364+
assert headers.get("x-goog-user-project") == "new-project-456"
362365

363366
def test_from_authorized_user_info(self):
364367
info = test_credentials.AUTH_USER_INFO.copy()

packages/google-auth/tests_async/oauth2/test_service_account_async.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,18 @@ def test_with_claims(self):
139139
new_credentials = credentials.with_claims({"meep": "moop"})
140140
assert new_credentials._additional_claims == {"meep": "moop"}
141141

142-
def test_with_quota_project(self):
142+
@pytest.mark.asyncio
143+
async def test_with_quota_project(self):
143144
credentials = self.make_credentials()
144145
new_credentials = credentials.with_quota_project("new-project-456")
145146
assert new_credentials.quota_project_id == "new-project-456"
147+
request = mock.create_autospec(transport.Request, instance=True)
146148
hdrs = {}
147-
new_credentials.apply(hdrs, token="tok")
148-
assert "x-goog-user-project" in hdrs
149+
new_credentials.token = "tok"
150+
await new_credentials.before_request(
151+
request, "GET", "https://example.com", hdrs
152+
)
153+
assert hdrs.get("x-goog-user-project") == "new-project-456"
149154

150155
def test__make_authorization_grant_assertion(self):
151156
credentials = self.make_credentials()

0 commit comments

Comments
 (0)