From ddad20b3c3d2e6bf482e34dad85fa4b0ff90e1b1 Mon Sep 17 00:00:00 2001 From: HemangChothani <50404902+HemangChothani@users.noreply.github.com> Date: Fri, 15 May 2020 21:53:01 +0530 Subject: [PATCH] fix(storage): add projection parameter to blob.reload method (#146) * fix(storage): add projection parameter to blob.reload method * fix(storage): add system test * fix(storage): nit --- google/cloud/storage/_helpers.py | 8 +++++++- tests/system/test_system.py | 12 ++++++++++++ tests/unit/test__helpers.py | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/google/cloud/storage/_helpers.py b/google/cloud/storage/_helpers.py index dee5cbc42..f8d3bddfd 100644 --- a/google/cloud/storage/_helpers.py +++ b/google/cloud/storage/_helpers.py @@ -136,6 +136,7 @@ def _query_params(self): def reload( self, client=None, + projection="noAcl", timeout=_DEFAULT_TIMEOUT, if_generation_match=None, if_generation_not_match=None, @@ -151,6 +152,11 @@ def reload( :param client: the client to use. If not passed, falls back to the ``client`` stored on the current object. + :type projection: str + :param projection: (Optional) If used, must be 'full' or 'noAcl'. + Defaults to ``'noAcl'``. Specifies the set of + properties to return. + :type timeout: float or tuple :param timeout: (Optional) The amount of time, in seconds, to wait for the server response. @@ -183,7 +189,7 @@ def reload( query_params = self._query_params # Pass only '?projection=noAcl' here because 'acl' and related # are handled via custom endpoints. - query_params["projection"] = "noAcl" + query_params["projection"] = projection _add_generation_match_parameters( query_params, if_generation_match=if_generation_match, diff --git a/tests/system/test_system.py b/tests/system/test_system.py index 6ca87edb1..c96b10ce1 100644 --- a/tests/system/test_system.py +++ b/tests/system/test_system.py @@ -888,6 +888,18 @@ def test_resumable_upload_with_generation_match(self): with open(file_data["path"], "rb") as file_obj: blob.upload_from_file(file_obj, if_metageneration_match=3) + def test_upload_blob_owner(self): + blob = self.bucket.blob("MyBuffer") + file_contents = b"Hello World" + blob.upload_from_string(file_contents) + self.case_blobs_to_delete.append(blob) + + same_blob = self.bucket.blob("MyBuffer") + same_blob.reload(projection="full") # Initialize properties. + user_email = Config.CLIENT._credentials.service_account_email + owner = same_blob.owner + self.assertIn(user_email, owner["entity"]) + class TestUnicode(unittest.TestCase): @vpcsc_config.skip_if_inside_vpcsc diff --git a/tests/unit/test__helpers.py b/tests/unit/test__helpers.py index 7f1f8998e..dee170efc 100644 --- a/tests/unit/test__helpers.py +++ b/tests/unit/test__helpers.py @@ -187,6 +187,30 @@ def test_reload_w_user_project(self): ) self.assertEqual(derived._changes, set()) + def test_reload_w_projection(self): + connection = _Connection({"foo": "Foo"}) + client = _Client(connection) + derived = self._derivedClass("/path")() + # Make sure changes is not a set instance before calling reload + # (which will clear / replace it with an empty set), checked below. + derived._changes = object() + derived.reload(projection="full", client=client, timeout=42) + self.assertEqual(derived._properties, {"foo": "Foo"}) + kw = connection._requested + self.assertEqual(len(kw), 1) + self.assertEqual( + kw[0], + { + "method": "GET", + "path": "/path", + "query_params": {"projection": "full"}, + "headers": {}, + "_target_object": derived, + "timeout": 42, + }, + ) + self.assertEqual(derived._changes, set()) + def test__set_properties(self): mixin = self._make_one() self.assertEqual(mixin._properties, {})