-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage: HMAC add user_project param (#9237)
* test(unit): tests user_project is passed through to request * feat: implement user_project option for HMAC operations * fix style
- Loading branch information
Showing
4 changed files
with
130 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -954,7 +954,7 @@ def dummy_response(): | |
self.assertIsInstance(bucket, Bucket) | ||
self.assertEqual(bucket.name, blob_name) | ||
|
||
def _create_hmac_key_helper(self, explicit_project=None): | ||
def _create_hmac_key_helper(self, explicit_project=None, user_project=None): | ||
import datetime | ||
from pytz import UTC | ||
from six.moves.urllib.parse import urlencode | ||
|
@@ -996,6 +996,9 @@ def _create_hmac_key_helper(self, explicit_project=None): | |
if explicit_project is not None: | ||
kwargs["project_id"] = explicit_project | ||
|
||
if user_project is not None: | ||
kwargs["user_project"] = user_project | ||
|
||
metadata, secret = client.create_hmac_key(service_account_email=EMAIL, **kwargs) | ||
|
||
self.assertIsInstance(metadata, HMACKeyMetadata) | ||
|
@@ -1013,8 +1016,12 @@ def _create_hmac_key_helper(self, explicit_project=None): | |
"hmacKeys", | ||
] | ||
) | ||
QS_PARAMS = {"serviceAccountEmail": EMAIL} | ||
FULL_URI = "{}?{}".format(URI, urlencode(QS_PARAMS)) | ||
qs_params = {"serviceAccountEmail": EMAIL} | ||
|
||
if user_project is not None: | ||
qs_params["userProject"] = user_project | ||
|
||
FULL_URI = "{}?{}".format(URI, urlencode(qs_params)) | ||
http.request.assert_called_once_with( | ||
method="POST", url=FULL_URI, data=None, headers=mock.ANY | ||
) | ||
|
@@ -1025,6 +1032,9 @@ def test_create_hmac_key_defaults(self): | |
def test_create_hmac_key_explicit_project(self): | ||
self._create_hmac_key_helper(explicit_project="other-project-456") | ||
|
||
def test_create_hmac_key_user_project(self): | ||
self._create_hmac_key_helper(user_project="billed-project") | ||
|
||
def test_list_hmac_keys_defaults_empty(self): | ||
PROJECT = "PROJECT" | ||
CREDENTIALS = _make_credentials() | ||
|
@@ -1060,6 +1070,7 @@ def test_list_hmac_keys_explicit_non_empty(self): | |
MAX_RESULTS = 3 | ||
EMAIL = "[email protected]" | ||
ACCESS_ID = "ACCESS-ID" | ||
USER_PROJECT = "billed-project" | ||
CREDENTIALS = _make_credentials() | ||
client = self._make_one(project=PROJECT, credentials=CREDENTIALS) | ||
|
||
|
@@ -1083,6 +1094,7 @@ def test_list_hmac_keys_explicit_non_empty(self): | |
service_account_email=EMAIL, | ||
show_deleted_keys=True, | ||
project_id=OTHER_PROJECT, | ||
user_project=USER_PROJECT, | ||
) | ||
) | ||
|
||
|
@@ -1107,6 +1119,7 @@ def test_list_hmac_keys_explicit_non_empty(self): | |
"maxResults": str(MAX_RESULTS), | ||
"serviceAccountEmail": EMAIL, | ||
"showDeletedKeys": "True", | ||
"userProject": USER_PROJECT, | ||
} | ||
http.request.assert_called_once_with( | ||
method="GET", url=mock.ANY, data=None, headers=mock.ANY | ||
|
@@ -1160,12 +1173,14 @@ def test_get_hmac_key_metadata_wo_project(self): | |
) | ||
|
||
def test_get_hmac_key_metadata_w_project(self): | ||
from six.moves.urllib.parse import urlencode | ||
from google.cloud.storage.hmac_key import HMACKeyMetadata | ||
|
||
PROJECT = "PROJECT" | ||
OTHER_PROJECT = "other-project-456" | ||
EMAIL = "[email protected]" | ||
ACCESS_ID = "ACCESS-ID" | ||
USER_PROJECT = "billed-project" | ||
CREDENTIALS = _make_credentials() | ||
client = self._make_one(project=PROJECT, credentials=CREDENTIALS) | ||
|
||
|
@@ -1179,7 +1194,9 @@ def test_get_hmac_key_metadata_w_project(self): | |
http = _make_requests_session([_make_json_response(resource)]) | ||
client._http_internal = http | ||
|
||
metadata = client.get_hmac_key_metadata(ACCESS_ID, project_id=OTHER_PROJECT) | ||
metadata = client.get_hmac_key_metadata( | ||
ACCESS_ID, project_id=OTHER_PROJECT, user_project=USER_PROJECT | ||
) | ||
|
||
self.assertIsInstance(metadata, HMACKeyMetadata) | ||
self.assertIs(metadata._client, client) | ||
|
@@ -1197,6 +1214,10 @@ def test_get_hmac_key_metadata_w_project(self): | |
ACCESS_ID, | ||
] | ||
) | ||
|
||
qs_params = {"userProject": USER_PROJECT} | ||
FULL_URI = "{}?{}".format(URI, urlencode(qs_params)) | ||
|
||
http.request.assert_called_once_with( | ||
method="GET", url=URI, data=None, headers=mock.ANY | ||
method="GET", url=FULL_URI, data=None, headers=mock.ANY | ||
) |
Oops, something went wrong.